summaryrefslogtreecommitdiffstats
path: root/core/forms.py
blob: ae046b5b31aca06d100350bebf1daf09634b61ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
from django.forms import ModelForm
from models import Cave, Person, PersonExpedition, LogbookEntry, QM, Expedition, Entrance, CaveAndEntrance
import django.forms as forms
from django.forms.models import modelformset_factory
from django.contrib.admin.widgets import AdminDateWidget
import string
from datetime import date
from tinymce.widgets import TinyMCE

class CaveForm(ModelForm):
    underground_description = forms.CharField(required = False, widget=forms.Textarea())
    explorers = forms.CharField(required = False, widget=forms.Textarea())
    equipment = forms.CharField(required = False, widget=forms.Textarea())
    survey = forms.CharField(required = False, widget=forms.Textarea())
    kataster_status = forms.CharField(required = False, widget=forms.Textarea())
    underground_centre_line = forms.CharField(required = False, widget=forms.Textarea())
    notes = forms.CharField(required = False, widget=forms.Textarea())
    references = forms.CharField(required = False, widget=forms.Textarea())
    url = forms.CharField(required = True) 
    class Meta:
        model = Cave
        exclude = ("filename",)


    def clean(self):
        if self.cleaned_data.get("kataster_number") == "" and self.cleaned_data.get("unofficial_number") == "":
            self._errors["unofficial_number"] = self.error_class(["Either the kataster or unoffical number is required."]) 
        if self.cleaned_data.get("kataster_number") != "" and self.cleaned_data.get("official_name") == "":
            self._errors["official_name"] = self.error_class(["This field is required when there is a kataster number."]) 
        if self.cleaned_data.get("area") == []:
            self._errors["area"] = self.error_class(["This field is required."])
        if self.cleaned_data.get("url") and self.cleaned_data.get("url").startswith("/"):
            self._errors["url"] = self.error_class(["This field can not start with a /."])
        return self.cleaned_data

class VersionControlCommentForm(forms.Form):
    description_of_change = forms.CharField(required = True, widget=forms.Textarea())


class EntranceForm(ModelForm):
    #underground_description = forms.CharField(required = False, widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
    #explorers = forms.CharField(required = False, widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
    #equipment = forms.CharField(required = False, widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
    #survey = forms.CharField(required = False, widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
    #kataster_status = forms.CharField(required = False, widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
    #underground_centre_line = forms.CharField(required = False, widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
    #notes = forms.CharField(required = False, widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
    #references = forms.CharField(required = False, widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
    other_station = forms.CharField(required=False) # Trying to change this to a singl;e line entry
    tag_station = forms.CharField(required=False) # Trying to change this to a singl;e line entry
    exact_station = forms.CharField(required=False) # Trying to change this to a singl;e line entry
    northing = forms.CharField(required=False) # Trying to change this to a singl;e line entry
    easting = forms.CharField(required=False) # Trying to change this to a singl;e line entry
    alt = forms.CharField(required=False) # Trying to change this to a singl;e line entry
    class Meta:
        model = Entrance
        exclude = ("cached_primary_slug", "filename",)
    def clean(self):
        if self.cleaned_data.get("url").startswith("/"):
            self._errors["url"] = self.error_class(["This field can not start with a /."])
        return self.cleaned_data



CaveAndEntranceFormSet = modelformset_factory(CaveAndEntrance, exclude=('cave',))

class EntranceLetterForm(ModelForm):
    class Meta:
        model = CaveAndEntrance
        exclude = ('cave', 'entrance')

#class PersonForm(ModelForm):
#    class Meta:
#        model = Person

#class LogbookEntryForm(ModelForm):
#    class Meta:
#        model = LogbookEntry#

#    def wikiLinkHints(LogbookEntry=None):
#        """
#        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.
#        """
#        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())
	
#        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>')
	
#	return string.join(res, r'<br />')

#    def __init__(self, *args, **kwargs):
#	super(LogbookEntryForm, self).__init__(*args, **kwargs)
#        self.fields['text'].help_text=self.wikiLinkHints()#

#class CaveForm(forms.Form):
#    html = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30})) 

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):
        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, default=False)

    PersonTripFormSet = formset_factory(PersonTripForm, extra=1)

    return PersonTripFormSet, TripForm
    
def get_name(pe):
            if pe.nickname:
                return pe.nickname
            else:
                return pe.person.first_name

class UploadFileForm(forms.Form):
    title = forms.CharField(max_length=50)
    file  = forms.FileField()
    html = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
    lon_utm = forms.FloatField(required=False)
    lat_utm = forms.FloatField(required=False)
    slug = forms.CharField(max_length=50)
    date = forms.DateField(required=False)

    caves = [cave.slug for cave in Cave.objects.all()]
    caves.sort()
    caves = ["-----"] + caves
    cave = forms.ChoiceField([(c, c) for c in caves], required=False)

    entrance = forms.ChoiceField([("-----", "Please select a cave"), ], required=False) 
    qm = forms.ChoiceField([("-----", "Please select a cave"), ], required=False) 

    expeditions = [e.year for e in Expedition.objects.all()]
    expeditions.sort()
    expeditions = ["-----"] + expeditions
    expedition = forms.ChoiceField([(e, e) for e in expeditions], required=False)  

    logbookentry = forms.ChoiceField([("-----", "Please select an expedition"), ], required=False) 

    person = forms.ChoiceField([("-----", "Please select an expedition"), ], required=False) 
    
    survey_point = forms.CharField()