Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Generated by Django 4.2.21 on 2025-06-09 15:29

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


class Migration(migrations.Migration):

dependencies = [
('teams', '0063_alter_team_member_group'),
]

operations = [
migrations.CreateModel(
name='TeamShiftAssignment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True)),
('updated', models.DateTimeField(auto_now=True)),
('for_sale', models.BooleanField(default=False, help_text='Is the shift assignment for sale?')),
('team_member', models.ForeignKey(help_text='The team member on shift', on_delete=django.db.models.deletion.CASCADE, to='teams.teammember')),
('team_shift', models.ForeignKey(help_text='The shift', on_delete=django.db.models.deletion.CASCADE, to='teams.teamshift')),
],
options={
'abstract': False,
},
),
migrations.AddField(
model_name='teamshift',
name='team_members_new',
field=models.ManyToManyField(blank=True, related_name='team_members_new', through='teams.TeamShiftAssignment', to='teams.teammember'),
),
]
19 changes: 19 additions & 0 deletions src/teams/migrations/0065_teamshiftassignment_migrate_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.db import migrations, models
import django.db.models.deletion

def migrate_assignments(apps, schema_editor):
TeamShift = apps.get_model("teams", "TeamShift")
for teamshift in TeamShift.objects.all():
members = teamshift.team_members.all()
teamshift.team_members_new.set(members)
teamshift.save()

class Migration(migrations.Migration):

dependencies = [
('teams', '0064_teamshiftassignment_teamshift_team_members_new'),
]

operations = [
migrations.RunPython(migrate_assignments),
]
17 changes: 17 additions & 0 deletions src/teams/migrations/0066_remove_teamshift_team_members.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.21 on 2025-06-09 15:37

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('teams', '0065_teamshiftassignment_migrate_data'),
]

operations = [
migrations.RemoveField(
model_name='teamshift',
name='team_members',
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.21 on 2025-06-09 15:38

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('teams', '0066_remove_teamshift_team_members'),
]

operations = [
migrations.RenameField(
model_name='teamshift',
old_name='team_members_new',
new_name='team_members',
),
]
18 changes: 18 additions & 0 deletions src/teams/migrations/0068_alter_teamshift_team_members.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.21 on 2025-06-09 15:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('teams', '0067_rename_team_members_new_teamshift_team_members'),
]

operations = [
migrations.AlterField(
model_name='teamshift',
name='team_members',
field=models.ManyToManyField(blank=True, through='teams.TeamShiftAssignment', to='teams.teammember'),
),
]
18 changes: 18 additions & 0 deletions src/teams/migrations/0069_teamshiftassignment_updated_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.21 on 2025-06-22 17:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('teams', '0068_alter_teamshift_team_members'),
]

operations = [
migrations.AddField(
model_name='teamshiftassignment',
name='updated_at',
field=models.DateTimeField(auto_now=True),
),
]
53 changes: 49 additions & 4 deletions src/teams/models.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
"""All models for teams application."""

from __future__ import annotations

import logging
from typing import TYPE_CHECKING

from django.conf import settings
from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.contrib.postgres.fields import DateTimeRangeField
from django.db import models
from django.urls import reverse_lazy
from django_prometheus.models import ExportModelOperationsMixin

from camps.models import Permission as CampPermission
from utils.models import CampRelatedModel
from utils.models import CreatedUpdatedModel
from utils.models import UUIDModel
Expand Down Expand Up @@ -58,6 +56,7 @@

class Team(ExportModelOperationsMixin("team"), CampRelatedModel):
"""Model for team."""

