Ejemplo n.º 1
0
    def confirm_datasheet_upload(self, request, visit_id, sitevisitfile_id):
        # Read-only users cannot upload datasheets.
        if readonly_user(request.user):
            raise PermissionDenied

        obj = self.get_object(request, unquote(visit_id))

        sv_file = SiteVisitDataFile.objects.get(id=sitevisitfile_id)

        if request.method == 'POST':
            if request.POST.get('_confirm'):
                # delete existing sitevisit (which will delete all associated objects)
                SiteVisit.objects.filter(visit=sv_file.visit, site=sv_file.site).delete()

                # create the objects
                self.message_user(request, 'Datasheet for {} successfully uploaded!'.format(obj))
                builder = SiteVisitDataBuilder(sv_file)
                builder.build_all(keep_invalid=False)
            elif request.POST.get('_cancel'):
                self.message_user(request, 'Datasheet upload cancelled.'.format(obj),
                                  level=logging.WARNING)

            return HttpResponseRedirect(reverse('old_publish:publish_report'))
            # return HttpResponseRedirect(reverse('admin:main_visit_change', args=[visit_id]))

        context = {
            'original': obj,
            'has_permission': True,
            'has_add_permission': self.has_add_permission(request),
            'has_change_permission': self.has_change_permission(request, sv_file.visit),
            'title': 'Confirm upload of a completed datasheet for {}'.format(sv_file.visit),
            'site_url': admin.site.site_url,
            'opts': self.model._meta,
            'is_popup': False,
            'save_as': False,
        }
        return TemplateResponse(request, "main/visit_confirm_datasheet_upload.html", context, current_app='main')
Ejemplo n.º 2
0
    def confirm_datasheet_upload(self, request, visit_id, sitevisitfile_id):
        # Read-only users cannot upload datasheets.
        if readonly_user(request.user):
            raise PermissionDenied

        obj = self.get_object(request, unquote(visit_id))

        sv_file = SiteVisitDataFile.objects.get(id=sitevisitfile_id)

        if request.method == "POST":
            if request.POST.get("_confirm"):
                # delete existing sitevisit (which will delete all associated objects)
                SiteVisit.objects.filter(visit=sv_file.visit, site=sv_file.site).delete()

                # create the objects
                self.message_user(request, "Datasheet for {} successfully uploaded!".format(obj))
                builder = SiteVisitDataBuilder(sv_file)
                builder.build_all(keep_invalid=False)
            elif request.POST.get("_cancel"):
                self.message_user(request, "Datasheet upload cancelled.".format(obj), level=logging.WARNING)

            return HttpResponseRedirect(reverse("publish_report"))
            # return HttpResponseRedirect(reverse('admin:main_visit_change', args=[visit_id]))

        context = {
            "original": obj,
            "has_permission": True,
            "has_add_permission": self.has_add_permission(request),
            "has_change_permission": self.has_change_permission(request, sv_file.visit),
            "title": "Confirm upload of a completed datasheet for {}".format(sv_file.visit),
            "site_url": admin.site.site_url,
            "opts": self.model._meta,
            "is_popup": False,
            "save_as": False,
        }
        return TemplateResponse(request, "main/visit_confirm_datasheet_upload.html", context, current_app="main")
