[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

This commit is contained in:
julian 2009-01-18 00:25:59 +01:00
parent 22b7620253
commit cbb1acd1b4
3 changed files with 66 additions and 6 deletions

View File

@ -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"

View File

@ -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,6 +37,7 @@ 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<number>\d*)(?P<letter>[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()

View File

@ -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()
@ -36,6 +37,10 @@ urlpatterns = patterns('',
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
(r'^survey_files/listdir/(?P<path>.*)$', view_surveys.listdir),
(r'^survey_files/download/(?P<path>.*)$', view_surveys.download),
#(r'^survey_files/upload/(?P<path>.*)$', view_surveys.upload),
(r'^survey_scans/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.SURVEYS, 'show_indexes':True}),