Skip to content

Commit

Permalink
site subpath support
Browse files Browse the repository at this point in the history
  • Loading branch information
ferregutie committed Sep 3, 2024
1 parent f5ba88b commit 7872ef2
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 48 deletions.
37 changes: 19 additions & 18 deletions webapp/djakart/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#SRID = os.environ.get("REPO_CRS")
#SRID_CODE = SRID.split(":")[1]

SITE_SUBPATH = os.environ.get("SITE_SUBPATH","")

class importForm(forms.Form):
nuovo_dataset = forms.FileField()
#nuovo_dataset = forms.FileField()
Expand Down Expand Up @@ -90,6 +92,7 @@ def change_view(self, request, object_id, **kwargs):
if not "extra_context" in kwargs:
kwargs["extra_context"] = {}
kwargs["extra_context"]['crs'] = obj.crs.split(":")[1]
kwargs["extra_context"]['site_subpath'] = SITE_SUBPATH
return super(versioniAdmin, self).change_view(request, object_id, **kwargs) #, extra_context=extra_context

def require_file(parameter,estensione):
Expand Down Expand Up @@ -331,10 +334,10 @@ def log(self,obj):
log_items = obj.log_json
li_html = ""
for item in log_items:
li = '<li><a href="#" onclick="generateDiff(\'{versione}\',\'{commit}\',\'{parent}\');event.preventDefault();" target="_blank">{abbrevCommit}</a></br>{message}</br>authored by {authorName} {commitTime}</br></li>' #href="/versioni/diff/{versione}/{commit}/{parent}/"
li = '<li><a href="#" onclick="generateDiff(\'{subpath}\',\'{versione}\',\'{commit}\',\'{parent}\');event.preventDefault();" target="_blank">{abbrevCommit}</a></br>{message}</br>authored by {authorName} {commitTime}</br></li>' #href="/versioni/diff/{versione}/{commit}/{parent}/"
item["message"] = item["message"].replace("\n", "</br>")
item["parent"] = item["parents"][0] if item["parents"] else ""
li_html += li.format(versione=obj.nome, **item)
li_html += li.format(subpath=SITE_SUBPATH, versione=obj.nome, **item)
html = "<ul>%s</ul>" % li_html
return format_html(html)
else:
Expand All @@ -357,9 +360,9 @@ def status(self,obj):
elif obj.cambiamenti_non_registrati and not obj.is_faulty:
html = '''
<strong>The current version has not committed edits:
<a href="/djakart/diff/{versione}/HEAD/" target="_blank"> Verify</a>
<a href="{site_subpath}/djakart/diff/{versione}/HEAD/" target="_blank"> Verify</a>
</strong>
'''.format(versione=obj.nome)
'''.format(site_subpath=SITE_SUBPATH, versione=obj.nome)
else:
html = obj.status_()
return format_html(html)
Expand All @@ -378,7 +381,7 @@ def mapa(self, obj):

def get_project(self, obj):
if obj.nome:
html= '''<a href="/djakart/qgs/%s/" target="_blank"> Download</a>''' % obj.nome
html= '''<a href="%s/djakart/qgs/%s/" target="_blank"> Download</a>''' % (SITE_SUBPATH, obj.nome)
return format_html(html)
else:
return ''
Expand Down Expand Up @@ -408,7 +411,7 @@ def merge(self, request, obj):

obj.base.config_user(request.user.username, request.user.email)
obj.merge()
return HttpResponseRedirect("/admin/djakart/version/%s/" % obj.base.pk)
return HttpResponseRedirect("%s/admin/djakart/version/%s/" % (SITE_SUBPATH, obj.base.pk))

@action(label="Reconcile conflicts", description="Reconcile conflicting edits")
@resolve_conflicts
Expand All @@ -418,36 +421,36 @@ def risolvi_conflitti(self, request, obj):
cmd = obj.merge(conferma=True)
except Exception as E:
print("KART EXCEPTION",E)
return HttpResponseRedirect("/admin/djakart/version/%s/" % obj.pk)
return HttpResponseRedirect("%s/admin/djakart/version/%s/" % (SITE_SUBPATH, obj.pk))
risolvi_conflitti.short_description = 'reconcile conflicts'

@action(label="Undo merge", description="Refuse conflicted merge")
@require_confirmation
def annulla_merge(self, request, obj):
if obj.pk:
obj.merge(annulla=True)
return HttpResponseRedirect("/admin/djakart/version/%s/" % obj.pk)
return HttpResponseRedirect("%s/admin/djakart/version/%s/" % (SITE_SUBPATH, obj.pk))

@action(label="Apply merge", description="Apply reconciled merge")
@require_confirmation
def conferma_merge(self, request, obj):
if obj.pk:
obj.merge(conferma=True)
return HttpResponseRedirect("/admin/djakart/version/%s/" % obj.pk)
return HttpResponseRedirect("%s/admin/djakart/version/%s/" % (SITE_SUBPATH, obj.pk))

@action(label="Update", description="Update status cache")
@require_confirmation
def aggiorna(self, request, obj):
if obj.pk:
obj.aggiorna()
return HttpResponseRedirect("/admin/djakart/version/%s/" % obj.pk)
return HttpResponseRedirect("%s/admin/djakart/version/%s/" % (SITE_SUBPATH, obj.pk))

@action(label="Undo last commit", description="Go back one step in edits log")
@require_confirmation
def undo(self, request, obj):
if obj.pk:
obj.undo()
return HttpResponseRedirect("/admin/djakart/version/%s/" % obj.pk)
return HttpResponseRedirect("%s/admin/djakart/version/%s/" % (SITE_SUBPATH, obj.pk))

@action(label="Commit", description="Record edits as a commit")
@require_parameter("Commit message")
Expand All @@ -456,7 +459,7 @@ def commit(self, request, obj, **kwargs):
#print (kwargs["messaggio di registrazione"])
obj.config_user(request.user.username,request.user.email)
obj.commit("{}: {} ".format(obj.nome, kwargs["Commit message"]))
return HttpResponseRedirect("/admin/djakart/version/%s/" % obj.pk)
return HttpResponseRedirect("%s/admin/djakart/version/%s/" % (SITE_SUBPATH, obj.pk))

@action(label="New sub-version", description="New version from the existing one")
@require_parameter("New version name")
Expand All @@ -467,30 +470,28 @@ def nuova_versione_da_esistente(self, request, obj, **kwargs):
nuova_versione.base = obj
nuova_versione.crs = obj.crs
nuova_versione.save()
return HttpResponseRedirect("/admin/djakart/version/%s/" % nuova_versione.pk)
return HttpResponseRedirect("%s/admin/djakart/version/%s/" % (SITE_SUBPATH, nuova_versione.pk))

@action(label="Import geopackage", description="Import all tables from geopackage and commit to repository")
@require_file("importa_geopackage","gpkg")
def importa_geopackage(self, request, obj, **kwargs):
if obj.pk:
obj.importa(kwargs["importa_geopackage"])
return HttpResponseRedirect("/admin/djakart/version/%s/" % obj.pk)

return HttpResponseRedirect("/admin/djakart/version/%s/" % obj.pk)
return HttpResponseRedirect("%s/admin/djakart/version/%s/" % (SITE_SUBPATH, obj.pk))

@action(label="Import QGIS template", description="Import a QGS project as base template for repository (allows RW on tables)")
@require_file("importa_template","qgs")
def importa_template(self, request, obj, **kwargs):
if obj.pk:
obj.import_template(kwargs["importa_template"])
return HttpResponseRedirect("/admin/djakart/version/%s/" % obj.pk)
return HttpResponseRedirect("%s/admin/djakart/version/%s/" % (SITE_SUBPATH, obj.pk))

@action(label="Reset not commited edits", description="Reset not commited edits")
@require_confirmation
def restore(self, request, obj):
if obj.pk:
obj.restore()
return HttpResponseRedirect("/admin/djakart/version/%s/" % obj.pk)
return HttpResponseRedirect("%s/admin/djakart/version/%s/" % (SITE_SUBPATH, obj.pk))

def rigen_local_gpkg(self,obj):
if not obj.base:
Expand Down
20 changes: 14 additions & 6 deletions webapp/djakart/kart_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#defaults
from django.conf import settings
from django.template.loader import render_to_string
#from django.db import connections

import os
Expand All @@ -21,6 +22,8 @@
KART_PGUSER = os.environ.get("VERSION_VIEWER", "blabla")
KART_PGUSER_PWD = os.environ.get("VERSION_VIEWER_PASSWORD", "blabla")

SITE_SUBPATH = os.environ.get("SITE_SUBPATH", "")

SRID = os.environ.get("REPO_CRS")
SRID_CODE = SRID.split(":")[1]

Expand Down Expand Up @@ -224,7 +227,7 @@ def crea_nuovo_repository(repo_name,bare=True,readonly_workingcopy=None):
password=PG_PWD,
host=os.environ.get("POSTGRES_SERVER",'pgserver'),
port=os.environ.get("POSTGRES_PORT",'pgport'),
db=settings.DBPREFIX + os.environ.get("VERSION_DB",'pgdb'),
db=os.environ.get("VERSION_DB",'pgdb'),
schema=readonly_workingcopy
))
if bare:
Expand Down Expand Up @@ -267,7 +270,7 @@ def get_pg_uri(v):
password=PG_PWD,
host=os.environ.get("POSTGRES_SERVER",'pgserver'),
port=os.environ.get("POSTGRES_PORT",'pgport'),
db=settings.DBPREFIX + os.environ.get("VERSION_DB",'pgdb'),
db=os.environ.get("VERSION_DB",'pgdb'),
schema=v
)

Expand Down Expand Up @@ -462,20 +465,25 @@ def log_versione(versione, jsonoutput=False):
if os.path.exists(versione_path):
cmd = executeCmd(["--repo",versione_path,"log"], jsonoutput=jsonoutput)
return cmd


def _diff_view(crs,extent):
return render_to_string(
'diff-view.html',
{'crs': crs,'crscode': crs.split(":")[-1], 'extent': extent, 'site_subpath': SITE_SUBPATH}
)


def genera_diff_versione(versione, hash=None, prev=None, crs=SRID, format='html'):
def genera_diff_versione(versione, hash=None, prev=None, crs=SRID, extent=[0,0,0,0], format='html'):
versione_path = os.path.join(settings.KART_REPO,versione)
if os.path.exists(versione_path):
jlog = log_versione(versione,jsonoutput=True)
commit = list(filter(lambda target: target["commit"] == hash, jlog))
if commit and not prev:
prev = commit[0]["parents"][0]
if format == 'html':
#diff_template_location = os.path.join(os.path.dirname(os.path.realpath(__file__)),'diff-view.html')
response = requests.get("http://localhost:8000/djakart/diff-view/%s/" % versione)
with open("/tmp/diff-view.html", "w", encoding="utf8") as diff_template:
diff_template.write(response.text)
diff_template.write(_diff_view(crs, extent))
if hash == 'HEAD':
cmd = executeCmd(["--repo",versione_path,"diff","-o","html", "--html-template", '/tmp/diff-view.html', "--crs", crs, "--output", "-", hash])
else:
Expand Down
9 changes: 6 additions & 3 deletions webapp/djakart/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@

BASE_MAPPING_SERVICE = os.environ.get("QGIS_SERVER_EXTERNAL","qgis_server_external")
SRID = os.environ.get("REPO_CRS")
#SRID_CODE = SRID.split(":")[1]

try:
TEST = settings.TEST
except:
TEST = False

def can_modify(u,v):
return (v.riservato and u == v.referente) or not v.riservato or u.is_superuser
Expand All @@ -67,7 +71,7 @@ def writeQgs(versione_obj):

def getQgsProject(versione_obj):

if settings.TEST:
if TEST:
QGIS_HOST = "http://%s/qgisserver/" % (os.environ.get("NGINX_SERVER", 'qgis_host_internal'),)
PG_HOST = os.environ.get("POSTGRES_SERVER", 'postgres_host_internal')
PG_PORT = os.environ.get("POSTGRES_PORT", 'postgres_port_internal')
Expand All @@ -76,7 +80,6 @@ def getQgsProject(versione_obj):
PG_HOST = os.environ.get("HOST_EXTERNAL", 'postgres_host_external')
PG_PORT = os.environ.get("POSTGRES_PORT_EXTERNAL", 'postgres_port_external')

print ("ENVIRONMENT", settings.TEST, QGIS_HOST, PG_HOST, PG_PORT)
versione_name = versione_obj.nome if versione_obj.base else "%s_pub" % versione_obj.nome
template_obj = versione_obj.template_qgis or versione_obj.origine.template_qgis
if template_obj:
Expand Down
4 changes: 2 additions & 2 deletions webapp/djakart/static/djakart/generate_diff.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function generateDiff(versione,commit,parent) {
function generateDiff(subpath,versione,commit,parent) {
const baseElement = document.getElementById('version-source-diff')
const base = baseElement.options[baseElement.selectedIndex].value
if (base != 'PARENT') {
parent = commit
commit = base
} // /versioni/diff/{versione}/{commit}/{parent}/
window.open(`/djakart/diff/${versione}/${commit}/${parent}/`, '_blank');
window.open(`${subpath}/djakart/diff/${versione}/${commit}/${parent}/`, '_blank');
}
16 changes: 8 additions & 8 deletions webapp/djakart/templates/admin/djakart/change_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<link rel="stylesheet" href="https://rawgit.com/walkermatt/ol3-layerswitcher/master/src/ol3-layerswitcher.css" />
<link rel="stylesheet" href="https://cdn.rawgit.com/Viglino/ol-ext/master/dist/ol-ext.min.css" />
<!-- URB PD -->
<link rel="stylesheet" href="/static/djakart/versioni_finestramappa.css?id=123236600233" />
<link rel="stylesheet" href="{{ site_subpath }}/static/djakart/versioni_finestramappa.css?id=123236600233" />
<script
src="https://code.jquery.com/jquery-3.7.1.slim.min.js"
integrity="sha256-kmHvs0B+OpCW5GVHUNjv9rOmY0IvSIRcf7zGUDTDQM8="
Expand All @@ -17,13 +17,13 @@
<!-- DJAKART -->
<script type="text/javascript" src="https://epsg.io/{{ crs }}.js"></script>
<script type="text/javascript">var CRSID = "EPSG:{{ crs }}"; var CRSCODE = {{ crs }};</script>
<script type="text/javascript" src="/djakart/basemaps/foreground/"></script>
<script type="text/javascript" src="/djakart/basemaps/background/"></script>
<script type="text/javascript" src="/djakart/vlist/{{ object_id }}/"></script>
<script type="text/javascript" src="/static/djakart/versioni_finestramappa.js?id=55006603039"></script>
<script type="text/javascript" src="/static/djakart/generate_diff.js"></script>
<script type="text/javascript" src="/static/admin/js/calendar.js"></script>
<script type="text/javascript" src="/static/admin/js/admin/DateTimeShortcuts.js"></script>
<script type="text/javascript" src="{{ site_subpath }}/djakart/basemaps/foreground/"></script>
<script type="text/javascript" src="{{ site_subpath }}/djakart/basemaps/background/"></script>
<script type="text/javascript" src="{{ site_subpath }}/djakart/vlist/{{ object_id }}/"></script>
<script type="text/javascript" src="{{ site_subpath }}/static/djakart/versioni_finestramappa.js?id=55006603039"></script>
<script type="text/javascript" src="{{ site_subpath }}/static/djakart/generate_diff.js?id=9331006603"></script>
<script type="text/javascript" src="{{ site_subpath }}/static/admin/js/calendar.js"></script>
<script type="text/javascript" src="{{ site_subpath }}/static/admin/js/admin/DateTimeShortcuts.js"></script>
<script type="text/javascript">

</script>
Expand Down
4 changes: 2 additions & 2 deletions webapp/djakart/templates/diff-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<script src="https://rawgit.com/walkermatt/ol3-layerswitcher/master/src/ol3-layerswitcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.2.1/proj4.js"></script>
<script type="text/javascript" src="https://epsg.io/{{ crscode }}.js"></script>
<script type="text/javascript" src="/djakart/basemaps/foreground/"></script>
<script type="text/javascript" src="/djakart/basemaps/background/"></script>
<script type="text/javascript" src="{{ site_subpath }}/djakart/basemaps/foreground/"></script>
<script type="text/javascript" src="{{ site_subpath }}/djakart/basemaps/background/"></script>
<script type="text/javascript"></script>

<style>
Expand Down
14 changes: 5 additions & 9 deletions webapp/djakart/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
log_versione,
status_versione,
show_versione,
_diff_view,
commit_versione,
genera_diff_versione,
restore_versione,
Expand Down Expand Up @@ -57,19 +58,14 @@ def status(request,versione):

def diff(request,versione,hash,parent_hash=""):
versione_obj = version.objects.get(nome=versione)
return HttpResponse(genera_diff_versione(versione,hash,parent_hash,crs=versione_obj.crs))
diff = genera_diff_versione(versione,hash,parent_hash,crs=versione_obj.crs, extent=versione_obj.extent)
print (diff)
return HttpResponse(diff)

def diff_view(request,versione):
versione_obj = version.objects.get(nome=versione)
response = render(
request,
'diff-view.html',
{'crs': versione_obj.crs,'crscode': versione_obj.crs.split(":")[-1], 'extent': versione_obj.extent},
content_type="text/html; charset=utf-8"
)

response = HttpResponse(_diff_view(versione_obj.crs, versione_obj.extent))
response['Content-Disposition'] = 'inline; filename="diff-view.html"'

return response

def QGS_progetto(request,versione):
Expand Down

0 comments on commit 7872ef2

Please sign in to comment.