コード例 #1
0
ファイル: views.py プロジェクト: acadsamplesdb/acad_samples
def attach_file(request, pk, model):
    from django.contrib import messages
    object = get_object_or_404(model, pk=pk)

    if request.method == "POST":
        form = samples.forms.UploadFileForm(request.POST, request.FILES)

        if form.is_valid():
            from django.forms import ValidationError
            uploaded = request.FILES.getlist('file')
            for uf in uploaded:
                file = FileAttachment(file=uf)
                try:
                    file.save()
                except ValidationError as e:
                    return HttpResponse(e)
                object.file.add(file)
                object.save()

            cleaned_data = form.cleaned_data
            if cleaned_data["next"]:
                return HttpResponseRedirect(cleaned_data["next"] + "#files")
            else:
                return HttpResponse("it worked")
        else:
            return HttpResponseRedirect(form.data["next"] + "#files")

    else:
        return HttpResponse("implement something here")
コード例 #2
0
    def attach_file(self, fn, pk, model=Sample):
        try:
            target = model.objects.get(pk=pk)
            print(target)

            f = open(fn, 'rb')
            attachment = FileAttachment(file=File(f))
            try:
                attachment.save()
                target.file.add(attachment)
                target.save()
                self.stdout.write('%s has been attached to sample.pk = %s' %
                                  (fn, pk))
            except (S3ResponseError, S3CreateError, BotoClientError) as e:
                self.stderr.write(
                    'Storage error raised when attaching file "%s" to sample.pk = %s. Exception detail: '
                    % (fn, pk))
                self.stderr.write(str(e))
            except Exception as e:
                self.stderr.write(
                    'Failed to attach file "%s" to sample.pk = %s. Exception detail: '
                    % (fn, pk))
                self.stderr.write(str(e))
            else:
                f.close()
        except model.DoesNotExist:
            self.stderr.write('Model canot be found with pk = "%s"' % pk)
        except Exception as e:
            self.stderr.write(str(e))
コード例 #3
0
ファイル: views.py プロジェクト: acadsamplesdb/acad_samples
def upload_file(request):
    if request.method == "POST":
        form = samples.forms.UploadFileForm(request.POST, request.FILES)

        if form.is_valid():
            from django.forms import ValidationError
            file = FileAttachment(file=request.FILES["file"])
            try:
                file.save()
            except ValidationError as e:
                return HttpResponse(e)
            cleaned_data = form.cleaned_data
            if cleaned_data["next"]:
                return HttpResponseRedirect(cleaned_data["next"])
            else:
                return HttpResponse("it worked")
        else:
            print(form.cleaned_data)
            return HttpResponse("invalid form data")

    else:
        form = samples.forms.UploadFileForm()
        return render(request, "upload_file.html", {"form": form})