From cbb1acd1b499aee6782a87d176440cb5741ad642 Mon Sep 17 00:00:00 2001 From: julian Date: Sun, 18 Jan 2009 00:25:59 +0100 Subject: [PATCH] [svn r8172] Modifications to allow survey files to be pulled from another server. By providing directory listing and download functions,. which could also be used by tunnel --- troggle/expo/view_surveys.py | 44 ++++++++++++++++++++++++++++++++++++ troggle/parsers/surveys.py | 21 +++++++++++++---- troggle/urls.py | 7 +++++- 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 troggle/expo/view_surveys.py diff --git a/troggle/expo/view_surveys.py b/troggle/expo/view_surveys.py new file mode 100644 index 000000000..cd3210b0b --- /dev/null +++ b/troggle/expo/view_surveys.py @@ -0,0 +1,44 @@ +import troggle.settings as settings +from django.http import HttpResponse, Http404 +import os + +def listdir(request, path): + try: + l = [] + print settings.FILES, "t", path, "t" + root = os.path.join(settings.FILES, path) + print root + for p in os.listdir(root): + if os.path.isdir(os.path.join(root, p)): + l.append(p + "/") + elif os.path.isfile(os.path.join(root, p)): + l.append(p) + #Ignore non-files and non-directories + return HttpResponse(str(l), mimetype = "text/plain") + except: + try: + return HttpResponse(urllib.urlopen(settings.FILES + "listdir/" + name), mimetype = "text/plain") + except: + raise Http404 + +def upload(request, path): + pass + +def download(request, path): + try: + f = open(os.path.join(settings.FILES, path)) + except: + try: + f = urllib.urlopen(settings.FILES + "download/" + path) + except: + raise Http404 + return HttpResponse(f.read(), mimetype=getMimeType(path.split(".")[-1])) + +def getMimeType(extension): + try: + return {"txt": "text/plain", + "html": "text/html", + }[extension] + except: + print "unknown file type" + return "text/plain" \ No newline at end of file diff --git a/troggle/parsers/surveys.py b/troggle/parsers/surveys.py index cd9781be6..86d7dc6e0 100644 --- a/troggle/parsers/surveys.py +++ b/troggle/parsers/surveys.py @@ -1,5 +1,6 @@ import sys import os +import urllib import types sys.path.append('C:\\Expo\\expoweb') from troggle import * @@ -13,12 +14,21 @@ import csv import re import datetime -surveytab = open(os.path.join(settings.SURVEYS, "Surveys.csv")) -dialect=csv.Sniffer().sniff(surveytab.read()) -surveytab.seek(0,0) +def openFileOrWeb(name): + try: + f = open(os.path.join(settings.SURVEYS, name)) + except: + f = urllib.urlopen(settings.SURVEYS + name) + return f.readlines() + +surveytab = openFileOrWeb("Surveys.csv") + +dialect=csv.Sniffer().sniff(reduce(lambda x, y: x + "\n" + y, surveytab)) surveyreader = csv.reader(surveytab,dialect=dialect) +print surveyreader headers = surveyreader.next() header = dict(zip(headers, range(len(headers)))) #set up a dictionary where the indexes are header names and the values are column numbers +print header # test if the expeditions have been added yet if len(models.Expedition.objects.all())==0: @@ -27,9 +37,10 @@ if len(models.Expedition.objects.all())==0: models.ScannedImage.objects.all().delete() models.Survey.objects.all().delete() for survey in surveyreader: + print type(survey), survey walletNumberLetter = re.match(r'(?P\d*)(?P[a-zA-Z]*)',survey[header['Survey Number']]) #I hate this, but some surveys have a letter eg 2000#34a. This line deals with that. # print walletNumberLetter.groups() - + surveyobj = models.Survey( expedition = models.Expedition.objects.filter(year=survey[header['Year']])[0], wallet_number = walletNumberLetter.group('number'), @@ -43,7 +54,7 @@ for survey in surveyreader: pass surveyobj.save() print "added survey " + survey[header['Year']] + "#" + surveyobj.wallet_number - + # add survey scans def parseSurveyScans(year): yearPath=os.path.join(settings.SURVEYS, year.year) diff --git a/troggle/urls.py b/troggle/urls.py index fd00e4695..7ef7bd640 100644 --- a/troggle/urls.py +++ b/troggle/urls.py @@ -1,5 +1,6 @@ from django.conf.urls.defaults import * from expo.views import * +import expo.view_surveys as view_surveys import troggle.settings as settings from django.contrib import admin admin.autodiscover() @@ -12,7 +13,7 @@ urlpatterns = patterns('', (r'^cave/(?P[^/]+)/?(?P[^/])$', ent), #(r'^cave/(?P[^/]+)/edit/$', edit_cave), (r'^cavesearch', caveSearch), - + (r'^survex/(?P.*)\.index$', index), (r'^survex/(?P.*)\.svx$', svx), (r'^survex/(?P.*)\.3d$', threed), @@ -36,6 +37,10 @@ urlpatterns = patterns('', (r'^site_media/(?P.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), + (r'^survey_files/listdir/(?P.*)$', view_surveys.listdir), + (r'^survey_files/download/(?P.*)$', view_surveys.download), + #(r'^survey_files/upload/(?P.*)$', view_surveys.upload), + (r'^survey_scans/(?P.*)$', 'django.views.static.serve', {'document_root': settings.SURVEYS, 'show_indexes':True}),