8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-27 07:02:26 +00:00

Migration des permissions

This commit is contained in:
Hugo LEVY-FALK 2018-07-26 10:11:53 +02:00
parent bad89ea7c2
commit 1284d9ffec
2 changed files with 33 additions and 15 deletions

View file

@ -4,6 +4,7 @@ from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
from django.contrib.auth.management import create_permissions
import re2o.field_permissions import re2o.field_permissions
import re2o.mixins import re2o.mixins
@ -17,6 +18,30 @@ def reattribute_ids(apps, schema_editor):
f.baseinvoice_ptr = base f.baseinvoice_ptr = base
f.save() f.save()
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()
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
@ -42,7 +67,7 @@ class Migration(migrations.Migration):
('paid', models.BooleanField(verbose_name='Paid')), ('paid', models.BooleanField(verbose_name='Paid')),
], ],
bases=('cotisations.baseinvoice',), bases=('cotisations.baseinvoice',),
options={'permissions': (('view_custom_invoice', 'Can view a custom invoice'),)}, options={'permissions': (('view_custominvoice', 'Can view a custom invoice'),)},
), ),
migrations.AddField( migrations.AddField(
model_name='facture', model_name='facture',
@ -68,5 +93,10 @@ class Migration(migrations.Migration):
model_name='facture', model_name='facture',
name='baseinvoice_ptr', 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'), field=models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='cotisations.BaseInvoice'),
) ),
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'},
),
] ]

View file

@ -153,10 +153,6 @@ class Facture(BaseInvoice):
# TODO : change facture to invoice # TODO : change facture to invoice
('change_facture_control', ('change_facture_control',
_l("Can change the \"controlled\" state")), _l("Can change the \"controlled\" state")),
# TODO : seems more likely to be call create_facture_pdf
# or create_invoice_pdf
('change_facture_pdf',
_l("Can create a custom PDF invoice")),
('view_facture', ('view_facture',
_l("Can see an invoice's details")), _l("Can see an invoice's details")),
('change_all_facture', ('change_all_facture',
@ -215,14 +211,6 @@ class Facture(BaseInvoice):
_("You don't have the right to edit the \"controlled\" state.") _("You don't have the right to edit the \"controlled\" state.")
) )
@staticmethod
def can_change_pdf(user_request, *_args, **_kwargs):
""" Returns True if the user can change this invoice """
return (
user_request.has_perm('cotisations.change_facture_pdf'),
_("You don't have the right to edit an invoice.")
)
@staticmethod @staticmethod
def can_create(user_request, *_args, **_kwargs): def can_create(user_request, *_args, **_kwargs):
"""Check if a user can create an invoice. """Check if a user can create an invoice.
@ -271,7 +259,7 @@ def facture_post_delete(**kwargs):
class CustomInvoice(BaseInvoice): class CustomInvoice(BaseInvoice):
class Meta: class Meta:
permissions = ( permissions = (
('view_custom_invoice', _l("Can view a custom invoice")), ('view_custominvoice', _l("Can view a custom invoice")),
) )
recipient = models.CharField( recipient = models.CharField(
max_length=255, max_length=255,