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 #}
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 #}
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 #}
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 #}