From fffa0b1e538b718041bdfb54ba291065dba892de Mon Sep 17 00:00:00 2001 From: Kiran Jonnalagadda Date: Wed, 3 Jan 2024 09:37:15 +0530 Subject: [PATCH] Switch to enum for Project RSVP state --- funnel/forms/sync_ticket.py | 6 +++--- funnel/models/project.py | 20 ++++++++++++-------- funnel/views/project.py | 8 ++++---- tests/e2e/account/register_test.py | 2 +- tests/unit/views/rsvp_test.py | 2 +- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/funnel/forms/sync_ticket.py b/funnel/forms/sync_ticket.py index 1a6966acd..b00a88ba8 100644 --- a/funnel/forms/sync_ticket.py +++ b/funnel/forms/sync_ticket.py @@ -9,10 +9,10 @@ from baseframe import __, forms from ..models import ( - PROJECT_RSVP_STATE, Account, AccountEmail, Project, + ProjectRsvpStateEnum, TicketClient, TicketEvent, TicketParticipant, @@ -71,8 +71,8 @@ class ProjectBoxofficeForm(forms.Form): ) rsvp_state = forms.RadioField( __("Registrations"), - choices=PROJECT_RSVP_STATE.items(), - default=PROJECT_RSVP_STATE.NONE, + choices=[(int(member.value), member.title) for member in ProjectRsvpStateEnum], + default=int(ProjectRsvpStateEnum.NONE), ) is_subscription = forms.BooleanField( __("Paid tickets are for a subscription"), diff --git a/funnel/models/project.py b/funnel/models/project.py index ff0c15933..d0b6d3b03 100644 --- a/funnel/models/project.py +++ b/funnel/models/project.py @@ -6,6 +6,7 @@ from collections import OrderedDict, defaultdict from collections.abc import Sequence from datetime import datetime, timedelta +from enum import ReprEnum from typing import TYPE_CHECKING, Any, Literal, Self, cast, overload from flask_babel import format_date, get_locale @@ -48,13 +49,14 @@ from .helpers import ( RESERVED_NAMES, ImgeeType, + IntTitle, MarkdownCompositeDocument, add_search_trigger, valid_name, visual_field_delimiter, ) -__all__ = ['PROJECT_RSVP_STATE', 'Project', 'ProjectLocation', 'ProjectRedirect'] +__all__ = ['ProjectRsvpStateEnum', 'Project', 'ProjectLocation', 'ProjectRedirect'] # --- Constants --------------------------------------------------------------- @@ -76,10 +78,10 @@ class CFP_STATE(LabeledEnum): # noqa: N801 ANY = {NONE, PUBLIC, CLOSED} -class PROJECT_RSVP_STATE(LabeledEnum): # noqa: N801 - NONE = (1, __("Not accepting registrations")) - ALL = (2, __("Anyone can register")) - MEMBERS = (3, __("Only members can register")) +class ProjectRsvpStateEnum(IntTitle, ReprEnum): + NONE = 1, __("Not accepting registrations") + ALL = 2, __("Anyone can register") + MEMBERS = 3, __("Only members can register") # --- Models ------------------------------------------------------------------ @@ -177,8 +179,10 @@ class Project(UuidMixin, BaseScopedNameMixin[int, Account], Model): rsvp_state: Mapped[int] = with_roles( sa_orm.mapped_column( sa.SmallInteger, - StateManager.check_constraint('rsvp_state', PROJECT_RSVP_STATE), - default=PROJECT_RSVP_STATE.NONE, + StateManager.check_constraint( + 'rsvp_state', ProjectRsvpStateEnum, sa.SmallInteger + ), + default=ProjectRsvpStateEnum.NONE, nullable=False, ), read={'all'}, @@ -898,7 +902,7 @@ def end_at_localized(self): @hybrid_property def allow_rsvp(self) -> bool: """RSVP state as a boolean value (allowed for all or not).""" - return self.rsvp_state == PROJECT_RSVP_STATE.ALL + return self.rsvp_state == ProjectRsvpStateEnum.ALL @property def active_rsvps(self) -> Query[Rsvp]: diff --git a/funnel/views/project.py b/funnel/views/project.py index aaf13cf45..05d77af6c 100644 --- a/funnel/views/project.py +++ b/funnel/views/project.py @@ -30,10 +30,10 @@ ProjectTransitionForm, ) from ..models import ( - PROJECT_RSVP_STATE, RSVP_STATUS, Account, Project, + ProjectRsvpStateEnum, RegistrationCancellationNotification, RegistrationConfirmationNotification, Rsvp, @@ -164,9 +164,9 @@ def feature_project_rsvp(obj: Project) -> bool: obj.state.PUBLISHED and (obj.start_at is None or not obj.state.PAST) and ( - obj.rsvp_state == PROJECT_RSVP_STATE.ALL + obj.rsvp_state == ProjectRsvpStateEnum.ALL or ( - obj.rsvp_state == PROJECT_RSVP_STATE.MEMBERS + obj.rsvp_state == ProjectRsvpStateEnum.MEMBERS and obj.current_roles.account_member ) ) @@ -178,7 +178,7 @@ def feature_project_rsvp_for_members(obj: Project) -> bool: return bool( obj.state.PUBLISHED and (obj.start_at is None or not obj.state.PAST) - and obj.rsvp_state == PROJECT_RSVP_STATE.MEMBERS + and obj.rsvp_state == ProjectRsvpStateEnum.MEMBERS ) diff --git a/tests/e2e/account/register_test.py b/tests/e2e/account/register_test.py index 65b18aac9..0d4d4baee 100644 --- a/tests/e2e/account/register_test.py +++ b/tests/e2e/account/register_test.py @@ -28,7 +28,7 @@ def published_project(db_session, new_project: models.Project) -> models.Project: """Published project fixture.""" new_project.publish() - new_project.rsvp_state = models.PROJECT_RSVP_STATE.ALL + new_project.rsvp_state = models.ProjectRsvpStateEnum.ALL db_session.commit() return new_project diff --git a/tests/unit/views/rsvp_test.py b/tests/unit/views/rsvp_test.py index bffc747da..3a5db27ac 100644 --- a/tests/unit/views/rsvp_test.py +++ b/tests/unit/views/rsvp_test.py @@ -96,7 +96,7 @@ def test_valid_registration_form_schema( { 'org': '', 'item_collection_id': '', - 'rsvp_state': models.PROJECT_RSVP_STATE.ALL, + 'rsvp_state': int(models.ProjectRsvpStateEnum.ALL), 'is_subscription': False, 'register_button_txt': 'Follow', 'register_form_schema': app.json.dumps(valid_schema),