diff --git a/core/views/surveys.py b/core/views/surveys.py index 63eaf2b..7cf0d83 100644 --- a/core/views/surveys.py +++ b/core/views/surveys.py @@ -44,12 +44,24 @@ def tunneldata(request): return render(request, 'tunnelfiles.html', { 'tunnelfiles':tunnelfiles, 'settings': settings }) -def tunnelfilesingle(request, path): +def dwgfilesingle(request, path): '''sends a single binary file to the user, We should have a renderer that syntax-colours this Tunnel xml + but it might be a Therion file ''' - tunnelfile = TunnelFile.objects.get(tunnelpath=urlunquote(path)) # need to check if inavlid query string and produce friendly error + tunnelfile = TunnelFile.objects.get(tunnelpath=urlunquote(path)) # need to check if invalid query string and produce friendly error tfile = Path(settings.TUNNEL_DATA, tunnelfile.tunnelpath) - return HttpResponse(content=open(tfile), content_type="text/xhtml") # for display not download + try: # for display not download + return HttpResponse(content=open(tfile, errors='strict'), content_type="text/xhtml") + except UnicodeDecodeError: + try: + return HttpResponse(content=open(tfile,encoding='iso-8859-1'), content_type="text/xhtml") + except: + return HttpResponse(content=open(tfile,mode='rb'), content_type="text/xhtml") + else: + return HttpResponse(content=open(tfile, errors='ignore'), content_type="text/xhtml") + else: + return HttpResponse(content="Unable to understand the encoding for this file: not UTF-8 nor iso-8859-1, or some other read error happened.") + def tunnelfileupload(request, path): tunnelfile = TunnelFile.objects.get(tunnelpath=urlunquote(path)) # need to check if inavlid query string and produce friendly error diff --git a/parsers/surveys.py b/parsers/surveys.py index dbbc537..40a12f0 100644 --- a/parsers/surveys.py +++ b/parsers/surveys.py @@ -217,6 +217,8 @@ rx_pcpath = re.compile(rb'<pcarea area_signal="frame".*?sfsketch="([^"]*)" sfsty def settunnelfileinfo(tunnelfile): '''Read in the drawing file contents and sets values on the tunnelfile object + Should try to read the date too e.g. tunneldate="2010-08-16 22:51:57 + then we could display on the master calendar per expo. ''' ff = os.path.join(settings.TUNNEL_DATA, tunnelfile.tunnelpath) tunnelfile.filesize = os.stat(ff)[stat.ST_SIZE] diff --git a/urls.py b/urls.py index 5acb243..7bd6585 100644 --- a/urls.py +++ b/urls.py @@ -143,12 +143,12 @@ trogglepatterns = [ surveys.surveyscansingle, name="surveyscansingle"), # The tunnel and therion drawings files pages - url(r'^tunneldata/$', surveys.tunneldata, name="tunneldata"), - url(r'^tunneldataraw/(?P<path>.+?\.xml)$', surveys.tunnelfilesingle, name="tunnelfile"), - url(r'^tunneldataraw/(?P<path>.+?\.th)$', surveys.tunnelfilesingle, name="tunnelfile"), - url(r'^tunneldataraw/(?P<path>.+?\.th2)$', surveys.tunnelfilesingle, name="tunnelfile"), -# url(r'^tunneldatainfo/(?P<path>.+?\.xml)$', surveys.tunnelfileinfo, name="tunnelfileinfo"), # parses tunnel for info - url(r'^tunneldataraw/(?P<path>.+?\.xml)/upload$', surveys.tunnelfileupload, name="tunnelfileupload"), + url(r'^tunneldata/$', surveys.tunneldata, name="tunneldata"), + url(r'^tunneldataraw/(?P<path>.+?\.xml)$', surveys.dwgfilesingle, name="dwgfilesingle"), + url(r'^tunneldataraw/(?P<path>.+?\.th)$', surveys.dwgfilesingle, name="dwgfilesingle"), + url(r'^tunneldataraw/(?P<path>.+?\.th2)$', surveys.dwgfilesingle, name="dwgfilesingle"), +# url(r'^tunneldatainfo/(?P<path>.+?\.xml)$', surveys.tunnelfileinfo, name="tunnelfileinfo"), # parses tunnel for info + url(r'^tunneldataraw/(?P<path>.+?\.xml)/upload$', surveys.tunnelfileupload, name="tunnelfileupload"), # This next set are all intercepted by Apache, if it is running. url(r'^photos/(?P<subpath>.*)$', mediapage, {'doc_root': settings.PHOTOS_ROOT}, name="mediapage"), # photo galleries