Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfredo Sanchez committed Feb 8, 2014
1 parent ea14e16 commit eb73ae4
Showing 1 changed file with 81 additions and 58 deletions.
139 changes: 81 additions & 58 deletions calendarserver/webadmin/resource.py
Expand Up @@ -27,20 +27,13 @@
import operator import operator
import urlparse import urlparse


from calendarserver.tools.util import ( from zope.interface.declarations import implements
principalForPrincipalID, proxySubprincipal, action_addProxyPrincipal,
action_removeProxyPrincipal
)

from twistedcaldav.config import config
from twistedcaldav.extensions import DAVFile, ReadOnlyResourceMixIn


from twisted.internet.defer import inlineCallbacks, returnValue, succeed from twisted.internet.defer import inlineCallbacks, returnValue, succeed
from txweb2.http import Response
from twisted.python.modules import getModule from twisted.python.modules import getModule
from txweb2.http_headers import MimeType
from zope.interface.declarations import implements
from txweb2.stream import MemoryStream from txweb2.stream import MemoryStream
from txweb2.http import Response
from txweb2.http_headers import MimeType
from txweb2.http import HTTPError from txweb2.http import HTTPError
from txdav.xml import element as davxml from txdav.xml import element as davxml


Expand All @@ -49,6 +42,14 @@
Element, renderer, XMLFile, flattenString Element, renderer, XMLFile, flattenString
) )


from twistedcaldav.config import config
from twistedcaldav.extensions import DAVFile, ReadOnlyResourceMixIn

from calendarserver.tools.util import (
principalForPrincipalID, proxySubprincipal, action_addProxyPrincipal,
action_removeProxyPrincipal
)





