def post(self, request, **kwargs): form = MediaUploadForm(request.POST, request.FILES) model = get_model(kwargs['app_label'], kwargs['model'], kwargs['id']) redir_url = request.GET.get('redir_url', '/') permStr = '{0}.add_media_to_{1}'.format( model._meta.app_label, model.__class__.__name__.lower()) if not form.is_valid(): messages.warning(request, 'No file to upload') elif not request.user.has_perm(permStr): messages.error(request, 'Missing permissions') else: media = MediaFile(content_object=model, media=request.FILES['media'], filename=request.FILES['media'].name) media.save() messages.success(request, 'Media uploaded successfully') return HttpResponseRedirect(redir_url)
def post(self, request, **kwargs): form = MediaUploadForm(request.POST, request.FILES) model = get_model(kwargs['app_label'], kwargs['model'], kwargs['id']) redir_url = request.GET.get('redir_url', '/') permStr = '{0}.add_media_to_{1}'.format(model._meta.app_label, model.__class__.__name__.lower()) if not form.is_valid(): messages.warning(request, 'No file to upload') elif not request.user.has_perm(permStr): messages.error(request, 'Missing permissions') else: media = MediaFile(content_object = model, media = request.FILES['media'], filename = request.FILES['media'].name) media.save() messages.success(request, 'Media uploaded successfully') return HttpResponseRedirect(redir_url)
def get(self, request, pk): problem = get_object_or_404(Problem, pk=pk) s = io.BytesIO() with ZipFile(s, 'w') as curZip: curZip.writestr('problem.json', serialize('json', [problem])) curZip.writestr('test_group.json', serialize('json', problem.testgroup_set.all())) curZip.writestr('test.json', serialize('json', problem.test_set.all())) media = MediaFile.get_for_object(problem).all() for f in media: try: curZip.write(f.media.url[1:]) except FileNotFoundError: f.delete() curZip.writestr('media.json', serialize('json', media)) response = HttpResponse(s.getvalue(), content_type = \ 'application/x-zip-compressed') response['Content-Disposition'] = 'attachment; filename="problem.zip"' return response
def get(self, request, pk): problem = get_object_or_404(Problem, pk = pk) s = io.BytesIO() with ZipFile(s, 'w') as curZip: curZip.writestr('problem.json', serialize('json', [problem])) curZip.writestr('test_group.json', serialize('json', problem.testgroup_set.all())) curZip.writestr('test.json', serialize('json', problem.test_set.all())) media = MediaFile.get_for_object(problem).all() for f in media: try: curZip.write(f.media.url[1:]) except FileNotFoundError: f.delete() curZip.writestr('media.json', serialize('json', media)) response = HttpResponse(s.getvalue(), content_type = \ 'application/x-zip-compressed') response['Content-Disposition'] = 'attachment; filename="problem.zip"' return response