You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
4.4 KiB
152 lines
4.4 KiB
from rest_framework import serializers
|
|
import json
|
|
# from django.contrib.auth import get_user_model
|
|
# from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
from courses.models import Course, CourseTheme, Lesson, Homework, Exam
|
|
|
|
|
|
class LessonSerializer(serializers.ModelSerializer):
|
|
on_comment = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Lesson
|
|
fields = (
|
|
'id', 'title', 'on_comment', 'materials',
|
|
'free', 'video', 'video_id',
|
|
)
|
|
|
|
@staticmethod
|
|
def get_on_comment(self):
|
|
return self.on_comment == 'N' or self.on_comment == 'T' and self.theme.on_comment
|
|
|
|
|
|
class HomeworkSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = Homework
|
|
fields = '__all__'
|
|
|
|
|
|
class ExamSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = Exam
|
|
fields = '__all__'
|
|
|
|
|
|
class ThemeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = CourseTheme
|
|
exclude = ('price_type', '_type', 'sort', 'on_comment')
|
|
|
|
|
|
class CourseListSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
model = Course
|
|
fields = ['id', 'title']
|
|
|
|
|
|
class CourseTreeSerializer(serializers.ModelSerializer):
|
|
children = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Course
|
|
fields = ['id', 'title', 'children']
|
|
|
|
@staticmethod
|
|
def get_children(self):
|
|
theme_list = json.loads(self.normalmap.json_tree)
|
|
map = []
|
|
for theme_slim in theme_list:
|
|
theme = CourseTheme.objects.get(id=theme_slim['id'])
|
|
theme_obj = {
|
|
'id': theme.id,
|
|
'title': theme.title,
|
|
'lessons': [],
|
|
'tasks': [],
|
|
}
|
|
for simple_object in theme_slim['body']:
|
|
val = simple_object.split('_')[0]
|
|
if simple_object.split('_')[1] == 'L':
|
|
lesson = Lesson.objects.get(id=val)
|
|
lesson_obj = {'id': lesson.id, 'title': lesson.title}
|
|
theme_obj['lessons'].append(lesson_obj)
|
|
|
|
if simple_object.split('_')[1] == 'H':
|
|
task = Homework.objects.get(id=val)
|
|
task_obj = {
|
|
'id': task.id,
|
|
'is_exam': False,
|
|
}
|
|
theme_obj['tasks'].append(task_obj)
|
|
|
|
if simple_object.split('_')[1] == 'E':
|
|
task = Exam.objects.get(id=val)
|
|
task_obj = {
|
|
'id': task.id,
|
|
'is_exam': True,
|
|
}
|
|
theme_obj['tasks'].append(task_obj)
|
|
|
|
map.append(theme_obj)
|
|
|
|
return map
|
|
|
|
|
|
class CourseDetailSerializer(serializers.ModelSerializer):
|
|
level = serializers.SerializerMethodField()
|
|
direction = serializers.SerializerMethodField()
|
|
teachers = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Course
|
|
exclude = (
|
|
'slug', 'mentors', 'page', 'sort',
|
|
'preview', 'use_fail', 'basic_len',
|
|
'addition_len', 'min_price', 'buy_icon',
|
|
'must_build', 'keywords', 'recommend',
|
|
)
|
|
|
|
@staticmethod
|
|
def get_level(self):
|
|
return self.get_level_display()
|
|
|
|
@staticmethod
|
|
def get_direction(self):
|
|
return self.direction.title
|
|
|
|
@staticmethod
|
|
def get_teachers(self):
|
|
return [teacher.full_name() for teacher in self.teachers.all()]
|
|
|
|
|
|
# class UserSerializer(serializers.ModelSerializer):
|
|
# statistics = serializers.SerializerMethodField('get_statistic')
|
|
# games = serializers.SerializerMethodField('get_my_games')
|
|
# is_anonymous = serializers.BooleanField()
|
|
#
|
|
# @staticmethod
|
|
# def get_my_games(self):
|
|
# res = {}
|
|
# try:
|
|
# res['active'] = GameSerializer(self.game_set.get(state__lte=1)).data
|
|
# except ObjectDoesNotExist:
|
|
# res['active'] = {}
|
|
#
|
|
# res['archive'] = [GameSerializer(i).data for i in self.game_set.all().filter(state=2)]
|
|
# return res
|
|
#
|
|
# @staticmethod
|
|
# def get_statistic(self):
|
|
# try:
|
|
# statistics = StatisticSerializer(Statistic.objects.get(user=self)).data
|
|
# except ObjectDoesNotExist:
|
|
# statistics = {}
|
|
# return statistics
|
|
#
|
|
# class Meta:
|
|
# model = get_user_model()
|
|
# fields = ['id', 'username', 'email', 'is_active', 'statistics', 'games', 'is_anonymous'] |