1
2 __docformat__ = 'epytext'
3 __doc__ = """
4 A workspace is a conceptual container holding a set of collections.
5 The organization brought by workspaces is mainly for clarity.
6 Consider workspaces being a drawer and collections folders inside.
7 """
8
9 __all__ = ['AtomPubWorkspace']
10
11 import amara
12 from amplee.utils import ATOM10_PREFIX, ATOMPUB_PREFIX, \
13 ATOM10_NS, ATOMPUB_NS, XML_PREFIX, XML_NS
14 from amplee.utils import qname
15
17 - def __init__(self, service, name_or_id, title, xml_attrs=None):
18 """Atom Publishing Protocol workspace entity
19
20 @type service: L{AtomPubService}
21 @param service: parent of this workspace instance
22
23 @type name_or_id: string
24 @param name_or_id: internal identifier.
25
26 @type title: unicode
27 @param title: human readable label of this workspace.
28
29 @type xml_attrs: dict
30 @param xml_attrs: allows for XML attributes to be provided: lang, base
31 """
32 self.service = service
33 self.name_or_id = name_or_id
34 self.title = title
35 self.xml_attrs = xml_attrs
36 self.service.workspaces.append(self)
37
38
39 self.collections = []
40
42 """Returns a collection identified by L{name_or_id}
43
44 @type name_or_id: string
45 @param name_or_id: identifier of the collection to look up
46
47 @rtype: L{AtomPubCollection}
48 @return: the instance of the collection retrieved or C{None}
49 """
50 for collection in self.collections:
51 if collection.name_or_id == name_or_id:
52 return collection
53
55 """Generate a amara instance of the workspace
56
57 The first collection shown will be the one having the L{favorite}
58 attribute set to C{True}.
59
60 @type prefix: string
61 @param prefix: XML prefix to use
62
63 @type namespace: string
64 @param namespace: namespace associated with the workspace element
65
66 @type include_id: bool
67 @param include_id: set this to C{True} in order to include the C{name_or_id}
68 value as an C{xml:id} attribute.
69
70 @rtype: L{amara.root_base}
71 @return: amara instance of the workspace document
72 """
73 doc = amara.create_document()
74 attrs = {}
75 if self.xml_attrs is not None:
76 for attr in self.xml_attrs:
77 attrs[(qname(attr, XML_PREFIX), XML_NS)] = self.xml_attrs[attr]
78
79 if include_id:
80 attrs[(qname(u'id', XML_PREFIX), XML_NS)] = unicode(self.name_or_id)
81
82 workspace = doc.xml_create_element(qname(u"workspace", prefix),
83 ns=namespace, attributes=attrs)
84
85 workspace.xml_append(doc.xml_create_element(qname(u"title", ATOM10_PREFIX), ns=ATOM10_NS,
86 attributes={u'type': u'text'}, content=self.title))
87
88 for collection in self.collections:
89 col = collection.to_collection(prefix=prefix, namespace=namespace)
90
91
92 if not collection.favorite:
93 workspace.xml_append(col)
94 else:
95 first_col = workspace.xml_child_elements.get('collection')
96 if not first_col:
97 workspace.xml_append(col)
98 else:
99 workspace.xml_insert_before(first_col, col)
100
101 return workspace
102