Beispiel #1
0
    def collection_post(self):
        """ Agreement Change create """
        agreement = self.request.validated["agreement"]

        change = self.request.validated["change"]
        if change["dateSigned"]:
            changes = agreement.get("changes", [])
            active_changes = [c for c in changes if c.status == "active"]
            if len(active_changes) > 0:
                last_change = active_changes[-1]
                last_date_signed = last_change.dateSigned
                if not last_date_signed:  # BBB old active changes
                    last_date_signed = last_change.date
                obj_str = "last active change"
            else:
                last_date_signed = agreement.dateSigned
                obj_str = "agreement"

            if last_date_signed:  # BBB very old agreements
                if change["dateSigned"] < last_date_signed:
                    # Can't move validator because of code above
                    raise_operation_error(
                        self.request,
                        "Change dateSigned ({}) can't be earlier than {} dateSigned ({})"
                        .format(change["dateSigned"].isoformat(), obj_str,
                                last_date_signed.isoformat()),
                    )

        agreement.changes.append(change)
        warnings = apply_modifications(self.request, agreement)

        if save_agreement(self.request):
            self.LOGGER.info(
                "Created change {} of agreement {}".format(
                    change.id, agreement.id),
                extra=context_unpack(
                    self.request,
                    {"MESSAGE_ID": "agreement_change_create"},
                    {
                        "change_id": change.id,
                        "agreement_id": agreement.id
                    },
                ),
            )
            self.request.response.status = 201
            response_data = {"data": change.serialize("view")}
            if warnings:
                response_data["warnings"] = warnings
                self.LOGGER.info(
                    "warnings: {}".format(warnings),
                    extra=context_unpack(
                        self.request,
                        {"MESSAGE_ID": "agreement_change_create"},
                        {
                            "change_id": change.id,
                            "agreement_id": agreement.id
                        },
                    ),
                )
            return response_data
Beispiel #2
0
 def patch(self):
     agreement = self.context
     apply_patch(self.request, save=False, src=self.request.validated["agreement_src"])
     if save_agreement(self.request):
         self.LOGGER.info(
             "Updated agreement {}".format(agreement.id),
             extra=context_unpack(self.request, {"MESSAGE_ID": "agreement_patch"}),
         )
         return {"data": agreement.serialize("view")}
Beispiel #3
0
    def patch(self):
        agreement = self.request.validated["agreement"]

        access = set_ownership(agreement, self.request)
        if save_agreement(self.request):
            self.LOGGER.info(
                "Generate Agreement credentials {}".format(agreement.id),
                extra=context_unpack(self.request, {"MESSAGE_ID": "agreement_patch"}),
            )
            return {"data": agreement.serialize("view"), "access": access}
Beispiel #4
0
    def put(self):
        """ Agreement Document Update"""
        document = upload_file(self.request)
        self.request.validated["agreement"].documents.append(document)

        if save_agreement(self.request):
            self.LOGGER.info(
                "Updated agreement document {}".format(
                    self.request.context.id),
                extra=context_unpack(self.request,
                                     {"MESSAGE_ID": "agreement_document_put"}),
            )
            return {"data": document.serialize("view")}
Beispiel #5
0
 def test_save_agreement(self):
     request = MagicMock()
     agreement = MagicMock()
     agreement.mode = u"test"
     agreement.revisions = []
     agreement.dateModified = datetime.datetime(2018, 8, 2, 12, 9, 2,
                                                440566)
     type(agreement).revisions = MagicMock()
     agreement.rev = "12341234"
     request.validated = {
         "agreement": agreement,
         "agreement_src": "src_test"
     }
     res = save_agreement(request)
     self.assertTrue(res)
Beispiel #6
0
 def collection_post(self):
     """ Agreement Document Upload"""
     document = upload_file(self.request)
     self.context.documents.append(document)
     if save_agreement(self.request):
         self.LOGGER.info(
             "Created agreement document {}".format(document.id),
             extra=context_unpack(
                 self.request, {"MESSAGE_ID": "agreement_document_create"},
                 {"document_id": document.id}),
         )
         self.request.response.status = 201
         document_route = self.request.matched_route.name.replace(
             "collection_", "")
         self.request.response.headers[
             "Location"] = self.request.current_route_url(
                 _route_name=document_route,
                 document_id=document.id,
                 _query={})
         return {"data": document.serialize("view")}
