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.
251 lines
8.6 KiB
251 lines
8.6 KiB
# -*- coding: utf-8 -*-
|
|
from django.shortcuts import render_to_response, get_object_or_404
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
|
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
|
|
from django.views.generic import DeleteView
|
|
#models and forms
|
|
from forms import ArticleForm, ArticleDeleteForm, Article, NewsForm
|
|
from theme.models import Tag
|
|
from file.models import FileModel, TmpFile
|
|
from file.forms import FileModelForm, FileForm
|
|
|
|
#custom views
|
|
from functions.custom_views import objects_list, add_object_with_file, delete_object
|
|
from functions.views_help import get_referer
|
|
|
|
|
|
class ArticleDeleteView(DeleteView):
|
|
model = Article
|
|
template_name = "admin/article/article_confirm_delete.html"
|
|
|
|
def get_success_url(self):
|
|
if self.object.type == 1:
|
|
type = "blog"
|
|
else:
|
|
type = "news"
|
|
return "/admin/article/%s/all/" % type
|
|
|
|
|
|
def article_all(request):
|
|
"""
|
|
Return list of all articles with pagination
|
|
"""
|
|
return objects_list(request, Article, 'article_all.html')
|
|
|
|
@login_required
|
|
def article_copy(request, url):
|
|
|
|
article = Article.objects.safe_get(slug=url)
|
|
if not article:
|
|
return HttpResponseRedirect(get_referer(request))
|
|
else:
|
|
article.clone()
|
|
return HttpResponseRedirect(get_referer(request))
|
|
|
|
|
|
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.
|
|
"""
|
|
#get organiser from current user
|
|
init_data = {'author':request.user.organiser}
|
|
#choices field which will be filled by ajax
|
|
choices = {'tag': Tag}
|
|
return add_object_with_file(request, ArticleForm, 'article_add.html', '/admin/article/all',
|
|
choices, init_data)
|
|
|
|
|
|
def article_delete(request, url):
|
|
return delete_object(request, Article, ArticleDeleteForm, url, '/admin/article/all')
|
|
|
|
|
|
@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(slug=url)
|
|
file_form = FileModelForm(initial={'model': 'article.Article'})
|
|
article_id = getattr(article, 'id')
|
|
except:
|
|
return HttpResponseRedirect('/admin/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('/admin/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()]
|
|
#hidden field
|
|
data['article_id'] = article_id
|
|
|
|
#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)
|
|
|
|
#-----------------------
|
|
from django.views.generic import FormView
|
|
from functions.custom_views import ListView
|
|
from forms import BlogForm
|
|
|
|
class BlogList(ListView):
|
|
model = Article
|
|
template_name = 'admin/article/article_admin_list.html'
|
|
paginate_by = settings.ADMIN_PAGINATION
|
|
|
|
def get_queryset(self):
|
|
return self.model.objects.blogs()
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(BlogList, self).get_context_data(**kwargs)
|
|
context['blog_flag'] = True
|
|
return context
|
|
|
|
|
|
class BlogView(FormView):
|
|
form_class = BlogForm
|
|
template_name = 'admin/article/blog_form.html'
|
|
success_url = '/admin/article/blog/all/'
|
|
obj = None
|
|
|
|
def set_obj(self):
|
|
slug = self.kwargs.get('slug')
|
|
if slug:
|
|
article = get_object_or_404(Article, slug=slug)
|
|
self.obj = article
|
|
else:
|
|
self.obj = None
|
|
|
|
def form_valid(self, form):
|
|
self.set_obj()
|
|
author = self.request.user
|
|
form.save(author, article=self.obj)
|
|
return HttpResponseRedirect(self.success_url)
|
|
|
|
def get_form(self, form_class):
|
|
if self.request.POST:
|
|
return super(BlogView, self).get_form(form_class)
|
|
self.set_obj()
|
|
if self.obj:
|
|
article = self.obj
|
|
data = {}
|
|
data['slug'] = article.slug
|
|
if self.obj.type == Article.blog:
|
|
data['theme'] = [item.id for item in article.blog_theme.all()]
|
|
else:
|
|
data['theme'] = [item.id for item in article.theme.all()]
|
|
if article.exposition:
|
|
data['exposition'] = article.exposition.id
|
|
if article.conference:
|
|
data['conference'] = article.conference.id
|
|
|
|
data['publish_date'] = article.publish_date
|
|
a = ','.join(['%s:%s'%(item.id, item.name) for item in article.tag.all()])
|
|
|
|
data['tag'] = ','.join(['%s:%s'%(item.id, item.name) for item in article.tag.all()])
|
|
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
|
|
form = form_class(data)
|
|
#form.fields['tag'].widget.attrs['data-init-text'] = [item.name for item in article.tag.all()]
|
|
if article.exposition:
|
|
form.fields['exposition'].widget.attrs['data-init-text'] = article.exposition.name
|
|
if article.conference:
|
|
form.fields['conference'].widget.attrs['data-init-text'] = article.conference.name
|
|
|
|
return form
|
|
else:
|
|
return form_class()
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(BlogView, self).get_context_data(**kwargs)
|
|
self.set_obj()
|
|
|
|
context['article'] = self.obj
|
|
if context['article']:
|
|
context['file_form'] = FileForm(initial={'model': 'article.Article'})
|
|
context['files'] = FileModel.objects.filter(content_type=ContentType.objects.get_for_model(context['article']),
|
|
object_id=getattr(context['article'], 'id'))
|
|
context['languages'] = settings.LANGUAGES
|
|
return context
|
|
|
|
|
|
class NewsList(ListView):
|
|
model = Article
|
|
template_name = 'article/article_admin_list.html'
|
|
paginate_by = 20
|
|
|
|
def get_queryset(self):
|
|
return self.model.objects.news()
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(NewsList, self).get_context_data(**kwargs)
|
|
context['news_flag'] = True
|
|
return context
|
|
|
|
|
|
class NewsView(BlogView):
|
|
form_class = NewsForm
|
|
template_name = 'admin/article/blog_form.html'
|
|
success_url = '/admin/article/news/all/'
|
|
obj = None
|
|
|
|
|
|
|
|
|
|
"""
|
|
from django.views.generic import CreateView
|
|
from models import Blog
|
|
from forms import BlogForm
|
|
|
|
class BlogCreate(CreateView):
|
|
model = Blog
|
|
form_class = BlogForm
|
|
template_name = 'admin/blog/blog_add.html'
|
|
""" |