Skip to content

Commit

Permalink
Add default detail to custom exceptions (#916)
Browse files Browse the repository at this point in the history
Signed-off-by: Tmpecho <[email protected]>
Co-authored-by: Mads Nylund <[email protected]>
  • Loading branch information
Tmpecho and MadsNyl authored Oct 28, 2024
1 parent e690181 commit 2bff09a
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 48 deletions.
8 changes: 6 additions & 2 deletions app/codex/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
8 changes: 2 additions & 6 deletions app/codex/util/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
12 changes: 6 additions & 6 deletions app/communication/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -27,4 +27,4 @@ class APIAllChannelsUnselected(APIException):


class AllChannelsUnselected(ValueError):
pass
default_detail = "Du må velge minst en kommunikasjonsmetode"
30 changes: 16 additions & 14 deletions app/content/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,43 @@ 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 = (
"Du har ubesvarte evalueringsskjemaer som må besvares før du kan melde deg på"
)


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):
Expand Down
2 changes: 1 addition & 1 deletion app/content/models/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/content/util/event_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
6 changes: 3 additions & 3 deletions app/forms/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class APIDuplicateSubmissionException(APIException):


class DuplicateSubmission(ValueError):
pass
default_detail = "Spørreskjemaet tillater kun én innsending"


class APIFormNotOpenForSubmissionException(APIException):
Expand All @@ -17,7 +17,7 @@ class APIFormNotOpenForSubmissionException(APIException):


class FormNotOpenForSubmission(ValueError):
pass
default_detail = "Spørreskjemaet er ikke åpent for innsending"


class APIGroupFormOnlyForMembersException(APIException):
Expand All @@ -26,4 +26,4 @@ class APIGroupFormOnlyForMembersException(APIException):


class GroupFormOnlyForMembers(ValueError):
pass
default_detail = "Spørreskjemaet er kun åpent for medlemmer av gruppen"
17 changes: 4 additions & 13 deletions app/forms/models/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions app/group/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class APIUserIsNotInGroupException(APIException):


class UserIsNotInGroup(ValidationError):
pass
default_detail = "En av brukerne er ikke medlem av gruppen"


class APIGroupTypeNotInPublicGroupsException(APIException):
Expand All @@ -17,4 +17,4 @@ class APIGroupTypeNotInPublicGroupsException(APIException):


class GroupTypeNotInPublicGroups(ValueError):
pass
default_detail = "Ikke gylde gruppetype"

0 comments on commit 2bff09a

Please sign in to comment.