From 27816724f81f1655bd605cdbd3161a19a0222dfe Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Thu, 4 Jun 2020 23:38:57 +0100 Subject: [PATCH] moved 2 funct, deletion of FileAbstraction pending --- core/TESTS/tests.py | 2 +- core/fileAbstraction.py | 51 +++++++++-------------------------------- core/view_surveys.py | 42 ++++++++++++++++++++++++++++----- 3 files changed, 48 insertions(+), 47 deletions(-) diff --git a/core/TESTS/tests.py b/core/TESTS/tests.py index aa92210..5a4dd8d 100644 --- a/core/TESTS/tests.py +++ b/core/TESTS/tests.py @@ -48,7 +48,7 @@ class SimpleTest(SimpleTestCase): from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import get_object_or_404, render from troggle.core.models import Expedition - from troggle.core.models_caves import CaveSlug, Cave, CaveAndEntrance, Survey, QM, CaveDescription, EntranceSlug, Entrance, Area, SurvexStation + from troggle.core.models_caves import CaveSlug, Cave, CaveAndEntrance, QM, CaveDescription, EntranceSlug, Entrance, Area, SurvexStation from troggle.core.forms import CaveForm, CaveAndEntranceFormSet, VersionControlCommentForm, EntranceForm, EntranceLetterForm from troggle.helper import login_required_if_public diff --git a/core/fileAbstraction.py b/core/fileAbstraction.py index 0ebd6eb..0035bf8 100644 --- a/core/fileAbstraction.py +++ b/core/fileAbstraction.py @@ -1,44 +1,15 @@ -import troggle.settings as settings -import os -import urllib.request, urllib.parse, urllib.error -from functools import reduce +# import troggle.settings as settings +# import os +# import urllib.request, urllib.parse, urllib.error +# from functools import reduce -def urljoin(x, y): return x + "/" + y +# All unused, but kept pending deletion -def listdir(*path): - try: - strippedpath = [p for p in path if p] - root = os.path.join(settings.FILES, *strippedpath ) - l = "" - #l = root + "\n" - isdir = os.path.isdir(root) #This seems to be required for os.path.isdir to work... - #l += str(isdir) + "\n" - for p in os.listdir(root): - if os.path.isdir(os.path.join(root, p)): - l += p + "/\n" - - elif os.path.isfile(os.path.join(root, p)): - l += p + "\n" - #Ignore non-files and non-directories - return l - except: - if strippedpath: - c = reduce(urljoin, strippedpath) - else: - c = "" - c = c.replace("#", "%23") - print(("FILE: ", settings.FILES + "listdir/" + c)) - return urllib.request.urlopen(settings.FILES + "listdir/" + c).read() - -def dirsAsList(*path): - return [d for d in listdir(*path).split("\n") if len(d) > 0 and d[-1] == "/"] +# def urljoin(x, y): return x + "/" + y -def filesAsList(*path): - return [d for d in listdir(*path).split("\n") if len(d) > 0 and d[-1] != "/"] +# def dirsAsList(*path): + # return [d for d in listdir(*path).split("\n") if len(d) > 0 and d[-1] == "/"] + +# def filesAsList(*path): + # return [d for d in listdir(*path).split("\n") if len(d) > 0 and d[-1] != "/"] -def readFile(*path): - try: - f = open(os.path.join(settings.FILES, *path)) - except: - f = urllib.request.urlopen(settings.FILES + "download/" + reduce(urljoin, path)) - return f.read() \ No newline at end of file diff --git a/core/view_surveys.py b/core/view_surveys.py index db33557..d78c3e6 100644 --- a/core/view_surveys.py +++ b/core/view_surveys.py @@ -1,5 +1,5 @@ from django.conf import settings -from . import fileAbstraction +#from . import fileAbstraction from django.shortcuts import render_to_response from django.http import HttpResponse, Http404 import os, stat @@ -8,10 +8,40 @@ from troggle.core.models_survex import SurvexScansFolder, SurvexScanSingle, Surv import parsers.surveys import urllib.request, urllib.parse, urllib.error -# inline fileabstraction into here if it's not going to be useful anywhere else -# keep things simple and ignore exceptions everywhere for now - +# inlined use of fileabstraction into here +def fa_listdir(*path): + try: + strippedpath = [p for p in path if p] + root = os.path.join(settings.FILES, *strippedpath ) + l = "" + #l = root + "\n" + isdir = os.path.isdir(root) #This seems to be required for os.path.isdir to work... + #l += str(isdir) + "\n" + for p in os.listdir(root): + if os.path.isdir(os.path.join(root, p)): + l += p + "/\n" + + elif os.path.isfile(os.path.join(root, p)): + l += p + "\n" + #Ignore non-files and non-directories + return l + except: + if strippedpath: + c = reduce(urljoin, strippedpath) + else: + c = "" + c = c.replace("#", "%23") + print(("FILE: ", settings.FILES + "listdir/" + c)) + return urllib.request.urlopen(settings.FILES + "listdir/" + c).read() + +def fa_readFile(*path): + try: + f = open(os.path.join(settings.FILES, *path)) + except: + f = urllib.request.urlopen(settings.FILES + "download/" + reduce(urljoin, path)) + return f.read() + def getMimeType(extension): try: return {"txt": "text/plain", @@ -24,7 +54,7 @@ def getMimeType(extension): def listdir(request, path): #try: - return HttpResponse(fileAbstraction.listdir(path), content_type="text/plain") + return HttpResponse(fa_listdir(path), content_type="text/plain") #except: # raise Http404 @@ -34,7 +64,7 @@ def upload(request, path): def download(request, path): #try: - return HttpResponse(fileAbstraction.readFile(path), content_type=getMimeType(path.split(".")[-1])) + return HttpResponse(fa_readFile(path), content_type=getMimeType(path.split(".")[-1])) #except: # raise Http404