|
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 |
|
22 from cm.models import Text, TextVersion, Attachment, Comment, Configuration, Activity |
|
23 |
|
24 ACTIVITY_PAGINATION = 10 |
|
25 RECENT_TEXT_NB = 5 |
|
26 RECENT_COMMENT_NB = RECENT_TEXT_NB |
|
27 |
|
28 MODERATE_NB = 5 |
|
29 |
|
30 |
|
31 def dashboard(request): |
|
32 request.session.set_test_cookie() |
|
33 if request.user.is_authenticated(): |
|
34 act_view = { |
|
35 'view_texts' : get_int(request.GET, 'view_texts', 1), |
|
36 'view_comments' : get_int(request.GET, 'view_comments', 1), |
|
37 'view_users' : get_int(request.GET, 'view_users', 1), |
|
38 } |
|
39 |
|
40 paginate_by = get_int(request.GET, 'paginate', ACTIVITY_PAGINATION) |
|
41 |
|
42 # texts with can_view_unapproved_comment perms |
|
43 moderator_texts = get_texts_with_perm(request, 'can_view_unapproved_comment') |
|
44 viewer_texts = get_texts_with_perm(request, 'can_view_approved_comment') |
|
45 all_texts_ids = [t.id for t in moderator_texts] + [t.id for t in viewer_texts] |
|
46 |
|
47 span = get_among(request.GET, 'span', ('day', 'month', 'week',), 'week') |
|
48 template_dict = { |
|
49 'span' : span, |
|
50 'last_texts' : get_texts_with_perm(request, 'can_view_text').order_by('-modified')[:RECENT_TEXT_NB], |
|
51 'last_comments' : Comment.objects.filter(text_version__text__in=all_texts_ids).order_by('-created')[:RECENT_COMMENT_NB], # TODO: useful? |
|
52 #'last_users' : User.objects.all().order_by('-date_joined')[:5], |
|
53 } |
|
54 template_dict.update(act_view) |
|
55 |
|
56 #selected_activities = [] |
|
57 #[selected_activities.extend(Activity.VIEWABLE_ACTIVITIES[k]) for k in act_view.keys() if act_view[k]] |
|
58 activities = get_viewable_activities(request, act_view) |
|
59 |
|
60 if not has_perm(request, 'can_manage_workspace'): |
|
61 template_dict['to_mod_profiles'] = [] |
|
62 else: |
|
63 template_dict['to_mod_profiles'] = UserProfile.objects.filter(user__is_active=False).filter(is_suspended=True).order_by('-user__date_joined')[:MODERATE_NB] |
|
64 |
|
65 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'])] |
|
66 |
|
67 activities = activities.order_by('-created') |
|
68 return object_list(request, activities, |
|
69 template_name='site/dashboard.html', |
|
70 paginate_by=paginate_by, |
|
71 extra_context=template_dict, |
|
72 ) |
|
73 |
|
74 else: |
|
75 if request.method == 'POST': |
|
76 form = AuthenticationForm(request, request.POST) |
|
77 if form.is_valid(): |
|
78 user = form.get_user() |
|
79 user.backend = 'django.contrib.auth.backends.ModelBackend' |
|
80 cm_login(request, user) |
|
81 display_message(request, _(u"You're logged in!")) |
|
82 return HttpResponseRedirect(reverse('index')) |
|
83 else: |
|
84 form = AuthenticationForm() |
|
85 |
|
86 |
|
87 public_texts = get_texts_with_perm(request, 'can_view_text').order_by('-modified') |
|
88 |
|
89 template_dict = { |
|
90 'form' : form, |
|
91 'public_texts' : public_texts, |
|
92 } |
|
93 return render_to_response('site/non_authenticated_index.html', template_dict, context_instance=RequestContext(request)) |
|
94 |
|
95 |
|
96 class HeaderContactForm(forms.Form): |
|
97 name = forms.CharField( |
|
98 max_length=100, |
|
99 label=ugettext_lazy(u"Your name"), |
|
100 ) |
|
101 email = forms.EmailField(label=ugettext_lazy(u"Your email address"),) |
|
102 |
|
103 class BodyContactForm(forms.Form): |
|
104 title = forms.CharField(label=ugettext_lazy(u"Subject of the message"), max_length=100) |
|
105 body = forms.CharField(label=ugettext_lazy(u"Body of the message"), widget=forms.Textarea) |
|
106 copy = forms.BooleanField( |
|
107 label=ugettext_lazy(u"Send me a copy of the email"), |
|
108 #help_text=ugettext_lazy(u"also send me a copy of the email"), |
|
109 required=False) |
|
110 |
|
111 class ContactForm(HeaderContactForm, BodyContactForm): |
|
112 pass |
|
113 |
|
114 def contact(request): |
|
115 if request.method == 'POST': |
|
116 form = BodyContactForm(request.POST) if request.user.is_authenticated() else ContactForm(request.POST) |
|
117 if form.is_valid(): |
|
118 name = form.cleaned_data.get('name', None) or request.user.username |
|
119 email = form.cleaned_data.get('email', None) or request.user.email |
|
120 message = render_to_string('email/site_contact_email.txt', |
|
121 { |
|
122 'body' : form.cleaned_data['body'], |
|
123 'name' : name, |
|
124 'email' : email, |
|
125 'referer' : request.META.get('HTTP_REFERER', None), |
|
126 }, context_instance=RequestContext(request)) |
|
127 subject = form.cleaned_data['title'] |
|
128 # Email subject *must not* contain newlines |
|
129 subject = ''.join(subject.splitlines()) |
|
130 dest = settings.CONTACT_DEST |
|
131 send_mail(subject, message, email, [dest]) |
|
132 if form.cleaned_data['copy']: |
|
133 my_subject = _(u"Copy of message:") + u" " + subject |
|
134 send_mail(my_subject, message, email, [email]) |
|
135 display_message(request, _(u"Email sent. We will get back to you as quickly as possible.")) |
|
136 redirect_url = reverse('index') |
|
137 return HttpResponseRedirect(redirect_url) |
|
138 else: |
|
139 form = BodyContactForm() if request.user.is_authenticated() else ContactForm() |
|
140 return render_to_response('site/contact.html', {'form': form}, context_instance=RequestContext(request)) |
|
141 |
|
142 def global_feed(request): |
|
143 pass |
|
144 |
|
145 from cm.role_models import role_models_choices |
|
146 from django.utils.safestring import mark_safe |
|
147 |
|
148 class SettingsForm(forms.Form): |
|
149 workspace_name = forms.CharField(label=ugettext_lazy("Workspace name"), |
|
150 widget=forms.TextInput, |
|
151 required=True, |
|
152 ) |
|
153 |
|
154 workspace_tagline = forms.CharField(label=ugettext_lazy("Workspace tagline"), |
|
155 widget=forms.TextInput, |
|
156 required=False, |
|
157 ) |
|
158 |
|
159 workspace_registration = forms.BooleanField(label=ugettext_lazy("Workspace registration"), |
|
160 help_text=ugettext_lazy("Can users register themselves into the workspace? (if not, only invitations by managers can create new users)"), |
|
161 required=False, |
|
162 ) |
|
163 |
|
164 workspace_registration_moderation = forms.BooleanField(label=ugettext_lazy("Workspace registration moderation"), |
|
165 help_text=ugettext_lazy("Should new users be moderated (registration will require manager's approval)"), |
|
166 required=False, |
|
167 ) |
|
168 |
|
169 workspace_role_model = forms.ChoiceField(label=ugettext_lazy("Role model"), |
|
170 help_text=(ugettext_lazy("Change the roles available in the workspace")), |
|
171 choices=role_models_choices, |
|
172 required=False, |
|
173 ) |
|
174 # fields to save in the Configuration objects |
|
175 conf_fields = ['workspace_name', 'workspace_tagline', 'workspace_registration', 'workspace_registration_moderation', 'workspace_role_model'] |
|
176 |
|
177 def __init__(self, data=None, initial=None): |
|
178 forms.Form.__init__(self, data=data, initial=initial) |
|
179 for field in self.fields: |
|
180 if field in self.conf_fields: |
|
181 self.fields[field].initial = Configuration.objects.get_key(field) |
|
182 |
|
183 def save(self): |
|
184 for field in self.fields: |
|
185 if field in self.conf_fields: |
|
186 val = self.cleaned_data[field] |
|
187 Configuration.objects.set_key(field, val) |
|
188 |
|
189 @has_global_perm('can_manage_workspace') |
|
190 def settingss(request): |
|
191 if request.method == 'POST': |
|
192 form = SettingsForm(data=request.POST) |
|
193 if form.is_valid() : |
|
194 form.save() |
|
195 display_message(request, _(u'Settings saved')) |
|
196 return HttpResponseRedirect(reverse('index')) |
|
197 else: |
|
198 form = SettingsForm() |
|
199 |
|
200 return render_to_response('site/settings.html', {'form' : form, 'help_links' : {'workspace_role_model':'role_model'}}, context_instance=RequestContext(request)) |
|
201 |
|
202 def help(request): |
|
203 return render_to_response('site/help.html', context_instance=RequestContext(request)) |
|
204 |