class WebAdminPage(Element): class WebAdminPage(Element):
Expand Down Expand Up @@ -111,9 +112,9 @@ def noSearchResults(self, request, tag):
@inlineCallbacks @inlineCallbacks
def performSearch(self, request): def performSearch(self, request):
""" """
Perform a directory search for users, groups, and resources based on the Perform a directory search for users, groups, and resources based on
resourceSearch query parameter. Cache the results of that search so the resourceSearch query parameter. Cache the results of that search
that it will only be done once per request. so that it will only be done once per request.
""" """
if self._searchResults is not None: if self._searchResults is not None:
returnValue(self._searchResults) returnValue(self._searchResults)
Expand Down Expand Up @@ -168,10 +169,10 @@ def searchToSlots(results, tag):
shortName=record.shortNames[0], shortName=record.shortNames[0],
name=record.fullName, name=record.fullName,
typeStr={ typeStr={
"users" : "User", "users": "User",
"groups" : "Group", "groups": "Group",
"locations" : "Place", "locations": "Place",
"resources" : "Resource", "resources": "Resource",
}.get(record.recordType, repr(record.recordType)), }.get(record.recordType, repr(record.recordType)),
shortNames=str(", ".join(record.shortNames)), shortNames=str(", ".join(record.shortNames)),
authIds=str(", ".join(record.authIDs)), authIds=str(", ".join(record.authIDs)),
Expand Down Expand Up @@ -227,8 +228,8 @@ def __init__(self, resourceId, principalResource, davPropertyName,
@renderer @renderer
def propertyParseError(self, request, tag): def propertyParseError(self, request, tag):
""" """
Renderer to display an error when the user specifies an invalid property Renderer to display an error when the user specifies an invalid
name. property name.
""" """
if self.error is None: if self.error is None:
return "" return ""
Expand All @@ -240,8 +241,8 @@ def propertyParseError(self, request, tag):
@inlineCallbacks @inlineCallbacks
def davProperty(self, request, tag): def davProperty(self, request, tag):
""" """
Renderer to display an error when the user specifies an invalid property Renderer to display an error when the user specifies an invalid
name. property name.
""" """
if self.name is not None: if self.name is not None:
try: try:
Expand All @@ -263,10 +264,12 @@ def autoSchedule(self, request, tag):
""" """
Renderer which elides its tag for non-resource-type principals. Renderer which elides its tag for non-resource-type principals.
""" """
if (self.principalResource.record.recordType != "users" and if (
self.principalResource.record.recordType != "users" and
self.principalResource.record.recordType != "groups" or self.principalResource.record.recordType != "groups" or
self.principalResource.record.recordType == "users" and self.principalResource.record.recordType == "users" and
config.Scheduling.Options.AutoSchedule.AllowUsers): config.Scheduling.Options.AutoSchedule.AllowUsers
):
return tag return tag
return "" return ""


Expand Down Expand Up @@ -296,8 +299,8 @@ def isntAutoSchedule(self, request, tag):
@renderer @renderer
def autoScheduleModeDefault(self, request, tag): def autoScheduleModeDefault(self, request, tag):
""" """
Renderer which sets the 'selected' attribute on its tag based on the resource Renderer which sets the 'selected' attribute on its tag based on the
auto-schedule-mode. resource auto-schedule-mode.
""" """
if self.principalResource.getAutoScheduleMode() == "default": if self.principalResource.getAutoScheduleMode() == "default":
tag(selected='selected') tag(selected='selected')
Expand All @@ -307,8 +310,8 @@ def autoScheduleModeDefault(self, request, tag):
@renderer @renderer
def autoScheduleModeNone(self, request, tag): def autoScheduleModeNone(self, request, tag):
""" """
Renderer which sets the 'selected' attribute on its tag based on the resource Renderer which sets the 'selected' attribute on its tag based on the
auto-schedule-mode. resource auto-schedule-mode.
""" """
if self.principalResource.getAutoScheduleMode() == "none": if self.principalResource.getAutoScheduleMode() == "none":
tag(selected='selected') tag(selected='selected')
Expand All @@ -318,8 +321,8 @@ def autoScheduleModeNone(self, request, tag):
@renderer @renderer
def autoScheduleModeAcceptAlways(self, request, tag): def autoScheduleModeAcceptAlways(self, request, tag):
""" """
Renderer which sets the 'selected' attribute on its tag based on the resource Renderer which sets the 'selected' attribute on its tag based on the
auto-schedule-mode. resource auto-schedule-mode.
""" """
if self.principalResource.getAutoScheduleMode() == "accept-always": if self.principalResource.getAutoScheduleMode() == "accept-always":
tag(selected='selected') tag(selected='selected')
Expand All @@ -329,8 +332,8 @@ def autoScheduleModeAcceptAlways(self, request, tag):
@renderer @renderer
def autoScheduleModeDeclineAlways(self, request, tag): def autoScheduleModeDeclineAlways(self, request, tag):
""" """
Renderer which sets the 'selected' attribute on its tag based on the resource Renderer which sets the 'selected' attribute on its tag based on the
auto-schedule-mode. resource auto-schedule-mode.
""" """
if self.principalResource.getAutoScheduleMode() == "decline-always": if self.principalResource.getAutoScheduleMode() == "decline-always":
tag(selected='selected') tag(selected='selected')
Expand All @@ -340,8 +343,8 @@ def autoScheduleModeDeclineAlways(self, request, tag):
@renderer @renderer
def autoScheduleModeAcceptIfFree(self, request, tag): def autoScheduleModeAcceptIfFree(self, request, tag):
""" """
Renderer which sets the 'selected' attribute on its tag based on the resource Renderer which sets the 'selected' attribute on its tag based on the
auto-schedule-mode. resource auto-schedule-mode.
""" """
if self.principalResource.getAutoScheduleMode() == "accept-if-free": if self.principalResource.getAutoScheduleMode() == "accept-if-free":
tag(selected='selected') tag(selected='selected')
Expand All @@ -351,8 +354,8 @@ def autoScheduleModeAcceptIfFree(self, request, tag):
@renderer @renderer
def autoScheduleModeDeclineIfBusy(self, request, tag): def autoScheduleModeDeclineIfBusy(self, request, tag):
""" """
Renderer which sets the 'selected' attribute on its tag based on the resource Renderer which sets the 'selected' attribute on its tag based on the
auto-schedule-mode. resource auto-schedule-mode.
""" """
if self.principalResource.getAutoScheduleMode() == "decline-if-busy": if self.principalResource.getAutoScheduleMode() == "decline-if-busy":
tag(selected='selected') tag(selected='selected')
Expand All @@ -362,10 +365,13 @@ def autoScheduleModeDeclineIfBusy(self, request, tag):
@renderer @renderer
def autoScheduleModeAutomatic(self, request, tag): def autoScheduleModeAutomatic(self, request, tag):
""" """
Renderer which sets the 'selected' attribute on its tag based on the resource Renderer which sets the 'selected' attribute on its tag based on the
auto-schedule-mode. resource auto-schedule-mode.
""" """
if self.principalResource.getAutoScheduleMode() == "automatic" or not self.principalResource.getAutoScheduleMode(): if (
self.principalResource.getAutoScheduleMode() == "automatic" or
not self.principalResource.getAutoScheduleMode()
):
tag(selected='selected') tag(selected='selected')
return tag return tag


Expand Down Expand Up @@ -402,6 +408,7 @@ def proxyMatrix(self, request):
# FIXME: 'else' case needs to be handled by separate renderer # FIXME: 'else' case needs to be handled by separate renderer
readProxies = [] readProxies = []
writeProxies = [] writeProxies = []

def getres(ref): def getres(ref):
return self.adminResource.getResourceById(request, return self.adminResource.getResourceById(request,
str(proxyHRef)) str(proxyHRef))
Expand Down Expand Up @@ -438,7 +445,8 @@ def noProxies(self, request, tag):
@inlineCallbacks @inlineCallbacks
def hasProxies(self, request, tag): def hasProxies(self, request, tag):
""" """
Renderer which shows its tag if there are any proxies for this resource. Renderer which shows its tag if there are any proxies for this
resource.
""" """
mtx = yield self.proxyMatrix(request) mtx = yield self.proxyMatrix(request)
if mtx: if mtx:
Expand Down Expand Up @@ -570,8 +578,9 @@ def __init__(self, path, root, directory, store, principalCollections=()):
self.root = root self.root = root
self.directory = directory self.directory = directory
self.store = store self.store = store
super(WebAdminResource, self).__init__(path, super(WebAdminResource, self).__init__(
principalCollections=principalCollections) path, principalCollections=principalCollections
)




# Only allow administrators to access # Only allow administrators to access
Expand Down Expand Up @@ -640,28 +649,36 @@ def queryValues(arg):
# Update the auto-schedule value if specified. # Update the auto-schedule value if specified.
if autoSchedule is not None and (autoSchedule == "true" or if autoSchedule is not None and (autoSchedule == "true" or
autoSchedule == "false"): autoSchedule == "false"):
if (principal.record.recordType != "users" and if (
principal.record.recordType != "groups" or principal.record.recordType != "users" and
principal.record.recordType == "users" and principal.record.recordType != "groups" or
config.Scheduling.Options.AutoSchedule.AllowUsers): principal.record.recordType == "users" and
(yield principal.setAutoSchedule(autoSchedule == "true")) config.Scheduling.Options.AutoSchedule.AllowUsers
(yield principal.setAutoScheduleMode(autoScheduleMode)) ):
yield principal.setAutoSchedule(autoSchedule == "true")
yield principal.setAutoScheduleMode(autoScheduleMode)


# Update the proxies if specified. # Update the proxies if specified.
for proxyId in removeProxies: for proxyId in removeProxies:
proxy = self.getResourceById(request, proxyId) proxy = self.getResourceById(request, proxyId)
(yield action_removeProxyPrincipal(self.root, self.directory, self.store, yield action_removeProxyPrincipal(
principal, proxy, proxyTypes=["read", "write"])) self.root, self.directory, self.store,
principal, proxy, proxyTypes=["read", "write"]
)


for proxyId in makeReadProxies: for proxyId in makeReadProxies:
proxy = self.getResourceById(request, proxyId) proxy = self.getResourceById(request, proxyId)
(yield action_addProxyPrincipal(self.root, self.directory, self.store, yield action_addProxyPrincipal(
principal, "read", proxy)) self.root, self.directory, self.store, principal, "read", proxy
)


for proxyId in makeWriteProxies: for proxyId in makeWriteProxies:
proxy = self.getResourceById(request, proxyId) proxy = self.getResourceById(request, proxyId)
(yield action_addProxyPrincipal(self.root, self.directory, self.store, yield action_addProxyPrincipal(
principal, "write", proxy)) self.root, self.directory, self.store, principal,
"write", proxy
)





@inlineCallbacks @inlineCallbacks
Expand All @@ -678,9 +695,9 @@ def render(self, request):
response = Response() response = Response()
response.stream = MemoryStream(htmlContent) response.stream = MemoryStream(htmlContent)
for (header, value) in ( for (header, value) in (
("content-type", self.contentType()), ("content-type", self.contentType()),
("content-encoding", self.contentEncoding()), ("content-encoding", self.contentEncoding()),
): ):
if value is not None: if value is not None:
response.headers.setHeader(header, value) response.headers.setHeader(header, value)
returnValue(response) returnValue(response)
Expand All @@ -690,10 +707,16 @@ def getResourceById(self, request, resourceId):
if resourceId.startswith("/"): if resourceId.startswith("/"):
return request.locateResource(resourceId) return request.locateResource(resourceId)
else: else:
return principalForPrincipalID(resourceId, directory=self.directory) return principalForPrincipalID(
resourceId, directory=self.directory
)




@inlineCallbacks @inlineCallbacks
def search(self, searchStr): def search(self, searchStr):
records = list((yield self.directory.recordsMatchingTokens(searchStr.strip().split()))) records = list((
yield self.directory.recordsMatchingTokens(
searchStr.strip().split()
)
))
returnValue(records) returnValue(records)

0 comments on commit eb73ae4

Please sign in to comment.