Skip to content

Commit

Permalink
Override group on event
Browse files Browse the repository at this point in the history
  • Loading branch information
rebkwok committed Dec 29, 2023
1 parent 7c21954 commit be073ab
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 4.1.1 on 2023-12-29 12:04

import booking.models
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('booking', '0091_datamigration_permissions'),
]

operations = [
migrations.AlterModelOptions(
name='allowedgroup',
options={'ordering': ('group__name',)},
),
migrations.AlterModelOptions(
name='eventtype',
options={'ordering': ('event_type', 'subtype')},
),
migrations.AddField(
model_name='event',
name='allowed_group_override',
field=models.ForeignKey(blank=True, help_text='Override group allowed to book this event (defaults to same group as the event type)', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='events', to='booking.allowedgroup'),
),
migrations.AlterField(
model_name='event',
name='allowed_group',
field=models.ForeignKey(blank=True, help_text='Override group allowed to book this event (defaults to same group as the event type)', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='booking.allowedgroup'),
),
migrations.AlterField(
model_name='event',
name='payment_time_allowed',
field=models.PositiveIntegerField(blank=True, help_text='Number of hours allowed for payment after booking (after this bookings will be cancelled.)', null=True),
),
migrations.AlterField(
model_name='eventtype',
name='allowed_group',
field=models.ForeignKey(default=booking.models.get_default_allowed_group_id, help_text='Group allowed to book this type of event', on_delete=models.SET(booking.models.get_default_allowed_group), related_name='event_types', to='booking.allowedgroup'),
),
]
17 changes: 17 additions & 0 deletions booking/migrations/0093_remove_event_allowed_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.1.1 on 2023-12-29 12:07

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('booking', '0092_alter_allowedgroup_options_alter_eventtype_options_and_more'),
]

operations = [
migrations.RemoveField(
model_name='event',
name='allowed_group',
),
]
33 changes: 21 additions & 12 deletions booking/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ def create_with_group(cls, group_name, description=None):
return cls.objects.create(group=group, description=description)


def get_default_allowed_group():
return AllowedGroup.default_group()


def get_default_allowed_group_id():
return AllowedGroup.default_group().id


class EventType(models.Model):
TYPE_CHOICE = (
('CL', 'Class'),
Expand All @@ -101,8 +109,11 @@ class EventType(models.Model):
"should match the event type used in "
"the Block Type.")
allowed_group = models.ForeignKey(
AllowedGroup, null=True, blank=True, related_name="event_types",
help_text="Group allowed to book this type of event", on_delete=models.SET_NULL
AllowedGroup,
default=get_default_allowed_group_id,
related_name="event_types",
help_text="Group allowed to book this type of event",
on_delete=models.SET(get_default_allowed_group),
)

def __str__(self):
Expand All @@ -123,11 +134,6 @@ def add_permission_to_book(self, user):
def allowed_group_description(self):
return self.allowed_group.description

def save(self, *args, **kwargs):
if not self.allowed_group:
self.allowed_group = AllowedGroup.default_group()
return super().save(*args, **kwargs)

class Meta:
unique_together = ('event_type', 'subtype')
ordering = ('event_type', 'subtype')
Expand Down Expand Up @@ -213,7 +219,7 @@ class Event(models.Model):
visible_on_site = models.BooleanField(default=True)
categories = models.ManyToManyField(FilterCategory)

allowed_group = models.ForeignKey(
allowed_group_override = models.ForeignKey(
AllowedGroup, null=True, blank=True, related_name="events", on_delete=models.SET_NULL,
help_text="Override group allowed to book this event (defaults to same group as the event type)"
)
Expand Down Expand Up @@ -273,6 +279,12 @@ def can_cancel(self):
def show_video_link(self):
return (self.is_online and (timezone.now() > self.date - timedelta(minutes=20)) or self.event_type.event_type == "OT")

@property
def allowed_group(self):
if self.allowed_group_override:
return self.allowed_group_override
return self.event_type.allowed_group

def has_permission_to_book(self, user):
return self.allowed_group.has_permission(user)

Expand All @@ -283,7 +295,7 @@ def allowed_group_description(self):
def allowed_group_for_event(self):
if self.allowed_group == AllowedGroup.default_group():
return "-"
return self.allowed_group or self.event_type.allowed_group
return self.allowed_group

def get_absolute_url(self):
return reverse("booking:event_detail", kwargs={'slug': self.slug})
Expand Down Expand Up @@ -330,9 +342,6 @@ def save(self, *args, **kwargs):
self.payment_open = False
self.booking_open = False

if not self.allowed_group:
self.allowed_group = self.event_type.allowed_group

super(Event, self).save(*args, **kwargs)


Expand Down
2 changes: 1 addition & 1 deletion booking/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_online_class_show_video_link(self):

def test_allowed_group(self):
pp_et = baker.make_recipe("booking.event_type_PP")
pp = baker.make_recipe('booking.future_PP', event_type=pp_et, allowed_group=None)
pp = baker.make_recipe('booking.future_PP', event_type=pp_et, allowed_group_override=None)
assert pp.allowed_group_for_event() == pp_et.allowed_group
assert pp.allowed_group_description == pp_et.allowed_group.description

Expand Down
8 changes: 4 additions & 4 deletions studioadmin/forms/event_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class Meta:
model = Event
fields = (
'name', 'event_type', 'date', 'categories', 'new_category',
'allowed_group',
'allowed_group_override',
'video_link', 'video_link_available_after_class',
'description', 'location',
'max_participants', 'contact_person', 'contact_email', 'cost',
Expand Down Expand Up @@ -390,7 +390,7 @@ class Meta:
'paypal_email': forms.EmailInput(
attrs={'class': "form-control"}
),
'allowed_group': forms.Select(
'allowed_group_override': forms.Select(
attrs={'class': "form-control"}
),
}
Expand Down Expand Up @@ -427,8 +427,8 @@ class Meta:
'Check this carefully! If you enter an incorrect email, '
'payments will fail or could be paid to the wrong account!'
),
'allowed_group': _(
"Group allowed to book this event (leave blank to default to same group as the event type)"
'allowed_group_override': _(
"Override the group allowed to book this event (leave blank to default to same group as the event type)"
)
}

Expand Down
5 changes: 3 additions & 2 deletions studioadmin/tests/test_views/test_setup_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@

@pytest.fixture(autouse=True)
def cleanup_groups():
AllowedGroup.objects.all().delete()
default_group = AllowedGroup.default_group()
AllowedGroup.objects.exclude(id=default_group.id).delete()
yield
AllowedGroup.objects.all().delete()
AllowedGroup.objects.exclude(id=default_group.id).delete()


def test_event_types_list_staff_only(client, staff_user, instructor_user):
Expand Down

0 comments on commit be073ab

Please sign in to comment.