Skip to content
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

apps/votes: change migration to delete and recreate tables #4938

Merged
merged 1 commit into from
Feb 9, 2023
Merged
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
150 changes: 94 additions & 56 deletions meinberlin/apps/votes/migrations/0003_voting_token_add_token_hash.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,68 @@
# Generated by Django 3.2.16 on 2023-01-24 11:01
import hashlib
# Generated by Django 3.2.17 on 2023-02-09 10:12

from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone
import meinberlin.apps.votes.models


def set_token_hash(apps, schema_editor):
voting_token = apps.get_model("meinberlin_votes", "VotingToken")
tokens = voting_token.objects.all()
for token in tokens:
token.token_hash = get_hash_for_token(apps, token.token, token.module)
token.save()


def get_hash_for_token(apps, token, module):
salt = get_or_create_salt_for_module(apps, module)
token_hash = hashlib.sha3_512((salt + token).encode()).hexdigest()
return token_hash


def get_or_create_salt_for_module(apps, module):
TokenSalt = apps.get_model("meinberlin_votes", "TokenSalt")
try:
token_salt = TokenSalt.objects.get(module=module)
except TokenSalt.DoesNotExist:
token_salt = TokenSalt.objects.create(module=module)
return token_salt.salt


class Migration(migrations.Migration):
dependencies = [
("a4modules", "0006_module_blueprint_type"),
("contenttypes", "0002_remove_content_type_name"),
("meinberlin_votes", "0002_votingtoken_change_primary_key_to_id"),
]

operations = [
migrations.DeleteModel(
name="VotingToken",
),
migrations.DeleteModel(
name="TokenVote",
),
migrations.CreateModel(
name="VotingToken",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"token",
models.CharField(
blank=True,
default=meinberlin.apps.votes.models.get_token_12,
editable=False,
max_length=40,
),
),
(
"token_hash",
models.CharField(editable=False, max_length=128, unique=True),
),
("allowed_votes", models.PositiveSmallIntegerField(default=5)),
("package_number", models.PositiveIntegerField()),
(
"is_active",
models.BooleanField(
default=True,
help_text="Designates whether this token should be treated as active. Unselect this instead of deleting tokens.",
),
),
(
"module",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="a4modules.module",
),
),
],
),
migrations.CreateModel(
name="TokenSalt",
fields=[
Expand Down Expand Up @@ -66,36 +93,47 @@ class Migration(migrations.Migration):
),
],
),
migrations.AlterUniqueTogether(
name="votingtoken",
unique_together=set(),
),
migrations.AlterField(
model_name="votingtoken",
name="token",
field=models.CharField(
blank=True,
default=meinberlin.apps.votes.models.get_token_12,
editable=False,
max_length=40,
),
),
migrations.AddField(
model_name="votingtoken",
name="token_hash",
field=models.CharField(default="", editable=False, max_length=128),
preserve_default=False,
),
migrations.RunPython(set_token_hash),
migrations.AlterField(
model_name="votingtoken",
name="token_hash",
field=models.CharField(editable=False, max_length=128, unique=True),
),
migrations.AddField(
model_name="votingtoken",
name="package_number",
field=models.PositiveIntegerField(default=0),
preserve_default=False,
migrations.CreateModel(
name="TokenVote",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"created",
models.DateTimeField(
default=django.utils.timezone.now, editable=False
),
),
(
"modified",
models.DateTimeField(blank=True, editable=False, null=True),
),
("object_pk", models.PositiveIntegerField()),
(
"content_type",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="contenttypes.contenttype",
),
),
(
"token",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="meinberlin_votes.votingtoken",
),
),
],
options={
"unique_together": {("content_type", "object_pk", "token")},
"index_together": {("content_type", "object_pk")},
},
),
]