From 2bff09ac34c0a8bd4322108fe9ab2671d998044e Mon Sep 17 00:00:00 2001 From: Johannes Aamot-Skeidsvoll <82368148+Tmpecho@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:15:01 +0100 Subject: [PATCH] Add default detail to custom exceptions (#916) Signed-off-by: Tmpecho Co-authored-by: Mads Nylund <73914541+MadsNyl@users.noreply.github.com> --- app/codex/exceptions.py | 8 ++++++-- app/codex/util/event.py | 8 ++------ app/communication/exceptions.py | 12 ++++++------ app/content/exceptions.py | 30 ++++++++++++++++-------------- app/content/models/registration.py | 2 +- app/content/util/event_utils.py | 2 +- app/forms/exceptions.py | 6 +++--- app/forms/models/forms.py | 17 ++++------------- app/group/exceptions.py | 4 ++-- 9 files changed, 41 insertions(+), 48 deletions(-) diff --git a/app/codex/exceptions.py b/app/codex/exceptions.py index fd68fcce9..bcb72ba31 100644 --- a/app/codex/exceptions.py +++ b/app/codex/exceptions.py @@ -17,8 +17,12 @@ class APICodexEventEndRegistrationDateBeforeStartRegistrationDate(APIException): class CodexEventEndRegistrationDateAfterStartDate(ValueError): - pass + default_detail = ( + "Sluttdatoen for påmelding kan ikke være etter startdatoen for kurset" + ) class CodexEventEndRegistrationDateBeforeStartRegistrationDate(ValueError): - pass + default_detail = ( + "Sluttdatoen for påmelding kan ikke være før startdatoen for påmelding" + ) diff --git a/app/codex/util/event.py b/app/codex/util/event.py index 00b3979f1..2248a1eeb 100644 --- a/app/codex/util/event.py +++ b/app/codex/util/event.py @@ -6,11 +6,7 @@ def validate_event_dates(data: dict): if data["end_registration_at"] > data["start_date"]: - raise CodexEventEndRegistrationDateAfterStartDate( - "Påmeldingsslutt kan ikke være etter kursstart" - ) + raise CodexEventEndRegistrationDateAfterStartDate() if data["end_registration_at"] < data["start_registration_at"]: - raise CodexEventEndRegistrationDateBeforeStartRegistrationDate( - "Påmeldingsslutt kan ikke være før påmeldingsstart" - ) + raise CodexEventEndRegistrationDateBeforeStartRegistrationDate() diff --git a/app/communication/exceptions.py b/app/communication/exceptions.py index 26f6b3871..7ffc5deaf 100644 --- a/app/communication/exceptions.py +++ b/app/communication/exceptions.py @@ -8,17 +8,17 @@ class APIAnotherVisibleBannerException(APIException): default_detail = "Det finnes allerede et banner som er synlig i samme tidsrom" +class AnotherVisibleBannerError(ValidationError): + default_detail = "Det finnes allerede et banner som er synlig i samme tidsrom" + + class APIDatesMixedException(APIException): status_code = status.HTTP_400_BAD_REQUEST default_detail = "Datoen banneret er synlig til er satt etter datoen banneret for synlig fra. Bytt om disse to" -class AnotherVisibleBannerError(ValidationError): - pass - - class DatesMixedError(ValidationError): - pass + default_detail = "Datoen banneret er synlig til er satt etter datoen banneret for synlig fra. Bytt om disse to" class APIAllChannelsUnselected(APIException): @@ -27,4 +27,4 @@ class APIAllChannelsUnselected(APIException): class AllChannelsUnselected(ValueError): - pass + default_detail = "Du må velge minst en kommunikasjonsmetode" diff --git a/app/content/exceptions.py b/app/content/exceptions.py index 73f07dfe6..31ca909d3 100644 --- a/app/content/exceptions.py +++ b/app/content/exceptions.py @@ -22,6 +22,10 @@ class APIEventSignOffDeadlineHasPassed(APIException): default_detail = "Du kan ikke melde deg av etter avmeldingsfristen" +class EventSignOffDeadlineHasPassed(ValueError): + default_detail = "Du kan ikke melde deg av etter avmeldingsfristen" + + class APIUnansweredFormException(APIException): status_code = status.HTTP_400_BAD_REQUEST default_detail = ( @@ -29,34 +33,32 @@ class APIUnansweredFormException(APIException): ) +class UnansweredFormError(ValueError): + default_detail = ( + "Du har ubesvarte evalueringsskjemaer som må besvares før du kan melde deg på" + ) + + class APIHasStrikeException(APIException): status_code = status.HTTP_403_FORBIDDEN default_detail = "Kan ikke melde deg på fordi du har en eller flere prikker" -class APIEventIsFullException(APIException): - status_code = status.HTTP_400_BAD_REQUEST - default_detail = "Du kan ikke flytte opp en fra ventelisten når arrangementet er fullt. Flytt en bruker ned først." - - -class EventSignOffDeadlineHasPassed(ValueError): - pass - - class StrikeError(ValueError): - pass + default_detail = "Kan ikke melde deg på fordi du har en eller flere prikker" -class UnansweredFormError(ValueError): - pass +class APIEventIsFullException(APIException): + status_code = status.HTTP_400_BAD_REQUEST + default_detail = "Du kan ikke flytte opp en fra ventelisten når arrangementet er fullt. Flytt en bruker ned først." class EventIsFullError(ValueError): - pass + default_detail = "Du kan ikke flytte opp en fra ventelisten når arrangementet er fullt. Flytt en bruker ned først." class RefundFailedError(ValueError): - pass + default_detail = "Tilbakebetaling feilet" class FeideError(ValueError): diff --git a/app/content/models/registration.py b/app/content/models/registration.py index 220066371..20449987f 100644 --- a/app/content/models/registration.py +++ b/app/content/models/registration.py @@ -165,7 +165,7 @@ def save(self, *args, **kwargs): and not self.is_on_wait and self in self.event.get_waiting_list() ): - raise EventIsFullError + raise EventIsFullError() self.send_notification_and_mail() return super().save(*args, **kwargs) diff --git a/app/content/util/event_utils.py b/app/content/util/event_utils.py index e611adc96..304431a71 100644 --- a/app/content/util/event_utils.py +++ b/app/content/util/event_utils.py @@ -76,4 +76,4 @@ def refund_vipps_order(order_id, event, transaction_text): except Exception as refund_error: capture_exception(refund_error) - raise RefundFailedError("Tilbakebetaling feilet") + raise RefundFailedError() diff --git a/app/forms/exceptions.py b/app/forms/exceptions.py index c5ad930d8..932613f86 100644 --- a/app/forms/exceptions.py +++ b/app/forms/exceptions.py @@ -8,7 +8,7 @@ class APIDuplicateSubmissionException(APIException): class DuplicateSubmission(ValueError): - pass + default_detail = "Spørreskjemaet tillater kun én innsending" class APIFormNotOpenForSubmissionException(APIException): @@ -17,7 +17,7 @@ class APIFormNotOpenForSubmissionException(APIException): class FormNotOpenForSubmission(ValueError): - pass + default_detail = "Spørreskjemaet er ikke åpent for innsending" class APIGroupFormOnlyForMembersException(APIException): @@ -26,4 +26,4 @@ class APIGroupFormOnlyForMembersException(APIException): class GroupFormOnlyForMembers(ValueError): - pass + default_detail = "Spørreskjemaet er kun åpent for medlemmer av gruppen" diff --git a/app/forms/models/forms.py b/app/forms/models/forms.py index 737fb84e7..2385a4994 100644 --- a/app/forms/models/forms.py +++ b/app/forms/models/forms.py @@ -112,7 +112,6 @@ def has_object_read_permission(self, request): class EventForm(Form): - event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name="forms") type = models.CharField( max_length=40, choices=EventFormType.choices, default=EventFormType.SURVEY @@ -160,7 +159,6 @@ def has_object_read_permission(self, request): class GroupForm(Form): - read_access = [Groups.TIHLDE] email_receiver_on_submit = models.EmailField(max_length=200, null=True, blank=True) group = models.ForeignKey(Group, on_delete=models.CASCADE, related_name="forms") @@ -212,7 +210,6 @@ def has_object_write_permission(self, request): class Field(OrderedModel): - id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) title = models.CharField(max_length=400) form = models.ForeignKey(Form, on_delete=models.CASCADE, related_name="fields") @@ -234,7 +231,6 @@ class Meta(OrderedModel.Meta): class Option(OrderedModel): - id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) title = models.CharField(max_length=400, default="") field = models.ForeignKey(Field, on_delete=models.CASCADE, related_name="options") @@ -298,7 +294,7 @@ def check_multiple_submissions(self): elif isinstance(self.form, GroupForm): self.check_group_form_can_submit_multiple() else: - raise DuplicateSubmission("Spørreskjemaet tillater kun én innsending") + raise DuplicateSubmission() def check_event_form_has_registration(self): user_has_registration = self.form.event.registrations.filter( @@ -311,21 +307,17 @@ def check_event_form_has_registration(self): def check_group_form_can_submit_multiple(self): if not self.form.can_submit_multiple: - raise DuplicateSubmission("Spørreskjemaet tillater kun én innsending") + raise DuplicateSubmission() def check_group_form_open_for_submissions(self): if not self.form.is_open_for_submissions: - raise FormNotOpenForSubmission( - "Spørreskjemaet er ikke åpent for innsending" - ) + raise FormNotOpenForSubmission() def check_group_form_only_for_members(self): if self.form.only_for_group_members and not self.user.is_member_of( self.form.group ): - raise GroupFormOnlyForMembers( - "Spørreskjemaet er kun åpent for medlemmer av gruppen" - ) + raise GroupFormOnlyForMembers() @classmethod def _get_form_from_request(cls, request): @@ -388,7 +380,6 @@ def has_download_permission(cls, request): class Answer(BaseModel): - id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) submission = models.ForeignKey( Submission, on_delete=models.CASCADE, related_name="answers" diff --git a/app/group/exceptions.py b/app/group/exceptions.py index c0ca1867d..6c1c8a802 100644 --- a/app/group/exceptions.py +++ b/app/group/exceptions.py @@ -8,7 +8,7 @@ class APIUserIsNotInGroupException(APIException): class UserIsNotInGroup(ValidationError): - pass + default_detail = "En av brukerne er ikke medlem av gruppen" class APIGroupTypeNotInPublicGroupsException(APIException): @@ -17,4 +17,4 @@ class APIGroupTypeNotInPublicGroupsException(APIException): class GroupTypeNotInPublicGroups(ValueError): - pass + default_detail = "Ikke gylde gruppetype"