diff --git a/apps/content/views.py b/apps/content/views.py index 4bb8e9fa..2838acc4 100644 --- a/apps/content/views.py +++ b/apps/content/views.py @@ -1,5 +1,8 @@ +import os + +from django.conf import settings from django.contrib.auth.decorators import login_required -from django.http import JsonResponse +from django.http import JsonResponse, HttpResponse from django.shortcuts import get_object_or_404 from django.template import loader from django.utils.decorators import method_decorator @@ -9,6 +12,7 @@ from django.views.generic import TemplateView, DetailView from apps.content.models import Contest, ContestWork, FAQ from apps.course.models import ContestWorkComment +from apps.user.models import Child @method_decorator(login_required, name='dispatch') @@ -124,3 +128,21 @@ class FAQView(TemplateView): } for f in FAQ.objects.all()] return self.render_to_response(context) +from apps.notification.tasks import draw_cert + +@csrf_exempt +def get_certificate(request, cert): + signed_path_pattern = 'signed-user-certificates/%d.jpg' + fn = signed_path_pattern % cert + path = os.path.join(settings.RESOURCES_ROOT, fn) + child_id = request.GET.get('child') + if child_id: + child = get_object_or_404(Child, pk=child_id) + path = draw_cert(path, child.user.email, child.first_name, child.last_name) + f = open(path, 'rb') + response = HttpResponse(f, content_type='image/jpeg') + response['Content-Disposition'] = 'attachment; filename=%s' % fn + f.close() + if child_id: + os.remove(path) + return response diff --git a/apps/notification/tasks.py b/apps/notification/tasks.py index 1e82d59e..9b2173fa 100644 --- a/apps/notification/tasks.py +++ b/apps/notification/tasks.py @@ -50,7 +50,6 @@ def draw_cert(path, email, first_name, last_name, x=None, y=900, color=(29, 115, @app.task def send_certificates(email=None, date_end=None, dry_run=False): - path_pattern = 'user-certificates/%d.jpg' signed_path_pattern = 'signed-user-certificates/%d.jpg' if email: fn = draw_cert(os.path.join(settings.RESOURCES_ROOT, signed_path_pattern % 1), email, 'Имя', 'Фамилия') @@ -83,31 +82,19 @@ def send_certificates(email=None, date_end=None, dry_run=False): if dry_run: continue - file_names = [] un.certificate_number = un.certificate_number + 1 \ if un.certificate_number and os.path.isfile(os.path.join(settings.RESOURCES_ROOT, - path_pattern % (un.certificate_number + 1))) \ + signed_path_pattern % (un.certificate_number + 1))) \ else 1 - if un.user.child_filled: - fn = os.path.join(settings.RESOURCES_ROOT, signed_path_pattern % un.certificate_number) - for child in un.user.childs.all(): - file_names.append(draw_cert(fn, un.user.email, child.first_name, child.last_name)) - else: - file_names.append(os.path.join(settings.RESOURCES_ROOT, signed_path_pattern % un.certificate_number)) - files = [open(fn, 'rb') for fn in file_names] + un.save() try: send_email('Грамота от Lil School', un.user.email, 'notification/email/certificate.html', - attachments=[(f.name, f.read(), 'image/jpeg') for f in files], user_notification=un, + childs=un.user.childs.all(), user_notification=un, many_childs=un.user.childs.all().count() > 1, child_filled=un.user.child_filled) - except: + except Exception as e: print('Not OK') + print(e) continue - finally: - for f in files: - f.close() - if un.user.child_filled: - for fn in file_names: - os.remove(fn) un.certificate_last_email = now() un.save() diff --git a/apps/notification/templates/notification/email/certificate.html b/apps/notification/templates/notification/email/certificate.html index 2e9b2f29..9b396841 100644 --- a/apps/notification/templates/notification/email/certificate.html +++ b/apps/notification/templates/notification/email/certificate.html @@ -1,4 +1,5 @@ {% extends "notification/email/_base.html" %} +{% load settings %} {% block content %}

Привет!

@@ -6,8 +7,8 @@ {% if not user_notification or user_notification.certificate_number == 1 %}

Поздравляем! Вы прошли месяц обучения в Lil School.
-К письму {% if many_childs %}прикреплены грамоты. Распечатайте их и вручите вашим детям. - {% else %}прикреплена грамота. Распечатайте её{% if not child_filled %}, подпишите{% endif %} и вручите вашему ребенку.{% endif %}
+В письме {% if many_childs %}ссылки на грамоты. Распечатайте их и вручите вашим детям. + {% else %}ссылка на грамоту. Распечатайте её{% if not child_filled %}, подпишите{% endif %} и вручите вашему ребенку.{% endif %}
Ждём вас в следующем месяце на наших творческих занятиях!

