pull the "current (principal) resource" out to the same level as "current collection"
[jelmer/calypso.git] / calypso / principal.py
1 import xml.etree.ElementTree as ET
2 from . import config, paths
3 from .xmlutils_generic import _tag
4
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.
9
10     Their interfaces for propfind could possibly be inherited to Collection and
11     collection Item in the future."""
12
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."""
16
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"""
20         return [self]
21
22     urlpath = None # this should be present ... implement as abstract property?
23
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
28
29     owner = property(lambda self: self.username)
30
31     def propfind(self, tag, element):
32         super(Principal, self).propfind(tag, element)
33
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 + '/'
40             element.append(tag)
41         elif tag == _tag("A", "addressbook-home-set"):
42             tag = ET.Element(_tag("D", "href"))
43             tag.text = self.urlpath + AddressbookHomeSet.type_dependent_suffix + '/'
44             element.append(tag)
45
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
50
51     owner = property(lambda self: self.username)
52
53     def propfind_children(self, depth, context):
54         # FIXME ignoring depth
55
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
61
62 class AddressbookHomeSet(HomeSet):
63     type_dependent_suffix = "addressbooks"
64     single_collection = "addresses"
65
66 class CalendarHomeSet(HomeSet):
67     type_dependent_suffix = "calendars"
68     single_collection = "calendar"
69