From 156f42aa9553cde491fd0b0ccab1b149baf5d5ed Mon Sep 17 00:00:00 2001 From: Tore Brede <Tore.Brede@uib.no> Date: Mon, 8 Nov 2021 14:18:52 +0100 Subject: [PATCH] GREG-94: Fixing formatting --- gregui/api/serializers/guest.py | 21 ++++++++++++++++----- gregui/api/urls.py | 10 ++++++++-- gregui/api/views/invitation.py | 22 +++++++++++++++------- gregui/mailutils.py | 16 +++++++++++----- gregui/validation.py | 2 +- 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/gregui/api/serializers/guest.py b/gregui/api/serializers/guest.py index 6387d938..3ae182f4 100644 --- a/gregui/api/serializers/guest.py +++ b/gregui/api/serializers/guest.py @@ -1,7 +1,10 @@ from rest_framework import serializers from greg.models import Identity, Person -from gregui.validation import validate_phone_number, validate_norwegian_national_id_number +from gregui.validation import ( + validate_phone_number, + validate_norwegian_national_id_number, +) class GuestRegisterSerializer(serializers.ModelSerializer): @@ -23,14 +26,20 @@ class GuestRegisterSerializer(serializers.ModelSerializer): if "email" in validated_data: email = validated_data.pop("email") - create_identity_or_update(Identity.IdentityType.PRIVATE_EMAIL, email, instance) + create_identity_or_update( + Identity.IdentityType.PRIVATE_EMAIL, email, instance + ) if not instance.private_mobile: - create_identity_or_update(Identity.IdentityType.PRIVATE_MOBILE_NUMBER, mobile_phone, instance) + create_identity_or_update( + Identity.IdentityType.PRIVATE_MOBILE_NUMBER, mobile_phone, instance + ) if "fnr" in validated_data: fnr = validated_data.pop("fnr") - create_identity_or_update(Identity.IdentityType.NORWEGIAN_NATIONAL_ID_NUMBER, fnr, instance) + create_identity_or_update( + Identity.IdentityType.NORWEGIAN_NATIONAL_ID_NUMBER, fnr, instance + ) # TODO: we only want to allow changing the name if we don't have one # from a reliable source (Feide/KORR) @@ -46,7 +55,9 @@ class GuestRegisterSerializer(serializers.ModelSerializer): read_only_fields = ("id",) -def create_identity_or_update(identity_type: Identity.IdentityType, value: str, person: Person): +def create_identity_or_update( + identity_type: Identity.IdentityType, value: str, person: Person +): existing_identity = person.identities.filter(type=identity_type).first() if not existing_identity: Identity.objects.create( diff --git a/gregui/api/urls.py b/gregui/api/urls.py index 26029489..accc5940 100644 --- a/gregui/api/urls.py +++ b/gregui/api/urls.py @@ -23,9 +23,15 @@ urlpatterns += [ re_path(r"units/$", UnitsViewSet.as_view(), name="units"), path("invited/", InvitedGuestView.as_view(), name="invited-info"), path("invitecheck/", CheckInvitationView.as_view(), name="invite-verify"), - path("invite/<int:person_id>/resend", ResendInvitationView.as_view(), name="invite-resend"), + path( + "invite/<int:person_id>/resend", + ResendInvitationView.as_view(), + name="invite-resend", + ), path("invite/", InvitationView.as_view(), name="invitation"), - path("resend/<int:person_id>", ResendInvitationView.as_view(), name="invite-resend"), + path( + "resend/<int:person_id>", ResendInvitationView.as_view(), name="invite-resend" + ), path("person/<int:id>", PersonView.as_view(), name="person-get"), path( "person/search/<searchstring>", PersonSearchView.as_view(), name="person-search" diff --git a/gregui/api/views/invitation.py b/gregui/api/views/invitation.py index 7f139d57..455dbb0e 100644 --- a/gregui/api/views/invitation.py +++ b/gregui/api/views/invitation.py @@ -74,8 +74,8 @@ class InvitationView(CreateAPIView, DestroyAPIView): person = serializer.save() for invitationlink in InvitationLink.objects.filter( - invitation__role__person_id=person.id, - invitation__role__sponsor_id=sponsor_user.sponsor_id, + invitation__role__person_id=person.id, + invitation__role__sponsor_id=sponsor_user.sponsor_id, ): send_invite_mail(invitationlink) @@ -91,12 +91,18 @@ class InvitationView(CreateAPIView, DestroyAPIView): # not be verified, but including that check just in case here. # If this is the case then there is an unexpected situation, the cancel option # should only apply to guests that have not completed the registration - logger.warning(f"Attempting to delete invitation for already registered guest with person ID {person_id}") + logger.warning( + f"Attempting to delete invitation for already registered guest with person ID {person_id}" + ) return Response(status.HTTP_400_BAD_REQUEST) # Since the person is not registered, all roles attached to invitations for him should be removed - invitationlinks = InvitationLink.objects.filter(invitation__role__person_id=person_id) - roles = set([invitationlink.invitation.role for invitationlink in invitationlinks]) + invitationlinks = InvitationLink.objects.filter( + invitation__role__person_id=person_id + ) + roles = set( + [invitationlink.invitation.role for invitationlink in invitationlinks] + ) for role in roles: # Delete the role, the cascading will cause all the invitation links connected @@ -282,7 +288,7 @@ class InvitedGuestView(GenericAPIView): @staticmethod def _get_identity_or_none( - person: Person, identity_type: Identity.IdentityType + person: Person, identity_type: Identity.IdentityType ) -> Optional[str]: try: return person.identities.get(type=identity_type).value @@ -297,7 +303,9 @@ class ResendInvitationView(UpdateModelMixin, APIView): def patch(self, request, *args, **kwargs) -> Response: person_id = kwargs["person_id"] - invitation_links = InvitationLink.objects.filter(invitation__role__person__id=person_id) + invitation_links = InvitationLink.objects.filter( + invitation__role__person__id=person_id + ) if invitation_links.count() == 0: # No invitation, not expected that the endpoint should be called in this case diff --git a/gregui/mailutils.py b/gregui/mailutils.py index ca1bf4f5..b2d78e1d 100644 --- a/gregui/mailutils.py +++ b/gregui/mailutils.py @@ -13,7 +13,7 @@ logger = logging.getLogger(__name__) def prepare_arguments( - template: EmailTemplate, context: dict[str, str], mail_to: str + template: EmailTemplate, context: dict[str, str], mail_to: str ) -> dict[str, Union[str, list[str]]]: """Combine input to a dict ready for use as arguments ti django's send_mail""" return { @@ -25,7 +25,7 @@ def prepare_arguments( def registration_template( - institution: str, sponsor: str, mail_to: str + institution: str, sponsor: str, mail_to: str ) -> dict[str, Union[str, list[str]]]: """ Prepare email for registration @@ -73,12 +73,18 @@ def send_confirmation_mail(mail_to: str, guest: str) -> str: def send_invite_mail(link: InvitationLink) -> Optional[str]: email_address = link.invitation.role.person.private_email if not email_address: - logger.warning(f"No e-mail address found for invitation link with ID: {link.id}") + logger.warning( + f"No e-mail address found for invitation link with ID: {link.id}" + ) return None sponsor = link.invitation.role.sponsor if not sponsor: - logger.warning("Unable to determine sponsor for invitation link with ID: {link.id}") + logger.warning( + "Unable to determine sponsor for invitation link with ID: {link.id}" + ) return None - return send_registration_mail(email_address.value, f"{sponsor.first_name} {sponsor.last_name}") + return send_registration_mail( + email_address.value, f"{sponsor.first_name} {sponsor.last_name}" + ) diff --git a/gregui/validation.py b/gregui/validation.py index 7da189d8..18ad553d 100644 --- a/gregui/validation.py +++ b/gregui/validation.py @@ -4,7 +4,7 @@ from rest_framework import serializers from greg.utils import is_valid_norwegian_national_id_number -_valid_email_regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' +_valid_email_regex = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b" def validate_norwegian_national_id_number(value): -- GitLab