You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

88 lines
3.2 KiB

# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response, get_object_or_404
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.contrib.contenttypes.models import ContentType
#models and forms
from forms import ArticleForm, Article
from theme.models import Tag
from file.models import FileModel, TmpFile
from file.forms import FileModelForm
#custom views
from functions.custom_views import objects_list, add_object_with_file
def article_all(request):
"""
Return list of all articles with pagination
"""
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.
If form is posted redirect on the page of all articles.
"""
try:
#check if article_id exists else redirect to the list of cities
article = Article.objects.get(url=url)
file_form = FileModelForm(initial={'model': 'article.Article'})
except:
return HttpResponseRedirect('/article/all')
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(getattr(article, 'id'))
return HttpResponseRedirect('/article/all')
else:
data = {}
#fill form with data from database
data['author'] = article.author
data['theme'] = [item.id for item in article.theme.all()]
data['tag'] = [item.id for item in article.tag.all()]
#data from translated fields
for code, name in settings.LANGUAGES:
obj = Article._meta.translations_model.objects.get(language_code = code,master__id=getattr(article, 'id')) #access to translated fields
data['main_title_%s' % code] = obj.main_title
data['preview_%s' % code] = obj.preview
data['description_%s' % code] = obj.description
data['title_%s' % code] = obj.title
data['keywords_%s' % code] = obj.keywords
data['descriptions_%s' % code] = obj.descriptions
#fill form
form = ArticleForm(initial=data)
#set choices filled by ajax
form.fields['tag'].choices = [(item.id, item.name) for item in Tag.objects.filter(theme__in=data['theme'])]
args = {}
args.update(csrf(request))
args['form'] = form
args['languages'] = settings.LANGUAGES
args['file_form'] = file_form
#get list of files which connected with specific model object
args['files'] = FileModel.objects.filter(content_type=ContentType.objects.get_for_model(article), object_id=getattr(article, 'id'))
args['obj_id'] = getattr(article, 'id')
return render_to_response('article_add.html', args)