2009-05-13 05:22:38 +01:00
|
|
|
from django.forms import ModelForm
|
2011-05-01 19:32:41 +01:00
|
|
|
from models import Cave, Person, PersonExpedition, LogbookEntry, QM
|
2009-05-13 06:13:31 +01:00
|
|
|
import django.forms as forms
|
|
|
|
from django.forms.formsets import formset_factory
|
|
|
|
from django.contrib.admin.widgets import AdminDateWidget
|
|
|
|
import string
|
2009-06-10 05:37:53 +01:00
|
|
|
from datetime import date
|
2011-05-01 19:32:41 +01:00
|
|
|
from tinymce.widgets import TinyMCE
|
2009-05-13 05:22:38 +01:00
|
|
|
|
|
|
|
class CaveForm(ModelForm):
|
|
|
|
class Meta:
|
2009-05-13 05:48:10 +01:00
|
|
|
model = Cave
|
|
|
|
|
|
|
|
class PersonForm(ModelForm):
|
|
|
|
class Meta:
|
2009-05-13 06:08:04 +01:00
|
|
|
model = Person
|
2009-05-13 06:13:31 +01:00
|
|
|
|
|
|
|
class LogbookEntryForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = LogbookEntry
|
|
|
|
|
|
|
|
def wikiLinkHints(LogbookEntry=None):
|
2009-06-10 05:37:53 +01:00
|
|
|
"""
|
|
|
|
This function returns html-formatted paragraphs for each of the
|
|
|
|
wikilink types that are related to this logbookentry. Each paragraph
|
|
|
|
contains a list of all of the related wikilinks.
|
|
|
|
|
|
|
|
Perhaps an admin javascript solution would be better.
|
|
|
|
"""
|
2009-05-13 06:13:31 +01:00
|
|
|
res = ["Please use the following wikilinks, which are related to this logbook entry:"]
|
|
|
|
|
|
|
|
res.append(r'</p><p style="float: left;"><b>QMs found:</b>')
|
|
|
|
for QM in LogbookEntry.instance.QMs_found.all():
|
|
|
|
res.append(QM.wiki_link())
|
|
|
|
|
|
|
|
res.append(r'</p><p style="float: left;"><b>QMs ticked off:</b>')
|
|
|
|
for QM in LogbookEntry.instance.QMs_ticked_off.all():
|
|
|
|
res.append(QM.wiki_link())
|
|
|
|
|
2009-05-13 06:14:42 +01:00
|
|
|
# res.append(r'</p><p style="float: left; "><b>People</b>')
|
|
|
|
# for persontrip in LogbookEntry.instance.persontrip_set.all():
|
|
|
|
# res.append(persontrip.wiki_link())
|
|
|
|
# res.append(r'</p>')
|
2009-05-13 06:13:31 +01:00
|
|
|
|
|
|
|
return string.join(res, r'<br />')
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(LogbookEntryForm, self).__init__(*args, **kwargs)
|
2011-05-01 19:32:41 +01:00
|
|
|
self.fields['text'].help_text=self.wikiLinkHints()
|
|
|
|
|
|
|
|
def getTripForm(expedition):
|
|
|
|
|
|
|
|
class TripForm(forms.Form):
|
|
|
|
date = forms.DateField()
|
|
|
|
title = forms.CharField(max_length=200)
|
|
|
|
caves = [cave.reference() for cave in Cave.objects.all()]
|
|
|
|
caves.sort()
|
|
|
|
caves = ["-----"] + caves
|
|
|
|
cave = forms.ChoiceField([(c, c) for c in caves], required=False)
|
|
|
|
location = forms.CharField(max_length=200, required=False)
|
|
|
|
caveOrLocation = forms.ChoiceField([("cave", "Cave"), ("location", "Location")], widget = forms.widgets.RadioSelect())
|
|
|
|
html = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
print dir(self)
|
|
|
|
if self.cleaned_data.get("caveOrLocation") == "cave" and not self.cleaned_data.get("cave"):
|
|
|
|
self._errors["cave"] = self.error_class(["This field is required"])
|
|
|
|
if self.cleaned_data.get("caveOrLocation") == "location" and not self.cleaned_data.get("location"):
|
|
|
|
self._errors["location"] = self.error_class(["This field is required"])
|
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
class PersonTripForm(forms.Form):
|
|
|
|
def get_name(pe):
|
|
|
|
if pe.nickname:
|
|
|
|
return pe.nickname
|
|
|
|
else:
|
|
|
|
return pe.person.first_name
|
|
|
|
names = [get_name(pe) for pe in PersonExpedition.objects.filter(expedition = expedition)]
|
|
|
|
names.sort()
|
|
|
|
names = ["-----"] + names
|
|
|
|
name = forms.ChoiceField([(n, n) for n in names])
|
|
|
|
TU = forms.FloatField(required=False)
|
|
|
|
author = forms.BooleanField(required=False)
|
|
|
|
|
|
|
|
PersonTripFormSet = formset_factory(PersonTripForm, extra=1)
|
|
|
|
|
|
|
|
return PersonTripFormSet, TripForm
|
|
|
|
|