Ticket #6071: views_add.py

File views_add.py, 5.8 KB (added by hans.rauch@…, 17 years ago)
Line 
1# -*- coding: utf-8 -*-
2"""
3/dms/newsbookitem/views_add.py
4
5.. enthaelt den View zum Ergaenzen einer Nachricht
6 Django content Management System
7
8Hans Rauch
9hans.rauch@gmx.net
10
11Die Programme des dms-Systems koennen frei genutzt und den spezifischen
12Beduerfnissen entsprechend angepasst werden.
13
140.01 16.03.2007 Beginn der Arbeit
15"""
16
17import string
18import time
19import datetime
20
21from django.utils.encoding import smart_unicode
22from django.http import HttpResponse, HttpResponseRedirect
23from django.shortcuts import render_to_response
24from django import newforms as forms
25
26from django.utils.translation import ugettext as _
27
28from dms.models import DmsItem
29from dms.queries import save_item_values
30from dms.queries import get_random_question_answer
31from dms.queries import check_answer
32from dms.queries import get_user
33from dms.queries import get_site_url
34
35#from dms.roles import *
36from dms.utils import get_tabbed_form
37from dms.utils import get_license_choices
38from dms.mail import send_control_email
39from dms.utils_form import get_item_vars_add
40
41from dms.encode_decode import decode_html
42from dms.encode_decode import encode_html_dir
43
44from dms.views_error import show_error_spam
45
46from dms.newsitem.help_form import help_form
47
48from dms_ext.extension import * # dms-Funktionen ueberschreiben
49
50# -----------------------------------------------------
51def newsitem_add(request, item_container):
52 """ neue Nachricht anlegen """
53 question, answer = get_random_question_answer()
54 my_user = get_user(request.user.username)
55 if my_user != None:
56 my_name = my_user.get_full_name()
57 my_email = my_user.email
58 else:
59 my_name = ''
60 my_email = ''
61
62 def save_values(name, new, my_folder):
63 """ Daten sichern """
64 new['section'] = ''
65 new['visible_start'] = datetime.date.today().strftime('%d.%m.%Y')
66 save_item_values(request.user, 'dmsNewsItem', name, new, my_folder,
67 not my_folder.item.is_moderated, True,
68 visible_start=new['visible_start'],
69 visible_end=new['visible_end'])
70 send_control_email(item_container)
71
72 class DmsItemForm ( forms.Form ) :
73 if my_user == None:
74 string_1 = forms.CharField(max_length=60,
75 widget=forms.TextInput(attrs={'size':60}) )
76 string_2 = forms.CharField(required=False, max_length=200,
77 widget=forms.TextInput(attrs={'size':60}) )
78 anti_spam_question = forms.CharField(required=False,
79 widget=forms.HiddenInput(attrs={'value':question}) )
80 anti_spam_answer = forms.CharField(max_length=20,
81 widget=forms.TextInput(attrs={'size':20}) )
82 else:
83 # --- eigentlich koennte required=False entfallen
84 string_1 = forms.CharField(required=False,
85 widget=forms.HiddenInput(attrs={'value':my_name}) )
86 string_2 = forms.CharField(required=False,
87 widget=forms.HiddenInput(attrs={'value':my_email}) )
88 title = forms.CharField(max_length=240,
89 widget=forms.TextInput(attrs={'size':60}) )
90 sub_title = forms.CharField(required=False, max_length=240,
91 widget=forms.TextInput(attrs={'size':60}) )
92 text = forms.CharField(widget=forms.Textarea(
93 attrs={'rows':12, 'cols':60, 'id':'ta', 'style':'width:100%;'}) )
94 text_more = forms.CharField(required=False, widget=forms.Textarea(
95 attrs={'rows':24, 'cols':60, 'id':'ta1', 'style':'width:100%;'}) )
96 url_more = forms.CharField(required=False, max_length=200,
97 widget=forms.TextInput(attrs={'size':60}) )
98 visible_end = forms.DateField(input_formats=['%d.%m.%Y'],
99 widget=forms.TextInput(attrs={'size':10}))
100 #license = forms.ChoiceField(choices=get_license_choices(item_container),
101 # widget=forms.RadioSelect() )
102
103 app_name = 'newsitem'
104 my_title = _('Nachricht anlegen')
105 # --- Sind Daten vorhanden oder muessen Sie initialiSiert werden?
106 show_errors = ( request.method == 'POST' )
107 if show_errors:
108 data = request.POST.copy()
109 else :
110 data = { 'visible_end': (datetime.datetime.now() + datetime.timedelta(30)).strftime('%d.%m.%Y'),
111 'license': 1, }
112 f = DmsItemForm ( data )
113
114 if request.method == 'POST' and not f.errors:
115 #new = encode_html_dir(f.data)
116 new = f.data
117 if request.user.username == '':
118 if new.has_key('anti_spam_question'):
119 is_ok = check_answer(new['anti_spam_question'], new['anti_spam_answer'])
120 else:
121 is_ok = True
122 if is_ok:
123 now = smart_unicode(time.time())
124 name = 'news_item_' + now[:string.find(now, '.')] + '.html'
125 save_values(name, f.data, item_container)
126 return HttpResponseRedirect(get_site_url(item_container, 'index.html'))
127 else:
128 return show_error_spam(request, item_container)
129 else :
130 # --- Reihenfolge, Ueberschriften, Hilfetexte
131 if my_user == None:
132 tabs = [ ('tab_base', [ 'string_1', 'string_2', 'title', 'sub_title', 'text',
133 'text_more', 'url_more', 'visible_end',
134 'anti_spam_question', 'anti_spam_answer' ]),
135 #( 'tab_license', [ 'license', ] ),
136 ]
137 else:
138 tabs = [ ('tab_base', [ 'string_1', 'string_2', 'title', 'sub_title', 'text',
139 'text_more', 'url_more', 'visible_end', ]),
140 #( 'tab_license', [ 'license', ] ),
141 ]
142 content = get_tabbed_form(tabs, help_form, app_name, f)
143 if item_container.item.is_moderated:
144 moderated_text = help_form['moderated_text']['info']
145 else:
146 moderated_text = ''
147 vars = get_item_vars_add(request, item_container, app_name, my_title, content, show_errors)
148 vars['moderated_text'] = moderated_text
149 return render_to_response ( 'app/base_edit.html', vars )
Back to Top