1 import xml.etree.ElementTree as ET
2 from . import config, paths
3 from .xmlutils_generic import _tag
5 class Resource(object):
6 """Resources initially were pseudo-collections/items (so they could be
7 included in the propfind loop), but currently are objects that represent
8 resources on the server that are not collectons / collection items.
10 Their interfaces for propfind could possibly be inherited to Collection and
11 collection Item in the future."""
13 def propfind(self, tag, element):
14 """If self can respond to a propfind request on a tag, update the
15 prepared response element with child nodes."""
17 def propfind_children(self, depth, context):
18 """Return a list of resources / collections / items that are to be
19 responded with to a propfind of a given depth"""
22 urlpath = None # this should be present ... implement as abstract property?
24 class Principal(Resource):
25 def __init__(self, username):
26 self.username = username
27 self.urlpath = config.get("server", "user_principal") % {"user": self.username} # it's currently hardcoded anyway
29 owner = property(lambda self: self.username)
31 def propfind(self, tag, element):
32 super(Principal, self).propfind(tag, element)
34 # maybe move those out to generic resources; kaddressbook doesn't query
35 # for current-user-princial and ask there, but plain go to the
36 # requested url and propfind for home-sets
37 if tag == _tag("C", "calendar-home-set"):
38 tag = ET.Element(_tag("D", "href"))
39 tag.text = self.urlpath + CalendarHomeSet.type_dependent_suffix + '/'
41 elif tag == _tag("A", "addressbook-home-set"):
42 tag = ET.Element(_tag("D", "href"))
43 tag.text = self.urlpath + AddressbookHomeSet.type_dependent_suffix + '/'
46 class HomeSet(Resource):
47 def __init__(self, username):
48 self.username = username
49 self.urlpath = config.get("server", "user_principal") % {"user": self.username} + self.type_dependent_suffix + "/" # it's currently hardcoded anyway
51 owner = property(lambda self: self.username)
53 def propfind_children(self, depth, context):
54 # FIXME ignoring depth
56 collection_name = paths.collection_from_path(self.username + "/" + self.single_collection)
57 from calypso import collection_singleton
58 collection = collection_singleton(collection_name)
59 items = [collection] # + collection.items # FIXME sequence matters, see parentcollectionhack
60 return super(HomeSet, self).propfind_children(depth) + items
62 class AddressbookHomeSet(HomeSet):
63 type_dependent_suffix = "addressbooks"
64 single_collection = "addresses"
66 class CalendarHomeSet(HomeSet):
67 type_dependent_suffix = "calendars"
68 single_collection = "calendar"