Skip to content

Commit

Permalink
Added a generic AtomResponder and added get_atom method on models to
Browse files Browse the repository at this point in the history
easier map attributes to atom names.
Also did some code cleanup.
  • Loading branch information
ollej committed Jan 24, 2010
1 parent 9bd504b commit 51f87bc
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 14 deletions.
53 changes: 45 additions & 8 deletions forum/models.py
Expand Up @@ -25,6 +25,19 @@ class Profile(models.Model):
def get_absolute_url(self):
return ('profile', [str(self.id)])

def get_atom(self):
atom = dict(
title = self.name,
description = self.signature,
link = self.get_absolute_url(),
author_name = self.name,
author_email = self.email,
author_link = self.homepage,
pubdate = self.created,
)
return atom
atom = property(get_atom)

def __unicode__(self):
return self.name + "(" + self.email + ")"

Expand Down Expand Up @@ -56,6 +69,19 @@ def get_absolute_url(self):
def get_url(self):
return 'api/posts/' + str(self.id)

def get_atom(self):
atom = dict(
title = self.subject,
description = self.display_body,
link = self.get_absolute_url(),
author_name = self.author.name,
author_email = self.author.email,
author_link = self.author.homepage,
pubdate = self.created,
)
return atom
atom = property(get_atom)

def __unicode__(self):
return self.subject

Expand All @@ -71,24 +97,35 @@ def get_absolute_url(self):

def get_url(self):
return 'discussion/' + str(id)

url = property(get_url)

def get_author(self):
if self.posts[0]:
return self.posts[0].author

author = property(get_author)

def get_posts(self):
"""
Returns a QuerySet with all Posts in this Discussion.
"""
posts = Post.objects.select_related().filter(discussion=self.id)
return posts

posts = property(get_posts)

def get_author(self):
if self.posts[0]:
return self.posts[0].author
return Author()
author = property(get_author)

def get_atom(self):
atom = dict(
title = self.subject,
description = self.slug,
link = self.get_absolute_url(),
author_name = self.author.name,
author_email = self.author.email,
author_link = self.author.homepage,
pubdate = self.created,
)
return atom
atom = property(get_atom)

def __unicode__(self):
return self.subject

Expand Down
41 changes: 35 additions & 6 deletions urls.py
Expand Up @@ -25,13 +25,43 @@
responder = XMLResponder(paginate_by = 10)
)

class AtomResponder(XMLResponder):
def render(self, object_list):
"""
Serializes the objects in object_list into an Atom feed.
"""
f = feedgenerator.Atom1Feed(
title='Post list',
description='List of posts',
subtitle='List of posts',
link='',
feed_url='',
id='/posts',
guid='',
author_name='',
author_email='',
author_link='',
)
for o in object_list:
f.add_item(
title=o.atom['title'],
link=o.atom['link'],
description=o.atom['description'],
author_email=o.atom['author_email'],
author_name=o.atom['author_name'],
author_link=o.atom['author_link'],
pubdate=o.created
)
response = f.writeString("UTF-8")
return response


class AtomDiscussionResponder(XMLResponder):
def render(self, object_list):
"""
Serializes the first Discussion object in object_list into an Atom feed.
"""
obj = object_list[0]
posts = obj.posts
f = feedgenerator.Atom1Feed(
title=obj.subject,
description='Discussion: ' + obj.subject,
Expand Down Expand Up @@ -60,7 +90,6 @@ def render(self, object_list):
class DiscussionFeed(Collection):
def get_entry(self, did):
d = Discussion.objects.get(id=int(did))
posts = d.posts
entry = self.entry_class(self, d)
return entry

Expand Down Expand Up @@ -92,10 +121,10 @@ def delete(self, request, *args, **kwargs):
Deletes a discussion and all containing Posts.
TODO: Needs permission check.
"""
for p in Post.objects.filter(discussion=self.model):
for p in self.model.posts:
p.delete()
self.model.delete()
return HttpResponse(_("Object successfully deleted."), self.collection.responder.mimetype)
return HttpResponse(_("Discussion successfully deleted."), self.collection.responder.mimetype)
# ...

"""
Expand Down Expand Up @@ -127,7 +156,7 @@ def delete(self, request, *args, **kwargs):
xml_post_collection = Collection(
queryset = Post.objects.all(),
permitted_methods = ('GET', 'POST', 'PUT', 'DELETE'),
responder = XMLResponder(paginate_by = 10)
responder = AtomResponder(paginate_by = 10)
)

""""
Expand All @@ -142,7 +171,7 @@ def delete(self, request, *args, **kwargs):
xml_profile_resource = Collection(
queryset = Profile.objects.all(),
permitted_methods = ('GET', 'POST', 'PUT', 'DELETE'),
responder = XMLResponder(paginate_by = 10)
responder = AtomResponder(paginate_by = 10)
)

urlpatterns = patterns('',
Expand Down

0 comments on commit 51f87bc

Please sign in to comment.