views.py
8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import requests, json
from django.shortcuts import get_object_or_404, reverse
from django.contrib.auth import authenticate
from django.views.decorators.csrf import csrf_exempt
from django.utils.translation import ugettext as _
from django.template.loader import render_to_string
import textwrap
from datetime import datetime
from django.utils import formats
from django.utils.html import strip_tags
from channels import Group
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import detail_route, list_route
from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
from django.db.models import Q
from security.models import Security
from chat.serializers import ChatSerializer
from chat.models import TalkMessages, Conversation, ChatVisualizations
from subjects.serializers import SubjectSerializer
from subjects.models import Subject
from users.serializers import UserSerializer
from users.models import User
from oauth2_provider.views.generic import ProtectedResourceView
from oauth2_provider.models import Application
from django.http import HttpResponse
@csrf_exempt
def getToken(request):
oauth = Application.objects.filter(name = "amadeus-droid")
security = Security.objects.get(id = 1)
response = ""
if request.method == "POST":
json_data = json.loads(request.body.decode('utf-8'))
try:
username = json_data['email']
password = json_data['password']
user = authenticate(username = username, password = password)
if user is not None:
if not security.maintence or user.is_staff:
if oauth.count() > 0:
oauth = oauth[0]
data = {
"grant_type": "password",
"username": username,
"password": password
}
auth = (oauth.client_id, oauth.client_secret)
response = requests.post(request.build_absolute_uri(reverse('oauth2_provider:token')), data = data, auth = auth)
json_r = json.loads(response.content.decode('utf-8'))
json_r["message"] = ""
json_r["type"] = ""
json_r["title"] = ""
json_r["success"] = True
json_r["number"] = 1
json_r['extra'] = 0
response = json.dumps(json_r)
except KeyError:
response = "Error"
return HttpResponse(response)
class LoginViewset(viewsets.ReadOnlyModelViewSet):
queryset = User.objects.all()
permissions_classes = (IsAuthenticated,)
@csrf_exempt
@list_route(methods = ['POST'], permissions_classes = [IsAuthenticated])
def login(self, request):
username = request.data['email']
user = self.queryset.get(email = username)
response = ""
if not user is None:
serializer = UserSerializer(user)
json_r = json.dumps(serializer.data)
json_r = json.loads(json_r)
user_info = {}
user_info["data"] = json_r
user_info["message"] = ""
user_info["type"] = ""
user_info["title"] = ""
user_info["success"] = True
user_info["number"] = 1
user_info['extra'] = 0
response = json.dumps(user_info)
return HttpResponse(response)
class SubjectViewset(viewsets.ReadOnlyModelViewSet):
queryset = Subject.objects.all()
permissions_classes = (IsAuthenticated, )
@csrf_exempt
@list_route(methods = ['POST'], permissions_classes = [IsAuthenticated])
def get_subjects(self, request):
username = request.data['email']
user = User.objects.get(email = username)
subjects = None
response = ""
if not user is None:
if user.is_staff:
subjects = Subject.objects.all().order_by("name")
else:
pk = user.pk
subjects = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk)).distinct()
serializer = SubjectSerializer(subjects, many = True)
json_r = json.dumps(serializer.data)
json_r = json.loads(json_r)
sub_info = {}
sub_info["data"] = {}
sub_info["data"]["subjects"] = json_r
sub_info["message"] = ""
sub_info["type"] = ""
sub_info["title"] = ""
sub_info["success"] = True
sub_info["number"] = 1
sub_info['extra'] = 0
response = json.dumps(sub_info)
return HttpResponse(response)
class ParticipantsViewset(viewsets.ReadOnlyModelViewSet):
queryset = User.objects.all()
permissions_classes = (IsAuthenticated, )
@csrf_exempt
@list_route(methods = ['POST'], permissions_classes = [IsAuthenticated])
def get_participants(self, request):
username = request.data['email']
subject_slug = request.data['subject_slug']
participants = None
response = ""
if not subject_slug == "":
participants = User.objects.filter(Q(is_staff = True) | Q(subject_student__slug = subject_slug) | Q(professors__slug = subject_slug) | Q(coordinators__subject_category__slug = subject_slug)).exclude(email = username).distinct()
serializer = UserSerializer(participants, many = True)
json_r = json.dumps(serializer.data)
json_r = json.loads(json_r)
info = {}
info["data"] = {}
info["data"]["participants"] = json_r
info["message"] = ""
info["type"] = ""
info["title"] = ""
info["success"] = True
info["number"] = 1
info['extra'] = 0
response = json.dumps(info)
return HttpResponse(response)
class ChatViewset(viewsets.ModelViewSet):
queryset = TalkMessages.objects.all()
permissions_classes = (IsAuthenticated, )
@csrf_exempt
@list_route(methods = ['POST'], permissions_classes = [IsAuthenticated])
def get_messages(self, request):
username = request.data['email']
user_two = request.data['user_two']
messages = None
response = ""
if not user_two == "":
messages = TalkMessages.objects.filter((Q(talk__user_one__email = username) & Q(talk__user_two__email = user_two)) | (Q(talk__user_one__email = user_two) & Q(talk__user_two__email = username))).order_by('-create_date')
serializer = ChatSerializer(messages, many = True)
json_r = json.dumps(serializer.data)
json_r = json.loads(json_r)
info = {}
info["data"] = {}
info["data"]["messages"] = json_r
info["message"] = ""
info["type"] = ""
info["title"] = ""
info["success"] = True
info["number"] = 1
info['extra'] = 0
response = json.dumps(info)
return HttpResponse(response)
@csrf_exempt
@list_route(methods = ['POST'], permissions_classes = [IsAuthenticated])
def send_message(self, request):
username = request.data['email']
user_two = request.data['user_two']
subject = request.data['subject']
msg_text = request.data['text']
create_date = request.data['create_date']
info = {}
if not user_two == "" and not username == "":
user = User.objects.get(email = username)
user_to = User.objects.get(email = user_two)
talks = Conversation.objects.filter((Q(user_one__email = username) & Q(user_two__email = user_two)) | (Q(user_two__email = username) & Q(user_one__email = user_two)))
if talks.count() > 0:
talk = talks[0]
else:
talk = Conversation()
talk.user_one = user
talk.user_two = user_to
talk.save()
subject = Subject.objects.get(slug = subject)
message = TalkMessages()
message.text = "<p>" + msg_text + "</p>"
message.user = user
message.talk = talk
message.save()
if not message.pk is None:
simple_notify = textwrap.shorten(strip_tags(message.text), width = 30, placeholder = "...")
notification = {
"type": "chat",
"subtype": "subject",
"space": subject.slug,
"user_icon": message.user.image_url,
"notify_title": str(message.user),
"simple_notify": simple_notify,
"view_url": reverse("chat:view_message", args = (message.id, ), kwargs = {}),
"complete": render_to_string("chat/_message.html", {"talk_msg": message}, request),
"container": "chat-" + str(message.user.id),
"last_date": _("Last message in %s")%(formats.date_format(message.create_date, "SHORT_DATETIME_FORMAT"))
}
notification = json.dumps(notification)
Group("user-%s" % user_to.id).send({'text': notification})
ChatVisualizations.objects.create(viewed = False, message = message, user = user_to)
info["message"] = _("Message sent successfully!")
info["success"] = True
info["number"] = 1
else:
info["message"] = _("Error while sending message!")
info["success"] = False
info["number"] = 0
else:
info["message"] = _("No information received!")
info["success"] = False
info["number"] = 0
info["data"] = {}
info["data"]["messages"] = []
info["type"] = ""
info["title"] = _("Amadeus")
info['extra'] = 0
response = json.dumps(info)
return HttpResponse(response)