Beispiel #7
0
    def post(self):
        agreement = self.request.validated["agreement"]
        data = self.request.validated["ownership_data"]

        route_name = "{}.Agreement".format(agreement.agreementType)
        location = get_transfer_location(self.request,
                                         route_name,
                                         agreement_id=agreement.id)
        transfer = extract_transfer(self.request, transfer_id=data["id"])

        if transfer.get("usedFor") and transfer.get("usedFor") != location:
            self.request.errors.add("body", "transfer",
                                    "Transfer already used")
            self.request.errors.status = 403
            return

        update_ownership(agreement, transfer)
        self.request.validated["agreement"] = agreement

        transfer.usedFor = location
        self.request.validated["transfer"] = transfer

        if save_transfer(self.request):
            self.LOGGER.info(
                "Updated transfer relation {}".format(transfer.id),
                extra=context_unpack(
                    self.request, {"MESSAGE_ID": "transfer_relation_update"}),
            )

            if save_agreement(self.request):
                self.LOGGER.info(
                    "Updated ownership of agreement {}".format(agreement.id),
                    extra=context_unpack(
                        self.request,
                        {"MESSAGE_ID": "agreement_ownership_update"}),
                )

                return {"data": agreement.serialize("view")}
Beispiel #8
0
 def post(self):
     agreement = self.request.validated["agreement"]
     access = set_ownership(agreement, self.request)
     self.request.validated["agreement"] = agreement
     self.request.validated["agreement_src"] = {}
     if save_agreement(self.request):
         self.LOGGER.info(
             "Created agreement {} ({})".format(agreement.id,
                                                agreement.agreementID),
             extra=context_unpack(
                 self.request,
                 {"MESSAGE_ID": "agreement_create"},
                 {
                     "agreement_id": agreement.id,
                     "agreementID": agreement.agreementID
                 },
             ),
         )
         self.request.response.status = 201
         self.request.response.headers["Location"] = self.request.route_url(
             "{}.Agreement".format(agreement.agreementType),
             agreement_id=agreement.id)
         return {"data": agreement.serialize("view"), "access": access}
Beispiel #9
0
    def patch(self):
        """ Agreement change edit """
        change = self.request.validated["change"]
        data = self.request.validated["data"]

        if "status" in data and data[
                "status"] != change.status:  # status change
            validate_update_agreement_change_status(self.request)
            change["date"] = get_now()

        apply_patch(self.request, save=False, src=change.serialize())

        # Validate or apply agreement modifications
        warnings = []
        agreement = self.request.validated["agreement_src"]
        validated_agreement = self.request.validated["agreement"]
        if change.status == "active":
            if not change.modifications:
                raise_operation_error(
                    self.request,
                    "Modifications are required for change activation.")
            apply_modifications(self.request, validated_agreement, save=True)
        elif change.status != "cancelled":
            warnings = apply_modifications(self.request, validated_agreement)

        if change["dateSigned"]:
            changes = agreement.get("changes", [])
            active_changes = [c for c in changes if c["status"] == "active"]
            if len(active_changes) > 0:  # has previous changes
                last_change = active_changes[-1]
                last_date_signed = last_change.get("dateSigned")
                if not last_date_signed:  # BBB old active changes
                    last_date_signed = last_change.get("date")
                obj_str = "last active change"
            else:
                last_date_signed = agreement.get("dateSigned")
                obj_str = "agreement"

            if last_date_signed:  # BBB very old agreement
                if change["dateSigned"].isoformat() < last_date_signed:
                    # Can't move validator because of code above
                    raise_operation_error(
                        self.request,
                        "Change dateSigned ({}) can't be earlier than {} dateSigned ({})"
                        .format(change["dateSigned"].isoformat(), obj_str,
                                last_date_signed),
                    )

        if save_agreement(self.request):
            self.LOGGER.info(
                "Updated agreement change {}".format(change.id),
                extra=context_unpack(self.request,
                                     {"MESSAGE_ID": "agreement_change_patch"}),
            )
            response_data = {"data": change.serialize("view")}
            if warnings:
                response_data["warnings"] = warnings
                self.LOGGER.info(
                    "warnings: {}".format(warnings),
                    extra=context_unpack(
                        self.request,
                        {"MESSAGE_ID": "agreement_change_patch"}),
                )
            return response_data
 def save(self, request, **kwargs):
     return save_agreement(request)