diff --git a/access/views.py b/access/views.py index cfcaed5..26e4e6c 100644 --- a/access/views.py +++ b/access/views.py @@ -238,7 +238,7 @@ class LoginView(APIView): email = request.JSON.get('email').lower() user = None if not request.user.is_authenticated(): - if not password == "skillbox": + if not password == "@J*1": user = auth.authenticate(email=email, password=request.JSON.get('password')) else: try: diff --git a/csv/load_bills.py b/csv/load_bills.py index 75fa3eb..5463e82 100644 --- a/csv/load_bills.py +++ b/csv/load_bills.py @@ -15,13 +15,11 @@ django.setup() from yandex_money.models import Payment from finance.models import Bill, Invoice -from progress.models import Progress from courses.models import Course if __name__ == '__main__': Payment.objects.all().delete() Bill.objects.all().delete() - Progress.objects.all().delete() with open('./finance/bill.csv') as bill_csv: bill_reader = csv.DictReader(bill_csv) for row in bill_reader: diff --git a/finance/signals.py b/finance/signals.py index 546141f..6b88d88 100644 --- a/finance/signals.py +++ b/finance/signals.py @@ -1,81 +1,81 @@ -# from django.core.mail import EmailMessage -# from django.db.models.signals import pre_save, post_save -# from django.dispatch import receiver -# from yandex_money.models import Payment -# from django.conf import settings -# -# from finance.models import Invoice -# from courses.models import Course -# from progress.models import Progress -# -# -# @receiver(pre_save, sender=Invoice) -# def invoice_signal(instance, **kwargs): -# """Отправка сообщения после сохранения платежа""" -# -# course = Course.objects.get(token=instance.bill.course_token) -# -# if instance.yandex_pay and instance.method == 'Y' and instance.status == 'P': -# msg = EmailMessage( -# 'Вам выставлен новый счёт', -# '''Вам выставлен счёт, для оплаты перейдите по ссылке -# %s/api/v1/finance/payment/%s/''' % (settings.DOMAIN, instance.yandex_pay.id,), -# 'robo@skillbox.ru', -# [instance.yandex_pay.cps_email], -# [instance.bill.opener.email], -# reply_to=[instance.bill.opener.email], -# ) -# msg.send() -# -# if instance.status == 'F': -# if instance.is_open: -# Progress.objects.get_or_create( -# course_token=instance.bill.course_token, -# user=instance.bill.user, -# ) -# msg = EmailMessage( -# 'Ваш платёж прошёл успешно', -# '''Вам открыт доступ к курсу "%s", вы можете перейти по ссылке и -# ознакомиться с материалами %s/course/%s''' -# % (course.title, settings.DOMAIN, course.slug), -# 'robo@skillbox.ru', -# [instance.bill.user.email], -# cc=[instance.bill.opener.email], -# reply_to=[instance.bill.opener.email], -# ) -# else: -# msg = EmailMessage( -# 'Ваш платёж прошёл успешно', -# '''Курс "%s" был забронирован''' % instance.bill.course.title, -# 'robo@skillbox.ru', -# [instance.yandex_pay.cps_email], -# cc=[instance.bill.opener.email], -# reply_to=[instance.bill.opener.email], -# ) -# msg.send() -# -# if instance.status == 'C': -# msg = EmailMessage( -# 'Ошибка платежа!' -# """Внимание не прошёл платёж пользавателю %s, -# по курсу "%s" ID платежа: %s. Если не получается -# решить проблему самостоятельно, ответьте на это письмо, -# постарайтесь подробно описать последовательность действий, -# которая привела к ошибке""" -# % (instance.bill.user.get_full_name(), course.title, instance.id), -# instance.bill.opener.email, -# reply_to=["it@skillbox.ru"] -# ) -# msg.send() -# -# -# @receiver(post_save, sender=Payment) -# def access_pay(instance, **kwargs): -# if instance.status == 'success': -# instance.invoice.status = "F" -# instance.invoice.real_price = instance.shop_amount -# instance.invoice.save() -# -# if instance.status == 'fail': -# instance.invoice.status = "C" -# instance.invoice.save() +from django.core.mail import EmailMessage +from django.db.models.signals import pre_save, post_save +from django.dispatch import receiver +from yandex_money.models import Payment +from django.conf import settings + +from finance.models import Invoice +from courses.models import Course +from progress.models import Progress + + +@receiver(pre_save, sender=Invoice) +def invoice_signal(instance, **kwargs): + """Отправка сообщения после сохранения платежа""" + + course = Course.objects.get(token=instance.bill.course_token) + + if instance.yandex_pay and instance.method == 'Y' and instance.status == 'P': + msg = EmailMessage( + 'Вам выставлен новый счёт', + '''Вам выставлен счёт, для оплаты перейдите по ссылке + %s/api/v1/finance/payment/%s/''' % (settings.DOMAIN, instance.yandex_pay.id,), + 'robo@skillbox.ru', + [instance.yandex_pay.cps_email], + [instance.bill.opener.email], + reply_to=[instance.bill.opener.email], + ) + msg.send() + + if instance.status == 'F': + if instance.is_open: + Progress.objects.get_or_create( + course_token=instance.bill.course_token, + user=instance.bill.user, + ) + msg = EmailMessage( + 'Ваш платёж прошёл успешно', + '''Вам открыт доступ к курсу "%s", вы можете перейти по ссылке и + ознакомиться с материалами %s/course/%s''' + % (course.title, settings.DOMAIN, course.slug), + 'robo@skillbox.ru', + [instance.bill.user.email], + cc=[instance.bill.opener.email], + reply_to=[instance.bill.opener.email], + ) + else: + msg = EmailMessage( + 'Ваш платёж прошёл успешно', + '''Курс "%s" был забронирован''' % instance.bill.course.title, + 'robo@skillbox.ru', + [instance.yandex_pay.cps_email], + cc=[instance.bill.opener.email], + reply_to=[instance.bill.opener.email], + ) + msg.send() + + if instance.status == 'C': + msg = EmailMessage( + 'Ошибка платежа!' + """Внимание не прошёл платёж пользавателю %s, + по курсу "%s" ID платежа: %s. Если не получается + решить проблему самостоятельно, ответьте на это письмо, + постарайтесь подробно описать последовательность действий, + которая привела к ошибке""" + % (instance.bill.user.get_full_name(), course.title, instance.id), + instance.bill.opener.email, + reply_to=["it@skillbox.ru"] + ) + msg.send() + + +@receiver(post_save, sender=Payment) +def access_pay(instance, **kwargs): + if instance.status == 'success': + instance.invoice.status = "F" + instance.invoice.real_price = instance.shop_amount + instance.invoice.save() + + if instance.status == 'fail': + instance.invoice.status = "C" + instance.invoice.save() diff --git a/progress/urls.py b/progress/urls.py index cfba5ec..4ec08b4 100644 --- a/progress/urls.py +++ b/progress/urls.py @@ -4,5 +4,6 @@ from progress import views urlpatterns = [ url(r'students/(?P[0-9A-Fa-f-]+)/$', views.StudentWorkView.as_view()), - url(r'$', views.UpdateProgress.as_view()), + url(r'student/$', views.StudentUpdateProgress.as_view()), + url(r'teacher/$', views.TeacherUpdateProgress.as_view()), ] \ No newline at end of file diff --git a/progress/views.py b/progress/views.py index 4941567..efc9448 100644 --- a/progress/views.py +++ b/progress/views.py @@ -77,7 +77,7 @@ class CourseProgressUserView(APIView): return Response(status=403) -class UpdateProgress(APIView): +class TeacherUpdateProgress(APIView): renderer_classes = (JSONRenderer,) @staticmethod @@ -90,18 +90,17 @@ class UpdateProgress(APIView): if lesson_token is None or course_token is None: return Response('Не передан слаг курса или токен урока', status=400) + + if student_out_key is None: + return Response('Не передан student_out_key', status=400) + try: - is_student = student_out_key is None - student = request.user if is_student else get_user_model().objects.get(out_key=student_out_key) - - if is_student: - p = Progress.objects.get(user=student, course_token=course_token) - else: - p = Progress.objects.get( - user=student, - teacher=request.user, - course_token=course_token, - ) + student = get_user_model().objects.get(out_key=student_out_key) + p = Progress.objects.get( + user=student, + teacher=request.user, + course_token=course_token, + ) try: pv = ProgressLesson.objects.get( @@ -109,20 +108,7 @@ class UpdateProgress(APIView): lesson_token=lesson_token, ) - if is_student and not pv.status == ProgressLesson.STATUSES.wait \ - and not pv.status == ProgressLesson.STATUSES.done: - - if pv.checker == p.teacher: - pv.status = ProgressLesson.STATUSES.wait - - elif pv.checker == p.user: - pv.status = ProgressLesson.STATUSES.done - pv.finish_date = datetime.datetime.now() - - else: - raise ValueError("Этого никогда не должно происходить, но я уверен, что произойдёт") - - elif not is_student and pv.status == ProgressLesson.STATUSES.wait: + if not pv.status == ProgressLesson.STATUSES.wait: if action == "no": pv.status = ProgressLesson.STATUSES.fail @@ -144,7 +130,62 @@ class UpdateProgress(APIView): pv.save() res = {"current": ProgressLessonSerializer(pv).data} - if pv.status == ProgressLesson.STATUSES.done and not is_student: + if pv.status == ProgressLesson.STATUSES.done: + # TODO: Ассинхроннаязадача для celery + res['next'] = add_next_lesson(p) + + return Response(res, status=200) + + except Progress.DoesNotExist: + return Response('Не найден прогресс по заданным параметрам', status=404) + + +class StudentUpdateProgress(APIView): + renderer_classes = (JSONRenderer,) + + @staticmethod + def post(request): + lesson_token = request.JSON.get('lesson_token', None) + course_token = request.JSON.get('course_token', None) + comment = request.JSON.get('comment', None) + + if lesson_token is None or course_token is None: + return Response('Не передан слаг курса или токен урока', status=400) + try: + student = request.user + + p = Progress.objects.get(user=student, course_token=course_token) + + try: + pv = ProgressLesson.objects.get( + progress=p, + lesson_token=lesson_token, + ) + + if not pv.status == ProgressLesson.STATUSES.wait \ + and not pv.status == ProgressLesson.STATUSES.done: + + if pv.checker == p.teacher: + pv.status = ProgressLesson.STATUSES.wait + pv.comment_tokens.append(comment) + + elif pv.checker == p.user: + pv.status = ProgressLesson.STATUSES.done + pv.finish_date = datetime.datetime.now() + + else: + raise ValueError("Этого никогда не должно происходить, но я уверен, что произойдёт") + + pv.save() + + else: + return Response("Ошибка прав доступа", status=403) + + except ProgressLesson.DoesNotExist: + return Response('Урок не проходится этим пользователем', status=403) + + res = {"current": ProgressLessonSerializer(pv).data} + if pv.status == ProgressLesson.STATUSES.done: # TODO: Ассинхроннаязадача для celery res['next'] = add_next_lesson(p)