Skip to content

Commit

Permalink
Lucia/feature/add max number of participants to events (#1240)
Browse files Browse the repository at this point in the history
Add a max number of participants to events.
Show current cumber of participants and max number of participants in
event api.
When max capacity is reached, sign-up is not possible anymore

![bild](https://github.com/user-attachments/assets/40678dfc-3dbd-4d9b-9b46-39ebaa70f626)
image: api info


![bild](https://github.com/user-attachments/assets/cd071439-9177-4354-bae7-7a057b4e450d)
image: when event is fully booked

---------

Co-authored-by: Lucia Karens <Lucia Karens>
Co-authored-by: Didrik Munther <[email protected]>
  • Loading branch information
luciakar and didrikmunther authored Oct 10, 2024
1 parent f4cbaf1 commit 07a77a0
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 8 deletions.
8 changes: 6 additions & 2 deletions events/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
from people.models import Profile
from lib.KTH_Catalog import lookup_user_with_api, merge_user_info

from django.db.models import Count


@require_GET
def index(request):
"""
Returns all published events for this years fair
Returns all published events for this year's fair
"""

fair = Fair.objects.get(current=True)
events = Event.objects.filter(fair=fair, published=True).prefetch_related(
"signupquestion_set"
Expand Down Expand Up @@ -94,6 +95,9 @@ def signup(request, event_pk):
if not event.open_for_signup:
return JsonResponse({"error": "Event not open for sign ups."}, status=403)

if event.is_full():
return JsonResponse({"message": "Event is fully booked."}, status=400)

participant, _created = Participant.objects.get_or_create(
user_s=request.user, event=event
)
Expand Down
20 changes: 20 additions & 0 deletions events/migrations/0032_event_max_capacity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 2.2.24 on 2024-07-13 15:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("events", "0031_add_program_file_question_foreign_key"),
]

operations = [
migrations.AddField(
model_name="event",
name="event_max_capacity",
field=models.PositiveIntegerField(
blank=True, null=True, verbose_name="Event max capacity"
),
),
]
13 changes: 13 additions & 0 deletions events/migrations/0035_merge_20241008_1413.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Generated by Django 2.2.24 on 2024-10-08 14:13

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("events", "0034_remove_requires_invitation"),
("events", "0032_event_max_capacity"),
]

operations = []
14 changes: 14 additions & 0 deletions events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class Event(models.Model):
teams_default_max_capacity = models.PositiveIntegerField(
blank=True, null=True, verbose_name="Default max number of team members"
) # None => no limit

event_max_capacity = models.PositiveIntegerField(
blank=True, null=True, verbose_name="Event max capacity"
) # None => no limit

fee_s = models.PositiveIntegerField(
default=0, blank=False, null=False, verbose_name="Sign-up fee for students"
)
Expand All @@ -66,6 +71,15 @@ class Event(models.Model):
upload_to=UploadToDirUUID("events", "pictures"), blank=True, null=True
)

def is_full(self):
if self.event_max_capacity is None:
return False

return self.number_of_signups() >= self.event_max_capacity

def number_of_signups(self):
return self.participant_set.count()

class Meta:
ordering = ["date_start", "name"]

Expand Down
3 changes: 3 additions & 0 deletions events/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def event(event, request):
"can_join_teams": event.teams_participate_s,
"open_for_signup_student": event.open_for_signup and event.signup_s,
"open_for_signup_company": event.open_for_signup and event.signup_cr,
"event_max_capacity": event.event_max_capacity,
"participant_count": event.participant_set.count(),
"fully_booked": event.is_full(),
}

return data
Expand Down
9 changes: 7 additions & 2 deletions events/static/js/signup/components/SignupForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class SignupForm extends Component {
const {event, stripe_publishable, payment_url} = this.props;
const {answers, errors, payed} = this.state;

const open_for_signup = event.open_for_signup_student || event.open_for_signup_company
const open_for_signup = !event.fully_booked && (event.open_for_signup_student || event.open_for_signup_company)

return (
<Grid container spacing={16}>
Expand Down Expand Up @@ -160,7 +160,12 @@ class SignupForm extends Component {
{event.fee == 0 && (
<Grid item sm={12}>
<Typography style={{marginTop: 8}}>By signing up you agree to THS Armada's <a href="https://docs.google.com/document/d/14_dUZHTL6QUNF9UeL7fghJXO1wZimbi_aKG5ttcGd1s/edit#heading=h.hpqg0xn5jl2q" target="_blank" rel="noopener noreferrer" style={{ color: "#00d790" }}>Privacy Notice</a>.</Typography>
<Button

<Typography style={{marginTop: 16, color: "#B22222", fontSize: 20 }}>
{event.fully_booked ? 'Sorry, this event is fully booked.': ''}
</Typography>

<Button
disabled={!open_for_signup}
onClick={this.handleClick}
variant="contained"
Expand Down
11 changes: 8 additions & 3 deletions templates/events/event_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@
{% if perms.events.base %}
<td class="text-center">{{ event.num_participants }}</td> {% endif %}
<td>
{% if event.open_for_signup %}
<span class="label label-success">Open</span>
{% else %}
{% if not event.open_for_signup %}
<span class="label label-default">Closed</span>

{% elif event.is_full%}
<span class="label label-danger">Fully Booked</span>

{% else %}
<span class="label label-success">Open</span>

{% endif %}
</td>
{% if perms.events.base %}
Expand Down
2 changes: 1 addition & 1 deletion templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ <h3 class="text-xl mb-5">Student</h3>
<form method="post" class="flex flex-col gap-y-2">
{% csrf_token %}
<a class="flex justify-center" href="/login{% if next %}?next={{ next }}{% endif %}"><img src="{% static "images/kth_login_button.png" %}" alt="Login with KTH"></a>
<p>If you don't have an KTH account log in <a href="/accounts/login">here</a>.</p>
<p>If you don't have a KTH account log in <a class="text-emerald-400" href="/accounts/login">here</a>.</p>
</form>
</div>
</div>
Expand Down

0 comments on commit 07a77a0

Please sign in to comment.