From b89ccb351624330a94e25ebec15700cd403ef214 Mon Sep 17 00:00:00 2001 From: Nazar Kotjuk Date: Mon, 28 Oct 2013 14:59:26 +0200 Subject: [PATCH] create views 'add_object' and 'add_object_with_file' to custom views and add it to all views --- .gitignore | 4 ++ article/templates/article_add.html | 2 +- article/views.py | 50 ++++----------- city/templates/city_add.html | 4 +- city/views.py | 37 ++--------- company/templates/company_add.html | 4 +- company/views.py | 34 +---------- conference/templates/conference_add.html | 4 +- country/tamplates/country_add.html | 4 +- country/views.py | 40 ++---------- directories/views.py | 50 ++------------- functions/custom_views.py | 78 ++++++++++++++++++++++++ news/views.py | 17 ++++-- organiser/templates/organiser_add.html | 4 +- organiser/views.py | 34 ++--------- theme/views.py | 32 +--------- 16 files changed, 145 insertions(+), 253 deletions(-) diff --git a/.gitignore b/.gitignore index f4b6c0aa..aba19a52 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,8 @@ *.[oa] *~ *.egg-info +*.jpg +*.gif +*.doc .idea/ + diff --git a/article/templates/article_add.html b/article/templates/article_add.html index 855b41dd..c32e86b1 100644 --- a/article/templates/article_add.html +++ b/article/templates/article_add.html @@ -160,7 +160,7 @@ {# file purpose #}
- +
{{ file_form.purpose }} {{ file_form.purpose.errors }}
diff --git a/article/views.py b/article/views.py index 9eb99060..f06d6069 100644 --- a/article/views.py +++ b/article/views.py @@ -10,48 +10,10 @@ from forms import ArticleForm, Article, ArticleChangeForm from theme.models import Tag from file.models import FileModel, TmpFile from file.forms import FileModelForm -#pythom -import random #custom views -from functions.custom_views import objects_list +from functions.custom_views import objects_list, add_object_with_file -@login_required -def article_add(request): - """ - Return form of article and post it on the server. - If form is posted redirect on the page of all articles. - """ - #if form would be not valid key must be same - if request.POST.get('key'): - key = request.POST['key'] - else: - key = random.getrandbits(128) - - file_form = FileModelForm(initial={'key': key}) - - if request.POST: - form = ArticleForm(request.POST) - #set choices filled by ajax - form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.all()] - - if form.is_valid(): - form.save() - return HttpResponseRedirect('/article/all') - else: - form = ArticleForm(initial={'key': key}) - - args = {} - args.update(csrf(request)) - - args['form'] = form - args['languages'] = settings.LANGUAGES - args['file_form'] = file_form - args['files'] = TmpFile.objects.filter(key=key) - - return render_to_response('article_add.html', args) - -@login_required def article_all(request): """ Return list of all articles with pagination @@ -59,7 +21,17 @@ def article_all(request): return objects_list(request, Article, 'article_all.html') +def article_add(request): + """ + Return form of article and post it on the server. + + If form is posted redirect on the page of all articles. + """ + return add_object_with_file(request, ArticleForm, 'article_add.html', '/article/all', + {'tag': Tag}) + +@login_required def article_change(request, url): """ Return form and fill it with existing Article object data. diff --git a/city/templates/city_add.html b/city/templates/city_add.html index b9c1c2e9..ab2fa300 100644 --- a/city/templates/city_add.html +++ b/city/templates/city_add.html @@ -166,14 +166,14 @@ {# file_path #}
- +
{{ file_form.file_path }} {{ file_form.file_path.errors }}
{# file purpose #}
- +
{{ file_form.purpose }} {{ file_form.purpose.errors }}
diff --git a/city/views.py b/city/views.py index e68fc1b5..cf866de0 100644 --- a/city/views.py +++ b/city/views.py @@ -8,50 +8,26 @@ from django.contrib.contenttypes.models import ContentType #models and forms from forms import CityForm, CityChangeForm from models import City -from file.models import FileModel, TmpFile +from file.models import FileModel from file.forms import FileModelForm -#python -import random #custom views -from functions.custom_views import objects_list +from functions.custom_views import objects_list, add_object_with_file def city_all(request): """ return list of all cities with pagination """ - return objects_list(request, City, 'city_all.html', 1) + return objects_list(request, City, 'city_all.html') + -@login_required def city_add(request): """ Return form of city and post it on the server. If form is posted redirect on the page of all cities. """ - #if form would be not valid key must be same - if request.POST.get('key'): - key = request.POST['key'] - else: - key = random.getrandbits(128) - - file_form = FileModelForm(initial={'key': key}) - - if request.POST: - form = CityForm(request.POST) - if form.is_valid(): - form.save() - return HttpResponseRedirect('/city/all') - else: - form = CityForm(initial={'key': key}) - - args = {} - args.update(csrf(request)) - args['languages'] = settings.LANGUAGES - args['form'] = form - args['file_form'] = file_form - args['files'] = TmpFile.objects.filter(key=key) + return add_object_with_file(request, CityForm, 'city_add.html', '/city/all') - return render_to_response('city_add.html', args) @login_required def city_change(request, url): @@ -110,5 +86,4 @@ def city_change(request, url): object_id=getattr(c, 'id')) args['obj_id'] = city_id - return render_to_response('city_add.html', args) - + return render_to_response('city_add.html', args) \ No newline at end of file diff --git a/company/templates/company_add.html b/company/templates/company_add.html index 5b5f4b07..4c1e251f 100644 --- a/company/templates/company_add.html +++ b/company/templates/company_add.html @@ -235,14 +235,14 @@ {# file_path #}
- +
{{ file_form.file_path }} {{ file_form.file_path.errors }}
{# file purpose #}
- +
{{ file_form.purpose }} {{ file_form.purpose.errors }}
diff --git a/company/views.py b/company/views.py index 3262a310..e2d5b82b 100644 --- a/company/views.py +++ b/company/views.py @@ -12,10 +12,8 @@ from theme.models import Tag from city.models import City from file.models import FileModel, TmpFile from file.forms import FileModelForm -#python -import random #custom views -from functions.custom_views import objects_list +from functions.custom_views import objects_list, add_object_with_file def company_all(request): @@ -31,35 +29,9 @@ def company_add(request): Return form of company and post it on the server. If form is posted redirect on the page of all companies. """ - #if form would be not valid key must be same - if request.POST.get('key'): - key = request.POST['key'] - else: - key = random.getrandbits(128) - - file_form = FileModelForm(initial={'key': key}) - - if request.POST: - form = CompanyForm(request.POST) - #set choices filled by ajax - form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.all()] - form.fields['city'].choices = [(item.id, item.name) for item in City.objects.filter(country=request.POST['country'])] - - if form.is_valid(): - form.save() - return HttpResponseRedirect('/company/all/') - else: - form = CompanyForm(initial={'key': key}) - - args = {} - args.update(csrf(request)) + return add_object_with_file(request, CompanyForm, 'company_add.html', '/company/all/', + {'city': City, 'tag': Tag}) - args['form'] = form - args['languages'] = settings.LANGUAGES - args['file_form'] = file_form - args['files'] = TmpFile.objects.filter(key=key) - - return render_to_response('company_add.html', args) @login_required def company_change(request, company_id): diff --git a/conference/templates/conference_add.html b/conference/templates/conference_add.html index 3704478e..fdf2288e 100644 --- a/conference/templates/conference_add.html +++ b/conference/templates/conference_add.html @@ -329,14 +329,14 @@ {# file_path #}
- +
{{ file_form.file_path }} {{ file_form.file_path.errors }}
{# file purpose #}
- +
{{ file_form.purpose }} {{ file_form.purpose.errors }}
diff --git a/country/tamplates/country_add.html b/country/tamplates/country_add.html index b55efc72..3a470126 100644 --- a/country/tamplates/country_add.html +++ b/country/tamplates/country_add.html @@ -236,14 +236,14 @@ {# file_path #}
- +
{{ file_form.file_path }} {{ file_form.file_path.errors }}
{# file purpose #}
- +
{{ file_form.purpose }} {{ file_form.purpose.errors }}
diff --git a/country/views.py b/country/views.py index 0f2d38b7..caed66bf 100644 --- a/country/views.py +++ b/country/views.py @@ -12,51 +12,21 @@ from models import Country from forms import CountryForm, CountryChangeForm from file.models import FileModel, TmpFile from file.forms import FileModelForm -#python -import random -from functions.custom_views import objects_list +#custom views +from functions.custom_views import objects_list, add_object_with_file def country_all(request): """ Return list of all countries with pagination """ - return objects_list(request, Country, 'country_all.html', 1) + return objects_list(request, Country, 'country_all.html') + -@login_required def country_add(request): """ Return form of country and file and post it on the server. - Create key which will be check tmp files - If form is posted redirect on the page of all countries. - FileForm posts with ajax """ - #cheks if key already exist(when form wasn't validated) - if request.POST.get('key'): - key = request.POST['key'] - else: - key = random.getrandbits(128) - - file_form = FileModelForm(initial={'key': key}) - - if request.POST: - form = CountryForm(request.POST, request.FILES) - if form.is_valid(): - form.save() - return HttpResponseRedirect('/country/all/') - - else: - form = CountryForm(initial={'key': key}) - - args = {} - - args.update(csrf(request)) - args['languages'] = settings.LANGUAGES - args['form'] = form - args['file_form'] = file_form - args['files'] = TmpFile.objects.filter(key=key) - - - return render_to_response('country_add.html', args) + return add_object_with_file(request, CountryForm, 'country_add.html', '/country/all/') @login_required diff --git a/directories/views.py b/directories/views.py index fbf9dfea..ca3b1bb7 100644 --- a/directories/views.py +++ b/directories/views.py @@ -1,56 +1,18 @@ # -*- coding: utf-8 -*- -from django.shortcuts import render_to_response -from django.http import HttpResponseRedirect, HttpResponse from forms import LanguageForm, CurrencyForm, IataForm -from django.core.context_processors import csrf +#custom views +from functions.custom_views import add_object -from models import Language, Currency -from django.conf import settings -from django.contrib.auth.decorators import login_required - -@login_required def language_add(request): """ - Returns LanguageForm and post it on the server - - If form is posted redirects on the list of all countries + Return LanguageForm and post it on the server """ - if request.POST: - form = LanguageForm(request.POST) - if form.is_valid(): - form.save() - return HttpResponseRedirect('/language/add') - - else: - form = LanguageForm() - - args = {} - args.update(csrf(request)) - args['languages'] = settings.LANGUAGES - args['form'] = form + return add_object(request, LanguageForm, 'directories_add.html', '/language/add') - return render_to_response('directories_add.html', args) -@login_required def currency_add(request): """ - Returns CurrencyForm and post it on the server - - If form is posted redirects on the list of all countries + Return CurrencyForm and post it on the server """ - if request.POST: - form = CurrencyForm(request.POST) - if form.is_valid(): - form.save() - return HttpResponseRedirect('/currency/add') - - else: - form = CurrencyForm() - - args = {} - args.update(csrf(request)) - args['languages'] = settings.LANGUAGES - args['form'] = form - - return render_to_response('directories_add.html', args) \ No newline at end of file + return add_object(request, CurrencyForm, 'directories_add.html', '/currency/add') \ No newline at end of file diff --git a/functions/custom_views.py b/functions/custom_views.py index 39304813..56fd3277 100644 --- a/functions/custom_views.py +++ b/functions/custom_views.py @@ -1,7 +1,17 @@ # -*- coding: utf-8 -*- from django.shortcuts import render_to_response +from django.http import HttpResponseRedirect +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 +#forms and models +from file.forms import FileModelForm +from file.models import TmpFile +from theme.models import Tag +#python +import random + @login_required @@ -44,5 +54,73 @@ def filtered_list(request, objects, template, item_per_page=10): return render_to_response(template, {'objects': objects}) +@login_required +def add_object(request, Form, template_string, redirect_string, + choices={}): + if request.POST: + form = Form(request.POST) + #set choices filled by ajax + if 'city' in choices: + form.fields['city'].choices = [(item.id, item.name) for item in choices['city'].objects.filter(country=request.POST['country'])] + if 'tag' in choices: + form.fields['tag'].choices = [(item.id, item.name) for item in choices['tag'].objects.all()] + + if form.is_valid(): + form.save() + return HttpResponseRedirect(redirect_string) + else: + form = Form() + + args = {} + args.update(csrf(request)) + + args['languages'] = settings.LANGUAGES + args['form'] = form + return render_to_response(template_string, args) + + +@login_required +def add_object_with_file(request, Form, template, redirect_string, choices={}): + """ + Return form and FileForm and post it on the server. + + Create key which will be check tmp files + + If form is posted redirect on the page in redirect_string + + FileForm posts with ajax + + choices are ... + """ + #cheks if key already exist(when form wasn't validated) + if request.POST.get('key'): + key = request.POST['key'] + else: + key = random.getrandbits(128) + #initial FileForm with key for checking if file connected to object + file_form = FileModelForm(initial={'key': key}) + + if request.POST: + form = Form(request.POST) + #set choices filled by ajax + if 'city' in choices: + form.fields['city'].choices = [(item.id, item.name) for item in choices['city'].objects.filter(country=request.POST['country'])] + if 'tag' in choices: + form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.all()] + + if form.is_valid(): + form.save() + return HttpResponseRedirect(redirect_string) + else: + form = Form(initial={'key':key}) + args = {} + args.update(csrf(request)) + #languages uses in template + args['languages'] = settings.LANGUAGES + args['form'] = form + args['file_form'] = file_form + #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 diff --git a/news/views.py b/news/views.py index 9eba4d74..012b4d04 100644 --- a/news/views.py +++ b/news/views.py @@ -46,7 +46,11 @@ def news_add(request): form = NewsForm(request.POST) #set choices filled by ajax form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.all()] - form.fields['event_id'].choices = [(item.id, item.name) for item in Exposition.objects.all()] + + if request.POST['event'] == 'exposition.Exposition': + form.fields['event_id'].choices = [(item.id, item.name) for item in Exposition.objects.all()] + if request.POST['event'] == 'conference.Conference': + form.fields['event_id'].choices = [(item.id, item.name) for item in Conference.objects.all()] if form.is_valid(): form.save() @@ -83,7 +87,12 @@ def news_change(request, url): form = NewsChangeForm(request.POST) #set choices filled by ajax form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.all()] - form.fields['event_id'].choices = [(item.id, item.name) for item in Exposition.objects.all()] + + if request.POST['event'] == 'exposition.Exposition': + form.fields['event_id'].choices = [(item.id, item.name) for item in Exposition.objects.all()] + if request.POST['event'] == 'conference.Conference': + form.fields['event_id'].choices = [(item.id, item.name) for item in Conference.objects.all()] + if form.is_valid(): form.save(news_id) return HttpResponseRedirect('/news/all') @@ -120,9 +129,9 @@ def news_change(request, url): #set choices filled by ajax form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.filter(theme__in=data['theme'])] if data.get('event'): - if data['event']=='conference.Conference': + if data['event'] == 'conference.Conference': events = [(item.id, item.name) for item in Conference.objects.all()] - elif data['event']=='exposition.Exposition': + elif data['event'] == 'exposition.Exposition': events = [(item.id, item.name) for item in Exposition.objects.all()] form.fields['event_id'].choices = events diff --git a/organiser/templates/organiser_add.html b/organiser/templates/organiser_add.html index 846b1672..2a9089a4 100644 --- a/organiser/templates/organiser_add.html +++ b/organiser/templates/organiser_add.html @@ -258,14 +258,14 @@ {# file_path #}
- +
{{ file_form.file_path }} {{ file_form.file_path.errors }}
{# file purpose #}
- +
{{ file_form.purpose }} {{ file_form.purpose.errors }}
diff --git a/organiser/views.py b/organiser/views.py index 8dc2ff30..500a445b 100644 --- a/organiser/views.py +++ b/organiser/views.py @@ -15,7 +15,7 @@ from file.forms import FileModelForm #python import random #custom functions -from functions.custom_views import objects_list +from functions.custom_views import objects_list, add_object_with_file def organiser_all(request): @@ -25,40 +25,16 @@ def organiser_all(request): return objects_list(request, Organiser, 'organiser_all.html') + + def organiser_add(request): """ Return form of organiser and post it on the server. If form is posted redirect on the page of all organiser. """ - #if form would be not valid key must be same - if request.POST.get('key'): - key = request.POST['key'] - else: - key = random.getrandbits(128) - - file_form = FileModelForm(initial={'key': key}) - - if request.POST: - form = OrganiserForm(request.POST) - #set choices filled by ajax - form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.all()] - form.fields['city'].choices = [(item.id, item.name) for item in City.objects.filter(country=request.POST['country'])] - - if form.is_valid(): - form.save() - return HttpResponseRedirect('/organiser/all/') - else: - form = OrganiserForm(initial={'key':key}) - - args = {} - args.update(csrf(request)) - - args['form'] = form - args['languages'] = settings.LANGUAGES - args['file_form'] = file_form - args['files'] = TmpFile.objects.filter(key=key) + return add_object_with_file(request, OrganiserForm, 'organiser_add.html', '/organiser/all/', + choices={'city': City, 'tag': Tag}) - return render_to_response('organiser_add.html', args) def organiser_change(request, organiser_id): """ diff --git a/theme/views.py b/theme/views.py index 4763d7cc..f21f0209 100644 --- a/theme/views.py +++ b/theme/views.py @@ -11,7 +11,7 @@ from django.contrib.auth.decorators import login_required from forms import ThemeForm, TagForm from models import Theme, Tag #custom views -from functions.custom_views import objects_list +from functions.custom_views import objects_list, add_object def theme_all(request): @@ -22,39 +22,13 @@ def tag_all(request): return objects_list(request, Tag, 'tag_all.html') -@login_required def theme_add(request): - if request.POST: - form = ThemeForm(request.POST) - if form.is_valid(): - form.save() - return HttpResponseRedirect('/theme/theme/all') - else: - form = ThemeForm() + return add_object(request, ThemeForm, 'theme_add.html', '/theme/theme/all') - args = {} - args.update(csrf(request)) - - args['languages'] = settings.LANGUAGES - args['form'] = form - return render_to_response('theme_add.html', args) -@login_required def tag_add(request): - if request.POST: - form = TagForm(request.POST) - if form.is_valid(): - form.save() - return HttpResponseRedirect('/theme/tag/all') - else: - form = TagForm() + return add_object(request, TagForm, 'tag_add.html', '/theme/tag/all') - args = {} - args.update(csrf(request)) - - args['languages'] = settings.LANGUAGES - args['form'] = form - return render_to_response('tag_add.html', args) @login_required def theme_change(request, theme_id=None):