1
2
3 __doc__ = """
4 Base member class meant to handle Atom documents.
5
6 Use the AtomMember when your store has a collection
7 dealing with Atom documents. This measn that by
8 default every collections should use this class
9 to handle member resources which are simply
10 Atom entries.
11 """
12
13 import os
14 from xml.sax.saxutils import escape
15
16 from bridge import Element, Attribute
17 from bridge.common import ATOM10_PREFIX, ATOMPUB_PREFIX, XML_PREFIX, XML_NS, \
18 ATOM10_NS, ATOMPUB_NS, XHTML1_NS, XHTML1_PREFIX, atom_as_attr, atom_as_list, \
19 atom_attribute_of_element
20
21 from amplee.utils import generate_uuid_uri, get_isodate
22 from amplee.atompub.member import EntryMember
23 from amplee.atompub.member.helper import MemberHelper
24
26 - def __init__(self, collection, source, slug=None,
27 media_type=u'application/atom+xml',
28 entry_id_creator=None, **kwargs):
29 """
30
31 Keyword arguments:
32 collection -- AtomPubCollection instance carrying this member
33 source -- resource string to handle
34 slug -- hint on how to generate the name of the resource and
35 used as the last part of the edit and edit-media IRIs (default:None)
36 media_type -- mime-type for this member (default:u'application/atom+xml')
37 entry_id_creator -- callable which must return an unicode object used
38 for the id element of the entry (default:None)
39
40 The entry_id_creator callable, if provided, must take a bridge.Element
41 as unique parameter. This instance containing the newly created atom
42 entry or the one provided via the 'member' parameter.
43
44 When not provided the id of entry defaults to a default urn:uuid value.
45 """
46 EntryMember.__init__(self, collection)
47 self.media_type = media_type
48
49 doc = Element.load(source, as_attribute=atom_as_attr, as_list=atom_as_list,
50 as_attribute_of_element=atom_attribute_of_element)
51 doc.update_prefix(ATOM10_PREFIX, ATOM10_NS, ATOM10_NS)
52
53 id = media_id = member_id = generate_uuid_uri()
54 member_id = u'%s.atom' % member_id
55 self.member_id = member_id
56 self.media_id = media_id
57
58 if callable(entry_id_creator):
59 id = entry_id_creator(seed=doc)
60 elif doc.has_element('id', ATOM10_NS):
61 id = unicode(doc.id)
62
63 mh = MemberHelper(self.collection)
64 mh.initiate(id=id)
65 mh.copy_element('title', source=doc)
66 mh.copy_element('summary', source=doc)
67 mh.copy_elements('author', source=doc)
68 mh.copy_elements('category', source=doc)
69 attr = {u'rel': u'edit', u'type': self.collection.member_media_type,
70 u'href': unicode(escape("%s/%s" % (self.collection.base_edit_uri,
71 member_id))),}
72 mh.add_element('link', attributes=attr)
73
74 if doc.has_element('content', ATOM10_NS):
75 mh.copy_element('content', source=doc)
76 elif not doc.has_element('summary', ATOM10_NS):
77 mh.add_element('summary', attributes={u'type': u'text'})
78
79 self.entry = mh.entry
80 del mh
81