Commit 7f60c409e01341c3c9881c83c3cc70ec452b6236
1 parent
a4e7d783
Exists in
master
and in
2 other branches
Adding chat log
Showing
1 changed file
with
107 additions
and
13 deletions
Show diff stats
chat/views.py
... | ... | @@ -20,6 +20,10 @@ from amadeus.permissions import has_subject_view_permissions |
20 | 20 | from channels import Group |
21 | 21 | import json |
22 | 22 | |
23 | +from log.models import Log | |
24 | +from log.mixins import LogMixin | |
25 | +import time | |
26 | + | |
23 | 27 | from categories.models import Category |
24 | 28 | from subjects.models import Subject |
25 | 29 | from users.models import User |
... | ... | @@ -27,7 +31,12 @@ from users.models import User |
27 | 31 | from .models import Conversation, TalkMessages, ChatVisualizations, ChatFavorites |
28 | 32 | from .forms import ChatMessageForm |
29 | 33 | |
30 | -class GeneralIndex(LoginRequiredMixin, generic.ListView): | |
34 | +class GeneralIndex(LoginRequiredMixin, LogMixin, generic.ListView): | |
35 | + log_component = "chat" | |
36 | + log_action = "view" | |
37 | + log_resource = "general" | |
38 | + log_context = {} | |
39 | + | |
31 | 40 | login_url = reverse_lazy("users:login") |
32 | 41 | redirect_field_name = 'next' |
33 | 42 | |
... | ... | @@ -47,13 +56,24 @@ class GeneralIndex(LoginRequiredMixin, generic.ListView): |
47 | 56 | def get_context_data(self, **kwargs): |
48 | 57 | context = super(GeneralIndex, self).get_context_data(**kwargs) |
49 | 58 | |
59 | + self.log_context['timestamp_start'] = str(int(time.time())) | |
60 | + | |
61 | + super(GeneralIndex, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
62 | + | |
63 | + self.request.session['log_id'] = Log.objects.latest('id').id | |
64 | + | |
50 | 65 | context['title'] = _('Messages') |
51 | 66 | context['totals'] = self.totals |
52 | 67 | context['chat_menu_active'] = 'subjects_menu_active' |
53 | 68 | |
54 | 69 | return context |
55 | 70 | |
56 | -class GeneralParticipants(LoginRequiredMixin, generic.ListView): | |
71 | +class GeneralParticipants(LoginRequiredMixin, LogMixin, generic.ListView): | |
72 | + log_component = "chat" | |
73 | + log_action = "view" | |
74 | + log_resource = "general_participants" | |
75 | + log_context = {} | |
76 | + | |
57 | 77 | login_url = reverse_lazy("users:login") |
58 | 78 | redirect_field_name = 'next' |
59 | 79 | |
... | ... | @@ -74,6 +94,13 @@ class GeneralParticipants(LoginRequiredMixin, generic.ListView): |
74 | 94 | def get_context_data(self, **kwargs): |
75 | 95 | context = super(GeneralParticipants, self).get_context_data(**kwargs) |
76 | 96 | |
97 | + self.log_context['search_by'] = self.request.GET.get('search', '') | |
98 | + self.log_context['timestamp_start'] = str(int(time.time())) | |
99 | + | |
100 | + super(GeneralParticipants, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
101 | + | |
102 | + self.request.session['log_id'] = Log.objects.latest('id').id | |
103 | + | |
77 | 104 | context['title'] = _('Messages - Participants') |
78 | 105 | context['totals'] = self.totals |
79 | 106 | context['search'] = self.request.GET.get('search', '') |
... | ... | @@ -81,7 +108,12 @@ class GeneralParticipants(LoginRequiredMixin, generic.ListView): |
81 | 108 | |
82 | 109 | return context |
83 | 110 | |
84 | -class SubjectParticipants(LoginRequiredMixin, generic.ListView): | |
111 | +class SubjectParticipants(LoginRequiredMixin, LogMixin, generic.ListView): | |
112 | + log_component = "chat" | |
113 | + log_action = "view" | |
114 | + log_resource = "subject_participants" | |
115 | + log_context = {} | |
116 | + | |
85 | 117 | login_url = reverse_lazy("users:login") |
86 | 118 | redirect_field_name = 'next' |
87 | 119 | |
... | ... | @@ -90,13 +122,10 @@ class SubjectParticipants(LoginRequiredMixin, generic.ListView): |
90 | 122 | paginate_by = 10 |
91 | 123 | |
92 | 124 | def dispatch(self, request, *args,**kwargs): |
93 | - typep = self.request.GET.get('type', '') | |
94 | - | |
95 | - if not typep == 'modal': | |
96 | - subject = get_object_or_404(Subject, id = kwargs.get('subject', 0)) | |
125 | + subject = get_object_or_404(Subject, id = kwargs.get('subject', 0)) | |
97 | 126 | |
98 | - if not has_subject_view_permissions(request.user, subject): | |
99 | - return redirect(reverse_lazy('subjects:home')) | |
127 | + if not has_subject_view_permissions(request.user, subject): | |
128 | + return redirect(reverse_lazy('subjects:home')) | |
100 | 129 | |
101 | 130 | return super(SubjectParticipants, self).dispatch(request, *args, **kwargs) |
102 | 131 | |
... | ... | @@ -112,6 +141,13 @@ class SubjectParticipants(LoginRequiredMixin, generic.ListView): |
112 | 141 | def get_context_data(self, **kwargs): |
113 | 142 | context = super(SubjectParticipants, self).get_context_data(**kwargs) |
114 | 143 | |
144 | + self.log_context['search_by'] = self.request.GET.get('search', '') | |
145 | + self.log_context['timestamp_start'] = str(int(time.time())) | |
146 | + | |
147 | + super(SubjectParticipants, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
148 | + | |
149 | + self.request.session['log_id'] = Log.objects.latest('id').id | |
150 | + | |
115 | 151 | sub = self.kwargs.get('subject', 0) |
116 | 152 | subject = get_object_or_404(Subject, id = sub) |
117 | 153 | |
... | ... | @@ -124,7 +160,12 @@ class SubjectParticipants(LoginRequiredMixin, generic.ListView): |
124 | 160 | |
125 | 161 | return context |
126 | 162 | |
127 | -class SubjectView(LoginRequiredMixin, generic.ListView): | |
163 | +class SubjectView(LoginRequiredMixin, LogMixin, generic.ListView): | |
164 | + log_component = "chat" | |
165 | + log_action = "view" | |
166 | + log_resource = "subject" | |
167 | + log_context = {} | |
168 | + | |
128 | 169 | login_url = reverse_lazy("users:login") |
129 | 170 | redirect_field_name = 'next' |
130 | 171 | |
... | ... | @@ -158,12 +199,26 @@ class SubjectView(LoginRequiredMixin, generic.ListView): |
158 | 199 | slug = self.kwargs.get('slug', None) |
159 | 200 | subject = get_object_or_404(Subject, slug = slug) |
160 | 201 | |
202 | + self.log_context['subject_id'] = subject.id | |
203 | + self.log_context['subject_name'] = subject.name | |
204 | + self.log_context['subject_slug'] = subject.slug | |
205 | + self.log_context['timestamp_start'] = str(int(time.time())) | |
206 | + | |
207 | + super(SubjectView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
208 | + | |
209 | + self.request.session['log_id'] = Log.objects.latest('id').id | |
210 | + | |
161 | 211 | context['title'] = _('%s - Messages')%(str(subject)) |
162 | 212 | context['subject'] = subject |
163 | 213 | |
164 | 214 | return context |
165 | 215 | |
166 | -class ParticipantProfile(LoginRequiredMixin, generic.DetailView): | |
216 | +class ParticipantProfile(LoginRequiredMixin, LogMixin, generic.DetailView): | |
217 | + log_component = "chat" | |
218 | + log_action = "view" | |
219 | + log_resource = "profile" | |
220 | + log_context = {} | |
221 | + | |
167 | 222 | login_url = reverse_lazy("users:login") |
168 | 223 | redirect_field_name = 'next' |
169 | 224 | |
... | ... | @@ -176,12 +231,23 @@ class ParticipantProfile(LoginRequiredMixin, generic.DetailView): |
176 | 231 | def get_context_data(self, **kwargs): |
177 | 232 | context = super(ParticipantProfile, self).get_context_data(**kwargs) |
178 | 233 | |
234 | + self.log_context['user_id'] = self.object.id | |
235 | + self.log_context['user_name'] = str(self.object) | |
236 | + self.log_context['user_email'] = self.object.email | |
237 | + | |
238 | + super(ParticipantProfile, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
239 | + | |
179 | 240 | context['space'] = self.request.GET.get('space', '0') |
180 | 241 | context['space_type'] = self.request.GET.get('space_type', 'general') |
181 | 242 | |
182 | 243 | return context |
183 | 244 | |
184 | -class GetTalk(LoginRequiredMixin, generic.ListView): | |
245 | +class GetTalk(LoginRequiredMixin, LogMixin, generic.ListView): | |
246 | + log_component = "chat" | |
247 | + log_action = "view" | |
248 | + log_resource = "talk" | |
249 | + log_context = {} | |
250 | + | |
185 | 251 | login_url = reverse_lazy("users:login") |
186 | 252 | redirect_field_name = 'next' |
187 | 253 | |
... | ... | @@ -221,9 +287,21 @@ class GetTalk(LoginRequiredMixin, generic.ListView): |
221 | 287 | context['form'] = ChatMessageForm() |
222 | 288 | context['form_url'] = reverse_lazy('chat:create', args = (), kwargs = {'email': self.kwargs.get('email', ''), 'talk_id': self.talk_id, 'space': self.request.GET.get('space', '0'), 'space_type': self.request.GET.get('space_type', 'general')}) |
223 | 289 | |
290 | + self.log_context['talk_id'] = self.talk_id | |
291 | + self.log_context['user_id'] = context['participant'].id | |
292 | + self.log_context['user_name'] = str(context['participant']) | |
293 | + self.log_context['user_email'] = context['participant'].email | |
294 | + | |
295 | + super(GetTalk, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
296 | + | |
224 | 297 | return context |
225 | 298 | |
226 | -class SendMessage(LoginRequiredMixin, generic.edit.CreateView): | |
299 | +class SendMessage(LoginRequiredMixin, LogMixin, generic.edit.CreateView): | |
300 | + log_component = "chat" | |
301 | + log_action = "send" | |
302 | + log_resource = "message" | |
303 | + log_context = {} | |
304 | + | |
227 | 305 | login_url = reverse_lazy("users:login") |
228 | 306 | redirect_field_name = 'next' |
229 | 307 | |
... | ... | @@ -292,6 +370,22 @@ class SendMessage(LoginRequiredMixin, generic.edit.CreateView): |
292 | 370 | return context |
293 | 371 | |
294 | 372 | def get_success_url(self): |
373 | + user = get_object_or_404(User, email = self.kwargs.get('email', '')) | |
374 | + | |
375 | + self.log_context = {} | |
376 | + | |
377 | + self.log_context['talk_id'] = self.object.talk.id | |
378 | + self.log_context['user_id'] = user.id | |
379 | + self.log_context['user_name'] = str(user) | |
380 | + self.log_context['user_email'] = user.email | |
381 | + | |
382 | + if self.object.subject: | |
383 | + self.log_context['subject_id'] = self.object.subject.id | |
384 | + self.log_context['subject_name'] = self.object.subject.name | |
385 | + self.log_context['subject_slug'] = self.object.subject.slug | |
386 | + | |
387 | + super(SendMessage, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | |
388 | + | |
295 | 389 | return reverse_lazy('chat:render_message', args = (self.object.id, self.object.talk.id, self.kwargs.get('space', '0'), self.kwargs.get('space_type', 'general'), self.kwargs.get('email', ''),)) |
296 | 390 | |
297 | 391 | def render_message(request, talk_msg, talk_id, space, space_type, email): | ... | ... |