{% endif %} @@ -20,7 +21,7 @@ {% if user_notification and user_notification.certificate_number == 3 %}

Вам понравился наш творческий месяц?
-В письме вы найдёте {% if many_childs %}грамоты, они{% else %}грамоту, она{% endif %} для вашей семьи.
+В письме вы найдёте {% if many_childs %}ссылки на грамоты, они{% else %}ссылку на грамоту, она{% endif %} для вашей семьи.
Как здорово, что у нас есть такие ученики!
Ждём вас в следующем месяце.

@@ -28,17 +29,17 @@ {% if user_notification and user_notification.certificate_number == 4 %}

Прошёл целый месяц обучения на платформе Lil School - месяц творчества, креатива и невероятных идей.
-Во вложении вас {% if many_childs %}ждут грамоты{% else %}ждёт грамота{% endif %}.
+Во письме вас {% if many_childs %}ждут ссылки на грамоты{% else %}ждёт ссылка на грамоту{% endif %}.
До встречи на занятиях!

{% endif %} {% if user_notification and user_notification.certificate_number == 5 %}

{% if many_childs %} - Ваши грамоты за успехи в учебе в Lil School ждут вас во вложении.
+ Ваши грамоты за успехи в учебе в Lil School ждут вас.
Скорее распечатайте их!
{% else %} - Ваша грамота за успехи в учебе в Lil School ждёт вас во вложении.
+ Ваша грамота за успехи в учебе в Lil School ждёт вас.
Скорее распечатайте её!
{% endif %} Вам есть чем гордится!
@@ -56,7 +57,7 @@ {% if user_notification and user_notification.certificate_number == 7 %}

Какой классный месяц у нас был! Вместе мы очень здорово и креативно потрудились.
-Во вложении вас {% if many_childs %}ждут заслуженные грамоты{% else %}ждёт заслуженная грамота{% endif %}!
+В письме вас {% if many_childs %}ждут ссылки на заслуженные грамоты{% else %}ждёт ссылка на заслуженную грамоту{% endif %}!
До встречи на уроках!

{% endif %} @@ -64,10 +65,21 @@

Месяц творчества и креатива пролетел как один миг! А как много работ мы вместе сделали!
Вы - большие молодцы.
-Во вложении вас {% if many_childs %}ждут грамоты{% else %}ждёт грамота{% endif %}!
+В письме вас {% if many_childs %}ждут ссылки на грамоты{% else %}ждёт ссылка на грамоту{% endif %}!
До встречи на занятиях.

{% endif %} +

+ {% if many_childs %} + Ссылки на грамоты:
+ {% for child in childs %} + Грамота ({{ child.full_name }})
+ {% endfor %} + {% else %} + Ссылка на грамоту:
+ Скачать грамоту + {% endif %} +

diff --git a/apps/school/views.py b/apps/school/views.py index a5621682..f2b407ed 100644 --- a/apps/school/views.py +++ b/apps/school/views.py @@ -114,7 +114,7 @@ class LiveLessonsDetailView(DetailView): user=request.user, date_start__lte=now(), date_end__gte=now() - timedelta(days=30), - package__duration__lte=9, + package__duration=12, ).exists() if not is_purchased and request.user.role not in [User.ADMIN_ROLE, User.TEACHER_ROLE]: raise Http404 diff --git a/apps/user/models.py b/apps/user/models.py index e442ed71..ad4b31fd 100644 --- a/apps/user/models.py +++ b/apps/user/models.py @@ -380,3 +380,7 @@ class Child(models.Model): class Meta: ordering = ('id',) + + @property + def full_name(self): + return ' '.join([self.first_name, self.last_name]) if self.last_name else self.first_name diff --git a/project/urls.py b/project/urls.py index e2bee7b0..1f8ff1dc 100644 --- a/project/urls.py +++ b/project/urls.py @@ -19,7 +19,8 @@ from django.views.generic import TemplateView from django.urls import path, include from apps.auth.views import LandingRegistrationView -from apps.content.views import ContestEditView, ContestView, ContestWorkView, contest_work_comment, FAQView +from apps.content.views import ContestEditView, ContestView, ContestWorkView, contest_work_comment, FAQView, \ + get_certificate from apps.course.views import ( CoursesView, likes, coursecomment, CourseView, LessonView, SearchView, @@ -106,6 +107,7 @@ urlpatterns = [ path('links', LinksView.as_view(), name='links'), path('prices', PackagesView.as_view(), name='packages'), path('landing-registration', LandingRegistrationView.as_view(), name='landing-registration'), + path('certificate//get', get_certificate, name='get-certificate'), ]