Skip to content

Commit

Permalink
Switch to enum for Project RSVP state
Browse files Browse the repository at this point in the history
  • Loading branch information
jace committed Jan 3, 2024
1 parent be1f00c commit fffa0b1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
6 changes: 3 additions & 3 deletions funnel/forms/sync_ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from baseframe import __, forms

from ..models import (
PROJECT_RSVP_STATE,
Account,
AccountEmail,
Project,
ProjectRsvpStateEnum,
TicketClient,
TicketEvent,
TicketParticipant,
Expand Down Expand Up @@ -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"),
Expand Down
20 changes: 12 additions & 8 deletions funnel/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ---------------------------------------------------------------
Expand All @@ -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 ------------------------------------------------------------------
Expand Down Expand Up @@ -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'},
Expand Down Expand Up @@ -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]:
Expand Down
8 changes: 4 additions & 4 deletions funnel/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
ProjectTransitionForm,
)
from ..models import (
PROJECT_RSVP_STATE,
RSVP_STATUS,
Account,
Project,
ProjectRsvpStateEnum,
RegistrationCancellationNotification,
RegistrationConfirmationNotification,
Rsvp,
Expand Down Expand Up @@ -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
)
)
Expand All @@ -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
)


Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/account/register_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/views/rsvp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit fffa0b1

Please sign in to comment.