diff --git a/api/v1/serializers/course.py b/api/v1/serializers/course.py index 2b7d2806..551830ad 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, @@ -329,6 +329,7 @@ class CommentSerializer(serializers.ModelSerializer): 'parent', 'deactivated_at', 'created_at', + 'created_at_humanize', 'update_at', ) @@ -344,6 +345,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): @@ -376,3 +379,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/serializers/mixins.py b/api/v1/serializers/mixins.py index 40584503..50e7d19d 100644 --- a/api/v1/serializers/mixins.py +++ b/api/v1/serializers/mixins.py @@ -32,35 +32,23 @@ class DispatchContentMixin(object): if 'id' in cdata and cdata['id']: t = Text.objects.get(id=cdata.pop('id')) serializer = TextCreateSerializer(t, data=cdata) - if serializer.is_valid(): - serializer.save() else: serializer = TextCreateSerializer(data=cdata) - if serializer.is_valid(): - serializer.save() + if serializer.is_valid(): + serializer.save() elif ctype == 'image': if 'id' in cdata and cdata['id']: image = Image.objects.get(id=cdata.pop('id')) serializer = ImageCreateSerializer(image, data=cdata) - if serializer.is_valid(): - image = serializer.save() - else: - continue - try: - image_object = ImageObject.objects.get(id=cdata['img']) - except ImageObject.DoesNotExist: - pass - else: - image.img = image_object - image.save() else: serializer = ImageCreateSerializer(data=cdata) - if serializer.is_valid(): - image = serializer.save() - else: - continue + if serializer.is_valid(): + image = serializer.save() + else: + continue + if 'img' in cdata: try: - image_object = ImageObject.objects.get(id=cdata['img']) + image_object = ImageObject.objects.get(id=cdata.get('img')) except ImageObject.DoesNotExist: pass else: @@ -71,25 +59,15 @@ class DispatchContentMixin(object): if 'id' in cdata and cdata['id']: it = ImageText.objects.get(id=cdata.pop('id')) serializer = ImageTextCreateSerializer(it, data=cdata) - if serializer.is_valid(): - it = serializer.save() - else: - continue - try: - image_object = ImageObject.objects.get(id=cdata['img']) - except ImageObject.DoesNotExist: - pass - else: - it.img = image_object - it.save() else: serializer = ImageTextCreateSerializer(data=cdata) - if serializer.is_valid(): - it = serializer.save() - else: - continue + if serializer.is_valid(): + it = serializer.save() + else: + continue + if 'img' in cdata: try: - image_object = ImageObject.objects.get(id=cdata['img']) + image_object = ImageObject.objects.get(id=cdata.get('img')) except ImageObject.DoesNotExist: pass else: @@ -100,52 +78,36 @@ class DispatchContentMixin(object): if 'id' in cdata and cdata['id']: v = Video.objects.get(id=cdata.pop('id')) serializer = VideoCreateSerializer(v, data=cdata) - if serializer.is_valid(): - serializer.save() else: serializer = VideoCreateSerializer(data=cdata) - if serializer.is_valid(): - serializer.save() + if serializer.is_valid(): + serializer.save() elif ctype == 'images': if 'id' in cdata and cdata['id']: g = Gallery.objects.get(id=cdata['id']) g.position = cdata['position'] g.title = cdata['title'] g.uuid = cdata['uuid'] - setattr(g, obj_type, obj) - g.save() - if 'images' in cdata: - for image in cdata['images']: - if 'img' in image and image['img']: - if 'id' in image and image['id']: - gi = GalleryImage.objects.get(id=image['id']) - gi.img = ImageObject.objects.get(id=image['img']) - gi.save() - else: - gi = GalleryImage.objects.create( - gallery=g, - img=ImageObject.objects.get(id=image['img']) - ) else: g = Gallery( position=cdata['position'], title=cdata['title'], uuid=cdata['uuid'], ) - setattr(g, obj_type, obj) - g.save() - if 'images' in cdata: - for image in cdata['images']: - if 'img' in image and image['img']: - if 'id' in image and image['id']: - gi = GalleryImage.objects.get(id=image['id']) - gi.img = ImageObject.objects.get(id=image['img']) - gi.save() - else: - gi = GalleryImage.objects.create( - gallery=g, - img=ImageObject.objects.get(id=image['img']) - ) + setattr(g, obj_type, obj) + g.save() + if 'images' in cdata: + for image in cdata['images']: + if 'img' in image and image['img']: + if 'id' in image and image['id']: + gi = GalleryImage.objects.get(id=image['id']) + gi.img = ImageObject.objects.get(id=image['img']) + gi.save() + else: + gi = GalleryImage.objects.create( + gallery=g, + img=ImageObject.objects.get(id=image['img']) + ) class DispatchMaterialMixin(object): diff --git a/api/v1/serializers/payment.py b/api/v1/serializers/payment.py index 6268a63d..b388be21 100644 --- a/api/v1/serializers/payment.py +++ b/api/v1/serializers/payment.py @@ -46,8 +46,29 @@ class AuthorBalanceCreateSerializer(serializers.ModelSerializer): return AuthorBalanceSerializer(instance, context=self.context).to_representation(instance) +class PaymentSerializer(serializers.ModelSerializer): + user = UserSerializer() + + class Meta: + model = Payment + fields = BASE_PAYMENT_FIELDS + read_only_fields = ( + 'id', + 'user', + 'created_at', + 'update_at', + ) + + def to_representation(self, instance): + if isinstance(instance, CoursePayment): + return CoursePaymentSerializer(instance, context=self.context).to_representation(instance) + elif isinstance(instance, SchoolPayment): + return SchoolPaymentSerializer(instance, context=self.context).to_representation(instance) + + class AuthorBalanceSerializer(serializers.ModelSerializer): author = UserSerializer() + payment = serializers.SerializerMethodField() class Meta: model = AuthorBalance @@ -70,6 +91,24 @@ class AuthorBalanceSerializer(serializers.ModelSerializer): 'payment', ) + def get_payment(self, instance): + try: + p = instance.payment + except Exception: + return None + data = { + 'id': p.id, + 'created_at': p.created_at, + 'amount': p.amount, + 'data': p.data, + } + if isinstance(instance.payment, CoursePayment): + data['course'] = { + 'id': p.course.id, + 'title': p.course.title, + } + return data + class PaymentSerializer(serializers.ModelSerializer): user = UserSerializer() @@ -80,6 +119,7 @@ class PaymentSerializer(serializers.ModelSerializer): read_only_fields = ( 'id', 'user', + 'data', 'created_at', 'update_at', ) @@ -91,6 +131,20 @@ class PaymentSerializer(serializers.ModelSerializer): return SchoolPaymentSerializer(instance, context=self.context).to_representation(instance) +class CoursePaymentCreateSerializer(serializers.ModelSerializer): + + class Meta: + model = CoursePayment + fields = BASE_PAYMENT_FIELDS + ('course',) + read_only_fields = ( + 'id', + 'user', + 'course', + 'created_at', + 'update_at', + ) + + class CoursePaymentSerializer(serializers.ModelSerializer): user = UserSerializer() course = CourseSerializer() @@ -107,6 +161,24 @@ class CoursePaymentSerializer(serializers.ModelSerializer): ) +class SchoolPaymentCreateSerializer(serializers.ModelSerializer): + + class Meta: + model = SchoolPayment + fields = BASE_PAYMENT_FIELDS + ( + 'weekdays', + 'date_start', + 'date_end', + ) + read_only_fields = ( + 'id', + 'user', + 'course', + 'created_at', + 'update_at', + ) + + class SchoolPaymentSerializer(serializers.ModelSerializer): user = UserSerializer() 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 7620244b..d8129ebe 100644 --- a/api/v1/views.py +++ b/api/v1/views.py @@ -1,3 +1,5 @@ +from datetime import datetime + from django.contrib.auth import get_user_model from rest_framework import status, views, viewsets, generics @@ -11,10 +13,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, @@ -34,7 +37,7 @@ from .serializers.payment import ( AuthorBalanceSerializer, AuthorBalanceCreateSerializer, PaymentSerializer, CoursePaymentSerializer, SchoolPaymentSerializer, -) + CoursePaymentCreateSerializer, SchoolPaymentCreateSerializer) from .serializers.user import ( AuthorRequestSerializer, UserSerializer, UserPhotoSerializer, @@ -54,7 +57,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,13 +69,14 @@ 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() class AuthorBalanceViewSet(ExtendedModelViewSet): queryset = AuthorBalance.objects.filter( - author__role__in=[User.AUTHOR_ROLE, User.ADMIN_ROLE], + author__role__in=[User.AUTHOR_ROLE, User.ADMIN_ROLE, User.TEACHER_ROLE], ) serializer_class = AuthorBalanceCreateSerializer serializer_class_map = { @@ -429,15 +433,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 +450,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 @@ -453,7 +529,7 @@ class AuthorRequestViewSet(ExtendedModelViewSet): filter_fields = ('status',) -class PaymentViewSet(ExtendedModelViewSet): +class PaymentViewSet(viewsets.ModelViewSet): queryset = Payment.objects.all() serializer_class = PaymentSerializer permission_classes = (IsAdmin,) @@ -465,6 +541,16 @@ class PaymentViewSet(ExtendedModelViewSet): ) search_fields = ('user__email', 'user__first_name', 'user__last_name',) + def get_serializer(self, instance, *args, **kwargs): + 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 + kwargs['context'] = self.get_serializer_context() + return serializer_class(instance, *args, **kwargs) + def get_queryset(self): queryset = self.queryset course = self.request.query_params.get('course') @@ -481,10 +567,12 @@ class PaymentViewSet(ExtendedModelViewSet): user = request.query_params.get('user') course = request.query_params.get('course') weekdays = request.query_params.getlist('weekdays[]') + date_start = request.query_params.get('date_start') user = user and User.objects.get(pk=user) course = course and Course.objects.get(pk=course) + date_start = date_start and datetime.strptime(date_start, '%Y-%m-%d') - return Response(Payment.calc_amount(user=user, course=course, weekdays=weekdays)) + return Response(Payment.calc_amount(user=user, course=course, date_start=date_start, weekdays=weekdays)) class ContestViewSet(ExtendedModelViewSet): 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 9a72d454..c7927600 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 @@ -258,6 +259,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( @@ -302,5 +306,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 608153f0..3f820a17 100644 --- a/apps/course/templates/course/lesson.html +++ b/apps/course/templates/course/lesson.html @@ -2,6 +2,9 @@ {% load static %} {% block title %}{{ lesson.title }} - {{ block.super }}{% endblock title %} +{% block twurl %}{{ request.build_absolute_uri }}{% endblock twurl %} +{% block ogtitle %}{{ lesson.title }} - {{ block.super }}{% endblock ogtitle %} +{% block ogurl %}{{ request.build_absolute_uri }}{% endblock ogurl %} {% block ogimage %}http://{{request.META.HTTP_HOST}}{% if lesson.course.cover %}{{ lesson.course.cover.image.url }}{% else %}{% static 'img/og_courses.jpg' %}{% endif %}{% endblock ogimage %} {% block content %}
@@ -114,37 +117,8 @@
Задавайте вопросы:
-
- {% if request.user.is_authenticated %} -
- -
- -
-
- -
- -
- -
-
- {% else %} -
Только зарегистрированные пользователи могут оставлять комментарии.
- {% endif %} -
- {% include "templates/blocks/comments.html" with object=lesson %} -
+
+
diff --git a/apps/payment/models.py b/apps/payment/models.py index 21e6171f..e2d7cb13 100644 --- a/apps/payment/models.py +++ b/apps/payment/models.py @@ -111,13 +111,25 @@ class Payment(PolymorphicModel): ordering = ('created_at',) @classmethod - def calc_amount(cls, course_payment=None, school_payment=None, user=None, course=None, weekdays=None): + def add_months(cls, sourcedate, months=1): + result = arrow.get(sourcedate, settings.TIME_ZONE).shift(months=months) + if months == 1: + if (sourcedate.month == 2 and sourcedate.day >= 28) or (sourcedate.day == 31 and result.day <= 30) \ + or (sourcedate.month == 1 and sourcedate.day >= 29 and result.day == 28): + result = result.replace(day=1, month=result.month + 1) + return result.datetime + + @classmethod + def calc_amount(cls, course_payment=None, school_payment=None, user=None, course=None, date_start=None, weekdays=None): + date_start = date_start or now().date() + date_end = Payment.add_months(date_start, 1) if course_payment: course = course_payment.course user = course_payment.user if school_payment: user = school_payment.user weekdays = school_payment.weekdays + date_start = school_payment.date_start discount = 0 price = 0 if course: @@ -126,8 +138,8 @@ class Payment(PolymorphicModel): if user: school_payments = SchoolPayment.objects.filter( user=user, - date_start__lte=now().date(), - date_end__gte=now().date(), + date_start__lte=date_start, + date_end__gte=date_start, add_days=False, status__in=[ Pingback.PINGBACK_TYPE_REGULAR, @@ -147,7 +159,8 @@ class Payment(PolymorphicModel): weekday__in=weekdays, ) if add_days: - weekdays_count = weekdays_in_date_range(now().date(), prev_school_payment.date_end) + date_end = prev_school_payment.date_end + weekdays_count = weekdays_in_date_range(date_start, prev_school_payment.date_end) all_weekdays_count = weekdays_in_date_range(prev_school_payment.date_start, prev_school_payment.date_end) for ss in school_schedules: price += ss.month_price // all_weekdays_count.get(ss.weekday, 0) * weekdays_count.get( @@ -163,6 +176,8 @@ class Payment(PolymorphicModel): 'price': price, 'amount': amount, 'discount': discount, + 'date_start': date_start, + 'date_end': date_end, } def calc_commission(self): @@ -193,8 +208,9 @@ class CoursePayment(Payment): verbose_name_plural = 'Платежи за курсы' def save(self, *args, **kwargs): - amount_data = Payment.calc_amount(course_payment=self) - self.amount = amount_data.get('amount') + if self.status is None: + amount_data = Payment.calc_amount(course_payment=self) + self.amount = amount_data.get('amount') super().save(*args, **kwargs) author_balance = getattr(self, 'authorbalance', None) if not author_balance: @@ -226,8 +242,9 @@ class SchoolPayment(Payment): return days def save(self, *args, **kwargs): - amount_data = Payment.calc_amount(school_payment=self) - self.amount = amount_data.get('amount') + if self.status is None: + amount_data = Payment.calc_amount(school_payment=self) + self.amount = amount_data.get('amount') super().save(*args, **kwargs) @property diff --git a/apps/payment/views.py b/apps/payment/views.py index bb352346..ad5a76ef 100644 --- a/apps/payment/views.py +++ b/apps/payment/views.py @@ -1,3 +1,5 @@ +from decimal import Decimal + import arrow import json import logging @@ -25,7 +27,7 @@ from apps.course.models import Course from apps.school.models import SchoolSchedule from apps.payment.tasks import transaction_to_mixpanel, product_payment_to_mixpanel, transaction_to_roistat -from .models import AuthorBalance, CoursePayment, SchoolPayment +from .models import AuthorBalance, CoursePayment, SchoolPayment, Payment logger = logging.getLogger('django') @@ -99,6 +101,8 @@ class SchoolBuyView(TemplateView): host = str(host[0]) + '://' + str(host[1]) weekdays = set(request.GET.getlist('weekdays', [])) roistat_visit = request.COOKIES.get('roistat_visit', None) + date_start = request.GET.get('date_start') + date_start = date_start and datetime.datetime.strptime(date_start, '%Y-%m-%d') or now().date() if not weekdays: messages.error(request, 'Выберите несколько дней недели.') return redirect('school:summer-school') @@ -109,21 +113,21 @@ class SchoolBuyView(TemplateView): return redirect('school:summer-school') prev_school_payment = SchoolPayment.objects.filter( user=request.user, - date_start__lte=now().date(), - date_end__gte=now().date(), + date_start__lte=date_start, + date_end__gte=date_start, add_days=False, status__in=[ Pingback.PINGBACK_TYPE_REGULAR, Pingback.PINGBACK_TYPE_GOODWILL, Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED, ], - ).first() # ??? first? + ).last() add_days = bool(prev_school_payment) if add_days: school_payment = SchoolPayment.objects.create( user=request.user, weekdays=weekdays, - date_start=now().date(), + date_start=date_start, date_end=prev_school_payment.date_end, add_days=True, roistat_visit=roistat_visit, @@ -136,6 +140,8 @@ class SchoolBuyView(TemplateView): user=request.user, weekdays=weekdays, roistat_visit=roistat_visit, + date_start=date_start, + date_end=Payment.add_months(date_start), ) product = Product( f'school_{school_payment.id}', @@ -162,14 +168,6 @@ class SchoolBuyView(TemplateView): @method_decorator(csrf_exempt, name='dispatch') class PaymentwallCallbackView(View): - def add_months(self, sourcedate, months=1): - result = arrow.get(sourcedate, settings.TIME_ZONE).shift(months=months) - if months == 1: - if (sourcedate.month == 2 and sourcedate.day >= 28) or (sourcedate.day == 31 and result.day <= 30)\ - or (sourcedate.month == 1 and sourcedate.day >= 29 and result.day == 28): - result = result.replace(day=1, month=result.month + 1) - return result.datetime - def get_request_ip(self): x_forwarded_for = self.request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: @@ -206,7 +204,7 @@ class PaymentwallCallbackView(View): effective_amount = payment_raw_data.get('effective_price_amount') if effective_amount: - payment.amount = effective_amount + payment.amount = Decimal(effective_amount) transaction_to_mixpanel.delay( payment.user.id, @@ -215,30 +213,6 @@ class PaymentwallCallbackView(View): product_type_name, ) - if product_type_name == 'school': - school_payment = SchoolPayment.objects.filter( - user=payment.user, - add_days=False, - date_start__lte=now().date(), - date_end__gte=now().date(), - status__in=[ - Pingback.PINGBACK_TYPE_REGULAR, - Pingback.PINGBACK_TYPE_GOODWILL, - Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED, - ], - ).last() - if school_payment: - if payment.add_days: - date_start = now().date() - date_end = school_payment.date_end - else: - date_start = arrow.get(school_payment.date_end, settings.TIME_ZONE).shift(days=1).datetime - date_end = self.add_months(date_start) - else: - date_start = now().date() - date_end = self.add_months(date_start) - payment.date_start = date_start - payment.date_end = date_end if product_type_name == 'course': properties = { 'payment_id': payment.id, diff --git a/apps/school/models.py b/apps/school/models.py index a059172f..4eb71350 100644 --- a/apps/school/models.py +++ b/apps/school/models.py @@ -14,6 +14,7 @@ from apps.payment import models as payment_models class SchoolSchedule(models.Model): + WEEKDAY_SHORT_NAMES = ('пн', 'вт', 'ср', 'чт', 'пт', 'сб', 'вс') WEEKDAY_CHOICES = ( (1, 'понедельник'), (2, 'вторник'), diff --git a/apps/school/templates/blocks/schedule_item.html b/apps/school/templates/blocks/schedule_item.html index 7e860a4e..272ae2db 100644 --- a/apps/school/templates/blocks/schedule_item.html +++ b/apps/school/templates/blocks/schedule_item.html @@ -4,7 +4,7 @@
{{ school_schedule }} - {% if request.user_agent.is_mobile and school_schedule.trial_lesson %} + {% if not is_purchased and request.user_agent.is_mobile and school_schedule.trial_lesson %} Пробный урок {% endif %}
@@ -20,7 +20,7 @@ {% else %} {% include './day_pay_btn.html' %} {% endif %} - {% if not request.user_agent.is_mobile and school_schedule.trial_lesson %} + {% if not is_purchased and not request.user_agent.is_mobile and school_schedule.trial_lesson %} Пробный урок {% endif %}
diff --git a/apps/school/templates/school/livelesson_detail.html b/apps/school/templates/school/livelesson_detail.html index f553f447..d3e5c5af 100644 --- a/apps/school/templates/school/livelesson_detail.html +++ b/apps/school/templates/school/livelesson_detail.html @@ -2,6 +2,13 @@ {% load static %} {% block title %}{{ livelesson.title }} - {{ block.super }}{% endblock title %} +{% block twurl %}{{ request.build_absolute_uri }}{% endblock twurl %} +{% block ogtitle %}{{ livelesson.title }} - {{ block.super }}{% endblock ogtitle %} +{% block ogurl %}{{ request.build_absolute_uri }}{% endblock ogurl %} +{% if livelesson.cover and livelesson.cover.image %} +{% block ogimage %}http://{{request.META.HTTP_HOST}}{{ livelesson.cover.image.url }}{% endblock ogimage %} +{% endif %} + {% block content %}
@@ -14,7 +21,8 @@ mozallowfullscreen allowfullscreen> Если видео не загрузилось, - уменьшите качество видео или обновите страницу - + + {% else %} {% if livelesson.cover %} diff --git a/apps/school/templates/school/summer_school.html b/apps/school/templates/school/summer_school.html index 00da0bf8..0f0c1fe4 100644 --- a/apps/school/templates/school/summer_school.html +++ b/apps/school/templates/school/summer_school.html @@ -1,6 +1,6 @@ {% extends "templates/lilcity/index.html" %} {% load static %} {% block title %}Онлайн-школа LilCity{% endblock title%} -{% block ogimage %}http://{{request.META.HTTP_HOST}}{% static 'img/og_summer_school.jpg' %}{% endblock %} +{% block ogimage %}http://{{request.META.HTTP_HOST}}{% static 'img/og_main.jpg' %}{% endblock %} {% block content %} {% if not is_purchased %} {% include "../summer/promo.html" %} diff --git a/apps/school/templates/summer/prolong_btn.html b/apps/school/templates/summer/prolong_btn.html index c4026242..75021093 100644 --- a/apps/school/templates/summer/prolong_btn.html +++ b/apps/school/templates/summer/prolong_btn.html @@ -1,9 +1,5 @@ продлить diff --git a/apps/school/templates/summer/schedule_purchased.html b/apps/school/templates/summer/schedule_purchased.html index e22818c7..a2106299 100644 --- a/apps/school/templates/summer/schedule_purchased.html +++ b/apps/school/templates/summer/schedule_purchased.html @@ -5,14 +5,10 @@
diff --git a/apps/school/views.py b/apps/school/views.py index 3816c0eb..24e2ab61 100644 --- a/apps/school/views.py +++ b/apps/school/views.py @@ -140,21 +140,9 @@ class SchoolView(TemplateView): pass school_schedules_dict = {ss.weekday: ss for ss in school_schedules} school_schedules_dict[0] = school_schedules_dict.get(7) - all_schedules_purchased = [] + live_lessons = None + live_lessons_exists = False if self.request.user.is_authenticated: - all_schedules_purchased = SchoolPayment.objects.filter( - user=self.request.user, - status__in=[ - Pingback.PINGBACK_TYPE_REGULAR, - Pingback.PINGBACK_TYPE_GOODWILL, - Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED, - ], - date_start__range=[month_start, date_now], - ).annotate( - joined_weekdays=Func(F('weekdays'), function='unnest',) - ).distinct().values_list('joined_weekdays', flat=True) - all_schedules_purchased = list(map(lambda x: 1 if x == 7 else x+1, all_schedules_purchased)) - school_payment = SchoolPayment.objects.filter( user=self.request.user, status__in=[ @@ -173,18 +161,33 @@ class SchoolView(TemplateView): else: school_payment_exists = False school_schedules_purchased = [] - if all_schedules_purchased and is_previous: - live_lessons = LiveLesson.objects.filter( - date__range=[yesterday - timedelta(days=7), yesterday], - deactivated_at__isnull=True, - date__week_day__in=all_schedules_purchased, - ).order_by('-date') + if is_previous: + prev_range = [yesterday - timedelta(days=7), yesterday] + live_lessons = [] + # берем все подписки, которые были в периоде + for sp in SchoolPayment.objects.filter( + date_start__lte=prev_range[1], + date_end__gte=prev_range[0], + user=self.request.user, + status__in=[ + Pingback.PINGBACK_TYPE_REGULAR, + Pingback.PINGBACK_TYPE_GOODWILL, + Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED, + ], + ): + # берем все уроки в оплаченном промежутке + date_range = [max(sp.date_start, prev_range[0]), min(sp.date_end, prev_range[1])] + live_lessons += LiveLesson.objects.filter( + date__range=date_range, + deactivated_at__isnull=True, + date__week_day__in=list(map(lambda x: 1 if x == 7 else x+1, sp.weekdays)), + ).values_list('id', flat=True) + live_lessons = LiveLesson.objects.filter(id__in=set(live_lessons)).order_by('-date') for ll in live_lessons: ll.school_schedule = school_schedules_dict.get(ll.date.isoweekday()) live_lessons_exists = live_lessons.exists() - else: - live_lessons = None - live_lessons_exists = False + live_lessons = live_lessons or None + subscription_ends = school_payment.filter(add_days=False).last().date_end if school_payment_exists else None context.update({ 'online': online, 'live_lessons': live_lessons, @@ -197,7 +200,9 @@ class SchoolView(TemplateView): 'school_schedules': school_schedules, 'school_schedules_purchased': school_schedules_purchased, 'school_purchased_future': False, - 'subscription_ends': school_payment.filter(add_days=False).first().date_end if school_payment_exists else None, + 'subscription_ends': subscription_ends, + 'prolong_date_start': subscription_ends + timedelta(days=1), + 'allow_prolong': subscription_ends - date_now <= timedelta(days=14) }) return context diff --git a/apps/user/models.py b/apps/user/models.py index 0df7cd01..47008960 100644 --- a/apps/user/models.py +++ b/apps/user/models.py @@ -92,7 +92,7 @@ class User(AbstractUser): @property def balance(self): - aggregate = self.balances.filter( + income = self.balances.filter( type=0, payment__isnull=False, payment__status__isnull=False @@ -100,9 +100,13 @@ class User(AbstractUser): models.Sum('amount'), models.Sum('commission'), ) - amount = aggregate.get('amount__sum') or 0 - commission = aggregate.get('commission__sum') or 0 - return amount - commission + income_amount = income.get('amount__sum') or 0 + income_commission = income.get('commission__sum') or 0 + + payout = self.balances.filter(type=1, status=1).aggregate(models.Sum('amount')) + payout_amount = payout.get('amount__sum') or 0 + + return income_amount - income_commission - payout_amount @receiver(post_save, sender=User) diff --git a/apps/user/templates/user/profile.html b/apps/user/templates/user/profile.html index e8a81d33..9ad4d552 100644 --- a/apps/user/templates/user/profile.html +++ b/apps/user/templates/user/profile.html @@ -64,10 +64,7 @@
- - + {% if is_author %}
- {% if is_purchased_future %} -
-
-
Ваша подписка начинается {{school_purchased_future.date_start}}
-
-
- {% else %} - {% if is_purchased %} - {% include "blocks/schedule_purchased.html" %} - {% else %} -
-
-
Вы не подписаны на онлайн-школу!
-
- Купить подписку + -
-
-
+ {% endif %} {% if paid.exists %} {% include "course/course_items.html" with course_items=paid %} - {% else %} + {% endif %} + {% if not is_school_purchased and not paid.exists %}
-
Нет приобретённых курсов!
+
Вы пока ничего не приобрели...
@@ -125,7 +111,7 @@
{% if is_author %}
-
+
{% if published.exists %} {% include "course/course_items.html" with course_items=published %} diff --git a/apps/user/views.py b/apps/user/views.py index 3f4ca843..103ff126 100644 --- a/apps/user/views.py +++ b/apps/user/views.py @@ -16,7 +16,7 @@ from django.contrib.auth import get_user_model from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.hashers import check_password, make_password from django.http import Http404 -from django.db.models import F, Func +from django.db.models import F, Func, Sum, Min, Max from django.urls import reverse_lazy from django.utils.decorators import method_decorator from django.utils.timezone import now @@ -55,11 +55,11 @@ class ProfileView(TemplateView): def get_context_data(self, object): context = super().get_context_data() context['user'] = self.request.user - context['is_author'] = self.request.user.role == User.AUTHOR_ROLE context['published'] = Course.objects.filter( author=self.object, ) + context['is_author'] = context['published'] or self.request.user.role == User.AUTHOR_ROLE context['paid'] = Course.objects.filter( payments__in=CoursePayment.objects.filter( user=self.object, @@ -80,22 +80,18 @@ class ProfileView(TemplateView): Pingback.PINGBACK_TYPE_RISK_REVIEWED_ACCEPTED, ], ) - school_schedules_purchased = school_payment.annotate( - joined_weekdays=Func(F('weekdays'), function='unnest',) - ).values_list('joined_weekdays', flat=True).distinct() - context['school_schedules_purchased'] = school_schedules_purchased - context['school_payment'] = school_payment - context['is_purchased'] = school_payment.exists() + context['is_school_purchased'] = school_payment.exists() + if context['is_school_purchased']: + school_schedules_purchased = school_payment.annotate( + joined_weekdays=Func(F('weekdays'), function='unnest',) + ).values_list('joined_weekdays', flat=True).distinct() + aggregated = school_payment.aggregate(Sum('amount'), Min('date_start'), Max('date_end'),) + context['school_purchased_weekdays'] = '-'.join(map(lambda wd: SchoolSchedule.WEEKDAY_SHORT_NAMES[wd-1], + set(sorted(school_schedules_purchased)))) + context['school_purchased_price'] = aggregated.get('amount__sum') or 0 + context['school_purchased_dates'] = [aggregated.get('date_start__min'), aggregated.get('date_end__max')] + context['profile'] = True - if school_payment.exists() and school_payment.last().date_end: - context['subscription_ends'] = school_payment.last().date_end - context['school_schedules'] = SchoolSchedule.objects.filter( - weekday__in=school_schedules_purchased if school_payment.exists() else [], - ).all() - context['all_school_schedules'] = SchoolSchedule.objects.all() - - context['is_purchased_future'] = False - context['school_purchased_future'] = False return context diff --git a/project/context_processors.py b/project/context_processors.py index e4788dc4..ebb36f25 100644 --- a/project/context_processors.py +++ b/project/context_processors.py @@ -1,3 +1,4 @@ +from django.db.models import Func, F from django.utils.timezone import now from paymentwall.pingback import Pingback @@ -14,7 +15,7 @@ def baner(request): return {'baner': Baner.objects.filter(use=True).first()} -def is_summer_school_purchased(request): +def school_purchased(request): if request.user.is_authenticated: n = now().date() school_payment = SchoolPayment.objects.filter( @@ -27,5 +28,14 @@ def is_summer_school_purchased(request): date_start__lte=n, date_end__gte=n ) - return {'is_summer_school_purchased': school_payment.exists()} - return {'is_summer_school_purchased': False} + school_schedules_purchased = school_payment.annotate( + joined_weekdays=Func(F('weekdays'), function='unnest', ) + ).values_list('joined_weekdays', flat=True).distinct() + return { + 'is_school_purchased': school_payment.exists(), + 'school_schedules_purchased': school_schedules_purchased, + } + return { + 'is_school_purchased': False, + 'school_schedules_purchased': [], + } diff --git a/project/pusher.py b/project/pusher.py new file mode 100644 index 00000000..af6c74a8 --- /dev/null +++ b/project/pusher.py @@ -0,0 +1,14 @@ +from django.conf import settings +from pusher import Pusher + + +def pusher(): + try: + pusher_cluster = settings.PUSHER_CLUSTER + except AttributeError: + pusher_cluster = 'mt1' + + return Pusher(app_id=settings.PUSHER_APP_ID, + key=settings.PUSHER_KEY, + secret=settings.PUSHER_SECRET, + cluster=pusher_cluster) diff --git a/project/settings.py b/project/settings.py index b9232bcd..51545f31 100644 --- a/project/settings.py +++ b/project/settings.py @@ -94,7 +94,7 @@ TEMPLATES = [ 'context_processors': [ 'project.context_processors.config', 'project.context_processors.baner', - 'project.context_processors.is_summer_school_purchased', + 'project.context_processors.school_purchased', 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', @@ -197,6 +197,13 @@ TWILIO_FROM_PHONE = os.getenv('TWILIO_FROM_PHONE', '+37128914409') ACTIVE_LINK_STRICT = True +# PUSHER settings + +PUSHER_APP_ID = os.getenv('PUSHER_APP_ID', '') +PUSHER_KEY = os.getenv('PUSHER_KEY', '') +PUSHER_SECRET = os.getenv('PUSHER_SECRET', '') +PUSHER_CLUSTER = 'eu' + # DRF settings REST_FRAMEWORK = { diff --git a/project/templates/blocks/lil_store_js.html b/project/templates/blocks/lil_store_js.html index 15eb772d..5bda5974 100644 --- a/project/templates/blocks/lil_store_js.html +++ b/project/templates/blocks/lil_store_js.html @@ -1,14 +1,19 @@ {% load static %} +{% load setting from settings %} diff --git a/project/templates/blocks/popup_buy.html b/project/templates/blocks/popup_buy.html index 3294f273..6aba682d 100644 --- a/project/templates/blocks/popup_buy.html +++ b/project/templates/blocks/popup_buy.html @@ -20,40 +20,6 @@
- {% if all_school_schedules %} - {% for school_schedule in all_school_schedules %} - - {% endfor %} - {% else %} {% for school_schedule in school_schedules %} {% endfor %} - {% endif %}
@@ -102,6 +68,7 @@
ШКОЛА
+
diff --git a/project/templates/lilcity/edit_index.html b/project/templates/lilcity/edit_index.html index 18be67aa..70691489 100644 --- a/project/templates/lilcity/edit_index.html +++ b/project/templates/lilcity/edit_index.html @@ -316,12 +316,12 @@
{% include 'templates/blocks/lil_store_js.html' %} - {% block foot %}{% endblock foot %} + diff --git a/project/templates/lilcity/index.html b/project/templates/lilcity/index.html index 8d4ebf95..871e2dc1 100644 --- a/project/templates/lilcity/index.html +++ b/project/templates/lilcity/index.html @@ -56,6 +56,7 @@ powered by LiveChat + + diff --git a/web/src/components/CommentForm.vue b/web/src/components/CommentForm.vue new file mode 100644 index 00000000..a7c9fd29 --- /dev/null +++ b/web/src/components/CommentForm.vue @@ -0,0 +1,53 @@ + + + + diff --git a/web/src/components/Comments.vue b/web/src/components/Comments.vue new file mode 100644 index 00000000..ad7e88d1 --- /dev/null +++ b/web/src/components/Comments.vue @@ -0,0 +1,134 @@ + + + + diff --git a/web/src/components/ContestRedactor.vue b/web/src/components/ContestRedactor.vue index 41820a66..a0cffc9d 100644 --- a/web/src/components/ContestRedactor.vue +++ b/web/src/components/ContestRedactor.vue @@ -66,45 +66,7 @@
- -
- - - - - -
-
- - +
@@ -113,15 +75,9 @@ diff --git a/web/src/components/CourseRedactor.vue b/web/src/components/CourseRedactor.vue index 7888d9cc..767a4254 100644 --- a/web/src/components/CourseRedactor.vue +++ b/web/src/components/CourseRedactor.vue @@ -171,45 +171,7 @@
- -
- - - - - -
-
- - +