comparison forums/attachments.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/forums/attachments.py@72fd300685d5
children
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
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()
46
47 def has_attachments(self):
48 """
49 Return true if we have valid pending attachments.
50
51 """
52 return len(self.embeds) > 0
53
54 def get_ids(self):
55 """
56 Return the list of Oembed ids.
57
58 """
59 return self.pks