Пример #1
0
def reject_document(request, name, path=None):
    document = get_document(name=name, path=path)

    if document.current_edit_meta is None:
        raise PermissionDenied()

    notifications_factory.notification_document(
        request.user,
        document,
        strings.NOTIFICATION_DOCUMENT_REJECTED
    )

    edit_meta = document.current_edit_meta
    edit_meta.end_datetime = timezone.now()
    edit_meta.status = DocumentEditMeta.Status.REJECTED
    edit_meta.closed_by = request.user
    edit_meta.save()

    document.accepted_edit_meta = document.current_edit_meta
    document.current_edit_meta = None
    document.data = edit_meta.previous_document_data
    document.status = Document.Status.OPEN
    document.save()

    activity_factory.for_document(
        request.user,
        document,
        strings.i18n_format(
            strings.ACTIVITY_REVERT_DOCUMENT,
            datetime=edit_meta.previous_document_data.datetime
        ),
        document_data=edit_meta.previous_document_data
    )

    return Response({}, status=status.HTTP_200_OK)
Пример #2
0
    def perform_save(self, form):
        document = Document.objects.create(
            owner=self.request.user,
            parent=form.cleaned_data["parent"],
            name=form.cleaned_data["name"],
            status=form.cleaned_data["status"]
        )
        data = form.save(commit=False)
        data.document = document
        data.save()

        document.data = document.last_data
        document.owner = self.request.user
        document.followers.add(self.request.user)
        document.save()

        activity_factory.for_document(
            self.request.user,
            document,
            strings.i18n_format(
                strings.ACTIVITY_CREATE_DOCUMENT,
                name=document.name,
                path=document.path
            ),
            document_data=document.last_data
        )

        return document
Пример #3
0
def upload_new_version(request, name, path=None):
    document = get_document(name=name, path=path)

    if document.current_edit_meta is None:
        raise PermissionDenied()

    if request.method == "POST":
        # No instance because need new version of document and save old version
        form = UploadNewVersionForm(request.POST, request.FILES)
    else:
        form = UploadNewVersionForm(
            instance=document.data,
            initial={"name": name, "parent": document.parent}
        )

    context = browser_base_context(request)
    context.update({
        "form_title": _("Upload new version"),
        "form": form,
        "submit_title": _("Upload"),
        "validator_url": reverse("documents:validator-upload-new-version"),
    })

    if request.method == "POST" and form.is_valid():
        data = form.save(commit=False)
        data.document = document
        data.save()

        document.data = data

        activity_factory.for_document(
            request.user,
            document,
            form.cleaned_data["message"],
            document_data=data
        )

        # if document.name != form.cleaned_data["name"]:
        #     document.name = form.cleaned_data["name"]
        #     activity_factory.for_document(
        #         request.user,
        #         document,
        #         strings.i18n_format(
        #             strings.ACTIVITY_RENAME_DOCUMENT,
        #             old_name=document.name,
        #             new_name=form.cleaned_data["name"]
        #         )
        #     )

        document.save()
        messages.success(request, _("Successfully uploaded"))
        return redirect(document.permalink)

    return TemplateResponse(template="forms/form.html", request=request, context=context)
Пример #4
0
    def copy_document_data(dst_document, src_document_id):
        src_document = Document.objects.filter(pk=src_document_id).first()

        data = dst_document.data
        data.pk = None
        data.save()

        dst_document.data = data
        dst_document.save()

        activity_factory.for_document(
            request.user,
            dst_document,
            strings.ACTIVITY_COPY_DOCUMENT,
            ref={
                "instance": src_document,
                "text": strings.ACTIVITY_REF_SOURCE_DOCUMENT,
            }
        )
Пример #5
0
def rename_document(request, name, path=None):
    document = get_document(name=name, path=path)
    old_name = document.name

    if request.method == "POST":
        form = RenameDocumentForm(request.POST, instance=document)
    else:
        form = RenameDocumentForm(instance=document)

    context = browser_base_context(request)
    context.update({
        "form_title": _("Rename document"),
        "form": form,
        "submit_title": _("Rename"),
        "validator_url": reverse("documents:validator-rename-document"),
    })

    if request.method == "POST" and form.is_valid():
        redirect_next = request.GET.get("next", "single")

        if old_name != document.name:
            activity_factory.for_document(
                request.user,
                document,
                strings.i18n_format(
                    strings.ACTIVITY_RENAME_DOCUMENT,
                    old_name=old_name,
                    new_name=document.name
                )
            )

        form.save()
        messages.success(request, _("Successfully renamed"))

        if redirect_next == "list":
            return redirect(document.parent.permalink)
        else:
            return redirect(document.permalink)

    return TemplateResponse(template="forms/form.html", request=request, context=context)
Пример #6
0
def revert_document(request, name, path=None):
    document = get_document(name=name, path=path)
    activity_id = get_request_int_or_404(request, "data", "activity_id")
    activity = get_object_or_404(Activity, id=activity_id)

    if activity.document_data is None:
        raise ValidationError(_("Activity does not provide data"))

    if document.data.id == activity.document_data.id:
        raise ValidationError(_("Already this version"))

    document.data = activity.document_data
    document.save()

    activity_factory.for_document(
        request.user,
        document,
        strings.i18n_format(strings.ACTIVITY_REVERT_DOCUMENT, datetime=activity.datetime),
        document_data=activity.document_data
    )

    return Response({}, status=status.HTTP_200_OK)