...
 
Commits (3)
......@@ -38,24 +38,30 @@ class ImportFocusForm(forms.Form):
else:
content = file.read()
data = list(csv.DictReader(StringIO(content.decode('utf-8-sig')), delimiter=';'))
reader = csv.DictReader(StringIO(content.decode('utf-8-sig')), delimiter=';')
except zipfile.BadZipFile:
raise Exception('File ZIP non valido.')
except Exception:
raise Exception('File CSV non valido.')
else:
return self.process(data)
headers_diff = {'COD_LOCALE_PROGETTO', 'OC_FOCUS'}.difference(set(reader.fieldnames))
if headers_diff:
raise Exception('Formato colonne non valido. Le colonne mancanti sono: {}.'.format(list(headers_diff)))
else:
return self.process(list(reader))
def process(self, data):
from django.db import connection
from django.db.models import Count
field = 'progetti'
old_progettiwithfocus_pks = list(Progetto.objects.annotate(cnt=Count('focus')).filter(cnt__gt=0).values_list('pk', flat=True))
focus_slug2obj = {o.slug: o for o in Focus.objects.all()}
progetto_clp2pk = {x['codice_locale']: x['pk'] for x in Progetto.objects.filter(codice_locale__in=(r['COD_LOCALE_PROGETTO'] for r in data)).values('pk', 'codice_locale')}
ThroughModel = Focus.progetti.through
ThroughModel = getattr(Focus, field).through
with connection.cursor() as cursor:
cursor.execute('TRUNCATE TABLE "{}" RESTART IDENTITY CASCADE;'.format(ThroughModel._meta.db_table))
......@@ -66,7 +72,7 @@ class ImportFocusForm(forms.Form):
if focus_slug not in focus_slug2obj:
focus_slug2obj[focus_slug] = Focus.objects.create(slug=focus_slug, nome=focus_slug.replace('_', ' ').capitalize())
obj = focus_slug2obj[focus_slug]
objs.append(ThroughModel(**{obj.progetti.source_field_name: obj, '{}_id'.format(obj.progetti.target_field_name): progetto_clp2pk[row['COD_LOCALE_PROGETTO']]}))
objs.append(ThroughModel(**{getattr(obj, field).source_field_name: obj, '{}_id'.format(getattr(obj, field).target_field_name): progetto_clp2pk[row['COD_LOCALE_PROGETTO']]}))
objs = ThroughModel.objects.bulk_create(objs)
......@@ -163,12 +169,12 @@ class FocusAdmin(TabbedTranslationAdmin):
if request.method == 'POST':
if form.is_valid():
try:
counts = form.save()
count = form.save()
except Exception as e:
from django.contrib import messages
self.message_user(request, 'Import non riuscito. {}'.format(e), level=messages.ERROR)
else:
self.message_user(request, 'Import completato con successo. Sono stati processati {} elementi'.format(counts))
self.message_user(request, 'Import completato con successo. Sono state create {} relazioni focus-progetto.'.format(count))
return redirect('..')
......
......@@ -31,7 +31,7 @@ class Config(object):
# [p for p in programmi0713 if p.descrizione_it.upper().startswith('PIANO STRALCIO')],
[p for p in programmi0713 if p.descrizione_it.upper().startswith('PIANO STRAORDINARIO')],
[p for p in programmi0713 if p.descrizione_it.upper().startswith('PROGRAMMA OBIETTIVI DI SERVIZIO')],
[p for p in programmi0713 if p.codice in ('2017NAPOLI-POZZ', '2007AN0021FA01', 'FSCMILITSPORT', 'FSCMUSEOTERME', '2015XMINTFSC016', '2018TOSPIOMFSC', 'FSCCOMUNI', '2018FREJUSFSC')],
[p for p in programmi0713 if p.codice in ('2017NAPOLI-POZZ', '2007AN0021FA01', '2018FREJUSFSC', '2018TOSPIOMFSC', '2017PPORTTARFSC', 'FSCMUSEOTERME', 'FSCMILITSPORT', '2015XMINTFSC016', 'FSCCOMUNI')],
],
'pac_0713': [
[p for p in programmi0713 if p.descrizione_it.upper().startswith('PROGRAMMA PAC') and (' MINISTERO ' in p.descrizione_it.upper() or ' PCM ' in p.descrizione_it.upper() or ' GOVERNANCE ' in p.descrizione_it.upper())],
......@@ -71,7 +71,7 @@ class Config(object):
[p for p in programmi1420 if p.codice in ('2016PATTIABR', '2016PATTIBASIL', '2016PATTICAL', '2016PATTICAMP', '2018POFSCEMROM', '2016PATTILAZ', '2016PATTILOMB', '2016PATTIMOL', '2016PATTIPUG', '2016PATTISARD', '2016PATTISICI')],
],
'fsc_1420_3': [
[p for p in programmi1420 if p.codice in ('2018FSCGIOVIMP', '2019AGCOEFSC001', '2014XXSMDFSC004', 'COMMTARANTOFSC', 'CISFOGGIAFSC', '2015MSIAIFSC011', 'FONDOGARANFSC', '2018FSCVOUCHER', '2017TOPIOMBIFSC', '2018REGTOSCFSC', '2018PIEMPOLTEC', '2015MBACTFSC017', '2016XXPCMFSC002', '2018CAPCULTFSC', '2018MATERAFSC', '2019MISEDLCRE01')],
[p for p in programmi1420 if p.codice in ('2018FSCGIOVIMP', '2019AGCOEFSC001', '2014XXSMDFSC004', 'COMMTARANTOFSC', 'CISFOGGIAFSC', '2015MSIAIFSC011', 'FONDOGARANFSC', '2018FSCVOUCHER', '2017TOPIOMBIFSC', '2018REGPIEMFSC', '2018REGTOSCFSC', '2018PIEMPOLTEC', '2015MBACTFSC017', '2018CAPCULTFSC', '2018MATERAFSC', '2016XXPCMFSC002', '2019MISEDLCRE01')],
],
'pac_poc_1420': [
[p for p in programmi1420 if p.descrizione_it.upper().startswith('POC ')],
......
......@@ -3,11 +3,9 @@
{% load admin_urls %}
{% block object-tools-items %}
{% if user.is_superuser %}
<li>
<a href="{% url opts|admin_urlname:'import' %}">Importa {{ opts.verbose_name_plural }}</a>
</li>
{% endif %}
<li>
<a href="{% url opts|admin_urlname:'import' %}">Importa {{ opts.verbose_name_plural }}</a>
</li>
{{ block.super }}
{% endblock %}