Commit 8cc2a2f3fa0446f5dac456343d02063595359caf

Authored by Felipe Bormann
2 parents e7187da0 3d95bbd3

Merge branch 'refactoring' of https://github.com/amadeusproject/amadeuslms into refactoring

file_link/serializers.py
  1 +import os
  2 +import zipfile
  3 +import time
  4 +from django.conf import settings
1 5 from rest_framework import serializers
2 6  
3 7 from subjects.serializers import TagSerializer
... ... @@ -19,6 +23,27 @@ class SimpleFileLinkSerializer(serializers.ModelSerializer):
19 23  
20 24 return subject
21 25  
  26 + def validate(self, data):
  27 + files = self.context.get('files', None)
  28 +
  29 + if files:
  30 + file_path = os.path.join(settings.MEDIA_ROOT, data["file_content"])
  31 +
  32 + if os.path.isfile(file_path):
  33 + dst_path = os.path.join(settings.MEDIA_ROOT, "tmp")
  34 +
  35 + path = files.extract(data["file_content"], dst_path)
  36 +
  37 + new_name = "files/file_" + str(time.time()) + os.path.splitext(data["file_content"])[1]
  38 +
  39 + os.rename(os.path.join(dst_path, path), os.path.join(settings.MEDIA_ROOT, new_name))
  40 +
  41 + data["file_content"] = new_name
  42 + else:
  43 + path = files.extract(data["file_content"], settings.MEDIA_ROOT)
  44 +
  45 + return data
  46 +
22 47 class Meta:
23 48 model = FileLink
24 49 extra_kwargs = {
... ...
goals/serializers.py
... ... @@ -14,11 +14,16 @@ class GoalItemSerializer(serializers.ModelSerializer):
14 14 exclude = ('goal',)
15 15  
16 16 class SimpleGoalSerializer(serializers.ModelSerializer):
17   - topic = TopicSerializer()
  17 + topic = TopicSerializer('get_subject')
18 18 tags = TagSerializer(many = True)
19 19 item_goal = GoalItemSerializer(many = True)
20 20 pendencies_resource = PendenciesSerializer(many = True)
21 21  
  22 + def get_subject(self, obj):
  23 + subject = self.context.get("subject", None)
  24 +
  25 + return subject
  26 +
22 27 class Meta:
23 28 model = Goals
24 29 exclude = ('students', 'groups',)
... ...
links/serializers.py
... ... @@ -9,10 +9,15 @@ from users.serializers import UserBackupSerializer
9 9 from .models import Link
10 10  
11 11 class SimpleLinkSerializer(serializers.ModelSerializer):
12   - topic = TopicSerializer()
  12 + topic = TopicSerializer('get_subject')
13 13 tags = TagSerializer(many = True)
14 14 pendencies_resource = PendenciesSerializer(many = True)
15 15  
  16 + def get_subject(self, obj):
  17 + subject = self.context.get("subject", None)
  18 +
  19 + return subject
  20 +
16 21 class Meta:
17 22 model = Link
18 23 exclude = ('students', 'groups',)
... ...
pdf_file/serializers.py
... ... @@ -9,13 +9,34 @@ from users.serializers import UserBackupSerializer
9 9 from .models import PDFFile
10 10  
11 11 class SimplePDFFileSerializer(serializers.ModelSerializer):
12   - topic = TopicSerializer()
  12 + topic = TopicSerializer('get_subject')
13 13 tags = TagSerializer(many = True)
14 14 pendencies_resource = PendenciesSerializer(many = True)
15 15 file = serializers.CharField(required = False, allow_blank = True, max_length = 255)
16 16  
  17 + def get_subject(self, obj):
  18 + subject = self.context.get("subject", None)
  19 +
  20 + return subject
  21 +
17 22 def validate(self, data):
18   - print(self.context)
  23 + files = self.context.get('files', None)
  24 +
  25 + if files:
  26 + file_path = os.path.join(settings.MEDIA_ROOT, data["file_content"])
  27 +
  28 + if os.path.isfile(file_path):
  29 + dst_path = os.path.join(settings.MEDIA_ROOT, "tmp")
  30 +
  31 + path = files.extract(data["file_content"], dst_path)
  32 +
  33 + new_name = "files/file_" + str(time.time()) + os.path.splitext(data["file_content"])[1]
  34 +
  35 + os.rename(os.path.join(dst_path, path), os.path.join(settings.MEDIA_ROOT, new_name))
  36 +
  37 + data["file_content"] = new_name
  38 + else:
  39 + path = files.extract(data["file_content"], settings.MEDIA_ROOT)
19 40  
20 41 return data
21 42  
... ...
subjects/views.py
... ... @@ -896,11 +896,11 @@ def realize_restore(request, subject):
896 896 if line[0]["_my_subclass"] == "webpage":
897 897 serial = SimpleWebpageSerializer(data = line, many = True, context = {'subject': subject})
898 898 elif line[0]["_my_subclass"] == "filelink":
899   - serial = SimpleFileLinkSerializer(data = line, many = True, context = {'subject': subject})
  899 + serial = SimpleFileLinkSerializer(data = line, many = True, context = {'subject': subject, 'files': file})
900 900 elif line[0]["_my_subclass"] == "link":
901 901 serial = SimpleLinkSerializer(data = line, many = True, context = {'subject': subject})
902 902 elif line[0]["_my_subclass"] == "pdffile":
903   - serial = SimplePDFFileSerializer(data = line, many = True, context = {'subject': subject})
  903 + serial = SimplePDFFileSerializer(data = line, many = True, context = {'subject': subject, 'files': file})
904 904 elif line[0]["_my_subclass"] == "goals":
905 905 serial = SimpleGoalSerializer(data = line, many = True, context = {'subject': subject})
906 906 elif line[0]["_my_subclass"] == "ytvideo":
... ...
topics/serializers.py
1 1 from rest_framework import serializers
  2 +from django.shortcuts import get_object_or_404
  3 +
  4 +from subjects.models import Subject
2 5  
3 6 from .models import Topic
4 7  
... ... @@ -7,7 +10,18 @@ class TopicSerializer(serializers.ModelSerializer):
7 10 subject = self.context.get('subject', None)
8 11  
9 12 if subject:
10   - print(subject)
  13 + subject = get_object_or_404(Subject, slug = subject)
  14 + topic = Topic.objects.filter(subject = subject, name__unaccent__iexact = data["name"])
  15 +
  16 + if topic.exists():
  17 + data = topic[0]
  18 + else:
  19 + topic = Topic.objects.filter(subject = subject, repository = True)
  20 +
  21 + if topic.exists():
  22 + data = topic[0]
  23 + else:
  24 + data["id"] = ""
11 25  
12 26 return data
13 27  
... ...
webpage/serializers.py
... ... @@ -9,10 +9,15 @@ from users.serializers import UserBackupSerializer
9 9 from .models import Webpage
10 10  
11 11 class SimpleWebpageSerializer(serializers.ModelSerializer):
12   - topic = TopicSerializer()
  12 + topic = TopicSerializer('get_subject')
13 13 tags = TagSerializer(many = True)
14 14 pendencies_resource = PendenciesSerializer(many = True)
15 15  
  16 + def get_subject(self, obj):
  17 + subject = self.context.get("subject", None)
  18 +
  19 + return subject
  20 +
16 21 class Meta:
17 22 model = Webpage
18 23 exclude = ('students', 'groups',)
... ...
youtube_video/serializers.py
... ... @@ -9,10 +9,15 @@ from users.serializers import UserBackupSerializer
9 9 from .models import YTVideo
10 10  
11 11 class SimpleYTVideoSerializer(serializers.ModelSerializer):
12   - topic = TopicSerializer()
  12 + topic = TopicSerializer('get_subject')
13 13 tags = TagSerializer(many = True)
14 14 pendencies_resource = PendenciesSerializer(many = True)
15 15  
  16 + def get_subject(self, obj):
  17 + subject = self.context.get("subject", None)
  18 +
  19 + return subject
  20 +
16 21 class Meta:
17 22 model = YTVideo
18 23 exclude = ('students', 'groups',)
... ...