Make camp and slug unique together on villages. Also fix some url errors.

This commit is contained in:
Víðir Valberg Guðmundsson 2017-03-18 15:38:12 +01:00
parent b151192d02
commit 62381a50e6
4 changed files with 29 additions and 3 deletions

View file

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-03-18 14:06
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('camps', '0020_camp_read_only'),
('villages', '0009_auto_20161229_2143'),
]
operations = [
migrations.AlterUniqueTogether(
name='village',
unique_together=set([('slug', 'camp')]),
),
]

View file

@ -9,6 +9,7 @@ class Village(UUIDModel, CampRelatedModel):
class Meta: class Meta:
ordering = ['name'] ordering = ['name']
unique_together = ('slug', 'camp')
contact = models.ForeignKey('auth.User') contact = models.ForeignKey('auth.User')
camp = models.ForeignKey('camps.Camp') camp = models.ForeignKey('camps.Camp')

View file

@ -8,6 +8,7 @@
<button type="submit" class="btn btn-danger form-control">Confirm</button> <button type="submit" class="btn btn-danger form-control">Confirm</button>
<br /> <br />
<br /> <br />
<a href="{% url 'village_detail' slug=village.slug %}" class="btn btn-default form-control">Cancel</a> <a href="{% url 'village_detail' camp_slug=village.camp.slug slug=village.slug %}"
class="btn btn-default form-control">Cancel</a>
</form> </form>
{% endblock %} {% endblock %}

View file

@ -25,7 +25,6 @@ class VillageCreateView(LoginRequiredMixin, CreateView):
model = Village model = Village
template_name = 'village_form.html' template_name = 'village_form.html'
fields = ['name', 'description', 'private'] fields = ['name', 'description', 'private']
success_url = reverse_lazy('villages:list')
def form_valid(self, form): def form_valid(self, form):
village = form.save(commit=False) village = form.save(commit=False)
@ -34,6 +33,9 @@ class VillageCreateView(LoginRequiredMixin, CreateView):
village.save() village.save()
return HttpResponseRedirect(village.get_absolute_url()) return HttpResponseRedirect(village.get_absolute_url())
def get_success_url(self):
return reverse_lazy('village_list', kwargs={"camp_slug": self.object.camp.slug})
class EnsureUserOwnsVillageMixin(SingleObjectMixin): class EnsureUserOwnsVillageMixin(SingleObjectMixin):
model = Village model = Village
@ -61,7 +63,9 @@ class VillageUpdateView(EnsureUserOwnsVillageMixin, LoginRequiredMixin, UpdateVi
class VillageDeleteView(EnsureUserOwnsVillageMixin, LoginRequiredMixin, DeleteView): class VillageDeleteView(EnsureUserOwnsVillageMixin, LoginRequiredMixin, DeleteView):
model = Village model = Village
success_url = reverse_lazy('villages:list')
template_name = 'village_confirm_delete.html' template_name = 'village_confirm_delete.html'
context_object_name = 'village' context_object_name = 'village'
def get_success_url(self):
return reverse_lazy('village_list', kwargs={"camp_slug": self.object.camp.slug})