serializers.py
12.3 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
Copyright 2016, 2017 UFPE - Universidade Federal de Pernambuco
Este arquivo é parte do programa Amadeus Sistema de Gestão de Aprendizagem, ou simplesmente Amadeus LMS
O Amadeus LMS é um software livre; você pode redistribui-lo e/ou modifica-lo dentro dos termos da Licença Pública Geral GNU como publicada pela Fundação do Software Livre (FSF); na versão 2 da Licença.
Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU para maiores detalhes.
Você deve ter recebido uma cópia da Licença Pública Geral GNU, sob o título "LICENSE", junto com este programa, se não, escreva para a Fundação do Software Livre (FSF) Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
"""
import os
import zipfile
import time
from django.conf import settings
from django.core.files import File
from rest_framework import serializers
from django.shortcuts import get_object_or_404
from subjects.serializers import TagSerializer
from topics.serializers import TopicSerializer
from pendencies.serializers import PendenciesSerializer
from students_group.serializers import StudentsGroupSerializer
from users.serializers import UserBackupSerializer
from subjects.models import Tag, Subject
from topics.models import Topic, Resource
from pendencies.models import Pendencies
from students_group.models import StudentsGroup
from log.models import Log
from users.models import User
from .models import Bulletin
class SimpleBulletinSerializer(serializers.ModelSerializer):
topic = TopicSerializer('get_subject')
tags = TagSerializer(many = True)
pendencies_resource = PendenciesSerializer(many = True)
indicators = serializers.CharField(required = False, allow_blank = True, max_length = 255)
file_content = serializers.CharField(required = False, allow_blank = True, max_length = 255)
def get_subject(self, obj):
subject = self.context.get("subject", None)
return subject
def validate(self, data):
files = self.context.get('files', None)
if files:
if data["file_content"] in files.namelist():
file_path = os.path.join(settings.MEDIA_ROOT, data["file_content"])
if os.path.isfile(file_path):
dst_path = os.path.join(settings.MEDIA_ROOT, "tmp")
path = files.extract(data["file_content"], dst_path)
new_name = "goal_" + str(time.time()) + os.path.splitext(data["file_content"])[1]
new_path = os.path.join("bulletin", os.path.join("goals", new_name))
os.rename(os.path.join(dst_path, path), os.path.join(settings.MEDIA_ROOT, new_path))
data["file_content"] = new_path
else:
path = files.extract(data["file_content"], settings.MEDIA_ROOT)
else:
data["file_content"] = None
if data["indicators"] in files.namelist():
file_path = os.path.join(settings.MEDIA_ROOT, data["indicators"])
if os.path.isfile(file_path):
dst_path = os.path.join(settings.MEDIA_ROOT, "tmp")
path = files.extract(data["indicators"], dst_path)
new_name = "ind_" + str(time.time()) + os.path.splitext(data["indicators"])[1]
new_path = os.path.join("bulletin", os.path.join("indicators", new_name))
os.rename(os.path.join(dst_path, path), os.path.join(settings.MEDIA_ROOT, new_path))
data["indicators"] = new_path
else:
path = files.extract(data["indicators"], settings.MEDIA_ROOT)
else:
data["indicators"] = None
else:
data["file_content"] = None
data["indicators"] = None
return data
class Meta:
model = Bulletin
extra_kwargs = {
"tags": {
"validators": [],
},
}
exclude = ('students', 'groups',)
validators = []
def create(self, data):
topic = data['topic']
bulletin = None
if not topic["id"] is None:
if "subject" in topic:
r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"])
else:
r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"])
if not r_exits.exists():
if topic['id'] == "":
topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"])
if topic_exist.exists():
topic = topic_exist[0]
else:
topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order'], description = topic['description'])
data["topic"] = topic
else:
data["topic"] = get_object_or_404(Topic, id = topic["id"])
bulletin_data = data
pendencies = bulletin_data["pendencies_resource"]
del bulletin_data["pendencies_resource"]
bulletin = Bulletin()
bulletin.name = bulletin_data["name"]
bulletin.brief_description = bulletin_data["brief_description"]
bulletin.show_window = bulletin_data["show_window"]
bulletin.all_students = bulletin_data["all_students"]
bulletin.visible = bulletin_data["visible"]
bulletin.order = bulletin_data["order"]
bulletin.topic = bulletin_data["topic"]
bulletin.content = bulletin_data["content"]
bulletin.file_content = bulletin_data["file_content"]
bulletin.indicators = bulletin_data["indicators"]
bulletin.save()
tags = data["tags"]
for tag in tags:
if not tag["name"] == "":
if tag["id"] == "":
tag = Tag.objects.create(name = tag["name"])
else:
tag = get_object_or_404(Tag, id = tag["id"])
bulletin.tags.add(tag)
resource = get_object_or_404(Resource, id = bulletin.id)
for pend in pendencies:
Pendencies.objects.create(resource = resource, **pend)
return bulletin
def update(self, instance, data):
return instance
class CompleteBulletinSerializer(serializers.ModelSerializer):
topic = TopicSerializer('get_subject')
tags = TagSerializer(many = True)
pendencies_resource = PendenciesSerializer(many = True)
groups = StudentsGroupSerializer('get_files', many = True)
students = UserBackupSerializer('get_files', many = True)
indicators = serializers.CharField(required = False, allow_blank = True, max_length = 255)
file_content = serializers.CharField(required = False, allow_blank = True, max_length = 255)
def get_subject(self, obj):
subject = self.context.get("subject", None)
return subject
def get_files(self, obj):
files = self.context.get("files", None)
return files
def validate(self, data):
files = self.context.get('files', None)
if files:
if data["file_content"] in files.namelist():
file_path = os.path.join(settings.MEDIA_ROOT, data["file_content"])
if os.path.isfile(file_path):
dst_path = os.path.join(settings.MEDIA_ROOT, "tmp")
path = files.extract(data["file_content"], dst_path)
new_name = "goal_" + str(time.time()) + os.path.splitext(data["file_content"])[1]
new_path = os.path.join("bulletin", os.path.join("goals", new_name))
os.rename(os.path.join(dst_path, path), os.path.join(settings.MEDIA_ROOT, new_path))
data["file_content"] = new_path
else:
path = files.extract(data["file_content"], settings.MEDIA_ROOT)
else:
data["file_content"] = None
if data["indicators"] in files.namelist():
file_path = os.path.join(settings.MEDIA_ROOT, data["indicators"])
if os.path.isfile(file_path):
dst_path = os.path.join(settings.MEDIA_ROOT, "tmp")
path = files.extract(data["indicators"], dst_path)
new_name = "ind_" + str(time.time()) + os.path.splitext(data["indicators"])[1]
new_path = os.path.join("bulletin", os.path.join("indicators", new_name))
os.rename(os.path.join(dst_path, path), os.path.join(settings.MEDIA_ROOT, new_path))
data["indicators"] = new_path
else:
path = files.extract(data["indicators"], settings.MEDIA_ROOT)
else:
data["indicators"] = None
else:
data["file_content"] = None
data["indicators"] = None
return data
class Meta:
model = Bulletin
extra_kwargs = {
"tags": {
"validators": [],
},
}
fields = '__all__'
validators = []
def create(self, data):
topic = data['topic']
bulletin = None
if not topic["id"] is None:
if "subject" in topic:
r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"])
else:
r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"])
if not r_exits.exists():
if topic['id'] == "":
topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"])
if topic_exist.exists():
topic = topic_exist[0]
else:
topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order'], description = topic['description'])
data["topic"] = topic
else:
data["topic"] = get_object_or_404(Topic, id = topic["id"])
bulletin_data = data
pendencies = bulletin_data["pendencies_resource"]
del bulletin_data["pendencies_resource"]
bulletin = Bulletin()
bulletin.name = bulletin_data["name"]
bulletin.brief_description = bulletin_data["brief_description"]
bulletin.show_window = bulletin_data["show_window"]
bulletin.all_students = bulletin_data["all_students"]
bulletin.visible = bulletin_data["visible"]
bulletin.order = bulletin_data["order"]
bulletin.topic = bulletin_data["topic"]
bulletin.content = bulletin_data["content"]
bulletin.file_content = bulletin_data["file_content"]
bulletin.indicators = bulletin_data["indicators"]
bulletin.save()
tags = data["tags"]
for tag in tags:
if not tag["name"] == "":
if tag["id"] == "":
tag = Tag.objects.create(name = tag["name"])
else:
tag = get_object_or_404(Tag, id = tag["id"])
bulletin.tags.add(tag)
resource = get_object_or_404(Resource, id = bulletin.id)
students = data["students"]
subject = get_object_or_404(Subject, slug = self.context.get("subject", None))
for student_data in students:
logs = student_data["get_items"]
if student_data["id"] == "":
u_exist = User.objects.filter(email = student_data["email"])
if not u_exist.exists():
student = u_exist[0]
for log in logs:
log["user_id"] = student.id
l_exists = Log.objects.filter(user_id = log["user_id"], user = log["user"], user_email = log["user_email"], action = log["action"], resource = log["resource"], component = log["component"], context = log["context"])
if not l_exists.exists():
Log.objects.create(**log)
else:
student = User()
student.email = student_data["email"]
student.username = student_data["username"]
student.last_name = student_data["last_name"]
student.social_name = student_data["social_name"]
student.show_email = student_data["show_email"]
student.is_staff = student_data["is_staff"]
student.is_active = student_data["is_active"]
student.image = student_data["image"]
student.save()
for log in logs:
log["user_id"] = student.id
Log.objects.create(**log)
else:
student = get_object_or_404(User, id = student_data["id"])
for log in logs:
l_exists = Log.objects.filter(user_id = log["user_id"], user = log["user"], user_email = log["user_email"], action = log["action"], resource = log["resource"], component = log["component"], context = log["context"])
if not l_exists.exists():
Log.objects.create(**log)
bulletin.students.add(student)
subject.students.add(student)
groups = data["groups"]
for group_data in groups:
g_exists = StudentsGroup.objects.filter(subject = subject, slug = group_data["slug"])
if g_exists.exists():
group = g_exists[0]
else:
group = StudentsGroup()
group.name = group_data["name"]
group.description = group_data["description"]
group.subject = subject
group.save()
for participant in group_data["participants"]:
p_user = get_object_or_404(User, email = participant["email"])
group.participants.add(p_user)
bulletin.groups.add(group)
for pend in pendencies:
Pendencies.objects.create(resource = resource, **pend)
return bulletin
def update(self, instance, data):
return instance