Created
May 20, 2009 01:31
-
-
Save whalesalad/114554 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<<<<<<< HEAD:blacktip/api/bounding_box.py | |
from django.db import transaction | |
======= | |
from cStringIO import StringIO | |
from decimal import Decimal | |
from PIL import Image | |
from urllib2 import urlopen | |
from django.db import transaction | |
from django.core.files.base import ContentFile | |
>>>>>>> jstagger:blacktip/api/bounding_box.py | |
from ilovephotos.blacktip import models | |
from ilovephotos.blacktip.util.parse_help import verify_args, get_list_ids | |
from ilovephotos.util.mq.publisher import DefaultPublisher as MQPublisher | |
# This is a helper function for getting bounding boxes. So don't need to verify args. | |
def get_bounding_boxes(requester, object_id, privilege='r'): | |
"""Helper function to retrieve BoundingBoxes for the given ID (or list of). | |
Returns a QuerySet of the objects which may be seen by the requester""" | |
import media as api_media | |
bbs_id = get_list_ids(object_id) | |
bbs = models.BoundingBox.objects.filter(id__in=bbs_id) | |
media = api_media.get_media(requester, [bb.media.id for bb in bbs], privilege) | |
return bbs.filter(media__id__in=[x.id for x in media]) | |
<<<<<<< HEAD:blacktip/api/bounding_box.py | |
======= | |
def _get_thumbnail_data(bounding_box): | |
"""Internal helper function that will return a thumbnail for the BoundingBox instance.""" | |
exponent = Decimal('1') | |
im = Image.open(StringIO(urlopen(bounding_box.media.medium_url).read())) | |
bb_x = int((Decimal(bounding_box.x) * Decimal(im.size[0])).quantize(exponent)) | |
bb_y = int((Decimal(bounding_box.y) * Decimal(im.size[1])).quantize(exponent)) | |
bb_w = int((Decimal(bounding_box.width) * Decimal(im.size[0])).quantize(exponent)) | |
bb_h = int((Decimal(bounding_box.height) * Decimal(im.size[1])).quantize(exponent)) | |
bb = im.crop((bb_x, bb_y, (bb_x + bb_w), (bb_y + bb_h))) | |
outfile = StringIO() | |
bb.save(outfile, 'JPEG') | |
return ContentFile(outfile.getvalue()) | |
>>>>>>> jstagger:blacktip/api/bounding_box.py | |
@verify_args | |
def get_info(requester, bounding_box_id): | |
return get_bounding_boxes(requester, bounding_box_id) | |
@transaction.commit_on_success | |
@verify_args | |
def edit_info(requester, bounding_box_id, x=None, y=None, width=None, height=None): | |
"""Command to edit the information within this object. Only changes the | |
member variables that are explicitly passed in. Non-specified member | |
variables will not change. Should any member variable be able to | |
be set to a None value, passing an empty string will set it as such.""" | |
bbs = get_bounding_boxes(requester, bounding_box_id, 'w') | |
for bb in bbs: | |
if x is not None: | |
bb.x = x | |
if y is not None: | |
bb.y = y | |
if width is not None: | |
bb.width = width | |
if height is not None: | |
bb.height = height | |
bb.save() | |
return bbs | |
@transaction.commit_on_success | |
@verify_args | |
def delete(requester, bounding_box_id): | |
"""Deletes the bounding box. If the bounding box has an originating media | |
on the server, the media is disconnected from any Tags that have utilized | |
this box to create Icons.""" | |
bbs = get_bounding_boxes(requester, bounding_box_id, 'w') | |
for bb in bbs: | |
# unbind the bb's media from the tag the bb icon is in | |
if bb.media: | |
tags = models.Tag.objects.filter(icon__bounding_box=bb) | |
for t in tags: | |
MQPublisher.publish(routing_key='photo.untagged', body={ | |
'photo': bb.media.pk, | |
'tag': t.pk, | |
}) | |
bb.media.tags.remove(t) | |
bb.delete() | |
<<<<<<< HEAD:blacktip/api/bounding_box.py | |
return [] | |
======= | |
return [] # Why? | |
>>>>>>> jstagger:blacktip/api/bounding_box.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment