Commit d2ba4692004feb2d2400475c8bab6da4282347ef
1 parent
c18c7f63
Exists in
master
and in
2 other branches
Avoiding duplicate inserts when trying to restore same backup in same subject mu…
…ltiple times & setting returning page
Showing
7 changed files
with
283 additions
and
251 deletions
Show diff stats
file_link/serializers.py
| @@ -67,55 +67,61 @@ class SimpleFileLinkSerializer(serializers.ModelSerializer): | @@ -67,55 +67,61 @@ class SimpleFileLinkSerializer(serializers.ModelSerializer): | ||
| 67 | file_link = None | 67 | file_link = None |
| 68 | 68 | ||
| 69 | if not topic["id"] is None: | 69 | if not topic["id"] is None: |
| 70 | - if topic['id'] == "": | ||
| 71 | - topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | ||
| 72 | - | ||
| 73 | - if topic_exist.exists(): | ||
| 74 | - topic = topic_exist[0] | ||
| 75 | - else: | ||
| 76 | - topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | ||
| 77 | - | ||
| 78 | - data["topic"] = topic | 70 | + if "subject" in topic: |
| 71 | + r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"]) | ||
| 79 | else: | 72 | else: |
| 80 | - data["topic"] = get_object_or_404(Topic, id = topic["id"]) | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - f = open(os.path.join(settings.MEDIA_ROOT, data["file_content"]), encoding="latin-1") | ||
| 84 | - file = File(f) | 73 | + r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"]) |
| 74 | + | ||
| 75 | + if not r_exits.exists(): | ||
| 76 | + if topic['id'] == "": | ||
| 77 | + topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | ||
| 78 | + | ||
| 79 | + if topic_exist.exists(): | ||
| 80 | + topic = topic_exist[0] | ||
| 81 | + else: | ||
| 82 | + topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | ||
| 83 | + | ||
| 84 | + data["topic"] = topic | ||
| 85 | + else: | ||
| 86 | + data["topic"] = get_object_or_404(Topic, id = topic["id"]) | ||
| 85 | 87 | ||
| 86 | - data["file_content"] = file | ||
| 87 | 88 | ||
| 88 | - file_link_data = data | ||
| 89 | - | ||
| 90 | - pendencies = file_link_data["pendencies_resource"] | ||
| 91 | - del file_link_data["pendencies_resource"] | 89 | + f = open(os.path.join(settings.MEDIA_ROOT, data["file_content"]), encoding="latin-1") |
| 90 | + file = File(f) | ||
| 92 | 91 | ||
| 93 | - file_link = FileLink() | ||
| 94 | - file_link.name = file_link_data["name"] | ||
| 95 | - file_link.brief_description = file_link_data["brief_description"] | ||
| 96 | - file_link.show_window = file_link_data["show_window"] | ||
| 97 | - file_link.all_students = file_link_data["all_students"] | ||
| 98 | - file_link.visible = file_link_data["visible"] | ||
| 99 | - file_link.order = file_link_data["order"] | ||
| 100 | - file_link.topic = file_link_data["topic"] | ||
| 101 | - file_link.file_content = file_link_data["file_content"] | 92 | + data["file_content"] = file |
| 102 | 93 | ||
| 103 | - file_link.save() | ||
| 104 | - | ||
| 105 | - tags = data["tags"] | 94 | + file_link_data = data |
| 95 | + | ||
| 96 | + pendencies = file_link_data["pendencies_resource"] | ||
| 97 | + del file_link_data["pendencies_resource"] | ||
| 98 | + | ||
| 99 | + file_link = FileLink() | ||
| 100 | + file_link.name = file_link_data["name"] | ||
| 101 | + file_link.brief_description = file_link_data["brief_description"] | ||
| 102 | + file_link.show_window = file_link_data["show_window"] | ||
| 103 | + file_link.all_students = file_link_data["all_students"] | ||
| 104 | + file_link.visible = file_link_data["visible"] | ||
| 105 | + file_link.order = file_link_data["order"] | ||
| 106 | + file_link.topic = file_link_data["topic"] | ||
| 107 | + file_link.file_content = file_link_data["file_content"] | ||
| 108 | + | ||
| 109 | + file_link.save() | ||
| 110 | + | ||
| 111 | + tags = data["tags"] | ||
| 106 | 112 | ||
| 107 | - for tag in tags: | ||
| 108 | - if tag["id"] == "": | ||
| 109 | - tag = Tag.objects.create(name = tag["name"]) | ||
| 110 | - else: | ||
| 111 | - tag = get_object_or_404(Tag, id = tag["id"]) | 113 | + for tag in tags: |
| 114 | + if tag["id"] == "": | ||
| 115 | + tag = Tag.objects.create(name = tag["name"]) | ||
| 116 | + else: | ||
| 117 | + tag = get_object_or_404(Tag, id = tag["id"]) | ||
| 112 | 118 | ||
| 113 | - file_link.tags.add(tag) | ||
| 114 | - | ||
| 115 | - resource = get_object_or_404(Resource, id = file_link.id) | 119 | + file_link.tags.add(tag) |
| 120 | + | ||
| 121 | + resource = get_object_or_404(Resource, id = file_link.id) | ||
| 116 | 122 | ||
| 117 | - for pend in pendencies: | ||
| 118 | - Pendencies.objects.create(resource = resource, **pend) | 123 | + for pend in pendencies: |
| 124 | + Pendencies.objects.create(resource = resource, **pend) | ||
| 119 | 125 | ||
| 120 | return file_link | 126 | return file_link |
| 121 | 127 |
goals/serializers.py
| @@ -39,56 +39,62 @@ class SimpleGoalSerializer(serializers.ModelSerializer): | @@ -39,56 +39,62 @@ class SimpleGoalSerializer(serializers.ModelSerializer): | ||
| 39 | goals = None | 39 | goals = None |
| 40 | 40 | ||
| 41 | if not topic["id"] is None: | 41 | if not topic["id"] is None: |
| 42 | - if topic['id'] == "": | ||
| 43 | - topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | ||
| 44 | - | ||
| 45 | - if topic_exist.exists(): | ||
| 46 | - topic = topic_exist[0] | ||
| 47 | - else: | ||
| 48 | - topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | ||
| 49 | - | ||
| 50 | - data["topic"] = topic | 42 | + if "subject" in topic: |
| 43 | + r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"]) | ||
| 51 | else: | 44 | else: |
| 52 | - data["topic"] = get_object_or_404(Topic, id = topic["id"]) | ||
| 53 | - | ||
| 54 | - goals_data = data | ||
| 55 | - | ||
| 56 | - pendencies = goals_data["pendencies_resource"] | ||
| 57 | - del goals_data["pendencies_resource"] | ||
| 58 | - | ||
| 59 | - goal_items = goals_data["item_goal"] | ||
| 60 | - del goals_data["item_goal"] | ||
| 61 | - | ||
| 62 | - goals = Goals() | ||
| 63 | - goals.name = goals_data["name"] | ||
| 64 | - goals.brief_description = goals_data["brief_description"] | ||
| 65 | - goals.show_window = goals_data["show_window"] | ||
| 66 | - goals.all_students = goals_data["all_students"] | ||
| 67 | - goals.visible = goals_data["visible"] | ||
| 68 | - goals.order = goals_data["order"] | ||
| 69 | - goals.topic = goals_data["topic"] | ||
| 70 | - goals.presentation = goals_data["presentation"] | ||
| 71 | - goals.limit_submission_date = goals_data["limit_submission_date"] | ||
| 72 | - | ||
| 73 | - goals.save() | ||
| 74 | - | ||
| 75 | - tags = data["tags"] | ||
| 76 | - | ||
| 77 | - for tag in tags: | ||
| 78 | - if tag["id"] == "": | ||
| 79 | - tag = Tag.objects.create(name = tag["name"]) | 45 | + r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"]) |
| 46 | + | ||
| 47 | + if not r_exits.exists(): | ||
| 48 | + if topic['id'] == "": | ||
| 49 | + topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | ||
| 50 | + | ||
| 51 | + if topic_exist.exists(): | ||
| 52 | + topic = topic_exist[0] | ||
| 53 | + else: | ||
| 54 | + topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | ||
| 55 | + | ||
| 56 | + data["topic"] = topic | ||
| 80 | else: | 57 | else: |
| 81 | - tag = get_object_or_404(Tag, id = tag["id"]) | 58 | + data["topic"] = get_object_or_404(Topic, id = topic["id"]) |
| 59 | + | ||
| 60 | + goals_data = data | ||
| 61 | + | ||
| 62 | + pendencies = goals_data["pendencies_resource"] | ||
| 63 | + del goals_data["pendencies_resource"] | ||
| 64 | + | ||
| 65 | + goal_items = goals_data["item_goal"] | ||
| 66 | + del goals_data["item_goal"] | ||
| 67 | + | ||
| 68 | + goals = Goals() | ||
| 69 | + goals.name = goals_data["name"] | ||
| 70 | + goals.brief_description = goals_data["brief_description"] | ||
| 71 | + goals.show_window = goals_data["show_window"] | ||
| 72 | + goals.all_students = goals_data["all_students"] | ||
| 73 | + goals.visible = goals_data["visible"] | ||
| 74 | + goals.order = goals_data["order"] | ||
| 75 | + goals.topic = goals_data["topic"] | ||
| 76 | + goals.presentation = goals_data["presentation"] | ||
| 77 | + goals.limit_submission_date = goals_data["limit_submission_date"] | ||
| 78 | + | ||
| 79 | + goals.save() | ||
| 80 | + | ||
| 81 | + tags = data["tags"] | ||
| 82 | + | ||
| 83 | + for tag in tags: | ||
| 84 | + if tag["id"] == "": | ||
| 85 | + tag = Tag.objects.create(name = tag["name"]) | ||
| 86 | + else: | ||
| 87 | + tag = get_object_or_404(Tag, id = tag["id"]) | ||
| 82 | 88 | ||
| 83 | - goals.tags.add(tag) | 89 | + goals.tags.add(tag) |
| 84 | 90 | ||
| 85 | - resource = get_object_or_404(Resource, id = goals.id) | 91 | + resource = get_object_or_404(Resource, id = goals.id) |
| 86 | 92 | ||
| 87 | - for item in goal_items: | ||
| 88 | - GoalItem.objects.create(goal = goals, **item) | 93 | + for item in goal_items: |
| 94 | + GoalItem.objects.create(goal = goals, **item) | ||
| 89 | 95 | ||
| 90 | - for pend in pendencies: | ||
| 91 | - Pendencies.objects.create(resource = resource, **pend) | 96 | + for pend in pendencies: |
| 97 | + Pendencies.objects.create(resource = resource, **pend) | ||
| 92 | 98 | ||
| 93 | return goals | 99 | return goals |
| 94 | 100 |
links/serializers.py
| @@ -33,49 +33,55 @@ class SimpleLinkSerializer(serializers.ModelSerializer): | @@ -33,49 +33,55 @@ class SimpleLinkSerializer(serializers.ModelSerializer): | ||
| 33 | link = None | 33 | link = None |
| 34 | 34 | ||
| 35 | if not topic["id"] is None: | 35 | if not topic["id"] is None: |
| 36 | - if topic['id'] == "": | ||
| 37 | - topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | ||
| 38 | - | ||
| 39 | - if topic_exist.exists(): | ||
| 40 | - topic = topic_exist[0] | ||
| 41 | - else: | ||
| 42 | - topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | ||
| 43 | - | ||
| 44 | - data["topic"] = topic | 36 | + if "subject" in topic: |
| 37 | + r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"]) | ||
| 45 | else: | 38 | else: |
| 46 | - data["topic"] = get_object_or_404(Topic, id = topic["id"]) | ||
| 47 | - | ||
| 48 | - link_data = data | ||
| 49 | - | ||
| 50 | - pendencies = link_data["pendencies_resource"] | ||
| 51 | - del link_data["pendencies_resource"] | ||
| 52 | - | ||
| 53 | - link = Link() | ||
| 54 | - link.name = link_data["name"] | ||
| 55 | - link.brief_description = link_data["brief_description"] | ||
| 56 | - link.show_window = link_data["show_window"] | ||
| 57 | - link.all_students = link_data["all_students"] | ||
| 58 | - link.visible = link_data["visible"] | ||
| 59 | - link.order = link_data["order"] | ||
| 60 | - link.topic = link_data["topic"] | ||
| 61 | - link.link_url = link_data["link_url"] | ||
| 62 | - | ||
| 63 | - link.save() | ||
| 64 | - | ||
| 65 | - tags = data["tags"] | ||
| 66 | - | ||
| 67 | - for tag in tags: | ||
| 68 | - if tag["id"] == "": | ||
| 69 | - tag = Tag.objects.create(name = tag["name"]) | 39 | + r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"]) |
| 40 | + | ||
| 41 | + if not r_exits.exists(): | ||
| 42 | + if topic['id'] == "": | ||
| 43 | + topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | ||
| 44 | + | ||
| 45 | + if topic_exist.exists(): | ||
| 46 | + topic = topic_exist[0] | ||
| 47 | + else: | ||
| 48 | + topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | ||
| 49 | + | ||
| 50 | + data["topic"] = topic | ||
| 70 | else: | 51 | else: |
| 71 | - tag = get_object_or_404(Tag, id = tag["id"]) | 52 | + data["topic"] = get_object_or_404(Topic, id = topic["id"]) |
| 72 | 53 | ||
| 73 | - link.tags.add(tag) | ||
| 74 | - | ||
| 75 | - resource = get_object_or_404(Resource, id = link.id) | 54 | + link_data = data |
| 55 | + | ||
| 56 | + pendencies = link_data["pendencies_resource"] | ||
| 57 | + del link_data["pendencies_resource"] | ||
| 58 | + | ||
| 59 | + link = Link() | ||
| 60 | + link.name = link_data["name"] | ||
| 61 | + link.brief_description = link_data["brief_description"] | ||
| 62 | + link.show_window = link_data["show_window"] | ||
| 63 | + link.all_students = link_data["all_students"] | ||
| 64 | + link.visible = link_data["visible"] | ||
| 65 | + link.order = link_data["order"] | ||
| 66 | + link.topic = link_data["topic"] | ||
| 67 | + link.link_url = link_data["link_url"] | ||
| 68 | + | ||
| 69 | + link.save() | ||
| 70 | + | ||
| 71 | + tags = data["tags"] | ||
| 72 | + | ||
| 73 | + for tag in tags: | ||
| 74 | + if tag["id"] == "": | ||
| 75 | + tag = Tag.objects.create(name = tag["name"]) | ||
| 76 | + else: | ||
| 77 | + tag = get_object_or_404(Tag, id = tag["id"]) | ||
| 78 | + | ||
| 79 | + link.tags.add(tag) | ||
| 80 | + | ||
| 81 | + resource = get_object_or_404(Resource, id = link.id) | ||
| 76 | 82 | ||
| 77 | - for pend in pendencies: | ||
| 78 | - Pendencies.objects.create(resource = resource, **pend) | 83 | + for pend in pendencies: |
| 84 | + Pendencies.objects.create(resource = resource, **pend) | ||
| 79 | 85 | ||
| 80 | return link | 86 | return link |
| 81 | 87 |
pdf_file/serializers.py
| @@ -60,55 +60,61 @@ class SimplePDFFileSerializer(serializers.ModelSerializer): | @@ -60,55 +60,61 @@ class SimplePDFFileSerializer(serializers.ModelSerializer): | ||
| 60 | pdf = None | 60 | pdf = None |
| 61 | 61 | ||
| 62 | if not topic["id"] is None: | 62 | if not topic["id"] is None: |
| 63 | - if topic['id'] == "": | ||
| 64 | - topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | ||
| 65 | - | ||
| 66 | - if topic_exist.exists(): | ||
| 67 | - topic = topic_exist[0] | ||
| 68 | - else: | ||
| 69 | - topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | ||
| 70 | - | ||
| 71 | - data["topic"] = topic | 63 | + if "subject" in topic: |
| 64 | + r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"]) | ||
| 72 | else: | 65 | else: |
| 73 | - data["topic"] = get_object_or_404(Topic, id = topic["id"]) | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - f = open(os.path.join(settings.MEDIA_ROOT, data["file"]), encoding="latin-1") | ||
| 77 | - file = File(f) | ||
| 78 | - | ||
| 79 | - data["file"] = file | ||
| 80 | - | ||
| 81 | - pdf_data = data | ||
| 82 | - | ||
| 83 | - pendencies = pdf_data["pendencies_resource"] | ||
| 84 | - del pdf_data["pendencies_resource"] | ||
| 85 | - | ||
| 86 | - pdf = PDFFile() | ||
| 87 | - pdf.name = pdf_data["name"] | ||
| 88 | - pdf.brief_description = pdf_data["brief_description"] | ||
| 89 | - pdf.show_window = pdf_data["show_window"] | ||
| 90 | - pdf.all_students = pdf_data["all_students"] | ||
| 91 | - pdf.visible = pdf_data["visible"] | ||
| 92 | - pdf.order = pdf_data["order"] | ||
| 93 | - pdf.topic = pdf_data["topic"] | ||
| 94 | - pdf.file = pdf_data["file"] | 66 | + r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"]) |
| 67 | + | ||
| 68 | + if not r_exits.exists(): | ||
| 69 | + if topic['id'] == "": | ||
| 70 | + topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | ||
| 71 | + | ||
| 72 | + if topic_exist.exists(): | ||
| 73 | + topic = topic_exist[0] | ||
| 74 | + else: | ||
| 75 | + topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | ||
| 76 | + | ||
| 77 | + data["topic"] = topic | ||
| 78 | + else: | ||
| 79 | + data["topic"] = get_object_or_404(Topic, id = topic["id"]) | ||
| 95 | 80 | ||
| 96 | - pdf.save() | ||
| 97 | 81 | ||
| 98 | - tags = data["tags"] | 82 | + f = open(os.path.join(settings.MEDIA_ROOT, data["file"]), encoding="latin-1") |
| 83 | + file = File(f) | ||
| 99 | 84 | ||
| 100 | - for tag in tags: | ||
| 101 | - if tag["id"] == "": | ||
| 102 | - tag = Tag.objects.create(name = tag["name"]) | ||
| 103 | - else: | ||
| 104 | - tag = get_object_or_404(Tag, id = tag["id"]) | 85 | + data["file"] = file |
| 105 | 86 | ||
| 106 | - pdf.tags.add(tag) | ||
| 107 | - | ||
| 108 | - resource = get_object_or_404(Resource, id = pdf.id) | 87 | + pdf_data = data |
| 88 | + | ||
| 89 | + pendencies = pdf_data["pendencies_resource"] | ||
| 90 | + del pdf_data["pendencies_resource"] | ||
| 91 | + | ||
| 92 | + pdf = PDFFile() | ||
| 93 | + pdf.name = pdf_data["name"] | ||
| 94 | + pdf.brief_description = pdf_data["brief_description"] | ||
| 95 | + pdf.show_window = pdf_data["show_window"] | ||
| 96 | + pdf.all_students = pdf_data["all_students"] | ||
| 97 | + pdf.visible = pdf_data["visible"] | ||
| 98 | + pdf.order = pdf_data["order"] | ||
| 99 | + pdf.topic = pdf_data["topic"] | ||
| 100 | + pdf.file = pdf_data["file"] | ||
| 101 | + | ||
| 102 | + pdf.save() | ||
| 103 | + | ||
| 104 | + tags = data["tags"] | ||
| 105 | + | ||
| 106 | + for tag in tags: | ||
| 107 | + if tag["id"] == "": | ||
| 108 | + tag = Tag.objects.create(name = tag["name"]) | ||
| 109 | + else: | ||
| 110 | + tag = get_object_or_404(Tag, id = tag["id"]) | ||
| 111 | + | ||
| 112 | + pdf.tags.add(tag) | ||
| 113 | + | ||
| 114 | + resource = get_object_or_404(Resource, id = pdf.id) | ||
| 109 | 115 | ||
| 110 | - for pend in pendencies: | ||
| 111 | - Pendencies.objects.create(resource = resource, **pend) | 116 | + for pend in pendencies: |
| 117 | + Pendencies.objects.create(resource = resource, **pend) | ||
| 112 | 118 | ||
| 113 | return pdf | 119 | return pdf |
| 114 | 120 |
subjects/views.py
| @@ -822,12 +822,6 @@ def realize_backup(request, subject): | @@ -822,12 +822,6 @@ def realize_backup(request, subject): | ||
| 822 | data_list.append(serializer_g.data) | 822 | data_list.append(serializer_g.data) |
| 823 | 823 | ||
| 824 | json.dump(data_list, file) | 824 | json.dump(data_list, file) |
| 825 | - # json.dump(serializer_w.data, file) | ||
| 826 | - # json.dump(serializer_y.data, file) | ||
| 827 | - # json.dump(serializer_f.data, file) | ||
| 828 | - # json.dump(serializer_l.data, file) | ||
| 829 | - # json.dump(serializer_p.data, file) | ||
| 830 | - # json.dump(serializer_g.data, file) | ||
| 831 | 825 | ||
| 832 | file.close() | 826 | file.close() |
| 833 | 827 | ||
| @@ -909,4 +903,6 @@ def realize_restore(request, subject): | @@ -909,4 +903,6 @@ def realize_restore(request, subject): | ||
| 909 | serial.is_valid() | 903 | serial.is_valid() |
| 910 | serial.save() | 904 | serial.save() |
| 911 | 905 | ||
| 912 | - return JsonResponse({'message': 'ok'}) | ||
| 913 | \ No newline at end of file | 906 | \ No newline at end of file |
| 907 | + messages.success(request, _('Backup restored successfully!')) | ||
| 908 | + | ||
| 909 | + return redirect(reverse_lazy('subjects:restore', kwargs = {"slug": subject})) | ||
| 914 | \ No newline at end of file | 910 | \ No newline at end of file |
webpage/serializers.py
| @@ -33,49 +33,55 @@ class SimpleWebpageSerializer(serializers.ModelSerializer): | @@ -33,49 +33,55 @@ class SimpleWebpageSerializer(serializers.ModelSerializer): | ||
| 33 | webpage = None | 33 | webpage = None |
| 34 | 34 | ||
| 35 | if not topic["id"] is None: | 35 | if not topic["id"] is None: |
| 36 | - if topic['id'] == "": | ||
| 37 | - topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | ||
| 38 | - | ||
| 39 | - if topic_exist.exists(): | ||
| 40 | - topic = topic_exist[0] | ||
| 41 | - else: | ||
| 42 | - topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | ||
| 43 | - | ||
| 44 | - data["topic"] = topic | 36 | + if "subject" in topic: |
| 37 | + r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"]) | ||
| 45 | else: | 38 | else: |
| 46 | - data["topic"] = get_object_or_404(Topic, id = topic["id"]) | ||
| 47 | - | ||
| 48 | - webpage_data = data | ||
| 49 | - | ||
| 50 | - pendencies = webpage_data["pendencies_resource"] | ||
| 51 | - del webpage_data["pendencies_resource"] | ||
| 52 | - | ||
| 53 | - webpage = Webpage() | ||
| 54 | - webpage.name = webpage_data["name"] | ||
| 55 | - webpage.brief_description = webpage_data["brief_description"] | ||
| 56 | - webpage.show_window = webpage_data["show_window"] | ||
| 57 | - webpage.all_students = webpage_data["all_students"] | ||
| 58 | - webpage.visible = webpage_data["visible"] | ||
| 59 | - webpage.order = webpage_data["order"] | ||
| 60 | - webpage.topic = webpage_data["topic"] | ||
| 61 | - webpage.content = webpage_data["content"] | ||
| 62 | - | ||
| 63 | - webpage.save() | ||
| 64 | - | ||
| 65 | - tags = data["tags"] | ||
| 66 | - | ||
| 67 | - for tag in tags: | ||
| 68 | - if tag["id"] == "": | ||
| 69 | - tag = Tag.objects.create(name = tag["name"]) | 39 | + r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"]) |
| 40 | + | ||
| 41 | + if not r_exits.exists(): | ||
| 42 | + if topic['id'] == "": | ||
| 43 | + topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | ||
| 44 | + | ||
| 45 | + if topic_exist.exists(): | ||
| 46 | + topic = topic_exist[0] | ||
| 47 | + else: | ||
| 48 | + topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | ||
| 49 | + | ||
| 50 | + data["topic"] = topic | ||
| 70 | else: | 51 | else: |
| 71 | - tag = get_object_or_404(Tag, id = tag["id"]) | 52 | + data["topic"] = get_object_or_404(Topic, id = topic["id"]) |
| 53 | + | ||
| 54 | + webpage_data = data | ||
| 55 | + | ||
| 56 | + pendencies = webpage_data["pendencies_resource"] | ||
| 57 | + del webpage_data["pendencies_resource"] | ||
| 58 | + | ||
| 59 | + webpage = Webpage() | ||
| 60 | + webpage.name = webpage_data["name"] | ||
| 61 | + webpage.brief_description = webpage_data["brief_description"] | ||
| 62 | + webpage.show_window = webpage_data["show_window"] | ||
| 63 | + webpage.all_students = webpage_data["all_students"] | ||
| 64 | + webpage.visible = webpage_data["visible"] | ||
| 65 | + webpage.order = webpage_data["order"] | ||
| 66 | + webpage.topic = webpage_data["topic"] | ||
| 67 | + webpage.content = webpage_data["content"] | ||
| 68 | + | ||
| 69 | + webpage.save() | ||
| 70 | + | ||
| 71 | + tags = data["tags"] | ||
| 72 | + | ||
| 73 | + for tag in tags: | ||
| 74 | + if tag["id"] == "": | ||
| 75 | + tag = Tag.objects.create(name = tag["name"]) | ||
| 76 | + else: | ||
| 77 | + tag = get_object_or_404(Tag, id = tag["id"]) | ||
| 72 | 78 | ||
| 73 | - webpage.tags.add(tag) | 79 | + webpage.tags.add(tag) |
| 74 | 80 | ||
| 75 | - resource = get_object_or_404(Resource, id = webpage.id) | 81 | + resource = get_object_or_404(Resource, id = webpage.id) |
| 76 | 82 | ||
| 77 | - for pend in pendencies: | ||
| 78 | - Pendencies.objects.create(resource = resource, **pend) | 83 | + for pend in pendencies: |
| 84 | + Pendencies.objects.create(resource = resource, **pend) | ||
| 79 | 85 | ||
| 80 | return webpage | 86 | return webpage |
| 81 | 87 |
youtube_video/serializers.py
| @@ -33,50 +33,56 @@ class SimpleYTVideoSerializer(serializers.ModelSerializer): | @@ -33,50 +33,56 @@ class SimpleYTVideoSerializer(serializers.ModelSerializer): | ||
| 33 | ytvideo = None | 33 | ytvideo = None |
| 34 | 34 | ||
| 35 | if not topic["id"] is None: | 35 | if not topic["id"] is None: |
| 36 | - if topic['id'] == "": | ||
| 37 | - topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | ||
| 38 | - | ||
| 39 | - if topic_exist.exists(): | ||
| 40 | - topic = topic_exist[0] | ||
| 41 | - else: | ||
| 42 | - topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | ||
| 43 | - | ||
| 44 | - data["topic"] = topic | 36 | + if "subject" in topic: |
| 37 | + r_exits = Resource.objects.filter(topic__subject = topic["subject"], name__unaccent__iexact = data["name"]) | ||
| 45 | else: | 38 | else: |
| 46 | - data["topic"] = get_object_or_404(Topic, id = topic["id"]) | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - ytvideo_data = data | ||
| 50 | - | ||
| 51 | - pendencies = ytvideo_data["pendencies_resource"] | ||
| 52 | - del ytvideo_data["pendencies_resource"] | ||
| 53 | - | ||
| 54 | - ytvideo = YTVideo() | ||
| 55 | - ytvideo.name = ytvideo_data["name"] | ||
| 56 | - ytvideo.brief_description = ytvideo_data["brief_description"] | ||
| 57 | - ytvideo.show_window = ytvideo_data["show_window"] | ||
| 58 | - ytvideo.all_students = ytvideo_data["all_students"] | ||
| 59 | - ytvideo.visible = ytvideo_data["visible"] | ||
| 60 | - ytvideo.order = ytvideo_data["order"] | ||
| 61 | - ytvideo.topic = ytvideo_data["topic"] | ||
| 62 | - ytvideo.url = ytvideo_data["url"] | ||
| 63 | - | ||
| 64 | - ytvideo.save() | ||
| 65 | - | ||
| 66 | - tags = data["tags"] | ||
| 67 | - | ||
| 68 | - for tag in tags: | ||
| 69 | - if tag["id"] == "": | ||
| 70 | - tag = Tag.objects.create(name = tag["name"]) | 39 | + r_exits = Resource.objects.filter(topic__subject__id = topic["subject_id"], name__unaccent__iexact = data["name"]) |
| 40 | + | ||
| 41 | + if not r_exits.exists(): | ||
| 42 | + if topic['id'] == "": | ||
| 43 | + topic_exist = Topic.objects.filter(subject = topic['subject'], name__unaccent__iexact = topic["name"]) | ||
| 44 | + | ||
| 45 | + if topic_exist.exists(): | ||
| 46 | + topic = topic_exist[0] | ||
| 47 | + else: | ||
| 48 | + topic = Topic.objects.create(name = topic['name'], subject = topic['subject'], repository = topic['repository'], visible = topic['visible'], order = topic['order']) | ||
| 49 | + | ||
| 50 | + data["topic"] = topic | ||
| 71 | else: | 51 | else: |
| 72 | - tag = get_object_or_404(Tag, id = tag["id"]) | 52 | + data["topic"] = get_object_or_404(Topic, id = topic["id"]) |
| 53 | + | ||
| 73 | 54 | ||
| 74 | - ytvideo.tags.add(tag) | 55 | + ytvideo_data = data |
| 75 | 56 | ||
| 76 | - resource = get_object_or_404(Resource, id = ytvideo.id) | 57 | + pendencies = ytvideo_data["pendencies_resource"] |
| 58 | + del ytvideo_data["pendencies_resource"] | ||
| 59 | + | ||
| 60 | + ytvideo = YTVideo() | ||
| 61 | + ytvideo.name = ytvideo_data["name"] | ||
| 62 | + ytvideo.brief_description = ytvideo_data["brief_description"] | ||
| 63 | + ytvideo.show_window = ytvideo_data["show_window"] | ||
| 64 | + ytvideo.all_students = ytvideo_data["all_students"] | ||
| 65 | + ytvideo.visible = ytvideo_data["visible"] | ||
| 66 | + ytvideo.order = ytvideo_data["order"] | ||
| 67 | + ytvideo.topic = ytvideo_data["topic"] | ||
| 68 | + ytvideo.url = ytvideo_data["url"] | ||
| 69 | + | ||
| 70 | + ytvideo.save() | ||
| 71 | + | ||
| 72 | + tags = data["tags"] | ||
| 73 | + | ||
| 74 | + for tag in tags: | ||
| 75 | + if tag["id"] == "": | ||
| 76 | + tag = Tag.objects.create(name = tag["name"]) | ||
| 77 | + else: | ||
| 78 | + tag = get_object_or_404(Tag, id = tag["id"]) | ||
| 79 | + | ||
| 80 | + ytvideo.tags.add(tag) | ||
| 81 | + | ||
| 82 | + resource = get_object_or_404(Resource, id = ytvideo.id) | ||
| 77 | 83 | ||
| 78 | - for pend in pendencies: | ||
| 79 | - Pendencies.objects.create(resource = resource, **pend) | 84 | + for pend in pendencies: |
| 85 | + Pendencies.objects.create(resource = resource, **pend) | ||
| 80 | 86 | ||
| 81 | return ytvideo | 87 | return ytvideo |
| 82 | 88 |