def get_upload(request): if request.POST: missing_keys = filter(lambda i: i not in request.POST.keys(), [u'md5sum', u'size']) if missing_keys: return render_to_json({ 'success': False, 'msg': _('md5sum and size required.')}, status=400) if int(request.POST['size']) > settings.MAXIMUM_UPLOAD_SIZE: return render_to_json({ 'success': False, 'msg': _('File is too big to upload.')}, status=400) for model in [Image, Video]: try: obj = model.objects.get(md5sum=request.POST['md5sum']) return render_to_json({'status': 'uploaded', 'object': obj.serialize()}) except model.DoesNotExist: pass upload = Upload.objects.create(md5sum=request.POST['md5sum'], size=int(request.POST['size']), owner=request.user) return render_to_json({'status': 'uploading', 'upload_id': upload.upload_id}) else: return render_to_json({ 'success': False, 'msg': _('post method required.')}, status=400)
def upload(request, upload_id): """ Get upload_id, content-range. IF Content Range start is not upload offset raise Error. Append chunk. IF file finished return obj. """ upload = get_object_or_404(Upload, upload_id=upload_id, owner=request.user, expires_at__gte=datetime.now()) chunk = request.body content_range = request.META.get("HTTP_CONTENT_RANGE", "") match = CONTENT_RANGE_PATTERN.match(content_range) if not match: return HttpResponseBadRequest("Content Range header required.") start = int(match.group("start")) end = int(match.group("end")) if upload.offset != start: return HttpResponseBadRequest("Offsets does not match.") chunk_size = end - start try: upload.append_data(chunk, size=chunk_size) except Exception as err: return render_to_json({"status": "failed", "msg": err.status}, status=400) if end >= upload.size: obj = upload.convert_to_model() return render_to_json({"status": "uploaded", "object": obj.serialize()}) return render_to_json({"upload_id": upload.upload_id, "status": "uploading", "offset": upload.offset})
def mark_as_read(request): resp = {'status'} if request.method == 'POST' and 'pk' in request.POST: nid = request.POST['pk'] nmeta = NotificationMeta.objects.get(sitenotification=nid) nmeta.is_read = True nmeta.save(update_fields=['is_read']) return render_to_json({'success': True}) return render_to_json({'success': False, 'message': 'Invalida data'})
def pending_follow_request_action(request): if request.method == 'POST': frpk = request.POST.get('pk') try: frpk = int(frpk) except ValueError: return render_to_json( {'errMsg': 'Invalida data'}, HttpResponseBadRequest) action = request.POST['action'] follow_request = get_object_or_404( UserFollow, pk=frpk, target=request.user) if action == 'accept': follow_request.status = UserFollow.FOLLOWING follow_request.save() follower_count_changed.send(sender=request.user) notify(ntype_slug='user_accepted_my_follow_request', sub=follow_request.target, obj=follow_request, recipient=follow_request.follower, url=follow_request.target.get_absolute_url()) return render_to_json({'success': True}) elif action == 'accept-restricted': follow_request.status = UserFollow.FOLLOWING_RESTRICTED follow_request.save() follower_count_changed.send(sender=request.user) notify(ntype_slug='user_accepted_my_follow_request', sub=follow_request.target, obj=follow_request, recipient=follow_request.follower, url=follow_request.target.get_absolute_url()) if action == 'decline': follow_request.delete() return render_to_json({'success': True}) return render_to_json({'success': False, 'message': 'Invalida data'})
def upload(request, upload_id): """ Get upload_id, content-range. IF Content Range start is not upload offset raise Error. Append chunk. IF file finished return obj. """ upload = get_object_or_404(Upload, upload_id=upload_id, owner=request.user, expires_at__gte=datetime.now()) chunk = request.body content_range = request.META.get('HTTP_CONTENT_RANGE', '') match = CONTENT_RANGE_PATTERN.match(content_range) if not match: return HttpResponseBadRequest('Content Range header required.') start = int(match.group('start')) end = int(match.group('end')) if upload.offset != start: return HttpResponseBadRequest('Offsets does not match.') chunk_size = end - start try: upload.append_data(chunk, size=chunk_size) except Exception as err: return render_to_json({'status': 'failed', 'msg': err.status}, status=400) if end >= upload.size: obj = upload.convert_to_model() return render_to_json({'status': 'uploaded', 'object': obj.serialize()}) return render_to_json({'upload_id': upload.upload_id, 'status': 'uploading', 'offset': upload.offset})
def get_upload(request): if request.POST: missing_keys = filter(lambda i: i not in request.POST.keys(), [u"md5sum", u"size"]) if missing_keys: return render_to_json({"success": False, "msg": _("md5sum and size required.")}, status=400) if int(request.POST["size"]) > settings.MAXIMUM_UPLOAD_SIZE: return render_to_json({"success": False, "msg": _("File is too big to upload.")}, status=400) for model in [Image, Video]: try: obj = model.objects.get(md5sum=request.POST["md5sum"]) return render_to_json({"status": "uploaded", "object": obj.serialize()}) except model.DoesNotExist: pass upload = Upload.objects.create( md5sum=request.POST["md5sum"], size=int(request.POST["size"]), owner=request.user ) return render_to_json({"status": "uploading", "upload_id": upload.upload_id}) else: return render_to_json({"success": False, "msg": _("post method required.")}, status=400)