-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement individual students stickying #1723
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from django import forms | ||
from django.contrib.auth import get_user_model | ||
from django.core.validators import ValidationError | ||
|
||
|
||
class UserMultipleChoiceField(forms.ModelMultipleChoiceField): | ||
"""Choose any user from the database.""" | ||
|
||
def clean(self, value): | ||
if not value and not self.required: | ||
return self.queryset.none() | ||
elif self.required: | ||
raise ValidationError(self.error_messages["required"], code="required") | ||
|
||
try: | ||
users = get_user_model().objects.filter(id__in=value) | ||
if len(users) != len(value): | ||
raise ValidationError(self.error_messages["invalid_choice"], code="invalid_choice") | ||
except (ValueError, TypeError) as e: | ||
raise ValidationError(self.error_messages["invalid_choice"], code="invalid_choice") from e | ||
return users | ||
|
||
def label_from_instance(self, obj): | ||
if isinstance(obj, get_user_model()): | ||
return f"{obj.get_full_name()} ({obj.username})" | ||
return super().label_from_instance(obj) |
20 changes: 20 additions & 0 deletions
20
intranet/apps/eighth/migrations/0071_eighthscheduledactivity_sticky_students.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 3.2.25 on 2024-10-12 21:12 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('eighth', '0070_eighthactivity_club_sponsors'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='eighthscheduledactivity', | ||
name='sticky_students', | ||
field=models.ManyToManyField(blank=True, related_name='sticky_scheduledactivity_set', to=settings.AUTH_USER_MODEL), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,13 @@ | |
import datetime | ||
import logging | ||
import string | ||
from collections.abc import Sequence | ||
from typing import Collection, Iterable, List, Optional, Union | ||
|
||
from cacheops import invalidate_obj | ||
from django.conf import settings | ||
from django.contrib.auth import get_user_model | ||
from django.contrib.auth.models import AbstractBaseUser | ||
from django.contrib.auth.models import Group as DjangoGroup | ||
from django.core import validators | ||
from django.core.cache import cache | ||
|
@@ -807,6 +809,11 @@ class EighthScheduledActivity(AbstractBaseEighthModel): | |
activity = models.ForeignKey(EighthActivity, on_delete=models.CASCADE) | ||
members = models.ManyToManyField(settings.AUTH_USER_MODEL, through="EighthSignup", related_name="eighthscheduledactivity_set") | ||
waitlist = models.ManyToManyField(settings.AUTH_USER_MODEL, through="EighthWaitlist", related_name="%(class)s_scheduledactivity_set") | ||
sticky_students = models.ManyToManyField( | ||
settings.AUTH_USER_MODEL, | ||
related_name="sticky_scheduledactivity_set", | ||
blank=True, | ||
) | ||
|
||
admin_comments = models.CharField(max_length=1000, blank=True) | ||
title = models.CharField(max_length=1000, blank=True) | ||
|
@@ -862,6 +869,24 @@ def title_with_flags(self) -> str: | |
name_with_flags = "Special: " + name_with_flags | ||
return name_with_flags | ||
|
||
def is_user_stickied(self, user: AbstractBaseUser) -> bool: | ||
"""Check if the given user is stickied to this activity. | ||
|
||
Args: | ||
user: The user to check for stickiness. | ||
""" | ||
return self.is_activity_sticky() or self.sticky_students.filter(pk=user.pk).exists() | ||
|
||
def is_activity_sticky(self) -> bool: | ||
"""Check if the scheduled activity or activity is sticky | ||
|
||
.. warning:: | ||
|
||
This method does NOT take into account individual user stickies. | ||
In 99.9% of cases, you should use :meth:`is_user_stickied` instead. | ||
""" | ||
return self.sticky or self.activity.sticky | ||
|
||
def get_true_sponsors(self) -> Union[QuerySet, Collection[EighthSponsor]]: # pylint: disable=unsubscriptable-object | ||
"""Retrieves the sponsors for the scheduled activity, taking into account activity defaults and | ||
overrides. | ||
|
@@ -920,13 +945,6 @@ def get_restricted(self) -> bool: | |
""" | ||
return self.restricted or self.activity.restricted | ||
|
||
def get_sticky(self) -> bool: | ||
"""Gets whether this scheduled activity is sticky. | ||
Returns: | ||
Whether this scheduled activity is sticky. | ||
""" | ||
return self.sticky or self.activity.sticky | ||
|
||
def get_finance(self) -> str: | ||
"""Retrieves the name of this activity's account with the | ||
finance office, if any. | ||
|
@@ -1097,10 +1115,50 @@ def notify_waitlist(self, waitlists: Iterable["EighthWaitlist"]): | |
[waitlist.user.primary_email_address], | ||
) | ||
|
||
def set_sticky_students(self, users: "Sequence[AbstractBaseUser]") -> None: | ||
"""Sets the given users to the sticky students list for this activity. | ||
|
||
This also sends emails to students. | ||
|
||
Args: | ||
users: The users to add to the sticky students list. | ||
|
||
Returns: | ||
A tuple of the new stickied students and the unstickied students. | ||
""" | ||
for user in users: | ||
signup = EighthSignup.objects.filter(user=user, scheduled_activity__block=self.block).first() | ||
if signup is not None: | ||
signup.remove_signup(user, force=True) | ||
self.add_user(user, force=True) | ||
|
||
old_sticky_students = self.sticky_students.all() | ||
self.sticky_students.set(users) | ||
|
||
# note: this will send separate emails to each student for each activity they are stickied in | ||
new_stickied_students = [user.notification_email for user in users if user not in old_sticky_students] | ||
unstickied_students = [user.notification_email for user in old_sticky_students if user not in users] | ||
email_send_task.delay( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this works |
||
"eighth/emails/students_stickied.txt", | ||
"eighth/emails/students_stickied.html", | ||
data={"activity": self}, | ||
subject="You have been stickied into an activity", | ||
emails=new_stickied_students, | ||
bcc=True, | ||
) | ||
email_send_task.delay( | ||
"eighth/emails/students_unstickied.txt", | ||
"eighth/emails/students_unstickied.html", | ||
data={"activity": self}, | ||
subject="You have been unstickied from an activity", | ||
emails=unstickied_students, | ||
bcc=True, | ||
) | ||
|
||
@transaction.atomic # This MUST be run in a transaction. Do NOT remove this decorator. | ||
def add_user( | ||
self, | ||
user: "get_user_model()", | ||
user: AbstractBaseUser, | ||
request: Optional[HttpRequest] = None, | ||
force: bool = False, | ||
no_after_deadline: bool = False, | ||
|
@@ -1160,8 +1218,9 @@ def add_user( | |
if ( | ||
EighthSignup.objects.filter(user=user, scheduled_activity__block__in=all_blocks) | ||
.filter(Q(scheduled_activity__activity__sticky=True) | Q(scheduled_activity__sticky=True)) | ||
.filter(Q(scheduled_activity__cancelled=False)) | ||
.filter(scheduled_activity__cancelled=False) | ||
.exists() | ||
or user.sticky_scheduledactivity_set.filter(block__in=all_blocks, cancelled=False).exists() | ||
): | ||
exception.Sticky = True | ||
|
||
|
@@ -1223,7 +1282,7 @@ def add_user( | |
if self.activity.users_blacklisted.filter(username=user).exists(): | ||
exception.Blacklisted = True | ||
|
||
if self.get_sticky(): | ||
if self.is_user_stickied(user): | ||
EighthWaitlist.objects.filter(user_id=user.id, block_id=self.block.id).delete() | ||
|
||
success_message = "Successfully added to waitlist for activity." if waitlist else "Successfully signed up for activity." | ||
|
@@ -1697,7 +1756,7 @@ def remove_signup(self, user: "get_user_model()" = None, force: bool = False, do | |
exception.ActivityDeleted = True | ||
|
||
# Check if the user is already stickied into an activity | ||
if self.scheduled_activity.activity and self.scheduled_activity.activity.sticky and not self.scheduled_activity.cancelled: | ||
if self.scheduled_activity.activity and self.scheduled_activity.is_user_stickied(user) and not self.scheduled_activity.cancelled: | ||
exception.Sticky = True | ||
|
||
if exception.messages() and not force: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes good