| author | gibus |
| Thu, 23 Jan 2014 10:12:25 +0100 | |
| changeset 566 | ff30d7bda752 |
| parent 504 | b2e0186daa5b |
| permissions | -rw-r--r-- |
| 0 | 1 |
from django.db.models import Q |
2 |
from django.contrib.auth.decorators import login_required |
|
3 |
from cm.security import get_viewable_comments, get_viewable_activities |
|
4 |
from cm.message import display_message |
|
5 |
from cm.models import Comment, Activity, UserProfile, Notification |
|
6 |
from cm.models_utils import Email |
|
7 |
from cm.security import has_global_perm |
|
8 |
from cm.security import get_texts_with_perm, has_perm |
|
9 |
from cm.utils import get_among, get_int |
|
10 |
from cm.utils.mail import send_mail |
|
11 |
from cm.views.user import cm_login |
|
12 |
from django import forms |
|
13 |
from django.conf import settings |
|
14 |
from django.contrib.auth.forms import AuthenticationForm |
|
15 |
from django.core.urlresolvers import reverse |
|
16 |
from django.http import HttpResponse, HttpResponseRedirect, Http404 |
|
17 |
from django.shortcuts import render_to_response |
|
18 |
from django.template import RequestContext |
|
19 |
from django.template.loader import render_to_string |
|
20 |
from django.utils.translation import get_language, ugettext as _, ugettext_lazy |
|
21 |
from django.views.generic.list_detail import object_list |
|
| 220 | 22 |
from django.contrib.auth.models import User |
| 0 | 23 |
from cm.models import Text, TextVersion, Attachment, Comment, Configuration, Activity |
| 566 | 24 |
from django.core.cache import cache |
| 0 | 25 |
|
26 |
ACTIVITY_PAGINATION = 10 |
|
27 |
RECENT_TEXT_NB = 5 |
|
28 |
RECENT_COMMENT_NB = RECENT_TEXT_NB |
|
29 |
||
30 |
MODERATE_NB = 5 |
|
31 |
||
32 |
||
33 |
def dashboard(request): |
|
34 |
request.session.set_test_cookie() |
|
35 |
if request.user.is_authenticated(): |
|
36 |
act_view = { |
|
37 |
'view_texts' : get_int(request.GET, 'view_texts', 1), |
|
38 |
'view_comments' : get_int(request.GET, 'view_comments', 1), |
|
39 |
'view_users' : get_int(request.GET, 'view_users', 1), |
|
40 |
} |
|
| 482 | 41 |
|
| 0 | 42 |
paginate_by = get_int(request.GET, 'paginate', ACTIVITY_PAGINATION) |
| 482 | 43 |
|
| 0 | 44 |
# texts with can_view_unapproved_comment perms |
45 |
moderator_texts = get_texts_with_perm(request, 'can_view_unapproved_comment') |
|
46 |
viewer_texts = get_texts_with_perm(request, 'can_view_approved_comment') |
|
47 |
all_texts_ids = [t.id for t in moderator_texts] + [t.id for t in viewer_texts] |
|
| 482 | 48 |
|
49 |
span = get_among(request.GET, 'span', ('day', 'month', 'week',), 'week') |
|
50 |
template_dict = { |
|
| 0 | 51 |
'span' : span, |
52 |
'last_texts' : get_texts_with_perm(request, 'can_view_text').order_by('-modified')[:RECENT_TEXT_NB], |
|
53 |
'last_comments' : Comment.objects.filter(text_version__text__in=all_texts_ids).order_by('-created')[:RECENT_COMMENT_NB], # TODO: useful? |
|
54 |
#'last_users' : User.objects.all().order_by('-date_joined')[:5], |
|
55 |
} |
|
56 |
template_dict.update(act_view) |
|
| 482 | 57 |
|
| 0 | 58 |
#selected_activities = [] |
59 |
#[selected_activities.extend(Activity.VIEWABLE_ACTIVITIES[k]) for k in act_view.keys() if act_view[k]] |
|
60 |
activities = get_viewable_activities(request, act_view) |
|
| 482 | 61 |
|
| 0 | 62 |
if not has_perm(request, 'can_manage_workspace'): |
63 |
template_dict['to_mod_profiles'] = [] |
|
64 |
else: |
|
65 |
template_dict['to_mod_profiles'] = UserProfile.objects.filter(user__is_active=False).filter(is_suspended=True).order_by('-user__date_joined')[:MODERATE_NB] |
|
| 482 | 66 |
|
| 0 | 67 |
template_dict['to_mod_comments'] = Comment.objects.filter(state='pending').filter(text_version__text__in=moderator_texts).order_by('-modified')[:MODERATE_NB - len(template_dict['to_mod_profiles'])] |
68 |
||
69 |
activities = activities.order_by('-created') |
|
70 |
return object_list(request, activities, |
|
71 |
template_name='site/dashboard.html', |
|
72 |
paginate_by=paginate_by, |
|
73 |
extra_context=template_dict, |
|
74 |
) |
|
| 482 | 75 |
|
| 0 | 76 |
else: |
77 |
if request.method == 'POST': |
|
78 |
form = AuthenticationForm(request, request.POST) |
|
79 |
if form.is_valid(): |
|
80 |
user = form.get_user() |
|
81 |
user.backend = 'django.contrib.auth.backends.ModelBackend' |
|
| 482 | 82 |
cm_login(request, user) |
| 566 | 83 |
cache.clear() |
| 0 | 84 |
display_message(request, _(u"You're logged in!")) |
85 |
return HttpResponseRedirect(reverse('index')) |
|
86 |
else: |
|
| 482 | 87 |
form = AuthenticationForm() |
| 0 | 88 |
|
89 |
||
90 |
public_texts = get_texts_with_perm(request, 'can_view_text').order_by('-modified') |
|
91 |
||
92 |
template_dict = { |
|
93 |
'form' : form, |
|
94 |
'public_texts' : public_texts, |
|
95 |
} |
|
96 |
return render_to_response('site/non_authenticated_index.html', template_dict, context_instance=RequestContext(request)) |
|
| 482 | 97 |
|
| 0 | 98 |
|
| 482 | 99 |
class HeaderContactForm(forms.Form): |
| 0 | 100 |
name = forms.CharField( |
101 |
max_length=100, |
|
102 |
label=ugettext_lazy(u"Your name"), |
|
103 |
) |
|
104 |
email = forms.EmailField(label=ugettext_lazy(u"Your email address"),) |
|
105 |
||
106 |
class BodyContactForm(forms.Form): |
|
107 |
title = forms.CharField(label=ugettext_lazy(u"Subject of the message"), max_length=100) |
|
108 |
body = forms.CharField(label=ugettext_lazy(u"Body of the message"), widget=forms.Textarea) |
|
109 |
copy = forms.BooleanField( |
|
110 |
label=ugettext_lazy(u"Send me a copy of the email"), |
|
| 482 | 111 |
#help_text=ugettext_lazy(u"also send me a copy of the email"), |
| 0 | 112 |
required=False) |
113 |
||
114 |
class ContactForm(HeaderContactForm, BodyContactForm): |
|
115 |
pass |
|
116 |
||
117 |
def contact(request): |
|
118 |
if request.method == 'POST': |
|
| 482 | 119 |
form = BodyContactForm(request.POST) if request.user.is_authenticated() else ContactForm(request.POST) |
| 0 | 120 |
if form.is_valid(): |
121 |
name = form.cleaned_data.get('name', None) or request.user.username |
|
122 |
email = form.cleaned_data.get('email', None) or request.user.email |
|
123 |
message = render_to_string('email/site_contact_email.txt', |
|
124 |
{ |
|
125 |
'body' : form.cleaned_data['body'], |
|
126 |
'name' : name, |
|
127 |
'email' : email, |
|
128 |
'referer' : request.META.get('HTTP_REFERER', None), |
|
129 |
}, context_instance=RequestContext(request)) |
|
130 |
subject = form.cleaned_data['title'] |
|
131 |
# Email subject *must not* contain newlines |
|
132 |
subject = ''.join(subject.splitlines()) |
|
133 |
dest = settings.CONTACT_DEST |
|
134 |
send_mail(subject, message, email, [dest]) |
|
135 |
if form.cleaned_data['copy']: |
|
136 |
my_subject = _(u"Copy of message:") + u" " + subject |
|
137 |
send_mail(my_subject, message, email, [email]) |
|
| 482 | 138 |
display_message(request, _(u"Email sent. We will get back to you as quickly as possible.")) |
139 |
redirect_url = reverse('index') |
|
| 0 | 140 |
return HttpResponseRedirect(redirect_url) |
| 482 | 141 |
else: |
| 0 | 142 |
form = BodyContactForm() if request.user.is_authenticated() else ContactForm() |
143 |
return render_to_response('site/contact.html', {'form': form}, context_instance=RequestContext(request)) |
|
144 |
||
145 |
def global_feed(request): |
|
146 |
pass |
|
147 |
||
148 |
from cm.role_models import role_models_choices |
|
149 |
from django.utils.safestring import mark_safe |
|
150 |
||
| 164 | 151 |
class BaseSettingsForm(forms.Form): |
152 |
def __init__(self, data=None, initial=None): |
|
153 |
forms.Form.__init__(self, data=data, initial=initial) |
|
154 |
for field in self.fields: |
|
155 |
if field in self.conf_fields: |
|
156 |
self.fields[field].initial = Configuration.objects.get_key(field) |
|
| 482 | 157 |
|
| 164 | 158 |
self.fields[field].initial = Configuration.objects.get_key(field) |
| 482 | 159 |
|
| 164 | 160 |
def save(self): |
161 |
for field in self.fields: |
|
162 |
if field in self.conf_fields: |
|
163 |
val = self.cleaned_data[field] |
|
164 |
Configuration.objects.set_key(field, val) |
|
| 482 | 165 |
|
| 164 | 166 |
class SettingsForm(BaseSettingsForm): |
| 0 | 167 |
workspace_name = forms.CharField(label=ugettext_lazy("Workspace name"), |
168 |
widget=forms.TextInput, |
|
| 160 | 169 |
required=False, |
| 0 | 170 |
) |
| 482 | 171 |
|
| 0 | 172 |
workspace_tagline = forms.CharField(label=ugettext_lazy("Workspace tagline"), |
173 |
widget=forms.TextInput, |
|
174 |
required=False, |
|
175 |
) |
|
176 |
||
177 |
workspace_registration = forms.BooleanField(label=ugettext_lazy("Workspace registration"), |
|
178 |
help_text=ugettext_lazy("Can users register themselves into the workspace? (if not, only invitations by managers can create new users)"), |
|
179 |
required=False, |
|
180 |
) |
|
181 |
||
182 |
workspace_registration_moderation = forms.BooleanField(label=ugettext_lazy("Workspace registration moderation"), |
|
| 158 | 183 |
help_text=ugettext_lazy("Should new users be moderated (registration will require manager's approval)?"), |
| 0 | 184 |
required=False, |
185 |
) |
|
186 |
||
187 |
workspace_role_model = forms.ChoiceField(label=ugettext_lazy("Role model"), |
|
188 |
help_text=(ugettext_lazy("Change the roles available in the workspace")), |
|
189 |
choices=role_models_choices, |
|
190 |
required=False, |
|
191 |
) |
|
| 482 | 192 |
|
|
504
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
193 |
workspace_category_1 = forms.CharField(label=ugettext_lazy("Label for the first category of comments"), |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
194 |
help_text=mark_safe(ugettext_lazy("Paragraphs including at least one comment of this category will have a vertical bar with this color: ") + '<span style="width: 2px; height: 5px; background-color: #1523f4"> </span>'), |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
195 |
widget=forms.TextInput, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
196 |
required=False, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
197 |
max_length=20, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
198 |
) |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
199 |
workspace_category_2 = forms.CharField(label=ugettext_lazy("Label for the second category of comments"), |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
200 |
help_text=mark_safe(ugettext_lazy("Paragraphs including at least one comment of this category will have a vertical bar with this color: ") + '<span style="width: 2px; height: 5px; background-color: #f4154f"> </span>'), |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
201 |
widget=forms.TextInput, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
202 |
required=False, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
203 |
max_length=20, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
204 |
) |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
205 |
workspace_category_3 = forms.CharField(label=ugettext_lazy("Label for the third category of comments"), |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
206 |
help_text=mark_safe(ugettext_lazy("Paragraphs including at least one comment of this category will have a vertical bar with this color: ") + '<span style="width: 2px; height: 5px; background-color: #09ff09"> </span>'), |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
207 |
widget=forms.TextInput, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
208 |
required=False, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
209 |
max_length=20, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
210 |
) |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
211 |
workspace_category_4 = forms.CharField(label=ugettext_lazy("Label for the fourth category of comments"), |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
212 |
help_text=mark_safe(ugettext_lazy("Paragraphs including at least one comment of this category will have a vertical bar with this color: ") + '<span style="width: 2px; height: 5px; background-color: #bc39cf"> </span>'), |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
213 |
widget=forms.TextInput, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
214 |
required=False, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
215 |
max_length=20, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
216 |
) |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
217 |
workspace_category_5 = forms.CharField(label=ugettext_lazy("Label for the fifth category of comments"), |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
218 |
help_text=mark_safe(ugettext_lazy("Paragraphs including at least one comment of this category will have a vertical bar with this color: ") + '<span style="width: 2px; height: 5px; background-color: #ffbd08"> </span>'), |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
219 |
widget=forms.TextInput, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
220 |
required=False, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
221 |
max_length=20, |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
222 |
) |
| 482 | 223 |
|
| 0 | 224 |
# fields to save in the Configuration objects |
|
504
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
482
diff
changeset
|
225 |
conf_fields = ['workspace_name', 'workspace_tagline', 'workspace_registration', 'workspace_registration_moderation', 'workspace_role_model', 'workspace_category_1', 'workspace_category_2', 'workspace_category_3', 'workspace_category_4', 'workspace_category_5'] |
| 0 | 226 |
|
| 164 | 227 |
|
| 0 | 228 |
@has_global_perm('can_manage_workspace') |
229 |
def settingss(request): |
|
230 |
if request.method == 'POST': |
|
| 160 | 231 |
if 'delete_logo' in request.POST: |
232 |
Configuration.objects.del_key('workspace_logo_file_key') |
|
| 0 | 233 |
display_message(request, _(u'Settings saved')) |
| 482 | 234 |
return HttpResponseRedirect(reverse('settings')) |
| 160 | 235 |
else: |
236 |
form = SettingsForm(data=request.POST) |
|
237 |
if form.is_valid() : |
|
238 |
form.save() |
|
239 |
display_message(request, _(u'Settings saved')) |
|
| 482 | 240 |
return HttpResponseRedirect(reverse('settings')) |
| 0 | 241 |
else: |
242 |
form = SettingsForm() |
|
| 482 | 243 |
|
| 0 | 244 |
return render_to_response('site/settings.html', {'form' : form, 'help_links' : {'workspace_role_model':'role_model'}}, context_instance=RequestContext(request)) |
245 |
||
| 164 | 246 |
class SettingsDesignForm(BaseSettingsForm): |
247 |
workspace_logo_file = forms.FileField(label=ugettext_lazy("Workspace logo"),required=False) |
|
| 482 | 248 |
|
249 |
custom_css = forms.CharField(label=ugettext_lazy("Custom CSS rules"), |
|
250 |
help_text=mark_safe(ugettext_lazy("Add stylesheet rules in CSS format (do not include <code><style></code> HTML tags). Warning: this code will be added to all content, make sure you know what you're doing before adding something here.")), |
|
| 164 | 251 |
widget=forms.Textarea, |
252 |
required=False, |
|
253 |
) |
|
254 |
||
| 482 | 255 |
custom_font = forms.CharField(label=ugettext_lazy("Custom font"), |
256 |
widget=forms.TextInput, |
|
257 |
help_text=mark_safe(ugettext_lazy("Custom alternative font family to 'modern', 'classic' and 'code' that visitors can chose for the body of co-ment texts. Enter a coma separated list of font families. Font family names including space characters should be enclosed in double quotes. Eg. ") + '<code>"Times New Roman", Times, serif</code>.'), |
|
258 |
required=False, |
|
259 |
) |
|
260 |
||
261 |
custom_titles_font = forms.CharField(label=ugettext_lazy("Custom font for titles"), |
|
262 |
widget=forms.TextInput, |
|
263 |
help_text=mark_safe(ugettext_lazy("Custom alternative font family to 'modern', 'classic' and 'code' that visitors can chose for titles (h1 to h6) of co-ment texts. Enter a coma separated list of font families. Font family names including space characters should be enclosed in double quotes. Eg. ") + '<code>"Gill Sans", Helvetica, sans-serif</code>.'), |
|
264 |
required=False, |
|
265 |
) |
|
266 |
conf_fields = ['custom_css', 'custom_font', 'custom_titles_font'] |
|
267 |
||
| 164 | 268 |
def save_file(self, logo_file): |
269 |
attach = Attachment.objects.create_attachment(filename='wp_logo', data=logo_file.read(), text_version=None) |
|
270 |
Configuration.objects.set_key('workspace_logo_file_key', attach.key) |
|
| 482 | 271 |
|
272 |
||
| 164 | 273 |
@has_global_perm('can_manage_workspace') |
274 |
def settings_design(request): |
|
275 |
if request.method == 'POST': |
|
276 |
if 'delete_logo' in request.POST: |
|
277 |
Configuration.objects.del_key('workspace_logo_file_key') |
|
278 |
display_message(request, _(u'Settings saved')) |
|
| 482 | 279 |
return HttpResponseRedirect(reverse('settings-design')) |
| 164 | 280 |
else: |
281 |
form = SettingsDesignForm(data=request.POST) |
|
282 |
if form.is_valid() : |
|
283 |
form.save() |
|
284 |
logo_file = request.FILES.get('workspace_logo_file',None) |
|
285 |
if logo_file: |
|
286 |
form.save_file(logo_file) |
|
287 |
display_message(request, _(u'Settings saved')) |
|
| 482 | 288 |
return HttpResponseRedirect(reverse('settings-design')) |
| 164 | 289 |
else: |
| 482 | 290 |
from cm.models import ApplicationConfiguration |
291 |
custom_css = ApplicationConfiguration.get_key('custom_css') |
|
292 |
if custom_css: |
|
293 |
default_css = custom_css |
|
294 |
else: |
|
295 |
default_css = ''' |
|
296 |
.voted { |
|
297 |
color: #008000; |
|
298 |
} |
|
299 |
||
300 |
.rejected, .fallen, .withdrawn { |
|
301 |
color: #ff0000; |
|
302 |
} |
|
303 |
||
304 |
div.frame { |
|
305 |
border: 1px solid #000; |
|
306 |
padding: 5px; |
|
307 |
} |
|
308 |
||
309 |
div.frame .title { |
|
310 |
font-weight: bold; |
|
311 |
text-align: center; |
|
312 |
}''' |
|
313 |
form = SettingsDesignForm(initial={'custom_css': default_css}) |
|
314 |
||
| 164 | 315 |
return render_to_response('site/settings_design.html', {'form' : form}, context_instance=RequestContext(request)) |
|
225
67e1a89d6bca
refactor forgot pw function to use django methods / add password change page in profile / i18n update
raph
parents:
220
diff
changeset
|
316 |
|
| 220 | 317 |
|
|
225
67e1a89d6bca
refactor forgot pw function to use django methods / add password change page in profile / i18n update
raph
parents:
220
diff
changeset
|
318 |
def password_reset_done(request): |
|
67e1a89d6bca
refactor forgot pw function to use django methods / add password change page in profile / i18n update
raph
parents:
220
diff
changeset
|
319 |
display_message(request, _(u'A link to reset your password has been sent to the profile email. Please check your email.')) |
|
67e1a89d6bca
refactor forgot pw function to use django methods / add password change page in profile / i18n update
raph
parents:
220
diff
changeset
|
320 |
return HttpResponseRedirect(reverse('index')) |
| 220 | 321 |
|
|
225
67e1a89d6bca
refactor forgot pw function to use django methods / add password change page in profile / i18n update
raph
parents:
220
diff
changeset
|
322 |
def password_reset_complete(request): |
|
67e1a89d6bca
refactor forgot pw function to use django methods / add password change page in profile / i18n update
raph
parents:
220
diff
changeset
|
323 |
display_message(request, _(u'Password changed')) |
|
67e1a89d6bca
refactor forgot pw function to use django methods / add password change page in profile / i18n update
raph
parents:
220
diff
changeset
|
324 |
return HttpResponseRedirect(reverse('index')) |
| 220 | 325 |
|
| 0 | 326 |
def help(request): |
327 |
return render_to_response('site/help.html', context_instance=RequestContext(request)) |
|
328 |