def mutate_and_get_payload(cls, root, info, **input):
        pier = get_node_from_global_id(
            info, input.pop("id"), only_type=PierNode, nullable=False,
        )

        if input.get("harbor_id"):
            harbor = get_node_from_global_id(
                info, input.pop("harbor_id"), only_type=HarborNode, nullable=False,
            )
            input["harbor"] = harbor

        boat_types = set()
        for boat_type_id in input.pop("suitable_boat_types", []):
            try:
                boat_type = BoatType.objects.get(pk=boat_type_id)
            except BoatType.DoesNotExist as e:
                raise VenepaikkaGraphQLError(e)
            boat_types.add(boat_type)

        try:
            update_object(pier, input)
            pier.suitable_boat_types.set(boat_types)
        except IntegrityError as e:
            raise VenepaikkaGraphQLError(e)

        return UpdatePierMutation(pier=pier)
    def mutate_and_get_payload(cls, root, info, **input):
        berth = get_node_from_global_id(
            info, input.pop("id"), only_type=BerthNode, nullable=False,
        )

        width = input.pop("width", None)
        length = input.pop("length", None)
        depth_in_input = "depth" in input
        mooring_type = input.pop("mooring_type", None)
        berth_type = None

        if any([width, length, depth_in_input, mooring_type]):
            old_berth_type = berth.berth_type
            berth_type, created = BerthType.objects.get_or_create(
                width=width or old_berth_type.width,
                length=length or old_berth_type.length,
                # Checking directly if it's in the input keys because it allows null values
                depth=input.pop("depth") if depth_in_input else old_berth_type.depth,
                mooring_type=mooring_type or old_berth_type.mooring_type,
            )

        if berth_type:
            input["berth_type"] = berth_type

        if input.get("pier_id"):
            input["pier"] = get_node_from_global_id(
                info, input.pop("pier_id"), only_type=PierNode, nullable=False,
            )

        update_object(berth, input)

        return UpdateBerthMutation(berth=berth)
    def mutate_and_get_payload(cls, root, info, **input):
        profile = get_node_from_global_id(info,
                                          input.pop("id"),
                                          only_type=ProfileNode,
                                          nullable=False)

        delete_organization = input.pop("delete_organization", False)
        if delete_organization:
            if "organization" in input:
                raise VenepaikkaGraphQLError(
                    _("You cannot pass deleteOrganization: true and organization input"
                      ))
            if not profile.organization:
                raise VenepaikkaGraphQLError(
                    _("The passed Profile is not associated with an Organization"
                      ))
            profile.organization.delete()
            profile.refresh_from_db()
        elif "organization" in input:
            Organization.objects.update_or_create(
                customer=profile, defaults=input.pop("organization"))
            profile.refresh_from_db()

        try:
            update_object(profile, input)
        except ValidationError as e:
            # Flatten all the error messages on a single list
            errors = sum(e.message_dict.values(), [])
            raise VenepaikkaGraphQLError(errors)

        return UpdateBerthServicesProfileMutation(profile=profile)
    def mutate_and_get_payload(cls, root, info, **input):
        winter_storage_place = get_node_from_global_id(
            info, input.pop("id"), only_type=WinterStoragePlaceNode, nullable=False,
        )

        width = input.pop("width", None)
        length = input.pop("length", None)
        place_type = None

        if any([width, length]):
            old_place_type = winter_storage_place.place_type
            place_type, created = WinterStoragePlaceType.objects.get_or_create(
                width=width or old_place_type.width,
                length=length or old_place_type.length,
            )

        if place_type:
            input["place_type"] = place_type

        if input.get("winter_storage_section_id"):
            input["winter_storage_section"] = get_node_from_global_id(
                info,
                input.pop("winter_storage_section_id"),
                only_type=WinterStorageSectionNode,
                nullable=False,
            )

        update_object(winter_storage_place, input)

        return UpdateWinterStoragePlaceMutation(
            winter_storage_place=winter_storage_place
        )
    def mutate_and_get_payload(cls, root, info, **input):
        winter_storage_place_type = get_node_from_global_id(
            info, input.pop("id"), only_type=WinterStoragePlaceTypeNode, nullable=False,
        )
        update_object(winter_storage_place_type, input)

        return UpdateWinterStoragePlaceTypeMutation(
            winter_storage_place_type=winter_storage_place_type
        )
    def mutate_and_get_payload(cls, root, info, **input):
        winter_storage_area = get_node_from_global_id(
            info, input.pop("id"), only_type=WinterStorageAreaNode, nullable=False,
        )
        cls.validate_availability_level_id(input)
        cls.validate_municipality_id(input)
        update_object(winter_storage_area, input)

        return UpdateWinterStorageAreaMutation(winter_storage_area=winter_storage_area)
