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.
155 lines
5.3 KiB
155 lines
5.3 KiB
# -*- coding: utf-8 -*-
|
|
from django.shortcuts import render_to_response, get_object_or_404
|
|
from django.http import HttpResponseRedirect
|
|
from django.db import models
|
|
from django.core.context_processors import csrf
|
|
from django.conf import settings
|
|
from django.contrib import admin
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.views.generic import DeleteView
|
|
|
|
from modeltranslation.admin import TabbedTranslationAdmin
|
|
from ckeditor.widgets import CKEditorWidget
|
|
from sorl.thumbnail.admin.current import AdminImageWidget
|
|
|
|
#models and forms
|
|
from .forms import NewsForm
|
|
from .models import Article, Author
|
|
|
|
from theme.models import Tag
|
|
from file.models import FileModel
|
|
from file.forms import FileModelForm
|
|
|
|
#custom views
|
|
from functions.custom_views import objects_list, add_object_with_file, delete_object
|
|
from functions.views_help import get_referer
|
|
from functions.admin import DefaultAdmin
|
|
|
|
|
|
class AuthorAdmin(DefaultAdmin, TabbedTranslationAdmin):
|
|
list_display = ['fullname', 'email']
|
|
formfield_overrides = {
|
|
models.TextField: {'widget': CKEditorWidget},
|
|
models.ImageField: {'widget': AdminImageWidget},
|
|
}
|
|
admin.site.register(Author, AuthorAdmin)
|
|
|
|
|
|
class ArticleDeleteView(DeleteView):
|
|
model = Article
|
|
template_name = "c_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
|
|
|
|
|
|
#-----------------------
|
|
from django.views.generic import FormView
|
|
from functions.custom_views import ListView
|
|
from forms import BlogForm
|
|
|
|
class BlogList(ListView):
|
|
model = Article
|
|
template_name = 'c_admin/article/article_admin_list.html'
|
|
paginate_by = settings.ADMIN_PAGINATION
|
|
|
|
def get_queryset(self):
|
|
return self.model.objects.blogs_all()
|
|
|
|
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 = 'c_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
|
|
data['author'] = article.author_s
|
|
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['draft'] = article.draft
|
|
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['short_description_%s' % code] = obj.short_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
|
|
context['languages'] = settings.LANGUAGES
|
|
return context
|
|
|
|
|
|
class NewsList(ListView):
|
|
model = Article
|
|
template_name = 'c_admin/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 = 'c_admin/article/blog_form.html'
|
|
success_url = '/admin/article/news/all/'
|
|
obj = None
|
|
|