OpenSlides/server/openslides/motions/migrations/0006_submitter_model.py

78 lines
2.6 KiB
Python
Raw Normal View History

2018-06-12 14:17:02 +02:00
# -*- coding: utf-8 -*-
# Generated by Django 1.10.8 on 2018-02-09 07:18
from __future__ import unicode_literals
import django.db.models.deletion
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.db import migrations, models
import openslides.utils.models
def move_submitters_to_own_model(apps, schema_editor):
2019-01-06 16:22:33 +01:00
Motion = apps.get_model("motions", "Motion")
Submitter = apps.get_model("motions", "Submitter")
2018-06-12 14:17:02 +02:00
for motion in Motion.objects.all():
weight = 0
for user in motion.submittersOld.all():
# We cannot use the add method here, so do it manually...
if Submitter.objects.filter(user=user, motion=motion).exists():
continue # The user is already a submitter. Skip this duplicate.
if isinstance(user, AnonymousUser):
continue # Skip the anonymous
submitter = Submitter(user=user, motion=motion, weight=weight)
submitter.save(force_insert=True, skip_autoupdate=True)
2018-06-12 14:17:02 +02:00
weight += 1
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
2019-01-06 16:22:33 +01:00
("motions", "0005_auto_20180202_1318"),
2018-06-12 14:17:02 +02:00
]
operations = [
migrations.RenameField(
2019-01-06 16:22:33 +01:00
model_name="motion", old_name="submitters", new_name="submittersOld"
2018-06-12 14:17:02 +02:00
),
migrations.CreateModel(
2019-01-06 16:22:33 +01:00
name="Submitter",
2018-06-12 14:17:02 +02:00
fields=[
2019-01-06 16:22:33 +01:00
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("weight", models.IntegerField(null=True)),
2018-06-12 14:17:02 +02:00
],
2019-01-06 16:22:33 +01:00
options={"default_permissions": ()},
2018-06-12 14:17:02 +02:00
bases=(openslides.utils.models.RESTModelMixin, models.Model),
),
migrations.AddField(
2019-01-06 16:22:33 +01:00
model_name="submitter",
name="motion",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="submitters",
to="motions.Motion",
),
2018-06-12 14:17:02 +02:00
),
migrations.AddField(
2019-01-06 16:22:33 +01:00
model_name="submitter",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
2018-06-12 14:17:02 +02:00
),
2019-01-06 16:22:33 +01:00
migrations.RunPython(move_submitters_to_own_model),
migrations.RemoveField(model_name="motion", name="submittersOld"),
2018-06-12 14:17:02 +02:00
]