示例#7
0
    def mutate_and_get_payload(cls, root, info, orders, **input):

        successful_orders = []
        failed_orders = []

        for order_input in orders:
            order_id = order_input.pop("id")
            try:
                with transaction.atomic():
                    order = get_node_from_global_id(info,
                                                    order_id,
                                                    only_type=OrderNode,
                                                    nullable=False)
                    if lease_id := order_input.pop("lease_id", None):
                        lease = None
                        try:
                            lease = get_node_from_global_id(info,
                                                            lease_id,
                                                            BerthLeaseNode,
                                                            nullable=True)
                        # If a different node type is received get_node raises an assertion error
                        # when trying to validate the type
                        except AssertionError:
                            lease = get_node_from_global_id(
                                info,
                                lease_id,
                                WinterStorageLeaseNode,
                                nullable=True)
                        finally:
                            if not lease:
                                raise VenepaikkaGraphQLError(
                                    _("Lease with the given ID does not exist")
                                )
                            order_input["lease"] = lease

                    # handle case where order_input has both lease and status.
                    # set order status only after changing the lease, because setting order status
                    # usually triggers a change in lease status.
                    new_status = order_input.pop("status", None)
                    update_object(order, order_input)
                    if new_status:
                        order.set_status(new_status,
                                         _("Manually updated by admin"))

            except (
                    ValidationError,
                    IntegrityError,
                    VenepaikkaGraphQLError,
                    OrderStatusTransitionError,
            ) as e:
                failed_orders.append(FailedOrderType(id=order_id,
                                                     error=str(e)))
            else:
                successful_orders.append(order)
        return UpdateOrdersMutation(successful_orders=successful_orders,
                                    failed_orders=failed_orders)
    def mutate_and_get_payload(cls, root, info, **input):
        harbor = get_node_from_global_id(
            info, input.pop("id"), only_type=HarborNode, nullable=False,
        )

        cls.validate_availability_level_id(input)
        cls.validate_municipality_id(input)
        update_object(harbor, input)

        return UpdateHarborMutation(harbor=harbor)
示例#9
0
 def mutate_and_get_payload(cls, root, info, **input):
     product = get_node_from_global_id(info,
                                       input.pop("id"),
                                       only_type=AdditionalProductNode,
                                       nullable=False)
     try:
         update_object(product, input)
     except (ValidationError, IntegrityError) as e:
         raise VenepaikkaGraphQLError(e)
     return UpdateAdditionalProductMutation(additional_product=product)
def update_boat_certificates(certificates, info):
    try:
        for cert_input in certificates:
            cert = get_node_from_global_id(
                info,
                cert_input.pop("id"),
                only_type=BoatCertificateNode,
                nullable=False,
            )
            update_object(cert, cert_input)
    except IntegrityError as e:
        raise VenepaikkaGraphQLError(e)
示例#11
0
    def mutate_and_get_payload(cls, root, info, **input):
        order_line = get_node_from_global_id(info,
                                             input.pop("id"),
                                             only_type=OrderLineNode,
                                             nullable=False)

        try:
            update_object(order_line, input)
        except (ValidationError, IntegrityError) as e:
            raise VenepaikkaGraphQLError(e)
        return UpdateOrderLineMutation(order_line=order_line,
                                       order=order_line.order)
