From 25a73c4ab7a23d87621c0d90b9c26c69361e8a6a Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 14 Aug 2012 22:51:15 +0200 Subject: [PATCH] Bug fixing of cave and entrance forms removal of slugs --- core/forms.py | 17 +++++++++++-- core/models.py | 6 ++--- core/views_caves.py | 48 ++++++++++++++++++++++++++--------- parsers/caves.py | 2 ++ templates/cave_entrances.html | 4 +-- templates/editentrance.html | 2 +- templates/prospecting.html | 4 +-- urls.py | 4 +-- 8 files changed, 63 insertions(+), 24 deletions(-) diff --git a/core/forms.py b/core/forms.py index da16d79..75814ee 100644 --- a/core/forms.py +++ b/core/forms.py @@ -16,12 +16,21 @@ class CaveForm(ModelForm): 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()) - slug = forms.CharField(required = True) 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."]) + return self.cleaned_data + class VersionControlCommentForm(forms.Form): description_of_change = forms.CharField(required = True, widget=forms.Textarea()) @@ -41,7 +50,6 @@ class EntranceForm(ModelForm): 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 - slug = forms.CharField() class Meta: model = Entrance exclude = ("cached_primary_slug", "filename",) @@ -50,6 +58,11 @@ class EntranceForm(ModelForm): CaveAndEntranceFormSet = modelformset_factory(CaveAndEntrance, exclude=('cave')) +class EntranceLetterForm(ModelForm): + class Meta: + model = CaveAndEntrance + exclude = ('cave', 'entrance') + #class PersonForm(ModelForm): # class Meta: # model = Person diff --git a/core/models.py b/core/models.py index 1c86dd5..180ac9d 100644 --- a/core/models.py +++ b/core/models.py @@ -571,13 +571,13 @@ class Entrance(TroggleModel): def find_location(self): if self.tag_station: s = SurvexStation.objects.lookup(self.tag_station) - return "%sE %sN %sAlt" % (s.x, s.y, s.z) + return "%0.0fE %0.0fN %0.0fAlt" % (s.x, s.y, s.z) if self.exact_station: s = SurvexStation.objects.lookup(self.exact_station) - return "%sE %sN %sAlt" % (s.x, s.y, s.z) + return "%0.0fE %0.0fN %0.0fAlt" % (s.x, s.y, s.z) if self.other_station: s = SurvexStation.objects.lookup(self.other_station) - return "%sE %sN %sAlt %s" % (s.x, s.y, s.z, self.other_description) + return "%0.0fE %0.0fN %0.0fAlt %s" % (s.x, s.y, s.z, self.other_description) if self.bearings: return self.bearings diff --git a/core/views_caves.py b/core/views_caves.py index 2590cf1..16dbaac 100644 --- a/core/views_caves.py +++ b/core/views_caves.py @@ -1,5 +1,5 @@ from troggle.core.models import CaveSlug, Cave, CaveAndEntrance, Survey, Expedition, QM, CaveDescription, EntranceSlug, Entrance, Area -from troggle.core.forms import CaveForm, CaveAndEntranceFormSet, VersionControlCommentForm, EntranceForm +from troggle.core.forms import CaveForm, CaveAndEntranceFormSet, VersionControlCommentForm, EntranceForm, EntranceLetterForm import troggle.core.models as models import troggle.settings as settings from troggle.helper import login_required_if_public @@ -104,11 +104,21 @@ def edit_cave(request, slug=None): versionControlForm = VersionControlCommentForm(request.POST) if form.is_valid() and ceFormSet.is_valid() and versionControlForm.is_valid(): cave = form.save(commit = False) - cave.filename = form.cleaned_data["slug"] + ".html" + if slug is None: + for a in form.cleaned_data["area"]: + if a.kat_area(): + myArea = a.kat_area() + if form.cleaned_data["kataster_number"]: + myslug = "%s-%s" % (myArea, form.cleaned_data["kataster_number"]) + else: + myslug = "%s-%s" % (myArea, form.cleaned_data["unofficial_number"]) + else: + myslug = slug + cave.filename = myslug + ".html" cave.save() form.save_m2m() if slug is None: - cs = CaveSlug(cave = cave, slug = form.cleaned_data["slug"], primary = True) + cs = CaveSlug(cave = cave, slug = myslug, primary = True) cs.save() ceinsts = ceFormSet.save(commit=False) for ceinst in ceinsts: @@ -129,7 +139,8 @@ def edit_cave(request, slug=None): }) @login_required_if_public -def editEntrance(request, slug=None): +def editEntrance(request, caveslug, slug=None): + cave = Cave.objects.get(caveslug__slug = caveslug) if slug is not None: entrance = Entrance.objects.get(entranceslug__slug = slug) else: @@ -137,25 +148,38 @@ def editEntrance(request, slug=None): if request.POST: form = EntranceForm(request.POST, instance = entrance) versionControlForm = VersionControlCommentForm(request.POST) - if form.is_valid() and versionControlForm.is_valid(): + if slug is None: + entletter = EntranceLetterForm(request.POST) + else: + entletter = None + if form.is_valid() and versionControlForm.is_valid() and (slug is not None or entletter.is_valid()): entrance = form.save(commit = False) - entrance.filename = form.cleaned_data["slug"] + ".html" if slug is None: - entrance.cached_primary_slug = form.cleaned_data["slug"] + slugname = cave.slug() + entletter.cleaned_data["entrance_letter"] + entrance.cached_primary_slug = slugname + entrance.filename = slugname + ".html" entrance.save() if slug is None: - es = EntranceSlug(entrance = entrance, slug = form.cleaned_data["slug"], primary = True) - es.save() + es = EntranceSlug(entrance = entrance, slug = slugname, primary = True) + es.save() + el = entletter.save(commit = False) + el.cave = cave + el.entrance = entrance + el.save() entrance.writeDataFile() - return HttpResponseRedirect("/" + entrance.url) + return HttpResponseRedirect("/" + cave.url) else: form = EntranceForm(instance = entrance) versionControlForm = VersionControlCommentForm() - + if slug is None: + entletter = EntranceLetterForm(request.POST) + else: + entletter = None return render_with_context(request, 'editentrance.html', {'form': form, - 'versionControlForm': versionControlForm + 'versionControlForm': versionControlForm, + 'entletter': entletter }) def qm(request,cave_id,qm_id,year,grade=None): diff --git a/parsers/caves.py b/parsers/caves.py index 1c8bf2f..d92483d 100644 --- a/parsers/caves.py +++ b/parsers/caves.py @@ -8,6 +8,8 @@ import re def readcaves(): newArea = models.Area(short_name = "1623", parent = None) newArea.save() + newArea = models.Area(short_name = "1626", parent = None) + newArea.save() print "Entrances" for filename in os.walk(settings.ENTRANCEDESCRIPTIONS).next()[2]: #Should be a better way of getting a list of files readentrance(filename) diff --git a/templates/cave_entrances.html b/templates/cave_entrances.html index 6468ebc..cb58a0a 100644 --- a/templates/cave_entrances.html +++ b/templates/cave_entrances.html @@ -7,7 +7,7 @@ {{ ent.entrance_letter|safe }} {% if ent.entrance.name %} {{ ent.entrance.name|safe }} - {% endif %}Edit + {% endif %}Edit
{% if ent.entrance.marking %}
Marking
{{ ent.entrance.marking_val|safe }}
@@ -67,5 +67,5 @@ {% endif %}

