From 466f883d3442d8177f8550236ebe4fc50e3b3796 Mon Sep 17 00:00:00 2001 From: Nazar Kotjuk Date: Thu, 7 Nov 2013 00:23:20 +0200 Subject: [PATCH] create delete view, which check if object has related objects --- city/forms.py | 14 +++++++-- city/models.py | 6 ++-- city/urls.py | 9 +++--- city/views.py | 7 +++-- country/forms.py | 7 +++++ country/models.py | 9 ++++-- country/urls.py | 4 +-- country/views.py | 39 +++++------------------- functions/custom_views.py | 28 ++++++++++++++++- templates/admin/city/city_all.html | 3 ++ templates/admin/country/country_all.html | 3 +- templates/delete.html | 20 ++++++++++++ 12 files changed, 99 insertions(+), 50 deletions(-) create mode 100644 templates/delete.html diff --git a/city/forms.py b/city/forms.py index e0204f26..01be8d49 100644 --- a/city/forms.py +++ b/city/forms.py @@ -111,9 +111,10 @@ class CityForm(forms.Form): def clean(self): id = self.cleaned_data.get('city_id') name_ru = self.cleaned_data.get('name_ru') - c = City.objects.get(pk=id) + country = self.cleaned_data.get('country') + city = City.objects.filter( - url='%s-%s'%(translit_with_separator(c.country.name), translit_with_separator(name_ru)) + url='%s-%s'%(translit_with_separator(country.name), translit_with_separator(name_ru)) ) if city and str(city[0].id) != id: msg = 'Город с таким названием уже существует' @@ -145,4 +146,11 @@ class CityForm(forms.Form): """ cleaned_data = super(CityForm, self).clean() population = cleaned_data.get('population').strip() - return is_positive_integer(population) \ No newline at end of file + return is_positive_integer(population) + +class CityDeleteForm(forms.ModelForm): + url = forms.CharField(widget=forms.HiddenInput()) + + class Meta: + model = City + fields = ('url',) \ No newline at end of file diff --git a/city/models.py b/city/models.py index 8df1fc83..3fa6e7a7 100644 --- a/city/models.py +++ b/city/models.py @@ -32,10 +32,12 @@ class City(TranslatableModel): services = BitField(flags=flags) url = models.SlugField(unique=True) - country = models.ForeignKey('country.Country', null=True, on_delete=models.PROTECT) + #relations + country = models.ForeignKey('country.Country', null=True, on_delete=models.PROTECT, related_name='cities') + code_IATA = models.ForeignKey(Iata, blank=True, null=True) + population = models.PositiveIntegerField(blank=True, null=True) phone_code = models.PositiveIntegerField(blank=True, null=True) - code_IATA = models.ForeignKey(Iata, null=True) #translated fields translations = TranslatedFields( name = models.CharField(max_length=50), diff --git a/city/urls.py b/city/urls.py index e3730b9b..fae1d4fc 100644 --- a/city/urls.py +++ b/city/urls.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- from django.conf.urls import patterns, url -urlpatterns = patterns('', - url(r'^add/$', 'city.views.city_add'), - url(r'^change/(.*)/$', 'city.views.city_change'), - url(r'^all/$', 'city.views.city_all'), +urlpatterns = patterns('city.views', + url(r'^add/$', 'city_add'), + url(r'^delete/(?P.*)/$', 'city_delete'), + url(r'^change/(.*)/$', 'city_change'), + url(r'^all/$', 'city_all'), ) \ No newline at end of file diff --git a/city/views.py b/city/views.py index fc926079..2293396c 100644 --- a/city/views.py +++ b/city/views.py @@ -6,12 +6,12 @@ from django.conf import settings from django.contrib.auth.decorators import login_required from django.contrib.contenttypes.models import ContentType #models and forms -from forms import CityForm +from forms import CityForm, CityDeleteForm from models import City from file.models import FileModel from file.forms import FileModelForm #custom views -from functions.custom_views import objects_list, add_object_with_file +from functions.custom_views import objects_list, add_object_with_file, delete_object def city_all(request): @@ -28,6 +28,9 @@ def city_add(request): """ return add_object_with_file(request, CityForm, 'city_add.html', '/city/all') +def city_delete(request, url): + return delete_object(request, City, CityDeleteForm, url, '/city/all/') + @login_required def city_change(request, url): diff --git a/country/forms.py b/country/forms.py index afb89485..b7d43b91 100644 --- a/country/forms.py +++ b/country/forms.py @@ -225,3 +225,10 @@ class CountryForm(forms.Form): cleaned_data = super(CountryForm, self).clean() time_delivery = cleaned_data.get('time_delivery').strip() return is_positive_integer(time_delivery) + +class CountryDeleteForm(forms.ModelForm): + url = forms.CharField(widget=forms.HiddenInput()) + + class Meta: + model = Country + fields = ('url',) \ No newline at end of file diff --git a/country/models.py b/country/models.py index e9c5a4b4..9c439435 100644 --- a/country/models.py +++ b/country/models.py @@ -31,14 +31,17 @@ class Country(TranslatableModel): REGIONS =('europa', 'asia', 'america', 'africa') url = models.SlugField(unique=True) + #relations + big_cities = models.ManyToManyField(City, blank=True, null=True, related_name='cities') capital = models.ForeignKey(City,blank=True, null=True, on_delete=models.PROTECT, related_name='capital') + language = models.ManyToManyField(Language, blank=True, null=True) + currency = models.ManyToManyField(Currency, blank=True, null=True) + population = models.PositiveIntegerField(blank=True, null=True) teritory = models.PositiveIntegerField(blank=True, null=True) timezone = models.FloatField(blank=True, null=True) phone_code = models.PositiveIntegerField(blank=True, null=True) - big_cities = models.ManyToManyField(City, blank=True, null=True, related_name='cities') - language = models.ManyToManyField(Language, blank=True, null=True) - currency = models.ManyToManyField(Currency, blank=True, null=True) + time_delivery = models.PositiveSmallIntegerField(blank=True, null=True) # region = EnumField(values=REGIONS) diff --git a/country/urls.py b/country/urls.py index 03ce9be1..dd9dfc0a 100644 --- a/country/urls.py +++ b/country/urls.py @@ -3,7 +3,7 @@ from django.conf.urls import patterns, include, url urlpatterns = patterns('country.views', url(r'^add.*/$', 'country_add'), - url(r'^delete/(.*)/$', 'country_delete'), - url(r'^change/(.*)/$', 'country_change'), + url(r'^delete/(?P.*)/$', 'country_delete'), + url(r'^change/(?P.*)/$', 'country_change'), url(r'^all/$', 'country_all'), ) \ No newline at end of file diff --git a/country/views.py b/country/views.py index 210c79d3..4c685af7 100644 --- a/country/views.py +++ b/country/views.py @@ -9,11 +9,14 @@ from django.db.models.loading import get_model from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage #models and forms from models import Country -from forms import CountryForm +from forms import CountryForm, CountryDeleteForm from file.models import FileModel, TmpFile from file.forms import FileModelForm #custom views -from functions.custom_views import objects_list, add_object_with_file +from functions.custom_views import objects_list, add_object_with_file, delete_object + +from django.db.models.deletion import ProtectedError + def country_all(request): """ @@ -28,37 +31,9 @@ def country_add(request): """ return add_object_with_file(request, CountryForm, 'country_add.html', '/country/all/') -def country_delete(request, url): - try: - c = Country.objects.get(url=url) - country_id = getattr(c, 'id') - except: - return HttpResponseRedirect('/country/all') - c.delete() - - return objects_list(request, Country, 'country_all.html') - - - -def objects_list(request, Model, template, item_per_page=10): - """ - Return template with all objects of model Model - Model - objects Model - item_per_page - how many objects view in the one page - """ - list = Model.objects.all() - paginator = Paginator(list, item_per_page) - page = request.GET.get('page') - try: - objects = paginator.page(page) - except PageNotAnInteger: - # If page is not an integer, deliver first page. - objects = paginator.page(1) - except EmptyPage: - # If page is out of range (e.g. 9999), deliver last page of results. - objects = paginator.page(paginator._num_pages) - return render_to_response(template, {'objects': objects}) +def country_delete(request, url): + return delete_object(request, Country, CountryDeleteForm, url, '/country/all/') @login_required diff --git a/functions/custom_views.py b/functions/custom_views.py index 56fd3277..6b874f9b 100644 --- a/functions/custom_views.py +++ b/functions/custom_views.py @@ -5,6 +5,7 @@ from django.core.context_processors import csrf from django.conf import settings from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage +from django.db.models.deletion import ProtectedError #forms and models from file.forms import FileModelForm from file.models import TmpFile @@ -123,4 +124,29 @@ def add_object_with_file(request, Form, template, redirect_string, choices={}): #list of files connected to this form args['files'] = TmpFile.objects.filter(key=key) - return render_to_response(template, args) \ No newline at end of file + return render_to_response(template, args) + +@login_required +def delete_object(request, Model, Form, url, prev_page,): + if request.GET: + url = request.GET.get('url') + object = Model.objects.get(url=url) + try: + object.delete() + return HttpResponseRedirect(prev_page) + except ProtectedError: + msg = 'Удаления %s требует удаления связаных объектов'%object + return render_to_response('delete.html', {'msg':msg, 'prev_page':prev_page}) + + else: + object = Model.objects.get(url=url) + form = Form(instance=object) + + args = {} + args.update(csrf(request)) + + args['form'] = form + args['country'] = object + args['prev_page'] = prev_page + + return render_to_response('delete.html', args) \ No newline at end of file diff --git a/templates/admin/city/city_all.html b/templates/admin/city/city_all.html index ac9dbac7..1eb21665 100644 --- a/templates/admin/city/city_all.html +++ b/templates/admin/city/city_all.html @@ -29,6 +29,9 @@ Displays lists of all cities in the table Изменить + + Удалить + {% endfor %} diff --git a/templates/admin/country/country_all.html b/templates/admin/country/country_all.html index 97ee386f..ee221fe8 100644 --- a/templates/admin/country/country_all.html +++ b/templates/admin/country/country_all.html @@ -31,7 +31,7 @@ Displays lists of all countries in the table Изменить - + Удалить @@ -72,5 +72,6 @@ Displays lists of all countries in the table + {% endblock %} diff --git a/templates/delete.html b/templates/delete.html new file mode 100644 index 00000000..ba475426 --- /dev/null +++ b/templates/delete.html @@ -0,0 +1,20 @@ +{% extends 'base.html' %} + +{% block body %} + {% if form %} +
+

Вы действительно хотите удалить {{ object }}?


+ {{ form.url }} + + + Нет +
+ {% else %} + {{ msg }} +
+ Вернуться + + + {% endif %} + +{% endblock %} \ No newline at end of file