示例#12
0
    def mutate_and_get_payload(cls, root, info, **input):
        offer = get_node_from_global_id(info,
                                        input.pop("id"),
                                        only_type=BerthSwitchOfferNode,
                                        nullable=False)
        try:
            # do not assign status directly on the object, use proper state transition through set_status
            new_status = input.pop("status", None)
            update_object(offer, input)
            if new_status:
                offer.set_status(new_status, _("Manually updated by admin"))

        except (ValidationError, IntegrityError) as e:
            raise VenepaikkaGraphQLError(e)
        return UpdateBerthSwitchOfferMutation(berth_switch_offer=offer)
    def mutate_and_get_payload(cls, root, info, **input):
        boat = get_node_from_global_id(info,
                                       input.pop("id"),
                                       only_type=BoatNode,
                                       nullable=False)

        add_certificates = input.pop("add_boat_certificates", [])
        update_certificates = input.pop("update_boat_certificates", [])
        remove_certificates = input.pop("remove_boat_certificates", [])

        try:
            update_object(boat, input)
            remove_boat_certificates(remove_certificates, boat)
            update_boat_certificates(update_certificates, info)
            add_boat_certificates(add_certificates, boat)
        except ValidationError as e:
            raise VenepaikkaGraphQLError(e)

        return UpdateBoatMutation(boat=boat)
示例#14
0
    def mutate_and_get_payload(cls, root, info, **input):
        product = get_node_from_global_id(info,
                                          input.pop("id"),
                                          only_type=WinterStorageProductNode,
                                          nullable=False)
        if "winter_storage_area_id" in input:
            input["winter_storage_area"] = get_node_from_global_id(
                info,
                input.pop("winter_storage_area_id", None),
                only_type=WinterStorageAreaNode,
                nullable=True,
            )
        try:
            update_object(product, input)
        except (ValidationError, IntegrityError) as e:
            raise VenepaikkaGraphQLError(e)

        return UpdateWinterStorageProductMutation(
            winter_storage_product=product)
    def mutate_and_get_payload(cls, root, info, **input):
        lease = get_node_from_global_id(
            info, input.pop("id"), only_type=WinterStorageLeaseNode, nullable=False,
        )
        application_id = input.get("application_id")

        if application_id:
            # If the application id was passed, raise an error if it doesn't exist
            application = get_node_from_global_id(
                info,
                application_id,
                only_type=WinterStorageApplicationNode,
                nullable=False,
            )
            if not application.customer:
                raise VenepaikkaGraphQLError(
                    _("Application must be connected to an existing customer first")
                )
            input["application"] = application
            input["customer"] = application.customer

        if input.get("boat_id", False):
            from customers.schema import BoatNode

            boat = get_node_from_global_id(
                info, input.pop("boat_id"), only_type=BoatNode, nullable=False,
            )

            if boat.owner.id != input["customer"].id:
                raise VenepaikkaGraphQLError(
                    _("Boat does not belong to the same customer as the Application")
                )

            input["boat"] = boat

        try:
            update_object(lease, input)
        except ValidationError as e:
            raise VenepaikkaGraphQLError(str(e))

        return UpdateWinterStorageLeaseMutation(winter_storage_lease=lease)
    def mutate_and_get_payload(cls, root, info, **input):
        winter_storage_section = get_node_from_global_id(
            info, input.pop("id"), only_type=WinterStorageSectionNode, nullable=False,
        )

        if input.get("area_id"):
            area = get_node_from_global_id(
                info,
                input.pop("area_id"),
                only_type=WinterStorageAreaNode,
                nullable=False,
            )
            input["area"] = area

        try:
            update_object(winter_storage_section, input)
        except IntegrityError as e:
            raise VenepaikkaGraphQLError(e)

        return UpdateWinterStorageSectionMutation(
            winter_storage_section=winter_storage_section
        )
                try:
                    choice = HarborChoice.objects.get(id=from_global_id(
                        choice_id, node_type=HarborChoiceType))
                    choice.delete()
                except HarborChoice.DoesNotExist:
                    pass

        if add_choices := input.pop("add_choices", []):
            for choice in add_choices:
                HarborChoice.objects.get_or_create(
                    harbor_id=from_global_id(choice.get("harbor_id")),
                    priority=choice.get("priority"),
                    application=application,
                )

        update_object(application, input)

        return UpdateBerthApplication(berth_application=application)


class DeleteBerthApplicationMutation(graphene.ClientIDMutation):
    class Input:
        id = graphene.ID(required=True)

    def get_nodes_to_check(info, **input):
        application = get_node_from_global_id(info,
                                              input.get("id"),
                                              only_type=BerthApplicationNode,
                                              nullable=True)
        return [application]