diff --git a/api/v1/serializers/course.py b/api/v1/serializers/course.py index 4382aae4..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, @@ -327,6 +327,7 @@ class CommentSerializer(serializers.ModelSerializer): 'parent', 'deactivated_at', 'created_at', + 'created_at_humanize', 'update_at', ) @@ -342,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): @@ -374,3 +377,67 @@ class LessonCommentSerializer(serializers.ModelSerializer): read_only_fields = CommentSerializer.Meta.read_only_fields + ( 'children', ) + + +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) + + class Meta: + model = Comment + fields = ( + 'id', + 'content', + 'author', + 'parent', + 'deactivated_at', + 'created_at', + 'update_at', + 'obj_type', + 'obj_id', + ) + + read_only_fields = ( + 'id', + 'deactivated_at', + 'created_at', + 'update_at', + ) + + def create(self, validated_data): + obj_type = validated_data.pop('obj_type', None) + obj_id = validated_data.pop('obj_id', None) + if obj_type == Comment.OBJ_TYPE_COURSE: + validated_data['course_id'] = obj_id + return CourseCommentSerializer().create(validated_data) + 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/urls.py b/api/v1/urls.py index 51610cb6..0eafbe10 100644 --- a/api/v1/urls.py +++ b/api/v1/urls.py @@ -17,7 +17,7 @@ from .views import ( GalleryViewSet, GalleryImageViewSet, UserViewSet, LessonViewSet, ImageObjectViewSet, SchoolScheduleViewSet, LiveLessonViewSet, - PaymentViewSet, + PaymentViewSet, ObjectCommentsViewSet, ContestViewSet, ContestWorkViewSet) router = DefaultRouter() @@ -27,6 +27,7 @@ router.register(r'baners', BanerViewSet, base_name='baners') router.register(r'categories', CategoryViewSet, base_name='categories') router.register(r'courses', CourseViewSet, base_name='courses') router.register(r'comments', CommentViewSet, base_name='comments') +router.register(r'obj-comments', ObjectCommentsViewSet, base_name='obj-comments') router.register(r'materials', MaterialViewSet, base_name='materials') router.register(r'lessons', LessonViewSet, base_name='lessons') router.register(r'likes', LikeViewSet, base_name='likes') diff --git a/api/v1/views.py b/api/v1/views.py index c161a90d..c2bc1f07 100644 --- a/api/v1/views.py +++ b/api/v1/views.py @@ -11,10 +11,11 @@ from .serializers.course import ( CategorySerializer, LikeSerializer, CourseSerializer, CourseCreateSerializer, CourseBulkChangeCategorySerializer, - CommentSerializer, + CommentSerializer, CommentCreateSerializer, MaterialSerializer, MaterialCreateSerializer, LessonSerializer, LessonCreateSerializer, - LikeCreateSerializer) + LikeCreateSerializer, CourseCommentSerializer, LessonCommentSerializer, + LiveLessonCommentSerializer) from .serializers.content import ( BanerSerializer, ImageSerializer, ImageCreateSerializer, @@ -54,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, @@ -66,6 +67,7 @@ from apps.payment.models import ( ) from apps.school.models import SchoolSchedule, LiveLesson from apps.user.models import AuthorRequest +from project.pusher import pusher User = get_user_model() @@ -429,15 +431,15 @@ class ConfigViewSet(generics.RetrieveUpdateAPIView): class CommentViewSet(ExtendedModelViewSet): - queryset = Comment.objects.filter(level=0) + queryset = Comment.objects.all() serializer_class = CommentSerializer - permission_classes = (IsAdmin,) + permission_classes = (IsAuthorObjectOrAdmin,) def get_queryset(self): queryset = self.queryset is_deactivated = self.request.query_params.get('is_deactivated', '0') if is_deactivated == '0': - queryset = queryset + queryset = queryset.filter(level=0) elif is_deactivated == '1': queryset = queryset.filter(deactivated_at__isnull=True) elif is_deactivated == '2': @@ -446,6 +448,78 @@ class CommentViewSet(ExtendedModelViewSet): return queryset +class ObjectCommentsViewSet(ExtendedModelViewSet): + queryset = Comment.objects.all() + serializer_class = CommentCreateSerializer + permission_classes = (IsAuthorObjectOrAdmin,) + ordering_fields = ('update_at', ) + + def get_queryset(self): + queryset = self.queryset + 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 == Comment.OBJ_TYPE_COURSE: + queryset = CourseComment.objects.filter(course=obj_id) + 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': + queryset = queryset.filter(deactivated_at__isnull=True) + elif is_deactivated == '2': + queryset = queryset.filter(deactivated_at__isnull=False) + return queryset + + def get_serializer_class(self): + if self.request.method == 'POST': + return CommentCreateSerializer + obj_type = self.request.query_params.get('obj_type') + serializer_class = CommentSerializer + if obj_type == Comment.OBJ_TYPE_COURSE: + serializer_class = CourseCommentSerializer + 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): + obj_type = self.request.data.get('obj_type') + obj_id = self.request.data.get('obj_id') + serializer.save() + try: + pusher().trigger(f'comments_{obj_type}_{obj_id}', 'add', serializer.data) + except Exception as e: + print(e) + + def perform_destroy(self, instance): + obj_type = None + obj_id = None + if isinstance(instance, LessonComment): + obj_type = Comment.OBJ_TYPE_LESSON + obj_id = instance.lesson_id + elif isinstance(instance, CourseComment): + 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) + except Exception as e: + print(e) + instance.delete() + + def list(self, request, *args, **kwargs): + queryset = self.filter_queryset(self.get_queryset()).filter(parent__isnull=True) + serializer = self.get_serializer(queryset, many=True) + return Response(serializer.data) + + class AuthorRequestViewSet(ExtendedModelViewSet): queryset = AuthorRequest.objects.all() serializer_class = AuthorRequestSerializer @@ -466,14 +540,12 @@ class PaymentViewSet(viewsets.ModelViewSet): search_fields = ('user__email', 'user__first_name', 'user__last_name',) def get_serializer(self, instance, *args, **kwargs): - print('instance', type(instance), self.action) serializer_class = self.get_serializer_class() if 'update' in self.action: if isinstance(instance, CoursePayment): serializer_class = CoursePaymentCreateSerializer elif isinstance(instance, SchoolPayment): serializer_class = SchoolPaymentCreateSerializer - print('serializer_class', serializer_class) kwargs['context'] = self.get_serializer_context() return serializer_class(instance, *args, **kwargs) 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/apps/course/templates/course/lesson.html b/apps/course/templates/course/lesson.html index 1d96476a..d36219f7 100644 --- a/apps/course/templates/course/lesson.html +++ b/apps/course/templates/course/lesson.html @@ -117,37 +117,8 @@