Skip to content

Commit

Permalink
Merge pull request #52 from geosolutions-it/dev
Browse files Browse the repository at this point in the history
 - Fixes: error management Mixin Class / Adds: Contatto to Piano
  • Loading branch information
Alessio Fabiani authored Feb 4, 2019
2 parents e2e22e0 + f3c2b6c commit f9ea172
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 45 deletions.
2 changes: 1 addition & 1 deletion requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ psycopg2-binary==2.7.6.1
graphene==2.1.3
graphene-django==2.2.0
graphene-django==2.2.0
graphene-django-extras==0.3.7
graphene-django-extras==0.4.3
graphene-file-upload==1.2.2
graphql-core==2.1
graphql-relay==0.4.5
Expand Down
42 changes: 26 additions & 16 deletions strt/base/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,6 @@
'serapide_core.api',
]

MIDDLEWARE = [
# Django middleware
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
# Wagtail middleware
'wagtail.core.middleware.SiteMiddleware',
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
# Django current user
'django_currentuser.middleware.ThreadLocalUserMiddleware',
]

ROOT_URLCONF = 'base.urls'

TEMPLATES = [
Expand Down Expand Up @@ -210,9 +194,35 @@
INTERNAL_IPS = EnvUtil.get_env_var('DJANGO_INTERNAL_IPS', list, [], ' ')

GRAPHENE = {
'SCHEMA_INDENT': 2,
'SCHEMA': 'serapide_core.schema.schema',
'SCHEMA_OUTPUT': 'data/schema.json', # defaults to schema.json
'MIDDLEWARE': [
'graphene_django.debug.DjangoDebugMiddleware',
'graphene_django_extras.ExtraGraphQLDirectiveMiddleware',
]
}

GRAPHENE_DJANGO_EXTRAS = {
'DEFAULT_PAGINATION_CLASS': 'graphene_django_extras.paginations.LimitOffsetGraphqlPagination',
'DEFAULT_PAGE_SIZE': 20,
'MAX_PAGE_SIZE': 50,
'CACHE_ACTIVE': True,
'CACHE_TIMEOUT': 300 # seconds
}

MIDDLEWARE = [
# Django middleware
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
# Wagtail middleware
'wagtail.core.middleware.SiteMiddleware',
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
# Django current user
'django_currentuser.middleware.ThreadLocalUserMiddleware',
]
47 changes: 32 additions & 15 deletions strt/serapide_core/api/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@
from graphene_django.filter import DjangoFilterConnectionField
from graphene_file_upload.scalars import Upload

# from graphene_django_extras import DjangoObjectType

from graphql_extensions.exceptions import GraphQLError

from strt_users.models import (
AppUser, Organization, OrganizationType
)

from serapide_core.helpers import (
get_errors, update_create_instance, is_RUP
get_errors, update_create_instance, is_RUP,
)
from serapide_core.modello.models import (
Piano, Fase, Risorsa, FasePianoStorico, RisorsePiano,
ProceduraVAS, RisorseVas
ProceduraVAS, RisorseVas,
)
from serapide_core.modello.enums import (
FASE, TIPOLOGIA_PIANO, TIPOLOGIA_VAS
FASE, TIPOLOGIA_PIANO, TIPOLOGIA_VAS,
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -203,7 +205,11 @@ def resolve_storico_fasi(self, info, **args):
return list(_hist)

def resolve_procedura_vas(self, info, **args):
_vas = ProceduraVAS.objects.get(piano=self)
_vas = None
try:
_vas = ProceduraVAS.objects.get(piano=self)
except BaseException:
pass
return _vas

class Meta:
Expand Down Expand Up @@ -252,14 +258,27 @@ class PianoCreateInput(InputObjectType):
fase = graphene.InputField(FaseCreateInput, required=False)


class PianoUpdateInput(InputObjectType):
"""
Class created to accept input data
from the interactive graphql console.
"""
url = graphene.String(required=False)
data_delibera = graphene.types.datetime.DateTime(required=False)
data_accettazione = graphene.types.datetime.DateTime(required=False)
data_avvio = graphene.types.datetime.DateTime(required=False)
data_approvazione = graphene.types.datetime.DateTime(required=False)
descrizione = graphene.InputField(graphene.List(graphene.String), required=False)


class ProceduraVASCreateInput(InputObjectType):
"""
Class created to accept input data
from the interactive graphql console.
"""
piano = graphene.InputField(PianoCreateInput, required=True)
tipologia = graphene.String(required=True)

piano = graphene.InputField(PianoUpdateInput, required=False)
note = graphene.InputField(graphene.List(graphene.String), required=False)
data_creazione = graphene.types.datetime.DateTime(required=False)
data_verifica = graphene.types.datetime.DateTime(required=False)
Expand Down Expand Up @@ -476,7 +495,7 @@ def mutate_and_get_payload(cls, root, info, **input):
class UpdatePiano(relay.ClientIDMutation):

class Input:
piano_operativo = graphene.Argument(PianoCreateInput)
piano_operativo = graphene.Argument(PianoUpdateInput)
codice = graphene.String(required=True)

errors = graphene.List(graphene.String)
Expand Down Expand Up @@ -525,6 +544,7 @@ class CreateProceduraVAS(relay.ClientIDMutation):

class Input:
procedura_vas = graphene.Argument(ProceduraVASCreateInput)
codice_piano = graphene.String(required=True)

nuova_procedura_vas = graphene.Field(ProceduraVASNode)

Expand All @@ -534,22 +554,19 @@ def mutate_and_get_payload(cls, root, info, **input):
try:
_procedura_vas_data = input.get('procedura_vas')

# Piano (M)
_data = _procedura_vas_data.pop('piano')
_piano = Piano.objects.get(codice=_data['codice'])
# ProceduraVAS (M)
_piano = Piano.objects.get(codice=input['codice_piano'])
_procedura_vas_data['piano'] = _piano
# Ente (M)
if 'ente' in _procedura_vas_data:
_data = _procedura_vas_data.pop('ente')
_ente = Organization.objects.get(code=_data['code'])
_procedura_vas_data['ente'] = _ente
else:
_procedura_vas_data['ente'] = _piano.ente
_procedura_vas_data['ente'] = _piano.ente
# Note (O)
if 'note' in _procedura_vas_data:
_data = _procedura_vas_data.pop('note')
_procedura_vas_data['note'] = _data[0]
_procedura_vas = ProceduraVAS()
_procedura_vas.piano = _piano
_procedura_vas_data['id'] = _procedura_vas.id
_procedura_vas_data['uuid'] = _procedura_vas.uuid
nuova_procedura_vas = update_create_instance(_procedura_vas, _procedura_vas_data)
return cls(nuova_procedura_vas=nuova_procedura_vas)
except BaseException as e:
Expand Down
15 changes: 10 additions & 5 deletions strt/serapide_core/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@ def dispatch(self, request, *args, **kwargs):
)
else:
result, status_code = self.get_response(request, data, show_graphiql)
_res = json.loads(result)
if 'errors' in _res and _res['errors'] and len(_res['errors']) > 0:
_res = json.loads(result) if result else None
if _res and 'errors' in _res and _res['errors'] and len(_res['errors']) > 0:
_error = _res['errors'][0]
e = HttpError(HttpResponse(status=_error['code'], content_type='application/json'), _error['message'])
_code = 500 if _error['code'] == 'error' else int(_error['code'])
e = HttpError(HttpResponse(status=_code, content_type='application/json'), _error['message'])
response = e.response
# response.content = self.json_encode(request, {'errors': [self.format_error(e)]})
response.content = self.json_encode(request, _res)
return response

return super().dispatch(request, *args, **kwargs)
if result and status_code:
return HttpResponse(
status=status_code, content=result, content_type="application/json"
)
else:
return super().dispatch(request, *args, **kwargs)


class PrivateGraphQLView(HTTPErrorAwareMixin,
Expand Down
2 changes: 2 additions & 0 deletions strt/serapide_core/modello/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .models import (Fase,
Risorsa,
Contatto,
Piano, RisorsePiano,
ProceduraVAS, RisorseVas)

Expand All @@ -34,5 +35,6 @@ class ProceduraVASAdmin(admin.ModelAdmin):

admin.site.register(Fase)
admin.site.register(Risorsa)
admin.site.register(Contatto)
admin.site.register(Piano, PianoAdmin)
admin.site.register(ProceduraVAS, ProceduraVASAdmin)
7 changes: 7 additions & 0 deletions strt/serapide_core/modello/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
('pubblicazione', _('PUBBLICAZIONE')),
)

TIPOLOGIA_CONTATTO = Choices(
('unknown', _('UNKNOWN')),
('generico', _('GENERICO')),
('acvas', _('AUT_COMP_VAS')),
('sca', _('SOGGETTO_SCA')),
)

TIPOLOGIA_PIANO = Choices(
('unknown', _('UNKNOWN')),
('operativo', _('OPERATIVO')),
Expand Down
76 changes: 76 additions & 0 deletions strt/serapide_core/modello/migrations/0011_auto_20190204_1332.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Generated by Django 2.0.8 on 2019-02-04 13:32

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

dependencies = [
('strt_users', '0002_auto_20181221_1112'),
('modello', '0010_auto_20190130_1551'),
]

operations = [
migrations.CreateModel(
name='AutoritaCompetenteVAS',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
options={
'db_table': 'strt_core_autorita_competente',
},
),
migrations.CreateModel(
name='Contatto',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('uuid', models.UUIDField(default=uuid.uuid4, editable=False, null=True)),
('nome', models.CharField(max_length=255)),
('tipologia', models.CharField(choices=[('unknown', 'UNKNOWN'), ('generico', 'GENERICO'), ('acvas', 'AUT_COMP_VAS'), ('sca', 'SOGGETTO_SCA')], default='unknown', max_length=20)),
('email', models.EmailField(max_length=254)),
('ente', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='strt_users.Organization', verbose_name='ente')),
],
options={
'verbose_name_plural': 'Contatti',
'db_table': 'strt_core_contatto',
},
),
migrations.CreateModel(
name='SoggettiSCA',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('piano', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='modello.Piano')),
('soggetto_sca', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='modello.Contatto')),
],
options={
'db_table': 'strt_core_soggetti_sca',
},
),
migrations.AddField(
model_name='autoritacompetentevas',
name='autorita_competente',
field=models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='modello.Contatto'),
),
migrations.AddField(
model_name='autoritacompetentevas',
name='piano',
field=models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='modello.Piano'),
),
migrations.AddField(
model_name='piano',
name='autorita_competente_vas',
field=models.ManyToManyField(related_name='autorita_competente_vas', through='modello.AutoritaCompetenteVAS', to='modello.Contatto'),
),
migrations.AddField(
model_name='piano',
name='soggetti_sca',
field=models.ManyToManyField(related_name='soggetti_sca', through='modello.SoggettiSCA', to='modello.Contatto'),
),
migrations.AddField(
model_name='piano',
name='soggetto_proponente',
field=models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='modello.Contatto', verbose_name='soggetto proponente'),
),
]
Loading

0 comments on commit f9ea172

Please sign in to comment.