-New Entrance +New Entrance diff --git a/templates/editentrance.html b/templates/editentrance.html index 3b0e12b..e772a11 100644 --- a/templates/editentrance.html +++ b/templates/editentrance.html @@ -10,7 +10,7 @@ {% endblock %} {% block content %}
{% csrf_token %} - {{ form }}
+ {{entletter}}{{ form }}
{{ versionControlForm }}

diff --git a/templates/prospecting.html b/templates/prospecting.html index c329e5c..0d7a0d6 100644 --- a/templates/prospecting.html +++ b/templates/prospecting.html @@ -14,7 +14,7 @@ {% for name, area in areas %}

{{name|safe}}

- + @@ -31,7 +31,7 @@ {% else %} diff --git a/urls.py b/urls.py index 35d8dad..99313fa 100644 --- a/urls.py +++ b/urls.py @@ -51,8 +51,8 @@ actualurlpatterns = patterns('', url(r'^cave/description/([^/]+)/?$', views_caves.caveDescription), url(r'^cave/qms/([^/]+)/?$', views_caves.caveQMs), url(r'^cave/logbook/([^/]+)/?$', views_caves.caveLogbook), - url(r'^entrance/(?P[^/]+)/edit/', views_caves.editEntrance, name = "editentrance"), - url(r'^entrance/new/', views_caves.editEntrance, name = "newentrance"), + url(r'^entrance/(?P[^/]+)/(?P[^/]+)/edit/', views_caves.editEntrance, name = "editentrance"), + url(r'^entrance/new/(?P[^/]+)/', views_caves.editEntrance, name = "newentrance"), #url(r'^cavedescription/(?P[^/]+)/?$', views_caves.cave_description, name="cavedescription"), #url(r'^cavedescription/?$', object_list, {'queryset':CaveDescription.objects.all(),'template_name':'object_list.html'}, name="cavedescriptions"), #url(r'^cavehref/(.+)$', views_caves.cave, name="cave"),url(r'cave'),
Cave NumberNameFinishedSurvey
Data
Survey
Drawn
MarkedPhotoPositionLocation
{{ cave.caveandentrance_set.all.0.entrance.has_photo }} {{ cave.caveandentrance_set.all.0.entrance.find_location }} {% if cave.caveandentrance_set.all.0.entrance.location_description %}Location: {{ cave.caveandentrance_set.all.0.entrance.location_description|safe }}{% endif %} - {% if cave.caveandentrance_set.all.0.entrance.map_description %}Map: {{ cave.caveandentrance_set.all.0.entrance.map_description|safe }}{% endif %} + {% if cave.caveandentrance_set.all.0.entrance.map_description %}Map: {{ cave.caveandentrance_set.all.0.entrance.map_description|safe }}{% endif %} {% if cave.caveandentrance_set.all.0.entrance.approach %}Approach: {{ cave.caveandentrance_set.all.0.entrance.approach|safe }}{% endif %}