Skip to content

Commit

Permalink
autocreate template on first dataset import
Browse files Browse the repository at this point in the history
  • Loading branch information
enricofer committed Sep 1, 2024
1 parent 305303b commit 6d71051
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
39 changes: 1 addition & 38 deletions webapp/djakart/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.core.validators import FileExtensionValidator
from django.core.files import File
from io import StringIO
from django.core.files.base import ContentFile
from django.conf import settings

import json
Expand All @@ -18,7 +17,6 @@
import shutil
import uuid
from datetime import datetime
import xml.etree.ElementTree as ET

from .models import version, can_modify, modelli, basemap
from .kart_api import (
Expand Down Expand Up @@ -484,42 +482,7 @@ def importa_geopackage(self, request, obj, **kwargs):
@require_file("importa_template","qgs")
def importa_template(self, request, obj, **kwargs):
if obj.pk:
with open(kwargs["importa_template"],"r") as template_file:
template_source = template_file.read()
template_root = ET.fromstring(template_source)
prop_element = template_root.find("properties")

if prop_element.find("Macros"):
prop_element.remove(prop_element.find("Macros"))

macro_element = ET.SubElement(prop_element, "Macros")
python_element = ET.SubElement(macro_element, "pythonCode")
python_element.set("type", "QString")
python_element.text = "{{ pythonmacro }}"
template_source = ET.tostring(template_root,encoding='unicode')

print (ET.tostring(prop_element,encoding='unicode'))

custom_order_section = re.search('<custom-order enabled="0">((.|\n)*?)<\/custom-order>', template_source)
layer_items = re.finditer("<item>((.|\n)*?)<\/item>", custom_order_section.group())
layer_ids = [lyr.group(1) for lyr in layer_items]

template_source = re.sub("(?<=(table\=\&quot\;))(.+)(?=(\&quot;\.))", "{{ versione }}", template_source)
template_source = re.sub("(?<=(table\=\"))(.+)(?=(\"\.))", "{{ versione }}", template_source)

#template_filepath = os.path.join(settings.MEDIA_ROOT,"tmp",'%s.qgs' % uuid.uuid4().hex)
#with open(template_filepath ,"w") as tf:
# tf.write(template_source)

qgstmpl = modelli()
qgstmpl.doc = ContentFile(template_source,"qgis_template.qgs")#File(StringIO(template_source),"qgis_template")
qgstmpl.titolo = "VERSIONI_" + obj.nome
qgstmpl.descrizione = json.dumps(layer_ids, indent=2)
qgstmpl.abilitato = False
qgstmpl.save()
obj.template_qgis = qgstmpl
obj.save()

obj.import_template(kwargs["importa_template"])
return HttpResponseRedirect("/admin/djakart/version/%s/" % obj.pk)

@action(label="Reset not commited edits", description="Reset not commited edits")
Expand Down
46 changes: 45 additions & 1 deletion webapp/djakart/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from datetime import datetime, timedelta
from django.template import Context, Template
from django.db.models import JSONField
from django.core.files.base import ContentFile

from .kart_api import (
KartException,
Expand Down Expand Up @@ -44,6 +45,7 @@
import requests
from xml.sax.saxutils import escape
from urllib.parse import quote,unquote,parse_qs
import xml.etree.ElementTree as ET

BASE_MAPPING_SERVICE = os.environ.get("QGIS_SERVER_EXTERNAL","qgis_server_external")
SRID = os.environ.get("REPO_CRS")
Expand All @@ -59,6 +61,8 @@ def writeQgs(versione_obj):
progetto = getQgsProject(versione_obj)
with open (versione_qgs_path,"w") as proqgs:
proqgs.write(progetto)
versione_obj.progetto = get_qgs_filename(versione_obj.schema)
versione_obj.save
return progetto

def getQgsProject(versione_obj):
Expand Down Expand Up @@ -436,11 +440,51 @@ def aggiorna_progetto(self):
def kart_tables(self):
return list_versioned_tables(self.nome)

def importa(self,dspath):
def import_template(self,tmplpath):
with open(tmplpath,"r") as template_file:
template_source = template_file.read()
template_root = ET.fromstring(template_source)
prop_element = template_root.find("properties")

if prop_element.find("Macros"):
prop_element.remove(prop_element.find("Macros"))

macro_element = ET.SubElement(prop_element, "Macros")
python_element = ET.SubElement(macro_element, "pythonCode")
python_element.set("type", "QString")
python_element.text = "{{ pythonmacro }}"
template_source = ET.tostring(template_root,encoding='unicode')

print (ET.tostring(prop_element,encoding='unicode'))

custom_order_section = re.search('<custom-order enabled="0">((.|\n)*?)<\/custom-order>', template_source)
layer_items = re.finditer("<item>((.|\n)*?)<\/item>", custom_order_section.group())
layer_ids = [lyr.group(1) for lyr in layer_items]

template_source = re.sub("(?<=(table\=\&quot\;))(.+)(?=(\&quot;\.))", "{{ versione }}", template_source)
template_source = re.sub("(?<=(table\=\"))(.+)(?=(\"\.))", "{{ versione }}", template_source)

#template_filepath = os.path.join(settings.MEDIA_ROOT,"tmp",'%s.qgs' % uuid.uuid4().hex)
#with open(template_filepath ,"w") as tf:
# tf.write(template_source)

qgstmpl = modelli()
qgstmpl.doc = ContentFile(template_source,"qgis_template.qgs")#File(StringIO(template_source),"qgis_template")
qgstmpl.titolo = "VERSIONI_" + self.nome
qgstmpl.descrizione = json.dumps(layer_ids, indent=2)
qgstmpl.abilitato = False
qgstmpl.save()
self.template_qgis = qgstmpl
self.save()

def importa(self, dspath):
ext = importa_dataset(self.nome, dspath, self.extent)
self.extent = ext
self.save()
self.salva_cache()
#autotemplate on first ds import
if not self.template_qgis:
self.import_template("/"+str(self.progetto))
return ext

def save(self, *args, **kwargs):
Expand Down

0 comments on commit 6d71051

Please sign in to comment.