|
|
|
|
@ -1,10 +1,14 @@ |
|
|
|
|
import os |
|
|
|
|
from datetime import datetime |
|
|
|
|
from PIL import Image |
|
|
|
|
from PIL import ImageFont |
|
|
|
|
from PIL import ImageDraw |
|
|
|
|
|
|
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
|
from django.contrib.contenttypes.models import ContentType |
|
|
|
|
from django.contrib.staticfiles.storage import staticfiles_storage |
|
|
|
|
from django.utils.timezone import now |
|
|
|
|
from django.db.models import Max |
|
|
|
|
from django.conf import settings |
|
|
|
|
|
|
|
|
|
from apps.notification.models import UserNotification |
|
|
|
|
from apps.notification.utils import send_email |
|
|
|
|
@ -17,11 +21,31 @@ from project.sengrid import get_sendgrid_client |
|
|
|
|
User = get_user_model() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw_cert(path, email, first_name, last_name): |
|
|
|
|
img = Image.open(path) |
|
|
|
|
draw = ImageDraw.Draw(img) |
|
|
|
|
font = ImageFont.truetype("/app/Microsoft Sans Serif.ttf", 92) |
|
|
|
|
text = "награждается " + first_name |
|
|
|
|
if last_name: |
|
|
|
|
text += ' ' + last_name |
|
|
|
|
w, h = draw.textsize(text, font=font) |
|
|
|
|
draw.text(((img.width - w) // 2, 750), text, (29, 115, 224), font=font) |
|
|
|
|
fn = os.path.join(settings.MEDIA_ROOT, 'tmp') |
|
|
|
|
try: |
|
|
|
|
os.mkdir(fn) |
|
|
|
|
except: |
|
|
|
|
pass |
|
|
|
|
fn = os.path.join(fn, 'certificate-for-%s.jpg' % email) |
|
|
|
|
img.save(fn) |
|
|
|
|
img.close() |
|
|
|
|
return fn |
|
|
|
|
|
|
|
|
|
@app.task |
|
|
|
|
def send_certificates(email=None, date=None, dry_run=False): |
|
|
|
|
path_pattern = 'img/user-certificates/%d.jpg' |
|
|
|
|
if email: |
|
|
|
|
file = open(staticfiles_storage.path(path_pattern % 1), 'rb') |
|
|
|
|
fn = draw_cert(staticfiles_storage.path(path_pattern % 1), email, 'Имя', 'Фамилия') |
|
|
|
|
file = open(fn, 'rb') |
|
|
|
|
try: |
|
|
|
|
send_email('Грамота от Lil School', email, 'notification/email/certificate.html', |
|
|
|
|
attachments=[(file.name, file.read(), 'image/jpeg')]) |
|
|
|
|
@ -31,6 +55,7 @@ def send_certificates(email=None, date=None, dry_run=False): |
|
|
|
|
print('Email has been sent') |
|
|
|
|
finally: |
|
|
|
|
file.close() |
|
|
|
|
os.remove(fn) |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
date = datetime.strptime(date, '%d-%m-%Y').date() if date else now().date() |
|
|
|
|
@ -52,7 +77,10 @@ def send_certificates(email=None, date=None, dry_run=False): |
|
|
|
|
un.certificate_number = un.certificate_number + 1 \ |
|
|
|
|
if un.certificate_number and staticfiles_storage.exists(path_pattern % (un.certificate_number + 1)) \ |
|
|
|
|
else 1 |
|
|
|
|
file = open(staticfiles_storage.path(path_pattern % un.certificate_number), 'rb') |
|
|
|
|
fn = staticfiles_storage.path(path_pattern % un.certificate_number) |
|
|
|
|
if un.user.child_first_name: |
|
|
|
|
fn = draw_cert(fn, un.user.email, un.user.child_first_name, un.user.child_last_name) |
|
|
|
|
file = open(fn, 'rb') |
|
|
|
|
try: |
|
|
|
|
send_email('Грамота от Lil School', un.user.email, 'notification/email/certificate.html', |
|
|
|
|
attachments=[(file.name, file.read(), 'image/jpeg')], user_notification=un) |
|
|
|
|
@ -61,6 +89,8 @@ def send_certificates(email=None, date=None, dry_run=False): |
|
|
|
|
continue |
|
|
|
|
finally: |
|
|
|
|
file.close() |
|
|
|
|
if un.user.child_first_name: |
|
|
|
|
os.remove(fn) |
|
|
|
|
un.certificate_last_email = now() |
|
|
|
|
un.save() |
|
|
|
|
|
|
|
|
|
|