2009-07-06 08:31:24 +01:00
|
|
|
from troggle.core.models import Cave, Expedition, Person, LogbookEntry, PersonExpedition, PersonTrip, Photo, QM
|
|
|
|
from django.conf import settings
|
2009-05-13 05:22:14 +01:00
|
|
|
from django import forms
|
2009-06-18 06:53:52 +01:00
|
|
|
from django.template import loader, Context
|
2009-05-13 05:22:14 +01:00
|
|
|
from django.db.models import Q
|
2009-05-13 06:15:48 +01:00
|
|
|
import databaseReset
|
2009-05-13 05:25:17 +01:00
|
|
|
import re
|
2009-05-13 05:31:44 +01:00
|
|
|
import randSent
|
2009-05-14 06:19:46 +01:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2009-05-13 05:35:59 +01:00
|
|
|
from django.core.urlresolvers import reverse
|
2009-07-03 05:31:49 +01:00
|
|
|
from utils import render_with_context
|
2009-07-02 20:43:18 +01:00
|
|
|
from core.models import *
|
2009-05-13 05:35:59 +01:00
|
|
|
|
2009-05-13 06:08:04 +01:00
|
|
|
def showrequest(request):
|
|
|
|
return HttpResponse(request.GET)
|
|
|
|
|
2009-05-13 05:22:14 +01:00
|
|
|
def stats(request):
|
|
|
|
statsDict={}
|
|
|
|
statsDict['expoCount'] = int(Expedition.objects.count())
|
|
|
|
statsDict['caveCount'] = int(Cave.objects.count())
|
|
|
|
statsDict['personCount'] = int(Person.objects.count())
|
|
|
|
statsDict['logbookEntryCount'] = int(LogbookEntry.objects.count())
|
2009-07-03 05:31:49 +01:00
|
|
|
return render_with_context(request,'statistics.html', statsDict)
|
2009-05-13 05:25:17 +01:00
|
|
|
|
2009-05-13 05:48:47 +01:00
|
|
|
def frontpage(request):
|
2009-07-15 01:55:26 +01:00
|
|
|
if request.user.is_authenticated():
|
2009-07-04 08:27:49 +01:00
|
|
|
return render_with_context(request,'tasks.html')
|
2009-05-13 05:39:52 +01:00
|
|
|
|
2009-05-13 06:19:45 +01:00
|
|
|
expeditions = Expedition.objects.order_by("-year")
|
|
|
|
logbookentry = LogbookEntry
|
|
|
|
cave = Cave
|
|
|
|
photo = Photo
|
2009-06-10 00:05:02 +01:00
|
|
|
from django.contrib.admin.templatetags import log
|
2009-07-03 05:31:49 +01:00
|
|
|
return render_with_context(request,'frontpage.html', locals())
|
2009-05-13 06:19:45 +01:00
|
|
|
|
|
|
|
def todo(request):
|
|
|
|
message = "no test message" #reverse('personn', kwargs={"name":"hkjhjh"})
|
|
|
|
if "reloadexpos" in request.GET:
|
|
|
|
message = LoadPersonsExpos()
|
|
|
|
message = "Reloaded personexpos"
|
|
|
|
if "reloadsurvex" in request.POST:
|
|
|
|
message = LoadAllSurvexBlocks()
|
|
|
|
message = "Reloaded survexblocks"
|
|
|
|
|
2009-05-13 05:35:59 +01:00
|
|
|
#'randSent':randSent.randomLogbookSentence(),
|
2009-05-13 05:44:35 +01:00
|
|
|
expeditions = Expedition.objects.order_by("-year")
|
2009-05-13 06:06:11 +01:00
|
|
|
totallogbookentries = LogbookEntry.objects.count()
|
2009-07-03 05:31:49 +01:00
|
|
|
return render_with_context(request,'index.html', {'expeditions':expeditions, 'all':'all', 'totallogbookentries':totallogbookentries, "message":message})
|
2009-05-13 06:19:45 +01:00
|
|
|
|
2009-05-13 05:35:11 +01:00
|
|
|
def calendar(request,year):
|
|
|
|
week=['S','S','M','T','W','T','F']
|
|
|
|
if year:
|
2009-05-13 06:15:48 +01:00
|
|
|
expedition=Expedition.objects.get(year=year)
|
|
|
|
PersonExpeditions=expedition.personexpedition_set.all()
|
|
|
|
|
2009-07-03 05:31:49 +01:00
|
|
|
return render_with_context(request,'calendar.html', locals())
|
2009-05-13 06:15:48 +01:00
|
|
|
|
|
|
|
def controlPanel(request):
|
2009-05-19 06:32:42 +01:00
|
|
|
jobs_completed=[]
|
2009-05-13 06:15:48 +01:00
|
|
|
if request.method=='POST':
|
2009-05-14 06:19:46 +01:00
|
|
|
if request.user.is_superuser:
|
2009-05-21 20:46:24 +01:00
|
|
|
|
|
|
|
#importlist is mostly here so that things happen in the correct order.
|
2009-05-24 23:24:59 +01:00
|
|
|
#http post data seems to come in an unpredictable order, so we do it this way.
|
2009-05-21 20:46:24 +01:00
|
|
|
importlist=['reload_db', 'import_people', 'import_cavetab', 'import_logbooks', 'import_surveys', 'import_QMs']
|
2009-05-19 06:32:42 +01:00
|
|
|
databaseReset.make_dirs()
|
|
|
|
for item in importlist:
|
|
|
|
if item in request.POST:
|
2009-05-14 06:19:46 +01:00
|
|
|
print "running"+ " databaseReset."+item+"()"
|
|
|
|
exec "databaseReset."+item+"()"
|
2009-05-19 06:32:42 +01:00
|
|
|
jobs_completed.append(item)
|
2009-05-14 06:19:46 +01:00
|
|
|
else:
|
2009-05-24 23:24:59 +01:00
|
|
|
if request.user.is_authenticated(): #The user is logged in, but is not a superuser.
|
2009-07-03 05:31:49 +01:00
|
|
|
return render_with_context(request,'controlPanel.html', {'caves':Cave.objects.all(),'error':'You must be a superuser to use that feature.'})
|
2009-05-24 23:24:59 +01:00
|
|
|
else:
|
|
|
|
return HttpResponseRedirect(reverse('auth_login'))
|
2009-05-14 06:19:46 +01:00
|
|
|
|
2009-07-03 05:31:49 +01:00
|
|
|
return render_with_context(request,'controlPanel.html', {'caves':Cave.objects.all(),'expeditions':Expedition.objects.all(),'jobs_completed':jobs_completed})
|
2009-05-13 06:15:48 +01:00
|
|
|
|
2009-05-14 06:19:46 +01:00
|
|
|
def downloadCavetab(request):
|
|
|
|
from export import tocavetab
|
|
|
|
response = HttpResponse(mimetype='text/csv')
|
2009-05-17 04:31:23 +01:00
|
|
|
response['Content-Disposition'] = 'attachment; filename=CAVETAB2.CSV'
|
2009-05-14 06:19:46 +01:00
|
|
|
tocavetab.writeCaveTab(response)
|
|
|
|
return response
|
2009-05-17 04:31:23 +01:00
|
|
|
|
|
|
|
def downloadSurveys(request):
|
|
|
|
from export import tosurveys
|
|
|
|
response = HttpResponse(mimetype='text/csv')
|
|
|
|
response['Content-Disposition'] = 'attachment; filename=Surveys.csv'
|
|
|
|
tosurveys.writeCaveTab(response)
|
|
|
|
return response
|
|
|
|
|
2009-06-18 06:53:52 +01:00
|
|
|
def downloadLogbook(request,year=None,extension=None,queryset=None):
|
|
|
|
|
|
|
|
if year:
|
|
|
|
expedition=Expedition.objects.get(year=year)
|
|
|
|
logbook_entries=LogbookEntry.objects.filter(expedition=expedition)
|
|
|
|
filename='logbook'+year
|
|
|
|
elif queryset:
|
|
|
|
logbook_entries=queryset
|
|
|
|
filename='logbook'
|
|
|
|
else:
|
|
|
|
return response(r"Error: Logbook downloader doesn't know what year you want")
|
|
|
|
|
|
|
|
if 'year' in request.GET:
|
|
|
|
year=request.GET['year']
|
|
|
|
if 'extension' in request.GET:
|
|
|
|
extension=request.GET['extension']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if extension =='txt':
|
|
|
|
response = HttpResponse(mimetype='text/plain')
|
|
|
|
style='2008'
|
|
|
|
elif extension == 'html':
|
|
|
|
response = HttpResponse(mimetype='text/html')
|
|
|
|
style='2005'
|
|
|
|
|
|
|
|
template='logbook'+style+'style.'+extension
|
|
|
|
response['Content-Disposition'] = 'attachment; filename='+filename+'.'+extension
|
|
|
|
t=loader.get_template(template)
|
|
|
|
c=Context({'logbook_entries':logbook_entries})
|
|
|
|
response.write(t.render(c))
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2009-05-17 04:31:23 +01:00
|
|
|
def downloadQMs(request):
|
2009-06-18 06:53:52 +01:00
|
|
|
# Note to self: use get_cave method for the below
|
2009-05-17 04:31:23 +01:00
|
|
|
if request.method=='GET':
|
|
|
|
try:
|
|
|
|
cave=Cave.objects.get(kataster_number=request.GET['cave_id'])
|
|
|
|
except Cave.DoesNotExist:
|
|
|
|
cave=Cave.objects.get(name=cave_id)
|
|
|
|
|
|
|
|
from export import toqms
|
|
|
|
|
|
|
|
response = HttpResponse(mimetype='text/csv')
|
|
|
|
response['Content-Disposition'] = 'attachment; filename=qm.csv'
|
|
|
|
toqms.writeQmTable(response,cave)
|
2009-05-19 06:32:42 +01:00
|
|
|
return response
|
|
|
|
|
|
|
|
def ajax_test(request):
|
|
|
|
post_text = request.POST['post_data']
|
|
|
|
return HttpResponse("{'response_text': '"+post_text+" recieved.'}",
|
2009-06-28 19:33:24 +01:00
|
|
|
mimetype="application/json")
|
|
|
|
|
|
|
|
def eyecandy(request):
|
2009-07-06 08:31:24 +01:00
|
|
|
return
|
2009-07-04 08:27:49 +01:00
|
|
|
|
|
|
|
def ajax_QM_number(request):
|
|
|
|
if request.method=='POST':
|
|
|
|
cave=Cave.objects.get(id=request.POST['cave'])
|
|
|
|
print cave
|
|
|
|
exp=Expedition.objects.get(pk=request.POST['year'])
|
|
|
|
print exp
|
|
|
|
res=cave.new_QM_number(exp.year)
|
|
|
|
|
2009-07-06 08:31:24 +01:00
|
|
|
return HttpResponse(res)
|
|
|
|
|
|
|
|
def logbook_entry_suggestions(request):
|
|
|
|
"""
|
|
|
|
Generates a html box with suggestions about what to do with QMs
|
|
|
|
in logbook entry text.
|
|
|
|
"""
|
|
|
|
unwiki_QM_pattern=r"(?P<whole>(?P<explorer_code>[ABC]?)(?P<cave>\d*)-?(?P<year>\d\d\d?\d?)-(?P<number>\d\d)(?P<grade>[ABCDXV]?)(?=\s))"
|
|
|
|
unwiki_QM_pattern=re.compile(unwiki_QM_pattern)
|
2009-07-06 08:35:08 +01:00
|
|
|
#wikilink_QM_pattern=settings.QM_PATTERN
|
2009-07-06 08:31:24 +01:00
|
|
|
|
|
|
|
slug=request.POST['slug']
|
|
|
|
date=request.POST['date']
|
|
|
|
lbo=LogbookEntry.objects.get(slug=slug, date=date)
|
|
|
|
|
|
|
|
#unwiki_QMs=re.findall(unwiki_QM_pattern,lbo.text)
|
|
|
|
unwiki_QMs=[m.groupdict() for m in unwiki_QM_pattern.finditer(lbo.text)]
|
|
|
|
|
|
|
|
print unwiki_QMs
|
|
|
|
for qm in unwiki_QMs:
|
|
|
|
if len(qm['year'])==2:
|
|
|
|
if int(qm['year'])<50:
|
|
|
|
qm['year']='20'+qm['year']
|
|
|
|
else:
|
|
|
|
qm['year']='19'+qm['year']
|
|
|
|
|
|
|
|
temp_QM=QM(found_by=lbo,number=qm['number'],grade=qm['grade'])
|
|
|
|
try:
|
|
|
|
temp_QM.grade=unwiki_QM['grade']
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
qm['wikilink']=temp_QM.wiki_link()
|
|
|
|
|
|
|
|
print unwiki_QMs
|
|
|
|
|
|
|
|
|
2009-07-06 08:35:08 +01:00
|
|
|
#wikilink_QMs=re.findall(wikilink_QM_pattern,lbo.text)
|
2009-07-06 08:31:24 +01:00
|
|
|
attached_QMs=lbo.QMs_found.all()
|
|
|
|
unmentioned_attached_QMs=''#not implemented, fill this in by subtracting wiklink_QMs from attached_QMs
|
|
|
|
|
|
|
|
#Find unattached_QMs. We only look at the QMs with a proper wiki link.
|
|
|
|
#for qm in wikilink_QMs:
|
|
|
|
#Try to look up the QM.
|
|
|
|
|
|
|
|
print 'got 208'
|
|
|
|
any_suggestions=True
|
|
|
|
print 'got 210'
|
|
|
|
return render_with_context(request,'suggestions.html',
|
|
|
|
{
|
|
|
|
'unwiki_QMs':unwiki_QMs,
|
|
|
|
'any_suggestions':any_suggestions
|
|
|
|
})
|