def json_to_db(filename): data = read_from_file(filename) for key,value in data.iteritems(): event = Event(str_id=key, name=value['name'], is_public=True) event.save() print "created event %s" %key for media in value['media']: media = Media(full_res=media['full_res'], thumbnail = media['thumbnail'], event=event) media.save()
def upload_image(request): success = True error_msg = "" obj_created = None try: event_id = request.POST.get("event_id") # TODO ensure user is part of the event media = Media(event=Event.objects.get(str_id=event_id)) media.save() filename = handle_uploaded_file(request.FILES["media_data"], media.id) media.full_res = filename # TODO create a real thumbnail media.thumbnail = filename media.save() obj_created = media.serialize() except ObjectDoesNotExist: success = False error_msg = "Event %s does not exist" % event_id except Exception as e: success = False error_msg = str(e) return {"success": success, "error_msg": error_msg, "obj_created": obj_created}