Merge pull request #5997 from jsangmeister/remove-duplicates-from-entitled-users

Prevent multiple entries in entitled_users_at_stop
This commit is contained in:
Emanuel Schütze 2021-04-08 15:39:14 +02:00 committed by GitHub
commit 26d5d81b6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 1 deletions

View File

@ -0,0 +1,18 @@
# Generated by jsangmeister on 2021-04-08 11:27
from django.db import migrations
from ...poll.migrations.poll_migration_helper import remove_entitled_users_duplicates
class Migration(migrations.Migration):
dependencies = [
("assignments", "0023_assignmentpoll_change_fields_2"),
]
operations = [
migrations.RunPython(
remove_entitled_users_duplicates("assignments", "AssignmentPoll")
),
]

View File

@ -0,0 +1,16 @@
# Generated by jsangmeister on 2021-04-08 11:27
from django.db import migrations
from ...poll.migrations.poll_migration_helper import remove_entitled_users_duplicates
class Migration(migrations.Migration):
dependencies = [
("motions", "0042_motionpoll_change_fields_2"),
]
operations = [
migrations.RunPython(remove_entitled_users_duplicates("motions", "MotionPoll")),
]

View File

@ -34,3 +34,25 @@ def calculate_vote_fields(poll_model_collection, poll_model_name):
poll.save(skip_autoupdate=True) poll.save(skip_autoupdate=True)
return _calculate_vote_fields return _calculate_vote_fields
def remove_entitled_users_duplicates(poll_model_collection, poll_model_name):
"""
Takes all polls of the given model and removes any duplicate entries from
entitled_users_at_stop
"""
def _remove_entitled_users_duplicates(apps, schema_editor):
PollModel = apps.get_model(poll_model_collection, poll_model_name)
for poll in PollModel.objects.all():
if poll.entitled_users_at_stop:
new_entitled_users = []
entitled_users_ids = set()
for entry in poll.entitled_users_at_stop:
if entry["user_id"] not in entitled_users_ids:
entitled_users_ids.add(entry["user_id"])
new_entitled_users.append(entry)
poll.entitled_users_at_stop = new_entitled_users
poll.save(skip_autoupdate=True)
return _remove_entitled_users_duplicates

View File

@ -275,9 +275,11 @@ class BasePoll(models.Model):
def calculate_entitled_users(self): def calculate_entitled_users(self):
entitled_users = [] entitled_users = []
entitled_users_ids = set()
for group in self.groups.all(): for group in self.groups.all():
for user in group.user_set.all(): for user in group.user_set.all():
if user.is_present: if user.is_present and user.id not in entitled_users_ids:
entitled_users_ids.add(user.id)
entitled_users.append( entitled_users.append(
{ {
"user_id": user.id, "user_id": user.id,

View File

@ -1208,6 +1208,19 @@ class StopMotionPoll(TestCase):
], ],
) )
def test_stop_poll_assert_no_duplicate_entitled_users(self):
self.setup_entitled_users()
delegate_group = get_group_model().objects.get(pk=GROUP_DELEGATE_PK)
self.admin.groups.add(delegate_group)
self.poll.groups.add(delegate_group)
response = self.client.post(reverse("motionpoll-stop", args=[self.poll.pk]))
self.assertHttpStatusVerbose(response, status.HTTP_200_OK)
self.assertEqual(
MotionPoll.objects.get().entitled_users_at_stop,
[{"user_id": self.admin.id, "voted": False, "vote_delegated_to_id": None}],
)
class PublishMotionPoll(TestCase): class PublishMotionPoll(TestCase):
def advancedSetUp(self): def advancedSetUp(self):