0032_custom_invoice.py 4.19 KB
Newer Older
Levy--Falk Hugo's avatar
Levy--Falk Hugo committed
1 2 3 4 5 6
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2018-07-21 20:01
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion
7
from django.contrib.auth.management import create_permissions
Levy--Falk Hugo's avatar
Levy--Falk Hugo committed
8 9 10 11 12 13 14 15 16 17 18 19 20
import re2o.field_permissions
import re2o.mixins


def reattribute_ids(apps, schema_editor):
    Facture = apps.get_model('cotisations', 'Facture')
    BaseInvoice = apps.get_model('cotisations', 'BaseInvoice')

    for f in Facture.objects.all():
        base = BaseInvoice.objects.create(id=f.pk, date=f.date)
        f.baseinvoice_ptr = base
        f.save()

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

def update_rights(apps, schema_editor):
    Permission = apps.get_model('auth', 'Permission')

    # creates needed permissions
    app = apps.get_app_config('cotisations')
    app.models_module = True
    create_permissions(app)
    app.models_module = False

    former = Permission.objects.get(codename='change_facture_pdf')
    new_1 = Permission.objects.get(codename='add_custominvoice')
    new_2 = Permission.objects.get(codename='change_custominvoice')
    new_3 = Permission.objects.get(codename='view_custominvoice')
    new_4 = Permission.objects.get(codename='delete_custominvoice')
    for group in former.group_set.all():
        group.permissions.remove(former)
        group.permissions.add(new_1)
        group.permissions.add(new_2)
        group.permissions.add(new_3)
        group.permissions.add(new_4)
        group.save()


Levy--Falk Hugo's avatar
Levy--Falk Hugo committed
45 46 47
class Migration(migrations.Migration):

    dependencies = [
48
        ('cotisations', '0031_comnpaypayment_production'),
Levy--Falk Hugo's avatar
Levy--Falk Hugo committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    ]

    operations = [
        migrations.CreateModel(
            name='BaseInvoice',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('date', models.DateTimeField(auto_now_add=True, verbose_name='Date')),
            ],
            bases=(re2o.mixins.RevMixin, re2o.mixins.AclMixin, re2o.field_permissions.FieldPermissionModelMixin, models.Model),
        ),
        migrations.CreateModel(
            name='CustomInvoice',
            fields=[
                ('baseinvoice_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='cotisations.BaseInvoice')),
                ('recipient', models.CharField(max_length=255, verbose_name='Recipient')),
                ('payment', models.CharField(max_length=255, verbose_name='Payment type')),
                ('address', models.CharField(max_length=255, verbose_name='Address')),
                ('paid', models.BooleanField(verbose_name='Paid')),
            ],
            bases=('cotisations.baseinvoice',),
70
            options={'permissions': (('view_custominvoice', 'Can view a custom invoice'),)},
Levy--Falk Hugo's avatar
Levy--Falk Hugo committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        ),
        migrations.AddField(
            model_name='facture',
            name='baseinvoice_ptr',
            field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='cotisations.BaseInvoice', null=True),
            preserve_default=False,
        ),
        migrations.RunPython(reattribute_ids),
        migrations.AlterField(
            model_name='vente',
            name='facture',
            field=models.ForeignKey(on_delete=models.CASCADE, verbose_name='Invoice', to='cotisations.BaseInvoice')
        ),
        migrations.RemoveField(
            model_name='facture',
            name='id',
        ),
        migrations.RemoveField(
            model_name='facture',
            name='date',
        ),
        migrations.AlterField(
            model_name='facture',
            name='baseinvoice_ptr',
            field=models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='cotisations.BaseInvoice'),
96 97 98 99 100 101
        ),
        migrations.RunPython(update_rights),
        migrations.AlterModelOptions(
            name='facture',
            options={'permissions': (('change_facture_control', 'Can change the "controlled" state'), ('view_facture', "Can see an invoice's details"), ('change_all_facture', 'Can edit all the previous invoices')), 'verbose_name': 'Invoice', 'verbose_name_plural': 'Invoices'},
        ),
Levy--Falk Hugo's avatar
Levy--Falk Hugo committed
102
    ]