except KeyError, err: pass except Exception, err: print str(err) raise tags = bundle.data.get('tags', []) if tags and isinstance(tags, list): tags = [x.lower() for x in tags] tag_str = ','.join(tags) bundle.obj.tags.clear() for tagname in tags: try: tag = Tag.objects.get(name=tagname) print "Tag old:", except Tag.DoesNotExist: tag = Tag(name=tagname) tag.save() print "Tag new:", print tagname, tag bundle.obj.tags.add(tag) if bundle.obj.content: bundle.obj.content.keywords = tag_str bundle.obj.content.save() # Copy coordinates from content (parsed while saving it if they existed) # FIXME: check that obj hasn't already valid point #if bundle.obj.content.point: # bundle.obj.point = bundle.obj.content.point #print "ON LISTA" if bundle.data.get('uid'): bundle.obj.uid = bundle.data.get('uid') # The owner of Entity is get from request's authenticated user
def api_upload(request): """ Renders the upload form page. """ try: if request.method == 'POST': # If the form has been submitted... #for header in request.META.keys(): # if header.startswith('HTTP'): # print header, request.META[header] #print request.raw_post_data[:1000] #if request.user.is_authenticated() is False: # return HttpResponse(status=401) form = UploadForm(request.POST, request.FILES) # A form bound to the POST data #validate_authorization(request) if form.is_valid(): # File was posted with form c = None data = dict(request.POST) jsondata = request.POST.get('data') if jsondata is None: # Temporary fix for ambiguous parameter name data / metadata jsondata = request.POST.get('metadata') if jsondata: try: # data is a json string containing the same keys as multipart form data = json.loads(jsondata) except: # if it was not valid json, use normal post data data = dict(request.POST) #raise SUPPORTED_FIELDS = ['title', 'caption', 'author'] kwargs = {} for field in SUPPORTED_FIELDS: kwargs[field] = data.get(field, '') try: kwargs['point'] = Point(float(data.get('lon')), float(data.get('lat'))) except: kwargs['point'] = None #raise pass # Create a new Egg e = Egg(**kwargs) print kwargs response_status = 201 # Created tags = data.get('tags') if tags: tags = [x.lower() for x in tags] tag_str = ','.join(tags) else: tags = [] tag_str = '' user = c = None file_md5_sums = [] for filefield, tmpname in handle_uploaded_file(request): print "HANDLING FILE:", filefield, tmpname c = Content(**kwargs) c.keywords = tag_str originalname = str(request.FILES["file"]) # c.user = request.user # Only authenticated users can use this view c.set_file(originalname, tmpname) # Save uploaded file to filesystem digest_maker = hashlib.md5() with open(c.file.path, 'rb') as f: buf = f.read(4096) while buf: digest_maker.update(buf) buf = f.read(4096) file_md5_sums.append(digest_maker.hexdigest()) #print digest_maker.hexdigest() c.get_type_instance() # Create thumbnail if it is supported c.save() # Copy coordinates from content (parsed while saving it if they existed) e.content = c if e.point is None and c.point: e.point = c.point e.uid = c.uid break # We save only the first file # Handle authorization after files are handled user = validate_4dnest_authorization(request, file_md5_sums) if c: c.user = user c.save() e.user = user print "USER", user if user: response_status = 201 # Created else: response_status = 401 # Unauthorized e.save() # We can handle tags after egg has id (it is saved) #print "LOOPING TAGS" for tagname in tags: try: tag = Tag.objects.get(name=tagname) #print "Tag old:", except Tag.DoesNotExist: tag = Tag(name=tagname) tag.save() #print "Tag new:", #print tagname, tag e.tags.add(tag) response = HttpResponse(status=response_status) #response.status_code = 201 # FIXME: use reverse() #return HttpResponseRedirect(reverse('egg api', args=[e.uid])) response['Location'] = '/fourdnest/api/v1/egg/%s/' % e.uid return response else: response = HttpResponse(status=204) return response else: raise Http404 except Exception, err: print err raise return HttpResponse("Server error", status=500)