Add migrations to remove duplicates from existing polls

This commit is contained in:
Joshua Sangmeister 2021-04-08 11:33:26 +02:00
parent f54050a83c
commit 1e6b042d71
3 changed files with 56 additions and 0 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