annotate forums/attachments.py @ 1156:8c901ab0df62

Update copyright to 2017.
author Brian Neal <bgneal@gmail.com>
date Mon, 16 Jan 2017 13:23:46 -0600
parents ee87ea74d46b
children
rev   line source
bgneal@285 1 """
bgneal@285 2 This module contains a class for handling attachments on forum posts.
bgneal@285 3 """
bgneal@285 4 from oembed.models import Oembed
bgneal@285 5 from forums.models import Attachment
bgneal@285 6
bgneal@285 7
bgneal@285 8 class AttachmentProcessor(object):
bgneal@285 9 """
bgneal@285 10 This class is aggregated by various form classes to handle
bgneal@285 11 attachments on forum posts. New posts can receive attachments and edited
bgneal@285 12 posts can have their attachments replaced, augmented, or deleted.
bgneal@285 13
bgneal@285 14 """
bgneal@285 15 def __init__(self, ids):
bgneal@285 16 """
bgneal@285 17 This class is constructed with a list of Oembed ids. We retrieve the
bgneal@285 18 actual Oembed objects associated with these keys for use in subsequent
bgneal@285 19 operations.
bgneal@286 20
bgneal@285 21 """
bgneal@285 22 # ensure all ids are integers
bgneal@285 23 self.pks = []
bgneal@285 24 for pk in ids:
bgneal@285 25 try:
bgneal@285 26 pk = int(pk)
bgneal@285 27 except ValueError:
bgneal@285 28 continue
bgneal@285 29 self.pks.append(pk)
bgneal@285 30
bgneal@285 31 self.embeds = []
bgneal@285 32 if self.pks:
bgneal@285 33 self.embeds = Oembed.objects.in_bulk(self.pks)
bgneal@285 34
bgneal@285 35 def save_attachments(self, post):
bgneal@285 36 """
bgneal@285 37 Create and save attachments to the supplied post object.
bgneal@285 38 Any existing attachments on the post are removed first.
bgneal@285 39
bgneal@285 40 """
bgneal@285 41 post.attachments.clear()
bgneal@285 42
bgneal@285 43 for n, pk in enumerate(self.pks):
bgneal@285 44 attachment = Attachment(post=post, embed=self.embeds[pk], order=n)
bgneal@285 45 attachment.save()
bgneal@286 46
bgneal@286 47 def has_attachments(self):
bgneal@286 48 """
bgneal@286 49 Return true if we have valid pending attachments.
bgneal@286 50
bgneal@286 51 """
bgneal@286 52 return len(self.embeds) > 0
bgneal@286 53
bgneal@286 54 def get_ids(self):
bgneal@286 55 """
bgneal@286 56 Return the list of Oembed ids.
bgneal@286 57
bgneal@286 58 """
bgneal@286 59 return self.pks