From 9c8090916d489485c99c78a1297b4f1143a76fed Mon Sep 17 00:00:00 2001 From: gzbender Date: Wed, 19 Sep 2018 16:57:28 +0500 Subject: [PATCH] LIL-559, LiveLessonComment and etc --- api/v1/serializers/course.py | 33 +++++++++++++++---- api/v1/views.py | 25 ++++++++------ .../migrations/0044_livelessoncomment.py | 30 +++++++++++++++++ apps/course/models.py | 14 ++++++++ project/templates/blocks/lil_store_js.html | 1 + web/src/components/CommentForm.vue | 8 ++++- web/src/components/Comments.vue | 4 +-- web/src/sass/_common.sass | 12 +++++++ 8 files changed, 108 insertions(+), 19 deletions(-) create mode 100644 apps/course/migrations/0044_livelessoncomment.py diff --git a/api/v1/serializers/course.py b/api/v1/serializers/course.py index c97fd13c..fecd2b3a 100644 --- a/api/v1/serializers/course.py +++ b/api/v1/serializers/course.py @@ -10,7 +10,7 @@ from apps.course.models import ( Comment, CourseComment, LessonComment, Material, Lesson, Like, -) + LiveLessonComment) from .content import ( ImageObjectSerializer, ContentSerializer, ContentCreateSerializer, GallerySerializer, GalleryImageSerializer, @@ -343,6 +343,8 @@ class CommentSerializer(serializers.ModelSerializer): return CourseCommentSerializer(instance, context=self.context).to_representation(instance) elif isinstance(instance, LessonComment): return LessonCommentSerializer(instance, context=self.context).to_representation(instance) + elif isinstance(instance, LiveLessonComment): + return LiveLessonCommentSerializer(instance, context=self.context).to_representation(instance) class CourseCommentSerializer(serializers.ModelSerializer): @@ -377,10 +379,24 @@ class LessonCommentSerializer(serializers.ModelSerializer): ) -class CommentCreateSerializer(serializers.ModelSerializer): - OBJ_TYPE_COURSE = 'course' - OBJ_TYPE_LESSON = 'lesson' +class LiveLessonCommentSerializer(serializers.ModelSerializer): + author = UserSerializer() + children = CommentSerializer(many=True) + + class Meta: + model = LiveLessonComment + fields = CommentSerializer.Meta.fields + ( + 'live_lesson', + 'children', + ) + + read_only_fields = CommentSerializer.Meta.read_only_fields + ( + 'children', + ) + + +class CommentCreateSerializer(serializers.ModelSerializer): obj_type = serializers.CharField(required=True) obj_id = serializers.IntegerField(required=True) @@ -408,15 +424,20 @@ class CommentCreateSerializer(serializers.ModelSerializer): def create(self, validated_data): obj_type = validated_data.pop('obj_type', None) obj_id = validated_data.pop('obj_id', None) - if obj_type == self.OBJ_TYPE_COURSE: + if obj_type == Comment.OBJ_TYPE_COURSE: validated_data['course_id'] = obj_id return CourseCommentSerializer().create(validated_data) - elif obj_type == self.OBJ_TYPE_LESSON: + elif obj_type == Comment.OBJ_TYPE_LESSON: validated_data['lesson_id'] = obj_id return LessonCommentSerializer().create(validated_data) + elif obj_type == Comment.OBJ_TYPE_LIVE_LESSON: + validated_data['live_lesson_id'] = obj_id + return LiveLessonCommentSerializer().create(validated_data) def to_representation(self, instance): if isinstance(instance, CourseComment): return CourseCommentSerializer(instance, context=self.context).to_representation(instance) elif isinstance(instance, LessonComment): return LessonCommentSerializer(instance, context=self.context).to_representation(instance) + elif isinstance(instance, LiveLessonComment): + return LiveLessonCommentSerializer(instance, context=self.context).to_representation(instance) diff --git a/api/v1/views.py b/api/v1/views.py index 96a0255a..c2bc1f07 100644 --- a/api/v1/views.py +++ b/api/v1/views.py @@ -15,7 +15,7 @@ from .serializers.course import ( MaterialSerializer, MaterialCreateSerializer, LessonSerializer, LessonCreateSerializer, LikeCreateSerializer, CourseCommentSerializer, LessonCommentSerializer, -) + LiveLessonCommentSerializer) from .serializers.content import ( BanerSerializer, ImageSerializer, ImageCreateSerializer, @@ -55,7 +55,7 @@ from apps.course.models import ( Comment, CourseComment, LessonComment, Material, Lesson, Like, -) + LiveLessonComment) from apps.config.models import Config from apps.content.models import ( Baner, Image, Text, ImageText, Video, @@ -449,8 +449,6 @@ class CommentViewSet(ExtendedModelViewSet): class ObjectCommentsViewSet(ExtendedModelViewSet): - OBJ_TYPE_COURSE = 'course' - OBJ_TYPE_LESSON = 'lesson' queryset = Comment.objects.all() serializer_class = CommentCreateSerializer permission_classes = (IsAuthorObjectOrAdmin,) @@ -461,10 +459,12 @@ class ObjectCommentsViewSet(ExtendedModelViewSet): obj_type = self.request.query_params.get('obj_type') obj_id = self.request.query_params.get('obj_id') is_deactivated = self.request.query_params.get('is_deactivated') - if obj_type == self.OBJ_TYPE_COURSE: + if obj_type == Comment.OBJ_TYPE_COURSE: queryset = CourseComment.objects.filter(course=obj_id) - elif obj_type == self.OBJ_TYPE_LESSON: + elif obj_type == Comment.OBJ_TYPE_LESSON: queryset = LessonComment.objects.filter(lesson=obj_id) + elif obj_type == Comment.OBJ_TYPE_LIVE_LESSON: + queryset = LiveLessonComment.objects.filter(live_lesson=obj_id) if is_deactivated == '0': queryset = queryset.filter(level=0) elif is_deactivated == '1': @@ -478,10 +478,12 @@ class ObjectCommentsViewSet(ExtendedModelViewSet): return CommentCreateSerializer obj_type = self.request.query_params.get('obj_type') serializer_class = CommentSerializer - if obj_type == self.OBJ_TYPE_COURSE: + if obj_type == Comment.OBJ_TYPE_COURSE: serializer_class = CourseCommentSerializer - elif obj_type == self.OBJ_TYPE_LESSON: + elif obj_type == Comment.OBJ_TYPE_LESSON: serializer_class = LessonCommentSerializer + elif obj_type == Comment.OBJ_TYPE_LIVE_LESSON: + serializer_class = LiveLessonCommentSerializer return serializer_class def perform_create(self, serializer): @@ -497,11 +499,14 @@ class ObjectCommentsViewSet(ExtendedModelViewSet): obj_type = None obj_id = None if isinstance(instance, LessonComment): - obj_type = self.OBJ_TYPE_LESSON + obj_type = Comment.OBJ_TYPE_LESSON obj_id = instance.lesson_id elif isinstance(instance, CourseComment): - obj_type = self.OBJ_TYPE_COURSE + obj_type = Comment.OBJ_TYPE_COURSE obj_id = instance.course_id + elif isinstance(instance, LiveLessonComment): + obj_type = Comment.OBJ_TYPE_LIVE_LESSON + obj_id = instance.live_lesson_id serializer = self.get_serializer(instance) try: pusher().trigger(f'comments_{obj_type}_{obj_id}', 'delete', serializer.data) diff --git a/apps/course/migrations/0044_livelessoncomment.py b/apps/course/migrations/0044_livelessoncomment.py new file mode 100644 index 00000000..be5102af --- /dev/null +++ b/apps/course/migrations/0044_livelessoncomment.py @@ -0,0 +1,30 @@ +# Generated by Django 2.0.6 on 2018-09-19 15:41 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('school', '0021_schoolschedule_trial_lesson'), + ('course', '0043_auto_20180824_2132'), + ] + + operations = [ + migrations.CreateModel( + name='LiveLessonComment', + fields=[ + ('comment_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='course.Comment')), + ('live_lesson', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='school.LiveLesson')), + ], + options={ + 'verbose_name': 'Комментарий урока школы', + 'verbose_name_plural': 'Комментарии уроков школы', + 'ordering': ('tree_id', 'lft'), + 'abstract': False, + 'base_manager_name': 'objects', + }, + bases=('course.comment',), + ), + ] diff --git a/apps/course/models.py b/apps/course/models.py index 843e768e..41dcfe29 100644 --- a/apps/course/models.py +++ b/apps/course/models.py @@ -10,6 +10,7 @@ from django.urls import reverse_lazy from django.conf import settings from polymorphic_tree.models import PolymorphicMPTTModel, PolymorphicTreeForeignKey +from apps.school.models import LiveLesson from project.mixins import BaseModel, DeactivatedMixin from apps.content.models import ImageObject, Gallery, Video, ContestWork @@ -240,6 +241,9 @@ class Material(models.Model): class Comment(PolymorphicMPTTModel, DeactivatedMixin): + OBJ_TYPE_COURSE = 'course' + OBJ_TYPE_LESSON = 'lesson' + OBJ_TYPE_LIVE_LESSON = 'live-lesson' content = models.TextField('Текст комментария', default='') author = models.ForeignKey(User, on_delete=models.CASCADE) parent = PolymorphicTreeForeignKey( @@ -284,5 +288,15 @@ class LessonComment(Comment): verbose_name_plural = 'Комментарии уроков' +class LiveLessonComment(Comment): + live_lesson = models.ForeignKey( + LiveLesson, on_delete=models.CASCADE, related_name='comments' + ) + + class Meta(Comment.Meta): + verbose_name = 'Комментарий урока школы' + verbose_name_plural = 'Комментарии уроков школы' + + class ContestWorkComment(Comment): contest_work = models.ForeignKey(ContestWork, on_delete=models.CASCADE, related_name='comments') diff --git a/project/templates/blocks/lil_store_js.html b/project/templates/blocks/lil_store_js.html index 43926192..b892425a 100644 --- a/project/templates/blocks/lil_store_js.html +++ b/project/templates/blocks/lil_store_js.html @@ -1,6 +1,7 @@ {% load static %}