| author | raph |
| Wed, 31 Mar 2010 16:26:00 +0200 | |
| changeset 240 | a00efaf32ea7 |
| parent 225 | 67e1a89d6bca |
| child 482 | 00f61fe2430a |
| 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 |
24 |
||
25 |
ACTIVITY_PAGINATION = 10 |
|
26 |
RECENT_TEXT_NB = 5 |
|
27 |
RECENT_COMMENT_NB = RECENT_TEXT_NB |
|
28 |
||
29 |
MODERATE_NB = 5 |
|
30 |
||
31 |
||
32 |
def dashboard(request): |
|
33 |
request.session.set_test_cookie() |
|
34 |
if request.user.is_authenticated(): |
|
35 |
act_view = { |
|
36 |
'view_texts' : get_int(request.GET, 'view_texts', 1), |
|
37 |
'view_comments' : get_int(request.GET, 'view_comments', 1), |
|
38 |
'view_users' : get_int(request.GET, 'view_users', 1), |
|
39 |
} |
|
40 |
||
41 |
paginate_by = get_int(request.GET, 'paginate', ACTIVITY_PAGINATION) |
|
42 |
||
43 |
# texts with can_view_unapproved_comment perms |
|
44 |
moderator_texts = get_texts_with_perm(request, 'can_view_unapproved_comment') |
|
45 |
viewer_texts = get_texts_with_perm(request, 'can_view_approved_comment') |
|
46 |
all_texts_ids = [t.id for t in moderator_texts] + [t.id for t in viewer_texts] |
|
47 |
||
48 |
span = get_among(request.GET, 'span', ('day', 'month', 'week',), 'week') |
|
49 |
template_dict = { |
|
50 |
'span' : span, |
|
51 |
'last_texts' : get_texts_with_perm(request, 'can_view_text').order_by('-modified')[:RECENT_TEXT_NB], |
|
52 |
'last_comments' : Comment.objects.filter(text_version__text__in=all_texts_ids).order_by('-created')[:RECENT_COMMENT_NB], # TODO: useful? |
|
53 |
#'last_users' : User.objects.all().order_by('-date_joined')[:5], |
|
54 |
} |
|
55 |
template_dict.update(act_view) |
|
56 |
||
57 |
#selected_activities = [] |
|
58 |
#[selected_activities.extend(Activity.VIEWABLE_ACTIVITIES[k]) for k in act_view.keys() if act_view[k]] |
|
59 |
activities = get_viewable_activities(request, act_view) |
|
60 |
||
61 |
if not has_perm(request, 'can_manage_workspace'): |
|
62 |
template_dict['to_mod_profiles'] = [] |
|
63 |
else: |
|
64 |
template_dict['to_mod_profiles'] = UserProfile.objects.filter(user__is_active=False).filter(is_suspended=True).order_by('-user__date_joined')[:MODERATE_NB] |
|
65 |
||
66 |
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'])] |
|
67 |
||
68 |
activities = activities.order_by('-created') |
|
69 |
return object_list(request, activities, |
|
70 |
template_name='site/dashboard.html', |
|
71 |
paginate_by=paginate_by, |
|
72 |
extra_context=template_dict, |
|
73 |
) |
|
74 |
||
75 |
else: |
|
76 |
if request.method == 'POST': |
|
77 |
form = AuthenticationForm(request, request.POST) |
|
78 |
if form.is_valid(): |
|
79 |
user = form.get_user() |
|
80 |
user.backend = 'django.contrib.auth.backends.ModelBackend' |
|
81 |
cm_login(request, user) |
|
82 |
display_message(request, _(u"You're logged in!")) |
|
83 |
return HttpResponseRedirect(reverse('index')) |
|
84 |
else: |
|
85 |
form = AuthenticationForm() |
|
86 |
||
87 |
||
88 |
public_texts = get_texts_with_perm(request, 'can_view_text').order_by('-modified') |
|
89 |
||
90 |
template_dict = { |
|
91 |
'form' : form, |
|
92 |
'public_texts' : public_texts, |
|
93 |
} |
|
94 |
return render_to_response('site/non_authenticated_index.html', template_dict, context_instance=RequestContext(request)) |
|
95 |
||
96 |
||
97 |
class HeaderContactForm(forms.Form): |
|
98 |
name = forms.CharField( |
|
99 |
max_length=100, |
|
100 |
label=ugettext_lazy(u"Your name"), |
|
101 |
) |
|
102 |
email = forms.EmailField(label=ugettext_lazy(u"Your email address"),) |
|
103 |
||
104 |
class BodyContactForm(forms.Form): |
|
105 |
title = forms.CharField(label=ugettext_lazy(u"Subject of the message"), max_length=100) |
|
106 |
body = forms.CharField(label=ugettext_lazy(u"Body of the message"), widget=forms.Textarea) |
|
107 |
copy = forms.BooleanField( |
|
108 |
label=ugettext_lazy(u"Send me a copy of the email"), |
|
109 |
#help_text=ugettext_lazy(u"also send me a copy of the email"), |
|
110 |
required=False) |
|
111 |
||
112 |
class ContactForm(HeaderContactForm, BodyContactForm): |
|
113 |
pass |
|
114 |
||
115 |
def contact(request): |
|
116 |
if request.method == 'POST': |
|
117 |
form = BodyContactForm(request.POST) if request.user.is_authenticated() else ContactForm(request.POST) |
|
118 |
if form.is_valid(): |
|
119 |
name = form.cleaned_data.get('name', None) or request.user.username |
|
120 |
email = form.cleaned_data.get('email', None) or request.user.email |
|
121 |
message = render_to_string('email/site_contact_email.txt', |
|
122 |
{ |
|
123 |
'body' : form.cleaned_data['body'], |
|
124 |
'name' : name, |
|
125 |
'email' : email, |
|
126 |
'referer' : request.META.get('HTTP_REFERER', None), |
|
127 |
}, context_instance=RequestContext(request)) |
|
128 |
subject = form.cleaned_data['title'] |
|
129 |
# Email subject *must not* contain newlines |
|
130 |
subject = ''.join(subject.splitlines()) |
|
131 |
dest = settings.CONTACT_DEST |
|
132 |
send_mail(subject, message, email, [dest]) |
|
133 |
if form.cleaned_data['copy']: |
|
134 |
my_subject = _(u"Copy of message:") + u" " + subject |
|
135 |
send_mail(my_subject, message, email, [email]) |
|
136 |
display_message(request, _(u"Email sent. We will get back to you as quickly as possible.")) |
|
137 |
redirect_url = reverse('index') |
|
138 |
return HttpResponseRedirect(redirect_url) |
|
139 |
else: |
|
140 |
form = BodyContactForm() if request.user.is_authenticated() else ContactForm() |
|
141 |
return render_to_response('site/contact.html', {'form': form}, context_instance=RequestContext(request)) |
|
142 |
||
143 |
def global_feed(request): |
|
144 |
pass |
|
145 |
||
146 |
from cm.role_models import role_models_choices |
|
147 |
from django.utils.safestring import mark_safe |
|
148 |
||
| 164 | 149 |
class BaseSettingsForm(forms.Form): |
150 |
def __init__(self, data=None, initial=None): |
|
151 |
forms.Form.__init__(self, data=data, initial=initial) |
|
152 |
for field in self.fields: |
|
153 |
if field in self.conf_fields: |
|
154 |
self.fields[field].initial = Configuration.objects.get_key(field) |
|
155 |
||
156 |
self.fields[field].initial = Configuration.objects.get_key(field) |
|
157 |
||
158 |
def save(self): |
|
159 |
for field in self.fields: |
|
160 |
if field in self.conf_fields: |
|
161 |
val = self.cleaned_data[field] |
|
162 |
Configuration.objects.set_key(field, val) |
|
163 |
||
164 |
class SettingsForm(BaseSettingsForm): |
|
| 0 | 165 |
workspace_name = forms.CharField(label=ugettext_lazy("Workspace name"), |
166 |
widget=forms.TextInput, |
|
| 160 | 167 |
required=False, |
| 0 | 168 |
) |
169 |
||
170 |
workspace_tagline = forms.CharField(label=ugettext_lazy("Workspace tagline"), |
|
171 |
widget=forms.TextInput, |
|
172 |
required=False, |
|
173 |
) |
|
174 |
||
175 |
workspace_registration = forms.BooleanField(label=ugettext_lazy("Workspace registration"), |
|
176 |
help_text=ugettext_lazy("Can users register themselves into the workspace? (if not, only invitations by managers can create new users)"), |
|
177 |
required=False, |
|
178 |
) |
|
179 |
||
180 |
workspace_registration_moderation = forms.BooleanField(label=ugettext_lazy("Workspace registration moderation"), |
|
| 158 | 181 |
help_text=ugettext_lazy("Should new users be moderated (registration will require manager's approval)?"), |
| 0 | 182 |
required=False, |
183 |
) |
|
184 |
||
185 |
workspace_role_model = forms.ChoiceField(label=ugettext_lazy("Role model"), |
|
186 |
help_text=(ugettext_lazy("Change the roles available in the workspace")), |
|
187 |
choices=role_models_choices, |
|
188 |
required=False, |
|
189 |
) |
|
| 160 | 190 |
|
191 |
||
| 0 | 192 |
# fields to save in the Configuration objects |
193 |
conf_fields = ['workspace_name', 'workspace_tagline', 'workspace_registration', 'workspace_registration_moderation', 'workspace_role_model'] |
|
194 |
||
| 164 | 195 |
|
| 0 | 196 |
@has_global_perm('can_manage_workspace') |
197 |
def settingss(request): |
|
198 |
if request.method == 'POST': |
|
| 160 | 199 |
if 'delete_logo' in request.POST: |
200 |
Configuration.objects.del_key('workspace_logo_file_key') |
|
| 0 | 201 |
display_message(request, _(u'Settings saved')) |
| 160 | 202 |
return HttpResponseRedirect(reverse('index')) |
203 |
else: |
|
204 |
form = SettingsForm(data=request.POST) |
|
205 |
if form.is_valid() : |
|
206 |
form.save() |
|
207 |
display_message(request, _(u'Settings saved')) |
|
208 |
return HttpResponseRedirect(reverse('index')) |
|
| 0 | 209 |
else: |
210 |
form = SettingsForm() |
|
211 |
||
212 |
return render_to_response('site/settings.html', {'form' : form, 'help_links' : {'workspace_role_model':'role_model'}}, context_instance=RequestContext(request)) |
|
213 |
||
| 164 | 214 |
class SettingsDesignForm(BaseSettingsForm): |
215 |
workspace_logo_file = forms.FileField(label=ugettext_lazy("Workspace logo"),required=False) |
|
216 |
||
217 |
workspace_code = forms.CharField(label=ugettext_lazy("Workspace html code"), |
|
218 |
help_text=(ugettext_lazy("Add stylesheets etc. Warning: this code will be added to the workspace code, make sure you know what you're doing before adding something here.")), |
|
219 |
widget=forms.Textarea, |
|
220 |
required=False, |
|
221 |
) |
|
222 |
||
223 |
conf_fields = ['workspace_code'] |
|
224 |
||
225 |
def save_file(self, logo_file): |
|
226 |
attach = Attachment.objects.create_attachment(filename='wp_logo', data=logo_file.read(), text_version=None) |
|
227 |
Configuration.objects.set_key('workspace_logo_file_key', attach.key) |
|
228 |
||
229 |
||
230 |
@has_global_perm('can_manage_workspace') |
|
231 |
def settings_design(request): |
|
232 |
if request.method == 'POST': |
|
233 |
if 'delete_logo' in request.POST: |
|
234 |
Configuration.objects.del_key('workspace_logo_file_key') |
|
235 |
display_message(request, _(u'Settings saved')) |
|
236 |
return HttpResponseRedirect(reverse('index')) |
|
237 |
else: |
|
238 |
form = SettingsDesignForm(data=request.POST) |
|
239 |
if form.is_valid() : |
|
240 |
form.save() |
|
241 |
logo_file = request.FILES.get('workspace_logo_file',None) |
|
242 |
if logo_file: |
|
243 |
form.save_file(logo_file) |
|
244 |
display_message(request, _(u'Settings saved')) |
|
245 |
return HttpResponseRedirect(reverse('index')) |
|
246 |
else: |
|
247 |
form = SettingsDesignForm() |
|
248 |
||
249 |
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
|
250 |
|
| 220 | 251 |
|
|
225
67e1a89d6bca
refactor forgot pw function to use django methods / add password change page in profile / i18n update
raph
parents:
220
diff
changeset
|
252 |
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
|
253 |
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
|
254 |
return HttpResponseRedirect(reverse('index')) |
| 220 | 255 |
|
|
225
67e1a89d6bca
refactor forgot pw function to use django methods / add password change page in profile / i18n update
raph
parents:
220
diff
changeset
|
256 |
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
|
257 |
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
|
258 |
return HttpResponseRedirect(reverse('index')) |
| 220 | 259 |
|
| 0 | 260 |
def help(request): |
261 |
return render_to_response('site/help.html', context_instance=RequestContext(request)) |
|
262 |