Mercurial > public > sg101
annotate 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 |
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@285 | 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() |