cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
news.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.wi.views.admin_clm.news
22 #
23 # @author Krzysztof Danielowski
24 # @author Piotr W√≥jcik
25 # @date 17.03.2011
26 #
27 from django.template import RequestContext
28 from django.template.loader import render_to_string
29 from django.utils.translation import ugettext as _
30 from django.views.decorators.csrf import csrf_protect
31 
32 from wi.forms.news import NewsForm
33 from wi.utils import messages_ajax
34 from wi.utils.decorators import django_view
35 from wi.utils.messages_ajax import ajax_request
36 from wi.utils.views import prep_data
37 
38 
39 @django_view
40 @ajax_request
41 @csrf_protect
42 ##
43 #
44 # Ajax view for editing a news entry.
45 #
46 def mai_ajax_edit_news(request, id1, template_name='generic/form.html', form_class=NewsForm):
47  if request.method == 'POST':
48  form = form_class(data=request.POST)
49  if form.is_valid():
50  dictionary = form.cleaned_data
51  dictionary.update({'news_id': id1})
52  prep_data(('admin_clm/news/edit/', dictionary), request.session)
53 
54  return messages_ajax.success(_('News entry edited.'))
55 
56  else:
57  rest_data = prep_data(('admin_clm/news/get_by_id/', {'news_id': id1}), request.session)
58 
59  rest_data['sticky'] = rest_data['sticky'] != 0
60  form = form_class(rest_data)
61 
62  return messages_ajax.success(render_to_string(template_name, {'form': form,
63  'text': _('Edit news data:'),
64  'confirmation': _('Save'),
65  'id': id1},
66  context_instance=RequestContext(request)),
67  status=1)
68