Created
January 27, 2014 03:15
-
-
Save hanleybrand/8642776 to your computer and use it in GitHub Desktop.
snippet from import_presentation -- see https://gist.github.com/hanleybrand/8642400 for sample metadata
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
from rooibos.data.models import Record, standardfield, Field, FieldValue, Collection, CollectionItem | |
from rooibos.presentation.models import Presentation, PresentationItem | |
from rooibos.storage.models import Media, Storage | |
from rooibos.util import guess_extension | |
import simplejson | |
import requests | |
from PIL import Image | |
from StringIO import StringIO | |
from django.contrib.auth.models import User | |
mdidBaseURL = 'http://mdid3.school.edu' | |
mdid_api = {'auth': mdidBaseURL + '/api/login/', | |
'presentations': mdidBaseURL + '/api/presentations/currentuser/', | |
'presentation': mdidBaseURL + '/api/presentation/', } | |
store = Storage.objects.get(pk=1) | |
collection = Collection.objects.get(pk=1) | |
target_user = 'validusername' | |
# fid specifically matches our VRC's id label | |
# I use [requests lib](http://docs.python-requests.org/en/v1.2.3/) to manage login, sessions and api requests | |
# and then simplejson to iterate over the returned json objects using: jp = simplejson.loads(mdid_api_presentation.content) | |
# I left that code out so my problem would be clearer, | |
# basically the variable jp is a python dict/simplejson object version of the json returned by | |
# http://mdid3.server.edu/api/presentation/' + showID + '/' | |
jp = simplejson.loads(mdid_api_presentation.content) | |
presentation = Presentation.objects.create(title=jp['title'], | |
owner=User.objects.get(username=target_user), | |
description=jp['description']) | |
# jp['content'] is the slide list | |
fid = Field.objects.get(label='ID', standard__prefix='aae') | |
for order, slide in enumerate(jp['content']): | |
rec_exists = False | |
rec_id = None | |
# first check if slide matches identifier from our vrc (fid) | |
for metadata in slide['metadata']: | |
if metadata['label'] == 'ID': | |
rec_id = metadata['value'] | |
if Record.by_fieldvalue(fid, rec_id): | |
rec_exists = True | |
print '%s already exists' % rec_id | |
break | |
# when finished checking for ID either add existing record to pres or create record and then add it | |
if rec_exists: | |
# note that record is the first record in the list that is returned byfieldvalue | |
# which is probably univrsally lazy, but good enough for this specific instance | |
record = Record.by_fieldvalue(fid, rec_id)[0] | |
presentation.items.create(order=order, record=record) | |
presentation.save() | |
print 'adding %s to presentation at position %s' % (rec_id, order) | |
else: | |
# in this application I'm basically always creating a personal record for target_user | |
# I'm thinking I should probably not use the rec_id in this case - maybe this is part of the problem? | |
# but it actualy works, so maybe rec_id = None gets the random r-##### name | |
print 'creating record for %s' % rec_id | |
print 'metadata:' | |
print slide['metadata'] | |
record = Record.objects.create(name=rec_id, owner=target_user) | |
record.save() | |
for metadata in slide['metadata']: | |
try: | |
target = Field.objects.get(label=metadata['label'], standard__prefix='aae') | |
record.fieldvalue_set.create(field=target, | |
value=metadata['value'], | |
label=metadata['label'], ) | |
except Exception as e: | |
print e | |
continue | |
try: | |
title = slide['title'] | |
except: | |
title = 'Untitled' | |
FieldValue.objects.create(record=record, | |
field=standardfield('title'), | |
order=0, | |
value=title) | |
col_i = CollectionItem.objects.create(collection=collection, record=record) | |
print 'collection item created: %s' % col_i | |
#### Everything above this line seems to be working - records appear with target_owner as the user, but they | |
# never have a thumbnail, and the media file doesn't get associated with the record, so this is the part where I'm | |
# missing something crucial, obviously | |
# This section is pretty hacky at this point because I've tried a number of avenues to download the | |
# image, but I'm missing something regarding storage, media and records. | |
media_url = mdidBaseURL + slide['image'] | |
# rc is handled in the redacted section - it just holds the session/login info | |
# the .get method is basically a normal browser request | |
media_req = requests.get(media_url, cookies=rc) | |
# media_req is a requests library object that holds all the different attributes of a browser request, | |
# so in this case where I'm requesting an image file directly: | |
# media_req.headers is the headers | |
# media_req.headers['content-type'] is the mimetype header | |
# media_req.content contains the binary image data | |
mimetype = media_req.headers['content-type'] | |
# according to the requests docs this is the correct way | |
# to work with binary image files contained in the requests object | |
# Image.open(StringIO(requests_object.content) | |
media_file = Image.open(StringIO(media_req.content), 'r') | |
# I'm not sure if this part is necessary, but it was for PIL's Image.save() method if I remember correctly | |
if guess_extension(mimetype) == '.jpeg': | |
filename = record.name + '.jpg' | |
extension = 'JPEG' | |
else: | |
filename = os.path.join(record.name + guess_extension(mimetype)) | |
extension = os.path.splitext(mimetype)[0] | |
print 'extension %s' % extension | |
# this way tried to use PIL's Image.save() method, and did not fully work (no media is associated with the record) | |
# but the file was saved in the correct directory | |
file_path = os.path.join(store.base, filename) | |
media_file.save(file_path) | |
# my first tries were all based around the methods in rooibos.storage, but I couldn't figure out | |
# why the file-like object media_file would throw attribute errors | |
# I also wasn't clear if I should use the Storage.save() method or the Media.save() method | |
#store.save_file(filename, media_file) | |
#media.save_file(filename, media_file) | |
#media_file.save() | |
# I think this works, but the media object doesn't get associated with the record | |
media = Media.objects.create(record=record, | |
name=os.path.splitext(filename)[0], | |
url=filename, | |
storage=store, | |
mimetype=mimetype) | |
media.save() | |
presentation.items.create(order=order, record=record) | |
presentation.save() | |
## so I end up with a presentation that has all the slides, but no thumbs and no images (but all of the records look correct) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment