comparison gpp/forums/attachments.py @ 285:8fd4984d5c3b

This is a first rough commit for #95, adding the ability to embed YouTube videos in forum posts. Some more polish and testing needs to happen at this point. I wanted to get all these changes off my hard drive and into the repository.
author Brian Neal <bgneal@gmail.com>
date Thu, 14 Oct 2010 02:39:35 +0000
parents
children 72fd300685d5
comparison
equal deleted inserted replaced
284:df2c81f705a8 285:8fd4984d5c3b
1 """
2 This module contains a class for handling attachments on forum posts.
3 """
4 from oembed.models import Oembed
5 from forums.models import Attachment
6
7
8 class AttachmentProcessor(object):
9 """
10 This class is aggregated by various form classes to handle
11 attachments on forum posts. New posts can receive attachments and edited
12 posts can have their attachments replaced, augmented, or deleted.
13
14 """
15 def __init__(self, ids):
16 """
17 This class is constructed with a list of Oembed ids. We retrieve the
18 actual Oembed objects associated with these keys for use in subsequent
19 operations.
20
21 """
22 # ensure all ids are integers
23 self.pks = []
24 for pk in ids:
25 try:
26 pk = int(pk)
27 except ValueError:
28 continue
29 self.pks.append(pk)
30
31 self.embeds = []
32 if self.pks:
33 self.embeds = Oembed.objects.in_bulk(self.pks)
34
35 def save_attachments(self, post):
36 """
37 Create and save attachments to the supplied post object.
38 Any existing attachments on the post are removed first.
39
40 """
41 post.attachments.clear()
42
43 for n, pk in enumerate(self.pks):
44 attachment = Attachment(post=post, embed=self.embeds[pk], order=n)
45 attachment.save()