# coding=utf-8
from django.core.mail import send_mail
from django.shortcuts import render
from django.template.loader import get_template
from lms.tools import out_date_format
from lms.settings import DOMAIN, DEFAULT_FROM_EMAIL, NAME
from management.mails import letter_decor
from random import choice
TEST_EMAIL = 'bez.b.unix@gmail.com'
@letter_decor()
def sent_clean_letter(title, content, to):
data = {
'title': title,
'email': to,
'text': content,
'type': u'Свободное'
}
return data
@letter_decor()
def sent_new_service_request(request, email):
data = {
'title': u'Новый запрос на покупку курса',
'email': email if email else TEST_EMAIL,
'text': u'
Пришел новый запрос на покупку курса.
'
u'
Курс:' + request.course.title if request else u'' + u'
'
u'
Имя: ' + request.student.get_full_name() if request else u'' + u'
' +
u'
Телефон: ' + request.student.phone if request else u'' + u', ' +
request.student.back_phone if request else u'' + u'
' +
u'
Почта: ' + request.student.email if request else u'' + u'
',
'type': u'Продажи'
}
return data
@letter_decor()
def sent_new_service_request_to_out(title, request, email):
context = {
'TITLE': title,
'USER_NAME': request.get('name'),
'PHONE': request.get('phone'),
'EMAIL': request.get('email'),
'HOST': request.get('host'),
'DOMAIN': DOMAIN,
'NAME': NAME
}
data = {
'title': title,
'text': '',
'email': email,
'type': 'Оповещения',
'result': get_template('mails/sent_out_order.html').render(context)
}
return data
@letter_decor()
def sent_new_self_bill(request, email):
data = {
'title': u'Новый оплаченый счет через форму',
'email': email if email else TEST_EMAIL,
'text': u'
Форма: ' + request.request.url if request else u'' + u'
'
u'
Услуга:' + request.request.name if request else u'' + u'
'
u'
Стоимость:' + str(request.request.sum) if request else u'' + u'
'
u'
Имя: ' + request.fio if request else u'' + u'
'
u'
Телефон: ' + request.phone if request else u'' + u'
'
u'
Почта: ' + request.email if request else u'' + u'
',
'type': u'Продажи'
}
return data
@letter_decor()
def sent_registration(user, title=u'Добро пожаловать в {0}'.format(NAME)):
if user: user.change_token()
data = {
'title': title,
'email': user.email if user else TEST_EMAIL,
'text': u'
Добро пожаловать!
'
u'
'
u'
Спасибо за регистрацию! Подтвердите свой аккаунт, перейдя по ссылке:'
u'{0}
'
u'
'.format(user.get_activation_url()),
'type': u'Сервисные'
}
return data
@letter_decor()
def sent_active_new_email(user):
data = {
'title': u'Изменение почтового ящика',
'email': user.changed_email if user else TEST_EMAIL,
'text': u'
В вашем профиле произошла замена адреса электронной почты. '
u'Если вы запрашивали такое изменение перейдите по '
u''
u'{2}access/activate_email/?email={0}&token={1}, для применения изменений. '
u'В ином случае игнорируйте это сообщение.
'
u'
'.format(user.email if user else u'', user.token if user else u'', DOMAIN),
'type': u'Сервисные'
}
return data
@letter_decor()
def sent_good_activation(user):
data = {
'title': u'Процесс активации прошел успешно',
'email': user.email if user else TEST_EMAIL,
'text': u'
Вы успешно прошли процесс активации!
'
u'
',
'type': u'Сервисные'
}
return data
@letter_decor()
def sent_forgot_password(user, password):
data = {
'title': u'Восстановление пароля',
'email': user.email if user else TEST_EMAIL,
'text': u'
Ваш временный пароль: {0}
'
u'
'.format(password if user else u''),
'type': u'Сервисные'
}
return data
@letter_decor()
def sent_accept_forgot(user):
data = {
'title': u'Восстановление пароля',
'email': user.email if user else TEST_EMAIL,
'text': u'
На ваш email поступил запрос для восстановления пароля. '
u'Если вы хотите восстановить пароль, перейдите по '
u''
u'{2}access/accept_forgot/?email={0}&token={1}. '
u'В ином случае просто игнорируйте это письмо.
'
u'
'.format(user.email if user else u'', user.token if user else u'', DOMAIN),
'type': u'Сервисные'
}
return data
@letter_decor()
def sent_comment_news(comment, new_comment, material, _type='L'):
data = {
'title': u'Ответ на ваш комментарий',
'email': comment.owner.email if comment else TEST_EMAIL,
'text': u'
'.format(new_comment.owner.get_short_name() if new_comment else u'', new_comment.get_text() if new_comment else u'', material.id if material else u'', u'урока' if _type=='L' else u'статьи', u'courses/lesson/' if _type=='L' else u'courses/lesson/', DOMAIN),
'type': u'Оповещения'
}
return data
@letter_decor()
def sent_teacher_answer(action):
data = {
'title': action.place if action else '',
'email': action.student.email if action else TEST_EMAIL,
'text': u'
' + action.text if action else u'' + u'
',
'type': u'Обучающий процесс'
}
return data
@letter_decor()
def sent_new_service_request(request, email):
data = {
'title': u'Новый запрос на покупку курса',
'email': email,
'text': u'
Пришел новый запрос на покупку курса.
'
u'
Тип запроса: {5}
'
u'
Курс: {0}
'
u'
Имя: {1}
'
u'
Телефон: {2}, {3}
'
u'
Почта: {4}
'.format(request.course.get_title() if request and request.course else u'',
request.student.get_full_name() if request else u'',
request.student.phone if request else u'',
request.student.back_phone if request else u'',
request.student.email if request else u'', request.get__type_display()),
'type': u'Продажи'
}
return data
@letter_decor()
def sent_new_self_bill(request, email):
data = {
'title': u'Новый оплаченый счет через форму',
'email': email if email else TEST_EMAIL,
'text': u'
Форма: ' + request.request.url if request else u'' + u'
'
u'
Услуга: ' + request.request.design.name if request else u'' + request.request.name if request else u'' + u'
'
u'
Стоимость:' + str(request.request.sum) if request else u'' + u'
'
u'
Имя: ' + request.fio if request else u'' + u'
'
u'
Телефон: ' + request.phone if request else u'' + u'
'
u'
Почта: ' + request.email if request else u'' + u'
',
'type': u'Продажи'
}
return data
@letter_decor()
def sent_created_self_bill(request, email):
data = {
'title': u'Клиент пытается оплатить счет через форму',
'email': email if email else TEST_EMAIL,
'text': u'
Форма: ' + request.request.url if request else u'' + '
'
u'
Услуга: ' + request.request.design.name if request else u'' + request.request.name if request else u'' + u'
'
u'
Стоимость:' + str(request.request.sum) if request else u'' + u'
'
u'
Имя: ' + request.fio if request else u'' + '
'
u'
Телефон: ' + request.phone if request else u'' + '
'
u'
Почта: ' + request.email if request else u'' + '
',
'type': u'Продажи'
}
return data
#@letter_decor()
#def sent_new_bill(bill):
# data = {
# 'title': u'Вам выставлен новый счет',
# 'email': bill.user.email if bill.user else TEST_EMAIL,
# 'text': u'
Состав заказа: {1}
'
# u'
Сумма для оплаты: {2}
'
# u'Чтобы выбрать способ оплаты и оплатить данный счет, воспользуйтесь ссылкой:'
# u'{0}'
# u'
или кнопкой ниже:
'
# u'
'.format(bill.gen_robokassa_url(), bill.get_name(), bill.price),
# 'type': u'Продажи'
#'template': '09Cg13KjU7'
# }
# return data
@letter_decor()
def sent_new_bill(bill):
context = {
'LINK': bill.gen_pay_link(),
'PRICE': bill.price,
'USER': bill.user.email,
'SERVICE': bill.get_name(),
'MANAGER': bill.manager.get_full_name(),
'MANAGER_EMAIL': bill.manager.get_email(),
'DOMAIN': DOMAIN,
'NAME': NAME
}
data = {
'title': 'Вам выставлен новый счет',
'text': '',
'email': bill.user.email,
'type': 'Продажи',
'result': get_template('mails/sent_order.html').render(context)
}
return data
@letter_decor()
def sent_welcome_without_password(user, title=None):
context = {
'EMAIL': user.email,
'LINK': user.get_activation_url()
}
data = {
'title': title if title else 'Вам выставлен новый счет',
'text': '',
'email': user.email,
'type': 'Сервисные',
'result': get_template('mails/new_users.html').render(context)
}
return data
@letter_decor()
def sent_welcome_to_webinar(user, title='Закрытый вебинар', template='mails/webinar.html'):
context = {
}
data = {
'title': title,
'text': '',
'email': user.email,
'type': 'Рассылки',
'result': get_template(template).render(context)
}
return data
@letter_decor()
def sent_new_comment(comment, lesson, email):
data = {
'title': u'Новый комментарий',
'email': email if email else TEST_EMAIL,
'text': u'
'.format(comment.owner if comment else '', comment.get_text() if comment else '', lesson.id),
'type': u'Оповещения'
}
return data
@letter_decor()
def pay_no_public_course(bill, email):
data = {
'title': u'Оплата не опубликованного курса',
'email': email if email else TEST_EMAIL,
'text': u'
Плательщик: {0}
'
u'
Услуга: {1}
'.format(bill.user.full_data(), bill.get_name()),
'type': u'Оповещения'
}
return data
@letter_decor()
def new_student(bill, email):
data = {
'title': u'У нас новенький / {0}'.format(bill.get_name()),
'email': email if email else TEST_EMAIL,
'text': u'
Плательщик: {0}
'
u'
Услуга: {1}
'.format(bill.user.full_data(), bill.get_name()),
'type': u'Оповещения'
}
return data
@letter_decor()
def sent_new_feedback(feedback, email):
data = {
'title': u'Новый вопрос',
'email': email if email else TEST_EMAIL,
'text': u'
Пришел новый комментарий от пользователя: {0}
'
u'
Почта: {1} Телефон:{2}
'
u'
Текст: {3}
'.format(feedback.name if feedback else '', feedback.email if feedback else '', feedback.phone if feedback else '', feedback.text if feedback else ''),
'type': u'Оповещения'
}
return data
@letter_decor()
def sent_new_expired(task, email):
data = {
'title': u'Просрочена проверка',
'email': email if email else TEST_EMAIL,
'text': u'
Просрочена от преподавателя: {0}
'
u'
Студент: {1}
'
u'
Дата отправки на проверку: {2}
'
u'
Ссылка: {3}
'.format(task.teacher.get_short_name() if task else '', task.student.get_short_name() if task else '',
out_date_format(task.date) if task else '', task.id if task else ''),
'type': u'Обучающий процесс'
}
return data
@letter_decor()
def sent_to_teacher_answer(action):
data = {
'title': action.place,
'email': action.student.email if action else TEST_EMAIL,
'text': u'
' + action.text + u'
',
'type': u'Оповещения'
}
return data
@letter_decor()
def sent_selfbill_remember(bill, status='day'):
data = {
'title': u'У нас есть не оплаченый счет',
'email': bill.email if bill else TEST_EMAIL,
'text': u'
'.format(bill.request.url if bill else u''),
'type': u'Продажи'
}
if status == 'day':
bill.day_sent = True
bill.save()
elif status == 'week':
bill.week_sent = True
bill.save()
elif status == 'month':
bill.month_sent = True
bill.save()
return data
@letter_decor()
def sent_created_my_self_bill(bill, email, title):
data = {
'title': title,
'email': email,
'text': u'Пользователь: {0}/{5}/{6} Продавец: {1} Курс: {4} Дата продажи: {2} Дата оплаты: {3} Сумма: {7} руб. Источник: {8}'.format(bill.user.get_short_name(), bill.manager.get_short_name(), out_date_format(bill.date), out_date_format(bill.finish_date), bill.get_name(), bill.user.email, bill.user.phone, bill.price, str(bill.traf_source)),
'type': u'Продажи'
}
return data
@letter_decor()
def sent_new_my_self_bill(bill, email):
data = {
'title': u'Выставлен новый счет',
'email': email,
'text': u'Пользователь: {0}/{5}/{6} '
u'Продавец: {1} '
u'Курс: {4} '
u'Дата продажи: {2} '
u'Дата оплаты: {3} '
u'Сумма: {7} руб. '
u'Внутренний комментарий: {9}'
u'Источник: {8}'.format(bill.user.get_short_name(), bill.manager.get_short_name(), out_date_format(bill.date), out_date_format(bill.finish_date), bill.get_name(), bill.user.email, bill.user.phone, bill.price, str(bill.traf_source), bill.description),
'type': u'Продажи'
}
return data
@letter_decor()
def sent_finish_self_bill(bill):
data = {
'title': '',
'email': '',
'text': ''
}
return data
@letter_decor()
def block_warning(users, course, email):
data = {
'title': u'Внимание, пользователи скоро дойдут до пустого материала',
'email': email if email else TEST_EMAIL,
'text': u'Курс: {0}'
u'
Пользователи: {1}
'.format(course if course else u'', str(users) if users else u''),
'type': u'Процесс обучения'
}
return data
@letter_decor()
def letter_delete_comment(comment, text):
data = {
'title': u'Ваш комментарий удален администрацией',
'email': comment.owner.email if comment else TEST_EMAIL,
'text': u'
Ваш комментарий:
'
u'
{0}
'
u'
Причина удаления:
'
u'
{1}
'.format(comment.get_text() if comment else '', text if text else ''),
'type': u'Сервисные'
}
return data
@letter_decor()
def free_week_end(bill, email):
c = bill.service.course.get_title() if bill.service.course else u''
data = {
'title': u'Закончилась бесплатная неделя',
'email': email if email else TEST_EMAIL,
'text': u'