Skip to content

Commit e6f4aae

Browse files
committed
Include penalty types to the model
Used to distinguish and delete the correct penalties, as penalties from the same type (and event) should in theory never stack.
1 parent 9ed1443 commit e6f4aae

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

lego/apps/events/models.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@
3030
from lego.apps.files.models import FileField
3131
from lego.apps.followers.models import FollowEvent
3232
from lego.apps.permissions.models import ObjectPermissionsModel
33-
from lego.apps.users.constants import AUTUMN, SPRING
33+
from lego.apps.users.constants import (
34+
AUTUMN,
35+
LATE_PRESENCE_PENALTY_WEIGHT,
36+
PENALTY_TYPES,
37+
SPRING,
38+
)
3439
from lego.apps.users.models import AbakusGroup, Membership, Penalty, User
3540
from lego.utils.models import BasisModel
3641
from lego.utils.youtube_validator import youtube_validator
@@ -935,8 +940,10 @@ def set_presence(self, presence: constants.PRESENCE_CHOICES) -> None:
935940
self.handle_user_penalty(presence)
936941
self.save()
937942

938-
def delete_penalties_for_event(self) -> None:
939-
for penalty in self.user.penalties.filter(source_event=self.event):
943+
def delete_presence_penalties_for_event(self) -> None:
944+
for penalty in self.user.penalties.filter(
945+
source_event=self.event, type=PENALTY_TYPES.PRESENCE
946+
):
940947
penalty.delete()
941948

942949
def handle_user_penalty(self, presence: constants.PRESENCE_CHOICES) -> None:
@@ -945,31 +952,34 @@ def handle_user_penalty(self, presence: constants.PRESENCE_CHOICES) -> None:
945952
newest presence is the only one that matters
946953
"""
947954

955+
print("\n\n\n")
956+
print(presence)
957+
print("\n\n\n")
958+
948959
if (
949960
self.event.heed_penalties
950961
and presence == constants.PRESENCE_CHOICES.NOT_PRESENT
951962
and self.event.penalty_weight_on_not_present
952963
):
953-
self.delete_penalties_for_event()
964+
self.delete_presence_penalties_for_event()
954965
Penalty.objects.create(
955966
user=self.user,
956967
reason=f"Møtte ikke opp på {self.event.title}.",
957968
weight=self.event.penalty_weight_on_not_present,
958969
source_event=self.event,
970+
type=PENALTY_TYPES.PRESENCE,
959971
)
960-
elif (
961-
self.event.heed_penalties
962-
and presence == constants.PRESENCE_CHOICES.TOO_LATE
963-
):
964-
self.delete_penalties_for_event()
972+
elif self.event.heed_penalties and presence == constants.PRESENCE_CHOICES.LATE:
973+
self.delete_presence_penalties_for_event()
965974
Penalty.objects.create(
966975
user=self.user,
967976
reason=f"Møtte for sent opp på {self.event.title}.",
968-
weight=1,
977+
weight=LATE_PRESENCE_PENALTY_WEIGHT,
969978
source_event=self.event,
979+
type=PENALTY_TYPES.PRESENCE,
970980
)
971981
else:
972-
self.delete_penalties_for_event()
982+
self.delete_presence_penalties_for_event()
973983

974984
def add_to_pool(self, pool: Pool) -> Registration:
975985
allowed: bool = False

lego/apps/users/constants.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from enum import Enum
22

3+
from django.db import models
4+
35
MALE = "male"
46
FEMALE = "female"
57
OTHER = "other"
@@ -95,8 +97,6 @@ def values(cls) -> list[str]:
9597
FSGroup.MSSECCLO: FOURTH_GRADE_KOMTEK,
9698
}
9799

98-
STUDENT_EMAIL_DOMAIN = "stud.ntnu.no"
99-
100100
GROUP_COMMITTEE = "komite"
101101
GROUP_INTEREST = "interesse"
102102
GROUP_BOARD = "styre"
@@ -142,3 +142,12 @@ def values(cls) -> list[str]:
142142
(LIGHT_THEME, LIGHT_THEME),
143143
(DARK_THEME, DARK_THEME),
144144
)
145+
146+
147+
LATE_PRESENCE_PENALTY_WEIGHT = 1
148+
149+
150+
class PENALTY_TYPES(models.TextChoices):
151+
PRESENCE = "presence"
152+
PAYMENT = "payment"
153+
OTHER = "other"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Generated by Django 4.0.10 on 2024-03-14 10:27
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("users", "0041_user_linkedin_id_alter_user_github_username"),
9+
]
10+
11+
operations = [
12+
migrations.AddField(
13+
model_name="penalty",
14+
name="type",
15+
field=models.CharField(
16+
choices=[
17+
("presence", "Presence"),
18+
("payment", "Payment"),
19+
("other", "Other"),
20+
],
21+
default="other",
22+
max_length=50,
23+
),
24+
),
25+
]

lego/apps/users/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,11 @@ class Penalty(BasisModel):
537537
source_event = models.ForeignKey(
538538
"events.Event", related_name="penalties", on_delete=models.CASCADE
539539
)
540+
type = models.CharField(
541+
max_length=50,
542+
choices=constants.PENALTY_TYPES.choices,
543+
default=constants.PENALTY_TYPES.OTHER,
544+
)
540545

541546
objects = UserPenaltyManager() # type: ignore
542547

0 commit comments

Comments
 (0)