Commit 5e2e0c8fb80eb15a8b394466726f00cb18d4324c
1 parent
d2ba4692
Exists in
master
and in
2 other branches
Start writing validationand save scripts for complete restore
Showing
6 changed files
with
451 additions
and
11 deletions
Show diff stats
file_link/serializers.py
| ... | ... | @@ -130,12 +130,102 @@ class SimpleFileLinkSerializer(serializers.ModelSerializer): |
| 130 | 130 | |
| 131 | 131 | class CompleteFileLinkSerializer(serializers.ModelSerializer): |
| 132 | 132 | file_content = serializers.CharField(required = False, allow_blank = True) |
| 133 | - topic = TopicSerializer() | |
| 133 | + topic = TopicSerializer('get_subject') | |
| 134 | 134 | tags = TagSerializer(many = True) |
| 135 | 135 | pendencies_resource = PendenciesSerializer(many = True) |
| 136 | 136 | groups = StudentsGroupSerializer(many = True) |
| 137 | 137 | students = UserBackupSerializer(many = True) |
| 138 | 138 | |
| 139 | + def get_subject(self, obj): | |
| 140 | + subject = self.context.get("subject", None) | |
| 141 | + | |
| 142 | + return subject | |
| 143 | + | |
| 144 | + def validate(self, data): | |
| 145 | + files = self.context.get('files', None) | |
| 146 | + | |
| 147 | + if files: | |
| 148 | + file_path = os.path.join(settings.MEDIA_ROOT, data["file_content"]) | |
| 149 | + | |
| 150 | + if os.path.isfile(file_path): | |
| 151 | + dst_path = os.path.join(settings.MEDIA_ROOT, "tmp") | |
| 152 | + | |
| 153 | + path = files.extract(data["file_content"], dst_path) | |
| 154 | + | |
| 155 | + new_name = "files/file_" + str(time.time()) + os.path.splitext(data["file_content"])[1] | |
| 156 | + | |
| 157 | + os.rename(os.path.join(dst_path, path), os.path.join(settings.MEDIA_ROOT, new_name)) | |
| 158 | + | |
| 159 | + data["file_content"] = new_name | |
| 160 | + else: | |
| 161 | + path = files.extract(data["file_content"], settings.MEDIA_ROOT) | |
| 162 | + | |
| 163 | + return data | |
| 164 | + | |
| 139 | 165 | class Meta: |
| 140 | 166 | model = FileLink |
| 141 | 167 | fields = '__all__' |
| 168 | + | |
| 169 | + def create(self, data): | |
| 170 | + topic = data['topic'] | |
| 171 | + | |
| 172 | + file_link = None | |
| 173 | + | |
| 174 | + if not topic["id"] is None: | |
| 175 | + if "subject" in topic: | |
| 176 | + r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"]) | |
| 177 | + else: | |
| 178 | + r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"]) | |
| 179 | + | |
| 180 | + if not r_exits.exists(): | |
| 181 | + if topic['id'] == "": | |
| 182 | + topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | |
| 183 | + | |
| 184 | + if topic_exist.exists(): | |
| 185 | + topic = topic_exist[0] | |
| 186 | + else: | |
| 187 | + topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | |
| 188 | + | |
| 189 | + data["topic"] = topic | |
| 190 | + else: | |
| 191 | + data["topic"] = get_object_or_404(Topic, id = topic["id"]) | |
| 192 | + | |
| 193 | + | |
| 194 | + f = open(os.path.join(settings.MEDIA_ROOT, data["file_content"]), encoding="latin-1") | |
| 195 | + file = File(f) | |
| 196 | + | |
| 197 | + data["file_content"] = file | |
| 198 | + | |
| 199 | + file_link_data = data | |
| 200 | + | |
| 201 | + pendencies = file_link_data["pendencies_resource"] | |
| 202 | + del file_link_data["pendencies_resource"] | |
| 203 | + | |
| 204 | + file_link = FileLink() | |
| 205 | + file_link.name = file_link_data["name"] | |
| 206 | + file_link.brief_description = file_link_data["brief_description"] | |
| 207 | + file_link.show_window = file_link_data["show_window"] | |
| 208 | + file_link.all_students = file_link_data["all_students"] | |
| 209 | + file_link.visible = file_link_data["visible"] | |
| 210 | + file_link.order = file_link_data["order"] | |
| 211 | + file_link.topic = file_link_data["topic"] | |
| 212 | + file_link.file_content = file_link_data["file_content"] | |
| 213 | + | |
| 214 | + file_link.save() | |
| 215 | + | |
| 216 | + tags = data["tags"] | |
| 217 | + | |
| 218 | + for tag in tags: | |
| 219 | + if tag["id"] == "": | |
| 220 | + tag = Tag.objects.create(name = tag["name"]) | |
| 221 | + else: | |
| 222 | + tag = get_object_or_404(Tag, id = tag["id"]) | |
| 223 | + | |
| 224 | + file_link.tags.add(tag) | |
| 225 | + | |
| 226 | + resource = get_object_or_404(Resource, id = file_link.id) | |
| 227 | + | |
| 228 | + for pend in pendencies: | |
| 229 | + Pendencies.objects.create(resource = resource, **pend) | |
| 230 | + | |
| 231 | + return file_link | ... | ... |
goals/serializers.py
| ... | ... | @@ -102,13 +102,83 @@ class SimpleGoalSerializer(serializers.ModelSerializer): |
| 102 | 102 | return instance |
| 103 | 103 | |
| 104 | 104 | class CompleteGoalSerializer(serializers.ModelSerializer): |
| 105 | - topic = TopicSerializer() | |
| 105 | + topic = TopicSerializer('get_subject') | |
| 106 | 106 | tags = TagSerializer(many = True) |
| 107 | 107 | item_goal = GoalItemSerializer(many = True) |
| 108 | 108 | pendencies_resource = PendenciesSerializer(many = True) |
| 109 | 109 | groups = StudentsGroupSerializer(many = True) |
| 110 | 110 | students = UserBackupSerializer(many = True) |
| 111 | 111 | |
| 112 | + def get_subject(self, obj): | |
| 113 | + subject = self.context.get("subject", None) | |
| 114 | + | |
| 115 | + return subject | |
| 116 | + | |
| 112 | 117 | class Meta: |
| 113 | 118 | model = Goals |
| 114 | - fields = '__all__' | |
| 115 | 119 | \ No newline at end of file |
| 120 | + fields = '__all__' | |
| 121 | + | |
| 122 | + def create(self, data): | |
| 123 | + topic = data['topic'] | |
| 124 | + | |
| 125 | + goals = None | |
| 126 | + | |
| 127 | + if not topic["id"] is None: | |
| 128 | + if "subject" in topic: | |
| 129 | + r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"]) | |
| 130 | + else: | |
| 131 | + r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"]) | |
| 132 | + | |
| 133 | + if not r_exits.exists(): | |
| 134 | + if topic['id'] == "": | |
| 135 | + topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | |
| 136 | + | |
| 137 | + if topic_exist.exists(): | |
| 138 | + topic = topic_exist[0] | |
| 139 | + else: | |
| 140 | + topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | |
| 141 | + | |
| 142 | + data["topic"] = topic | |
| 143 | + else: | |
| 144 | + data["topic"] = get_object_or_404(Topic, id = topic["id"]) | |
| 145 | + | |
| 146 | + goals_data = data | |
| 147 | + | |
| 148 | + pendencies = goals_data["pendencies_resource"] | |
| 149 | + del goals_data["pendencies_resource"] | |
| 150 | + | |
| 151 | + goal_items = goals_data["item_goal"] | |
| 152 | + del goals_data["item_goal"] | |
| 153 | + | |
| 154 | + goals = Goals() | |
| 155 | + goals.name = goals_data["name"] | |
| 156 | + goals.brief_description = goals_data["brief_description"] | |
| 157 | + goals.show_window = goals_data["show_window"] | |
| 158 | + goals.all_students = goals_data["all_students"] | |
| 159 | + goals.visible = goals_data["visible"] | |
| 160 | + goals.order = goals_data["order"] | |
| 161 | + goals.topic = goals_data["topic"] | |
| 162 | + goals.presentation = goals_data["presentation"] | |
| 163 | + goals.limit_submission_date = goals_data["limit_submission_date"] | |
| 164 | + | |
| 165 | + goals.save() | |
| 166 | + | |
| 167 | + tags = data["tags"] | |
| 168 | + | |
| 169 | + for tag in tags: | |
| 170 | + if tag["id"] == "": | |
| 171 | + tag = Tag.objects.create(name = tag["name"]) | |
| 172 | + else: | |
| 173 | + tag = get_object_or_404(Tag, id = tag["id"]) | |
| 174 | + | |
| 175 | + goals.tags.add(tag) | |
| 176 | + | |
| 177 | + resource = get_object_or_404(Resource, id = goals.id) | |
| 178 | + | |
| 179 | + for item in goal_items: | |
| 180 | + GoalItem.objects.create(goal = goals, **item) | |
| 181 | + | |
| 182 | + for pend in pendencies: | |
| 183 | + Pendencies.objects.create(resource = resource, **pend) | |
| 184 | + | |
| 185 | + return goals | |
| 116 | 186 | \ No newline at end of file | ... | ... |
links/serializers.py
| ... | ... | @@ -89,12 +89,75 @@ class SimpleLinkSerializer(serializers.ModelSerializer): |
| 89 | 89 | return instance |
| 90 | 90 | |
| 91 | 91 | class CompleteLinkSerializer(serializers.ModelSerializer): |
| 92 | - topic = TopicSerializer() | |
| 92 | + topic = TopicSerializer('get_subject') | |
| 93 | 93 | tags = TagSerializer(many = True) |
| 94 | 94 | pendencies_resource = PendenciesSerializer(many = True) |
| 95 | 95 | groups = StudentsGroupSerializer(many = True) |
| 96 | 96 | students = UserBackupSerializer(many = True) |
| 97 | 97 | |
| 98 | + def get_subject(self, obj): | |
| 99 | + subject = self.context.get("subject", None) | |
| 100 | + | |
| 101 | + return subject | |
| 102 | + | |
| 98 | 103 | class Meta: |
| 99 | 104 | model = Link |
| 100 | - fields = '__all__' | |
| 101 | 105 | \ No newline at end of file |
| 106 | + fields = '__all__' | |
| 107 | + | |
| 108 | + def create(self, data): | |
| 109 | + topic = data['topic'] | |
| 110 | + | |
| 111 | + link = None | |
| 112 | + | |
| 113 | + if not topic["id"] is None: | |
| 114 | + if "subject" in topic: | |
| 115 | + r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"]) | |
| 116 | + else: | |
| 117 | + r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"]) | |
| 118 | + | |
| 119 | + if not r_exits.exists(): | |
| 120 | + if topic['id'] == "": | |
| 121 | + topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | |
| 122 | + | |
| 123 | + if topic_exist.exists(): | |
| 124 | + topic = topic_exist[0] | |
| 125 | + else: | |
| 126 | + topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | |
| 127 | + | |
| 128 | + data["topic"] = topic | |
| 129 | + else: | |
| 130 | + data["topic"] = get_object_or_404(Topic, id = topic["id"]) | |
| 131 | + | |
| 132 | + link_data = data | |
| 133 | + | |
| 134 | + pendencies = link_data["pendencies_resource"] | |
| 135 | + del link_data["pendencies_resource"] | |
| 136 | + | |
| 137 | + link = Link() | |
| 138 | + link.name = link_data["name"] | |
| 139 | + link.brief_description = link_data["brief_description"] | |
| 140 | + link.show_window = link_data["show_window"] | |
| 141 | + link.all_students = link_data["all_students"] | |
| 142 | + link.visible = link_data["visible"] | |
| 143 | + link.order = link_data["order"] | |
| 144 | + link.topic = link_data["topic"] | |
| 145 | + link.link_url = link_data["link_url"] | |
| 146 | + | |
| 147 | + link.save() | |
| 148 | + | |
| 149 | + tags = data["tags"] | |
| 150 | + | |
| 151 | + for tag in tags: | |
| 152 | + if tag["id"] == "": | |
| 153 | + tag = Tag.objects.create(name = tag["name"]) | |
| 154 | + else: | |
| 155 | + tag = get_object_or_404(Tag, id = tag["id"]) | |
| 156 | + | |
| 157 | + link.tags.add(tag) | |
| 158 | + | |
| 159 | + resource = get_object_or_404(Resource, id = link.id) | |
| 160 | + | |
| 161 | + for pend in pendencies: | |
| 162 | + Pendencies.objects.create(resource = resource, **pend) | |
| 163 | + | |
| 164 | + return link | |
| 102 | 165 | \ No newline at end of file | ... | ... |
pdf_file/serializers.py
| ... | ... | @@ -123,12 +123,102 @@ class SimplePDFFileSerializer(serializers.ModelSerializer): |
| 123 | 123 | |
| 124 | 124 | class CompletePDFFileSerializer(serializers.ModelSerializer): |
| 125 | 125 | file = serializers.CharField(required = False, allow_blank = True) |
| 126 | - topic = TopicSerializer() | |
| 126 | + topic = TopicSerializer('get_subject') | |
| 127 | 127 | tags = TagSerializer(many = True) |
| 128 | 128 | pendencies_resource = PendenciesSerializer(many = True) |
| 129 | 129 | groups = StudentsGroupSerializer(many = True) |
| 130 | 130 | students = UserBackupSerializer(many = True) |
| 131 | 131 | |
| 132 | + def get_subject(self, obj): | |
| 133 | + subject = self.context.get("subject", None) | |
| 134 | + | |
| 135 | + return subject | |
| 136 | + | |
| 137 | + def validate(self, data): | |
| 138 | + files = self.context.get('files', None) | |
| 139 | + | |
| 140 | + if files: | |
| 141 | + file_path = os.path.join(settings.MEDIA_ROOT, data["file"]) | |
| 142 | + | |
| 143 | + if os.path.isfile(file_path): | |
| 144 | + dst_path = os.path.join(settings.MEDIA_ROOT, "tmp") | |
| 145 | + | |
| 146 | + path = files.extract(data["file"], dst_path) | |
| 147 | + | |
| 148 | + new_name = "files/file_" + str(time.time()) + os.path.splitext(data["file"])[1] | |
| 149 | + | |
| 150 | + os.rename(os.path.join(dst_path, path), os.path.join(settings.MEDIA_ROOT, new_name)) | |
| 151 | + | |
| 152 | + data["file"] = new_name | |
| 153 | + else: | |
| 154 | + path = files.extract(data["file"], settings.MEDIA_ROOT) | |
| 155 | + | |
| 156 | + return data | |
| 157 | + | |
| 132 | 158 | class Meta: |
| 133 | 159 | model = PDFFile |
| 134 | - fields = '__all__' | |
| 135 | 160 | \ No newline at end of file |
| 161 | + fields = '__all__' | |
| 162 | + | |
| 163 | + def create(self, data): | |
| 164 | + topic = data['topic'] | |
| 165 | + | |
| 166 | + pdf = None | |
| 167 | + | |
| 168 | + if not topic["id"] is None: | |
| 169 | + if "subject" in topic: | |
| 170 | + r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"]) | |
| 171 | + else: | |
| 172 | + r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"]) | |
| 173 | + | |
| 174 | + if not r_exits.exists(): | |
| 175 | + if topic['id'] == "": | |
| 176 | + topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | |
| 177 | + | |
| 178 | + if topic_exist.exists(): | |
| 179 | + topic = topic_exist[0] | |
| 180 | + else: | |
| 181 | + topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | |
| 182 | + | |
| 183 | + data["topic"] = topic | |
| 184 | + else: | |
| 185 | + data["topic"] = get_object_or_404(Topic, id = topic["id"]) | |
| 186 | + | |
| 187 | + | |
| 188 | + f = open(os.path.join(settings.MEDIA_ROOT, data["file"]), encoding="latin-1") | |
| 189 | + file = File(f) | |
| 190 | + | |
| 191 | + data["file"] = file | |
| 192 | + | |
| 193 | + pdf_data = data | |
| 194 | + | |
| 195 | + pendencies = pdf_data["pendencies_resource"] | |
| 196 | + del pdf_data["pendencies_resource"] | |
| 197 | + | |
| 198 | + pdf = PDFFile() | |
| 199 | + pdf.name = pdf_data["name"] | |
| 200 | + pdf.brief_description = pdf_data["brief_description"] | |
| 201 | + pdf.show_window = pdf_data["show_window"] | |
| 202 | + pdf.all_students = pdf_data["all_students"] | |
| 203 | + pdf.visible = pdf_data["visible"] | |
| 204 | + pdf.order = pdf_data["order"] | |
| 205 | + pdf.topic = pdf_data["topic"] | |
| 206 | + pdf.file = pdf_data["file"] | |
| 207 | + | |
| 208 | + pdf.save() | |
| 209 | + | |
| 210 | + tags = data["tags"] | |
| 211 | + | |
| 212 | + for tag in tags: | |
| 213 | + if tag["id"] == "": | |
| 214 | + tag = Tag.objects.create(name = tag["name"]) | |
| 215 | + else: | |
| 216 | + tag = get_object_or_404(Tag, id = tag["id"]) | |
| 217 | + | |
| 218 | + pdf.tags.add(tag) | |
| 219 | + | |
| 220 | + resource = get_object_or_404(Resource, id = pdf.id) | |
| 221 | + | |
| 222 | + for pend in pendencies: | |
| 223 | + Pendencies.objects.create(resource = resource, **pend) | |
| 224 | + | |
| 225 | + return pdf | |
| 136 | 226 | \ No newline at end of file | ... | ... |
webpage/serializers.py
| ... | ... | @@ -89,12 +89,75 @@ class SimpleWebpageSerializer(serializers.ModelSerializer): |
| 89 | 89 | return instance |
| 90 | 90 | |
| 91 | 91 | class CompleteWebpageSerializer(serializers.ModelSerializer): |
| 92 | - topic = TopicSerializer() | |
| 92 | + topic = TopicSerializer('get_subject') | |
| 93 | 93 | tags = TagSerializer(many = True) |
| 94 | 94 | pendencies_resource = PendenciesSerializer(many = True) |
| 95 | 95 | groups = StudentsGroupSerializer(many = True) |
| 96 | 96 | students = UserBackupSerializer(many = True) |
| 97 | 97 | |
| 98 | + def get_subject(self, obj): | |
| 99 | + subject = self.context.get("subject", None) | |
| 100 | + | |
| 101 | + return subject | |
| 102 | + | |
| 98 | 103 | class Meta: |
| 99 | 104 | model = Webpage |
| 100 | - fields = '__all__' | |
| 101 | 105 | \ No newline at end of file |
| 106 | + fields = '__all__' | |
| 107 | + | |
| 108 | + def create(self, data): | |
| 109 | + topic = data['topic'] | |
| 110 | + | |
| 111 | + webpage = None | |
| 112 | + | |
| 113 | + if not topic["id"] is None: | |
| 114 | + if "subject" in topic: | |
| 115 | + r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"]) | |
| 116 | + else: | |
| 117 | + r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"]) | |
| 118 | + | |
| 119 | + if not r_exits.exists(): | |
| 120 | + if topic['id'] == "": | |
| 121 | + topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | |
| 122 | + | |
| 123 | + if topic_exist.exists(): | |
| 124 | + topic = topic_exist[0] | |
| 125 | + else: | |
| 126 | + topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | |
| 127 | + | |
| 128 | + data["topic"] = topic | |
| 129 | + else: | |
| 130 | + data["topic"] = get_object_or_404(Topic, id = topic["id"]) | |
| 131 | + | |
| 132 | + webpage_data = data | |
| 133 | + | |
| 134 | + pendencies = webpage_data["pendencies_resource"] | |
| 135 | + del webpage_data["pendencies_resource"] | |
| 136 | + | |
| 137 | + webpage = Webpage() | |
| 138 | + webpage.name = webpage_data["name"] | |
| 139 | + webpage.brief_description = webpage_data["brief_description"] | |
| 140 | + webpage.show_window = webpage_data["show_window"] | |
| 141 | + webpage.all_students = webpage_data["all_students"] | |
| 142 | + webpage.visible = webpage_data["visible"] | |
| 143 | + webpage.order = webpage_data["order"] | |
| 144 | + webpage.topic = webpage_data["topic"] | |
| 145 | + webpage.content = webpage_data["content"] | |
| 146 | + | |
| 147 | + webpage.save() | |
| 148 | + | |
| 149 | + tags = data["tags"] | |
| 150 | + | |
| 151 | + for tag in tags: | |
| 152 | + if tag["id"] == "": | |
| 153 | + tag = Tag.objects.create(name = tag["name"]) | |
| 154 | + else: | |
| 155 | + tag = get_object_or_404(Tag, id = tag["id"]) | |
| 156 | + | |
| 157 | + webpage.tags.add(tag) | |
| 158 | + | |
| 159 | + resource = get_object_or_404(Resource, id = webpage.id) | |
| 160 | + | |
| 161 | + for pend in pendencies: | |
| 162 | + Pendencies.objects.create(resource = resource, **pend) | |
| 163 | + | |
| 164 | + return webpage | |
| 102 | 165 | \ No newline at end of file | ... | ... |
youtube_video/serializers.py
| ... | ... | @@ -90,12 +90,76 @@ class SimpleYTVideoSerializer(serializers.ModelSerializer): |
| 90 | 90 | return instance |
| 91 | 91 | |
| 92 | 92 | class CompleteYTVideoSerializer(serializers.ModelSerializer): |
| 93 | - topic = TopicSerializer() | |
| 93 | + topic = TopicSerializer('get_subject') | |
| 94 | 94 | tags = TagSerializer(many = True) |
| 95 | 95 | pendencies_resource = PendenciesSerializer(many = True) |
| 96 | 96 | groups = StudentsGroupSerializer(many = True) |
| 97 | 97 | students = UserBackupSerializer(many = True) |
| 98 | 98 | |
| 99 | + def get_subject(self, obj): | |
| 100 | + subject = self.context.get("subject", None) | |
| 101 | + | |
| 102 | + return subject | |
| 103 | + | |
| 99 | 104 | class Meta: |
| 100 | 105 | model = YTVideo |
| 101 | - fields = '__all__' | |
| 102 | 106 | \ No newline at end of file |
| 107 | + fields = '__all__' | |
| 108 | + | |
| 109 | + def create(self, data): | |
| 110 | + topic = data['topic'] | |
| 111 | + | |
| 112 | + ytvideo = None | |
| 113 | + | |
| 114 | + if not topic["id"] is None: | |
| 115 | + if "subject" in topic: | |
| 116 | + r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"]) | |
| 117 | + else: | |
| 118 | + r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"]) | |
| 119 | + | |
| 120 | + if not r_exits.exists(): | |
| 121 | + if topic['id'] == "": | |
| 122 | + topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | |
| 123 | + | |
| 124 | + if topic_exist.exists(): | |
| 125 | + topic = topic_exist[0] | |
| 126 | + else: | |
| 127 | + topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | |
| 128 | + | |
| 129 | + data["topic"] = topic | |
| 130 | + else: | |
| 131 | + data["topic"] = get_object_or_404(Topic, id = topic["id"]) | |
| 132 | + | |
| 133 | + | |
| 134 | + ytvideo_data = data | |
| 135 | + | |
| 136 | + pendencies = ytvideo_data["pendencies_resource"] | |
| 137 | + del ytvideo_data["pendencies_resource"] | |
| 138 | + | |
| 139 | + ytvideo = YTVideo() | |
| 140 | + ytvideo.name = ytvideo_data["name"] | |
| 141 | + ytvideo.brief_description = ytvideo_data["brief_description"] | |
| 142 | + ytvideo.show_window = ytvideo_data["show_window"] | |
| 143 | + ytvideo.all_students = ytvideo_data["all_students"] | |
| 144 | + ytvideo.visible = ytvideo_data["visible"] | |
| 145 | + ytvideo.order = ytvideo_data["order"] | |
| 146 | + ytvideo.topic = ytvideo_data["topic"] | |
| 147 | + ytvideo.url = ytvideo_data["url"] | |
| 148 | + | |
| 149 | + ytvideo.save() | |
| 150 | + | |
| 151 | + tags = data["tags"] | |
| 152 | + | |
| 153 | + for tag in tags: | |
| 154 | + if tag["id"] == "": | |
| 155 | + tag = Tag.objects.create(name = tag["name"]) | |
| 156 | + else: | |
| 157 | + tag = get_object_or_404(Tag, id = tag["id"]) | |
| 158 | + | |
| 159 | + ytvideo.tags.add(tag) | |
| 160 | + | |
| 161 | + resource = get_object_or_404(Resource, id = ytvideo.id) | |
| 162 | + | |
| 163 | + for pend in pendencies: | |
| 164 | + Pendencies.objects.create(resource = resource, **pend) | |
| 165 | + | |
| 166 | + return ytvideo | |
| 103 | 167 | \ No newline at end of file | ... | ... |