camp = models.ForeignKey(
"camps.Camp",
related_name="teams",
Expand Down Expand Up @@ -211,6 +210,7 @@ class Team(ExportModelOperationsMixin("team"), CampRelatedModel):

class Meta:
"""Meta."""

ordering: ClassVar[list[str]] = ["name"]
unique_together = (("name", "camp"), ("slug", "camp"))

Expand Down Expand Up @@ -394,6 +394,7 @@ def tasker_permission_set(self) -> str:

class TeamMember(ExportModelOperationsMixin("team_member"), CampRelatedModel):
"""Model for team member."""

user = models.ForeignKey(
"auth.User",
on_delete=models.PROTECT,
Expand Down Expand Up @@ -425,6 +426,7 @@ class TeamMember(ExportModelOperationsMixin("team_member"), CampRelatedModel):

class Meta:
"""Meta."""

ordering: ClassVar[list[str]] = ["-lead", "-approved"]

def __str__(self) -> str:
Expand Down Expand Up @@ -471,6 +473,7 @@ def update_group_membership(self, deleted=False) -> None:

class TeamTask(ExportModelOperationsMixin("team_task"), CampRelatedModel):
"""Model for team tasks."""

team = models.ForeignKey(
"teams.Team",
related_name="tasks",
Expand Down Expand Up @@ -498,6 +501,7 @@ class TeamTask(ExportModelOperationsMixin("team_task"), CampRelatedModel):

class Meta:
"""Meta."""

ordering: ClassVar[list[str]] = ["completed", "when", "name"]
unique_together = (("name", "team"), ("slug", "team"))

Expand Down Expand Up @@ -539,6 +543,7 @@ class TaskComment(
CreatedUpdatedModel,
):
"""Model for task comments."""

task = models.ForeignKey(
"teams.TeamTask",
on_delete=models.PROTECT,
Expand All @@ -548,10 +553,40 @@ class TaskComment(
comment = models.TextField()


class TeamShiftAssignment(CampRelatedModel):
"""Through model for the shift<>member m2m storing the for_sale state of the shift assignment."""

team_shift = models.ForeignKey(
"teams.TeamShift",
on_delete=models.CASCADE,
help_text="The shift",
)

team_member = models.ForeignKey(
"teams.TeamMember",
on_delete=models.CASCADE,
help_text="The team member on shift",
)

for_sale = models.BooleanField(
default=False,
help_text="Is the shift assignment for sale?",
)

updated_at = models.DateTimeField(auto_now=True)

@property
def camp(self) -> Camp:
"""All CampRelatedModels must have a camp FK or a camp property."""
return self.team_shift.camp


class TeamShift(ExportModelOperationsMixin("team_shift"), CampRelatedModel):
"""Model for team shifts."""

class Meta:
"""Meta."""

ordering = ("shift_range",)

team = models.ForeignKey(
Expand All @@ -563,7 +598,7 @@ class Meta:

shift_range = DateTimeRangeField()

team_members = models.ManyToManyField(TeamMember, blank=True)
team_members = models.ManyToManyField("teams.TeamMember", blank=True, through=TeamShiftAssignment)

people_required = models.IntegerField(default=1)

Expand All @@ -582,3 +617,13 @@ def __str__(self) -> str:
def users(self) -> list[TeamMember]:
"""Returns a list of team members on this shift."""
return [member.user for member in self.team_members.all()]

@property
def for_sale_users(self) -> list[TeamMember]:
"""Returns a list of team members who marked this shift for sale."""
return [member.user for member in self.team_members.filter(teamshiftassignment__for_sale=True)]

@property
def shifts_taken(self) -> int:
"""Returns the number of taken shifts that are not for sale."""
return self.team_members.filter(teamshiftassignment__for_sale=False).count()
15 changes: 15 additions & 0 deletions src/teams/templates/team_shift_confirm_action.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'team_base.html' %}

{% block title %}
{{ action }} | {{ block.super }}
{% endblock %}

{% block team_content %}

<form method="post">{% csrf_token %}
<p>{{ action }}</p>
<input type="submit" class="btn btn-danger" name="confirm" value="Confirm" />
<a class="btn btn-secondary" href="{% url 'teams:shifts' camp_slug=camp.slug team_slug=team.slug%}"><i class="fas fa-undo"></i> Cancel</a>
</form>

{% endblock %}
14 changes: 10 additions & 4 deletions src/teams/templates/team_shift_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ <h4>
{{ shift.shift_range.upper|date:'H:i' }}
</td>
<td>
{{ shift.people_required }}
{{ shift.team_members.count }} of {{ shift.people_required }} assigned
</td>
<td>
{% for member in shift.team_members.all %}
{{ member.user.profile.get_public_credit_name }}{% if not forloop.last %},{% endif %}
{% for member in shift.teamshiftassignment_set.all %}
{{ member.team_member.user.profile.get_public_credit_name }}{% if member.for_sale %} <em>(for sale!)</em>{% endif %}{% if not forloop.last %},{% endif %}
{% empty %}
None!
{% endfor %}
Expand All @@ -80,7 +80,13 @@ <h4>
href="{% url 'teams:shift_member_drop' camp_slug=camp.slug team_slug=team.slug pk=shift.pk %}">
<i class="fas fa-thumbs-down"></i> Unassign me
</a>
{% elif shift.people_required > shift.team_members.count %}
{% if user not in shift.for_sale_users %}
<a class="btn btn-warning"
href="{% url 'teams:shift_member_sell' camp_slug=camp.slug team_slug=team.slug pk=shift.pk %}">
<i class="fas fa-thumbs-down"></i> Make available
</a>
{% endif %}
{% elif shift.people_required > shift.shifts_taken %}
<a class="btn btn-success"
href="{% url 'teams:shift_member_take' camp_slug=camp.slug team_slug=team.slug pk=shift.pk %}">
<i class="fas fa-thumbs-up"></i> Assign me
Expand Down
12 changes: 8 additions & 4 deletions src/teams/templates/team_user_shifts.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ <h4>
</td>
<td>
{{ shift.shift_range.upper|date:'H:i' }}
</td>
<td>
<a class="btn btn-danger"
href="{% url 'teams:shift_member_drop' camp_slug=camp.slug team_slug=shift.team.slug pk=shift.pk %}">
</td>
<td>
<a class="btn btn-warning"
href="{% url 'teams:shift_member_sell' camp_slug=camp.slug team_slug=shift.team.slug pk=shift.pk %}">
<i class="fas fa-thumbs-down"></i> Sell shift
<td>
<a class="btn btn-danger"
href="{% url 'teams:shift_member_drop' camp_slug=camp.slug team_slug=shift.team.slug pk=shift.pk %}">
<i class="fas fa-thumbs-down"></i> Unassign me
</a>
</td>
Expand Down
Loading
Loading