Skip to content

Commit

Permalink
Include penalty types to the model
Browse files Browse the repository at this point in the history
Used to distinguish and delete the correct penalties, as penalties from
the same type (and event) should in theory never stack.
  • Loading branch information
ivarnakken committed Mar 14, 2024
1 parent 9ed1443 commit e6f4aae
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
32 changes: 21 additions & 11 deletions lego/apps/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@
from lego.apps.files.models import FileField
from lego.apps.followers.models import FollowEvent
from lego.apps.permissions.models import ObjectPermissionsModel
from lego.apps.users.constants import AUTUMN, SPRING
from lego.apps.users.constants import (
AUTUMN,
LATE_PRESENCE_PENALTY_WEIGHT,
PENALTY_TYPES,
SPRING,
)
from lego.apps.users.models import AbakusGroup, Membership, Penalty, User
from lego.utils.models import BasisModel
from lego.utils.youtube_validator import youtube_validator
Expand Down Expand Up @@ -935,8 +940,10 @@ def set_presence(self, presence: constants.PRESENCE_CHOICES) -> None:
self.handle_user_penalty(presence)
self.save()

def delete_penalties_for_event(self) -> None:
for penalty in self.user.penalties.filter(source_event=self.event):
def delete_presence_penalties_for_event(self) -> None:
for penalty in self.user.penalties.filter(
source_event=self.event, type=PENALTY_TYPES.PRESENCE
):
penalty.delete()

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

print("\n\n\n")
print(presence)
print("\n\n\n")

if (
self.event.heed_penalties
and presence == constants.PRESENCE_CHOICES.NOT_PRESENT
and self.event.penalty_weight_on_not_present
):
self.delete_penalties_for_event()
self.delete_presence_penalties_for_event()
Penalty.objects.create(
user=self.user,
reason=f"Møtte ikke opp på {self.event.title}.",
weight=self.event.penalty_weight_on_not_present,
source_event=self.event,
type=PENALTY_TYPES.PRESENCE,
)
elif (
self.event.heed_penalties
and presence == constants.PRESENCE_CHOICES.TOO_LATE
):
self.delete_penalties_for_event()
elif self.event.heed_penalties and presence == constants.PRESENCE_CHOICES.LATE:
self.delete_presence_penalties_for_event()
Penalty.objects.create(

Check warning on line 974 in lego/apps/events/models.py

View check run for this annotation

Codecov / codecov/patch

lego/apps/events/models.py#L973-L974

Added lines #L973 - L974 were not covered by tests
user=self.user,
reason=f"Møtte for sent opp på {self.event.title}.",
weight=1,
weight=LATE_PRESENCE_PENALTY_WEIGHT,
source_event=self.event,
type=PENALTY_TYPES.PRESENCE,
)
else:
self.delete_penalties_for_event()
self.delete_presence_penalties_for_event()

def add_to_pool(self, pool: Pool) -> Registration:
allowed: bool = False
Expand Down
13 changes: 11 additions & 2 deletions lego/apps/users/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from enum import Enum

from django.db import models

MALE = "male"
FEMALE = "female"
OTHER = "other"
Expand Down Expand Up @@ -95,8 +97,6 @@ def values(cls) -> list[str]:
FSGroup.MSSECCLO: FOURTH_GRADE_KOMTEK,
}

STUDENT_EMAIL_DOMAIN = "stud.ntnu.no"

GROUP_COMMITTEE = "komite"
GROUP_INTEREST = "interesse"
GROUP_BOARD = "styre"
Expand Down Expand Up @@ -142,3 +142,12 @@ def values(cls) -> list[str]:
(LIGHT_THEME, LIGHT_THEME),
(DARK_THEME, DARK_THEME),
)


LATE_PRESENCE_PENALTY_WEIGHT = 1


class PENALTY_TYPES(models.TextChoices):
PRESENCE = "presence"
PAYMENT = "payment"
OTHER = "other"
25 changes: 25 additions & 0 deletions lego/apps/users/migrations/0042_penalty_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.0.10 on 2024-03-14 10:27

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("users", "0041_user_linkedin_id_alter_user_github_username"),
]

operations = [
migrations.AddField(
model_name="penalty",
name="type",
field=models.CharField(
choices=[
("presence", "Presence"),
("payment", "Payment"),
("other", "Other"),
],
default="other",
max_length=50,
),
),
]
5 changes: 5 additions & 0 deletions lego/apps/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,11 @@ class Penalty(BasisModel):
source_event = models.ForeignKey(
"events.Event", related_name="penalties", on_delete=models.CASCADE
)
type = models.CharField(
max_length=50,
choices=constants.PENALTY_TYPES.choices,
default=constants.PENALTY_TYPES.OTHER,
)

objects = UserPenaltyManager() # type: ignore

Expand Down

0 comments on commit e6f4aae

Please sign in to comment.