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.
62 lines
2.4 KiB
62 lines
2.4 KiB
from django.views.generic import FormView
|
|
from django.shortcuts import render_to_response, get_object_or_404
|
|
from django.http import HttpResponseRedirect
|
|
from django.conf import settings
|
|
|
|
class AdminView(FormView):
|
|
obj = None
|
|
|
|
def set_obj(self):
|
|
url = self.kwargs.get('url')
|
|
if url:
|
|
obj = get_object_or_404(self.model, url=url)
|
|
self.obj = obj
|
|
return obj
|
|
slug = self.kwargs.get('slug')
|
|
if slug:
|
|
obj = get_object_or_404(self.model, slug=slug)
|
|
self.obj = obj
|
|
return obj
|
|
self.obj = None
|
|
return None
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(AdminView, self).get_context_data(**kwargs)
|
|
self.set_obj()
|
|
context['object'] = self.obj
|
|
context['languages'] = settings.LANGUAGES
|
|
return context
|
|
|
|
def form_valid(self, form):
|
|
self.set_obj()
|
|
form.save(obj=self.obj)
|
|
return HttpResponseRedirect(self.success_url)
|
|
# example get_form
|
|
"""
|
|
def get_form(self, form_class):
|
|
if self.request.POST:
|
|
return super(AdminView, self).get_form(form_class)
|
|
self.set_obj()
|
|
if self.obj:
|
|
obj = self.obj
|
|
data = {}
|
|
data['theme'] = [item.id for item in article.theme.all()]
|
|
data['exposition'] = article.exposition
|
|
data['conference'] = article.conference
|
|
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()]
|
|
return form_class(data)
|
|
else:
|
|
return form_class()
|
|
""" |