def html5upload(request): """ Renders the upload form page. """ if request.method == 'POST': # If the form has been submitted... result = [] for filefield, tmpname in handle_uploaded_file(request): c = Content() 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 c.get_type_instance() # Create thumbnail if it is supported c.save() #print originalname #generating json response array result.append({"name": originalname, "size": c.filesize, "url": c.uid, "thumbnail_url": '/content/instance/%s-200x200.jpg' % c.uid, "delete_url": '/content/delete/%s' % c.uid, "delete_type":"POST",}) #print result response_data = json.dumps(result) #print response_data #checking for json data type #big thanks to Guy Shapiro if "application/json" in request.META['HTTP_ACCEPT_ENCODING']: mimetype = 'application/json' else: mimetype = 'text/plain' return HttpResponse(response_data, mimetype=mimetype) return _render_to_response(request, 'content_html5upload.html', { })
def upload(request): """ Renders the upload form page. Args: request: the request object Returns: response: a http response object """ if request.method == 'POST': # If the form has been submitted... form = UploadForm(request.POST, request.FILES) # A form bound to the POST data if form.is_valid(): # All validation rules pass for filefield, tmpname in handle_uploaded_file(request): c = Content() 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 c.get_type_instance() # Create thumbnail if it is supported c.save() Uploadinfo.create(c, request) # uli.set_request_data(request) # uli.save() return HttpResponseRedirect(reverse('content:edit', args=[c.uid])) else: form = UploadForm(initial={}) # An unbound form return _render_to_response(request, 'content_upload.html', { 'uploadform': form, })
def api_upload(request): """ Renders the upload form page. Args: request: the request object Returns: response: a http response object """ 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 if form.is_valid(): # All validation rules pass for filefield, tmpname in handle_uploaded_file(request): SUPPORTED_FIELDS = ['title', 'caption', 'author'] kwargs = {} for field in SUPPORTED_FIELDS: kwargs[field] = request.POST.get(field) try: kwargs['point'] = Point(float(request.POST.get('lon')), float(request.POST.get('lat'))) except: # raise pass print kwargs c = Content(**kwargs) originalname = str(request.FILES["file"]) # Only authenticated users can use this view c.user = request.user # Save uploaded file to filesystem c.set_file(originalname, tmpname) # Create thumbnail if it is supported c.get_type_instance() c.save() Uploadinfo.create(c, request) break # We save only the first file response = HttpResponse(status=201) # response.status_code = 201 # FIXME: use reverse() response['Location'] = '/content/api/v1/content/%s/' % c.uid return response # return HttpResponseRedirect(reverse('content:edit', args=[c.uid])) else: response = HttpResponse(status=204) return response else: raise Http404
def html5upload(request): """ Renders the upload form page. Args: request: the request object Returns: response: a http response object """ if request.method == 'POST': # If the form has been submitted... result = [] for filefield, tmpname in handle_uploaded_file(request): c = Content() 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 c.get_type_instance() # Create thumbnail if it is supported c.save() Uploadinfo.create(c, request).save() # print originalname # generating json response array result.append({ "name": originalname, "size": c.filesize, "url": reverse('content:edit', args=[c.uid]), "thumbnail_url": '/content/instance/%s-200x200.jpg' % c.uid, "delete_url": reverse('content:edit', args=[c.uid]), "delete_type": "POST", }) # print result response_data = json.dumps(result) # print response_data # checking for json data type # big thanks to Guy Shapiro if "application/json" in request.META['HTTP_ACCEPT_ENCODING']: mimetype = 'application/json' else: mimetype = 'text/plain' return HttpResponse(response_data, mimetype=mimetype) return _render_to_response(request, 'content_html5upload.html', {})
def api_upload(request): """ Renders the upload form page. """ 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 if form.is_valid(): # All validation rules pass for filefield, tmpname in handle_uploaded_file(request): SUPPORTED_FIELDS = ['title', 'caption', 'author'] kwargs = {} for field in SUPPORTED_FIELDS: kwargs[field] = request.POST.get(field) try: kwargs['point'] = Point(float(request.POST.get('lon')), float(request.POST.get('lat'))) except: #raise pass print kwargs c = Content(**kwargs) 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 c.get_type_instance() # Create thumbnail if it is supported c.save() break # We save only the first file response = HttpResponse(status=201) #response.status_code = 201 # FIXME: use reverse() response['Location'] = '/content/api/v1/content/%s/' % c.uid return response #return HttpResponseRedirect(reverse('edit', args=[c.uid])) else: response = HttpResponse(status=204) return response else: raise Http404
def upload(request): """ Renders the upload form page. """ if request.method == 'POST': # If the form has been submitted... form = UploadForm(request.POST, request.FILES) # A form bound to the POST data if form.is_valid(): # All validation rules pass for filefield, tmpname in handle_uploaded_file(request): c = Content() 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 c.get_type_instance() # Create thumbnail if it is supported c.save() return HttpResponseRedirect(reverse('edit', args=[c.uid])) else: form = UploadForm(initial={}) # An unbound form return _render_to_response(request, 'content_upload.html', { 'uploadform' : form, })
def content_post(request): """ Create new Content(s) from file(s) found in posted multipart-form. Request must be multipart/form-data encoded and must have "Content-Type: multipart/form-data;" header present. Return a list of saved files containing uids, md5 and sha1 hash sums filesize and mimetype. """ saved_files = [] data = {} #print str(request.FILES) for filefield_name, filename in handle_uploaded_file(request): P = request.POST c = Content(user = request.user, author = P.get('sender', request.user.username), caption = P.get('caption'), keywords = P.get('tags'), privacy = P.get('privacy', 'PRIVATE'), ) originalname = str(request.FILES[filefield_name]) c.set_file(originalname, filename) c.get_type_instance() c.save() saved_files.append({ 'uid': c.uid, 'filesize': c.filesize, 'md5': c.md5, 'sha1': c.sha1, 'mimetype': c.mimetype, }) if len(saved_files) > 0: message = _(u'Saved %d file(s)' % len(saved_files)) data = { 'files': saved_files, 'id': saved_files[0]['uid'], # FIXME: remove backwards compatibility (pys60gps requires one id here) } return True, data, message else: message = _(u'File not found in post') return False, data, message