serializers.py
5.42 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
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
from topics.models import Topic, Resource
from pendencies.models import Pendencies
from .models import Goals, GoalItem
class GoalItemSerializer(serializers.ModelSerializer):
class Meta:
model = GoalItem
exclude = ('goal',)
class SimpleGoalSerializer(serializers.ModelSerializer):
topic = TopicSerializer('get_subject')
tags = TagSerializer(many = True)
item_goal = GoalItemSerializer(many = True)
pendencies_resource = PendenciesSerializer(many = True)
def get_subject(self, obj):
subject = self.context.get("subject", None)
return subject
class Meta:
model = Goals
exclude = ('students', 'groups',)
def create(self, data):
topic = data['topic']
goals = 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'])
data["topic"] = topic
else:
data["topic"] = get_object_or_404(Topic, id = topic["id"])
goals_data = data
pendencies = goals_data["pendencies_resource"]
del goals_data["pendencies_resource"]
goal_items = goals_data["item_goal"]
del goals_data["item_goal"]
goals = Goals()
goals.name = goals_data["name"]
goals.brief_description = goals_data["brief_description"]
goals.show_window = goals_data["show_window"]
goals.all_students = goals_data["all_students"]
goals.visible = goals_data["visible"]
goals.order = goals_data["order"]
goals.topic = goals_data["topic"]
goals.presentation = goals_data["presentation"]
goals.limit_submission_date = goals_data["limit_submission_date"]
goals.save()
tags = data["tags"]
for tag in tags:
if tag["id"] == "":
tag = Tag.objects.create(name = tag["name"])
else:
tag = get_object_or_404(Tag, id = tag["id"])
goals.tags.add(tag)
resource = get_object_or_404(Resource, id = goals.id)
for item in goal_items:
GoalItem.objects.create(goal = goals, **item)
for pend in pendencies:
Pendencies.objects.create(resource = resource, **pend)
return goals
def update(self, instance, data):
return instance
class CompleteGoalSerializer(serializers.ModelSerializer):
topic = TopicSerializer('get_subject')
tags = TagSerializer(many = True)
item_goal = GoalItemSerializer(many = True)
pendencies_resource = PendenciesSerializer(many = True)
groups = StudentsGroupSerializer(many = True)
students = UserBackupSerializer(many = True)
def get_subject(self, obj):
subject = self.context.get("subject", None)
return subject
class Meta:
model = Goals
fields = '__all__'
def create(self, data):
topic = data['topic']
goals = 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'])
data["topic"] = topic
else:
data["topic"] = get_object_or_404(Topic, id = topic["id"])
goals_data = data
pendencies = goals_data["pendencies_resource"]
del goals_data["pendencies_resource"]
goal_items = goals_data["item_goal"]
del goals_data["item_goal"]
goals = Goals()
goals.name = goals_data["name"]
goals.brief_description = goals_data["brief_description"]
goals.show_window = goals_data["show_window"]
goals.all_students = goals_data["all_students"]
goals.visible = goals_data["visible"]
goals.order = goals_data["order"]
goals.topic = goals_data["topic"]
goals.presentation = goals_data["presentation"]
goals.limit_submission_date = goals_data["limit_submission_date"]
goals.save()
tags = data["tags"]
for tag in tags:
if tag["id"] == "":
tag = Tag.objects.create(name = tag["name"])
else:
tag = get_object_or_404(Tag, id = tag["id"])
goals.tags.add(tag)
resource = get_object_or_404(Resource, id = goals.id)
for item in goal_items:
GoalItem.objects.create(goal = goals, **item)
for pend in pendencies:
Pendencies.objects.create(resource = resource, **pend)
return goals