Merge pull request #5997 from jsangmeister/remove-duplicates-from-entitled-users
Prevent multiple entries in entitled_users_at_stop
This commit is contained in:
commit
26d5d81b6f
@ -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")
|
||||
),
|
||||
]
|
@ -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")),
|
||||
]
|
@ -34,3 +34,25 @@ def calculate_vote_fields(poll_model_collection, poll_model_name):
|
||||
poll.save(skip_autoupdate=True)
|
||||
|
||||
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
|
||||
|
@ -275,9 +275,11 @@ class BasePoll(models.Model):
|
||||
|
||||
def calculate_entitled_users(self):
|
||||
entitled_users = []
|
||||
entitled_users_ids = set()
|
||||
for group in self.groups.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(
|
||||
{
|
||||
"user_id": user.id,
|
||||
|
@ -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):
|
||||
def advancedSetUp(self):
|
||||
|
Loading…
Reference in New Issue
Block a user