mirror of https://github.com/interlegis/sapl.git
12 changed files with 322 additions and 124 deletions
@ -1,8 +1,137 @@ |
|||||
from django import apps |
from builtins import LookupError |
||||
|
|
||||
|
from django.apps import apps |
||||
|
from django.contrib.auth.management import _get_all_permissions |
||||
|
from django.core import exceptions |
||||
|
from django.db import router |
||||
|
from django.db.models.signals import pre_migrate, post_migrate |
||||
|
from django.db.utils import DEFAULT_DB_ALIAS |
||||
|
from django.utils.translation import string_concat |
||||
from django.utils.translation import ugettext_lazy as _ |
from django.utils.translation import ugettext_lazy as _ |
||||
|
import django |
||||
|
|
||||
|
|
||||
|
def create_proxy_permissions( |
||||
|
app_config, verbosity=2, interactive=True, |
||||
|
using=DEFAULT_DB_ALIAS, **kwargs): |
||||
|
if not app_config.models_module: |
||||
|
return |
||||
|
|
||||
|
# print(app_config) |
||||
|
|
||||
|
try: |
||||
|
Permission = apps.get_model('auth', 'Permission') |
||||
|
except LookupError: |
||||
|
return |
||||
|
|
||||
|
if not router.allow_migrate_model(using, Permission): |
||||
|
return |
||||
|
|
||||
|
from django.contrib.contenttypes.models import ContentType |
||||
|
|
||||
|
permission_name_max_length = Permission._meta.get_field('name').max_length |
||||
|
|
||||
|
# This will hold the permissions we're looking for as |
||||
|
# (content_type, (codename, name)) |
||||
|
searched_perms = list() |
||||
|
# The codenames and ctypes that should exist. |
||||
|
ctypes = set() |
||||
|
for klass in list(app_config.get_models()): |
||||
|
opts = klass._meta |
||||
|
permissions = ( |
||||
|
("list_" + opts.model_name, |
||||
|
string_concat( |
||||
|
_('Visualizaçao da lista de'), ' ', |
||||
|
opts.verbose_name_plural)), |
||||
|
("detail_" + opts.model_name, |
||||
|
string_concat( |
||||
|
_('Visualização dos detalhes de'), ' ', |
||||
|
opts.verbose_name_plural)), |
||||
|
) |
||||
|
opts.permissions = tuple( |
||||
|
set(list(permissions) + list(opts.permissions))) |
||||
|
|
||||
|
if opts.proxy: |
||||
|
# Force looking up the content types in the current database |
||||
|
# before creating foreign keys to them. |
||||
|
app_label, model = opts.app_label, opts.model_name |
||||
|
|
||||
|
try: |
||||
|
ctype = ContentType.objects.db_manager( |
||||
|
using).get_by_natural_key(app_label, model) |
||||
|
except: |
||||
|
ctype = ContentType.objects.db_manager( |
||||
|
using).create(app_label=app_label, model=model) |
||||
|
else: |
||||
|
ctype = ContentType.objects.db_manager(using).get_for_model(klass) |
||||
|
|
||||
|
ctypes.add(ctype) |
||||
|
for perm in _get_all_permissions(klass._meta, ctype): |
||||
|
searched_perms.append((ctype, perm)) |
||||
|
|
||||
class AppConfig(apps.AppConfig): |
# Find all the Permissions that have a content_type for a model we're |
||||
|
# looking for. We don't need to check for codenames since we already have |
||||
|
# a list of the ones we're going to create. |
||||
|
all_perms = set(Permission.objects.using(using).filter( |
||||
|
content_type__in=ctypes, |
||||
|
).values_list( |
||||
|
"content_type", "codename" |
||||
|
)) |
||||
|
|
||||
|
perms = [ |
||||
|
Permission(codename=codename, name=name, content_type=ct) |
||||
|
for ct, (codename, name) in searched_perms |
||||
|
if (ct.pk, codename) not in all_perms |
||||
|
] |
||||
|
# Validate the permissions before bulk_creation to avoid cryptic database |
||||
|
# error when the name is longer than 255 characters |
||||
|
for perm in perms: |
||||
|
if len(perm.name) > permission_name_max_length: |
||||
|
raise exceptions.ValidationError( |
||||
|
'The permission name %s of %s.%s ' |
||||
|
'is longer than %s characters' % ( |
||||
|
perm.name, |
||||
|
perm.content_type.app_label, |
||||
|
perm.content_type.model, |
||||
|
permission_name_max_length, |
||||
|
) |
||||
|
) |
||||
|
Permission.objects.using(using).bulk_create(perms) |
||||
|
if verbosity >= 2: |
||||
|
for perm in perms: |
||||
|
print("Adding permission '%s'" % perm) |
||||
|
|
||||
|
|
||||
|
def run_sql_organizers( |
||||
|
app_config, verbosity=2, interactive=True, |
||||
|
using=DEFAULT_DB_ALIAS, **kwargs): |
||||
|
"""with connection.cursor() as cursor: |
||||
|
for line in lines: |
||||
|
line = line.strip() |
||||
|
if not line or line.startswith('#'): |
||||
|
continue |
||||
|
|
||||
|
try: |
||||
|
cursor.execute(line)""" |
||||
|
|
||||
|
print('aqui run_sql_organizer', app_config) |
||||
|
|
||||
|
""" update protocoloadm_protocolo set autor_id = null; |
||||
|
delete from materia_autoria; |
||||
|
delete from materia_proposicao; |
||||
|
|
||||
|
delete from materia_tipoproposicao; |
||||
|
""" |
||||
|
|
||||
|
|
||||
|
class AppConfig(django.apps.AppConfig): |
||||
name = 'sapl.base' |
name = 'sapl.base' |
||||
label = 'base' |
label = 'base' |
||||
verbose_name = _('Dados Básicos') |
verbose_name = _('Dados Básicos') |
||||
|
|
||||
|
def ready(self): |
||||
|
pre_migrate.connect(run_sql_organizers, self) |
||||
|
|
||||
|
post_migrate.connect( |
||||
|
receiver=create_proxy_permissions, |
||||
|
dispatch_uid="django.contrib.auth.management.create_permissions") |
||||
|
@ -0,0 +1,20 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# Generated by Django 1.9.7 on 2016-10-11 19:45 |
||||
|
from __future__ import unicode_literals |
||||
|
|
||||
|
from django.db import migrations |
||||
|
|
||||
|
|
||||
|
def clear_model_tipo_proposicao(apps, schema_editor): |
||||
|
TipoProposicao = apps.get_model("materia", "TipoProposicao") |
||||
|
TipoProposicao.objects.all().delete() |
||||
|
|
||||
|
|
||||
|
class Migration(migrations.Migration): |
||||
|
|
||||
|
dependencies = [ |
||||
|
('materia', '0056_merge'), |
||||
|
] |
||||
|
|
||||
|
operations = [ |
||||
|
migrations.RunPython(clear_model_tipo_proposicao), ] |
@ -0,0 +1,21 @@ |
|||||
|
# -*- coding: utf-8 -*- |
||||
|
# Generated by Django 1.9.7 on 2016-10-23 14:44 |
||||
|
from __future__ import unicode_literals |
||||
|
|
||||
|
from django.db import migrations, models |
||||
|
import django.db.models.deletion |
||||
|
|
||||
|
|
||||
|
class Migration(migrations.Migration): |
||||
|
|
||||
|
dependencies = [ |
||||
|
('protocoloadm', '0003_auto_20161009_1222'), |
||||
|
] |
||||
|
|
||||
|
operations = [ |
||||
|
migrations.AlterField( |
||||
|
model_name='tramitacaoadministrativo', |
||||
|
name='unidade_tramitacao_destino', |
||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='adm_tramitacoes_destino', to='materia.UnidadeTramitacao', verbose_name='Unidade Destino'), |
||||
|
), |
||||
|
] |
Loading…
Reference in new issue