Ejemplo n.º 3
0
    def upload_datasheet(self, request, object_id):
        # Read-only users cannot upload datasheets.
        if readonly_user(request.user):
            raise PermissionDenied

        obj = self.get_object(request, unquote(object_id))

        if request.method == 'POST':
            form = forms.UploadDatasheetForm(request.POST, request.FILES)

            if request.POST.get('_cancel'):
                self.message_user(request, 'Datasheet upload cancelled.'.format(obj), level=logging.WARNING)
                return HttpResponseRedirect(reverse('old_publish:publish_report'))
                # return HttpResponseRedirect(reverse('admin:main_visit_change', args=[object_id]))
            if form.is_valid():
                # create the SiteVisitDataFile
                file_ = form.cleaned_data['file']
                sv_file = SiteVisitDataFile.objects.create(file=file_, visit=obj, user=request.user)

                # get site visit if it exists so that user can be informed that the site visit has
                # already been approved before they have to fix all the errors
                site_visit = get_sitevisit_from_datasheet(sv_file)
                if site_visit is not None and site_visit.data_status == 'approved':
                    self.message_user(request, """Datasheet for this site visit has already been uploaded and approved by the
                    curator, so cannot be overwritten.""".format(obj), level=logging.WARNING)
                    return HttpResponseRedirect(reverse('old_publish:publish_report'))
                    # return HttpResponseRedirect(reverse('admin:main_visit_change', args=[object_id]))

                validator = SiteDataFileValidator(sv_file)
                error_obj = validator.validate()

                if error_obj:
                    html_error_message = 'The file contains errors:'
                    for error in error_obj.message.split('\n'):
                        html_error_message += '<p>{}</p>'.format(error)

                    self.message_user(request, html_error_message, level=logging.ERROR)
                else:
                    if site_visit is not None:
                        # check if sitevisit already exists here and redirect to confirm page
                        return HttpResponseRedirect(
                            reverse('admin:main_visit_confirm_datasheet_upload', args=[object_id, sv_file.id]))

                    # no error
                    # create the objects
                    self.message_user(request, 'Datasheet for {} successfully uploaded!'.format(obj))
                    builder = SiteVisitDataBuilder(sv_file)
                    builder.build_all(keep_invalid=False)

                    # Return to the old_publish/data view.
                    return HttpResponseRedirect(reverse('old_publish:publish_report'))
                    # return HttpResponseRedirect(reverse('admin:main_visit_change', args=[object_id]))

        else:
            form = forms.UploadDatasheetForm()

        context = {
            'original': obj,
            'has_permission': True,
            'has_add_permission': self.has_add_permission(request),
            'has_change_permission': self.has_change_permission(request, obj),
            'title': 'Upload a completed datasheet for {}'.format(obj),
            'site_url': admin.site.site_url,
            'errors': form.errors,
            'opts': self.model._meta,
            'is_popup': False,
            'save_as': False,
            'form': form,
        }
        request.current_app = 'main'
        return TemplateResponse(request, "main/visit_upload_datasheet.html", context)
Ejemplo n.º 4
0
    def upload_datasheet(self, request, object_id):
        # Read-only users cannot upload datasheets.
        if readonly_user(request.user):
            raise PermissionDenied

        obj = self.get_object(request, unquote(object_id))

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

            if request.POST.get("_cancel"):
                self.message_user(request, "Datasheet upload cancelled.".format(obj), level=logging.WARNING)
                return HttpResponseRedirect(reverse("publish_report"))
                # return HttpResponseRedirect(reverse('admin:main_visit_change', args=[object_id]))
            if form.is_valid():
                # create the SiteVisitDataFile
                file_ = form.cleaned_data["file"]
                sv_file = SiteVisitDataFile.objects.create(file=file_, visit=obj, user=request.user)

                # get site visit if it exists so that user can be informed that the site visit has
                # already been approved before they have to fix all the errors
                site_visit = get_sitevisit_from_datasheet(sv_file)
                if site_visit is not None and site_visit.data_status == "approved":
                    self.message_user(
                        request,
                        """Datasheet for this site visit has already been uploaded and approved by the
                    curator, so cannot be overwritten.""".format(
                            obj
                        ),
                        level=logging.WARNING,
                    )
                    return HttpResponseRedirect(reverse("publish_report"))
                    # return HttpResponseRedirect(reverse('admin:main_visit_change', args=[object_id]))

                validator = SiteDataFileValidator(sv_file)
                error_obj = validator.validate()

                if error_obj:
                    html_error_message = "The file contains errors:"
                    for error in error_obj.message.split("\n"):
                        html_error_message += "<p>{}</p>".format(error)

                    self.message_user(request, html_error_message, level=logging.ERROR)
                else:
                    if site_visit is not None:
                        # check if sitevisit already exists here and redirect to confirm page
                        return HttpResponseRedirect(
                            reverse("admin:main_visit_confirm_datasheet_upload", args=[object_id, sv_file.id])
                        )

                    # no error
                    # create the objects
                    self.message_user(request, "Datasheet for {} successfully uploaded!".format(obj))
                    builder = SiteVisitDataBuilder(sv_file)
                    builder.build_all(keep_invalid=False)

                    # Return to the publish/data view.
                    return HttpResponseRedirect(reverse("publish_report"))
                    # return HttpResponseRedirect(reverse('admin:main_visit_change', args=[object_id]))

        else:
            form = forms.UploadDatasheetForm()

        context = {
            "original": obj,
            "has_permission": True,
            "has_add_permission": self.has_add_permission(request),
            "has_change_permission": self.has_change_permission(request, obj),
            "title": "Upload a completed datasheet for {}".format(obj),
            "site_url": admin.site.site_url,
            "errors": form.errors,
            "opts": self.model._meta,
            "is_popup": False,
            "save_as": False,
            "form": form,
        }
        request.current_app = "main"
        return TemplateResponse(request, "main/visit_upload_datasheet.html", context)