def send_email_limit_notification(email, days, language): block_date = arrow.utcnow() + datetime.timedelta(days=days) subject, body = MessageTemplate.get_rendered_message(MessageTemplate.CUSTOMER_BALANCE_LIMIT, language=language, block_date=block_date.datetime) logbook.info("Sending email with balance limit notification to {}", email) send_email.delay(email, subject, body)
def notify_managers_about_new_service_in_tariff(self, customer_id, flavor_name): customer = Customer.get_by_id(customer_id) if not customer: logbook.error("Customer {} not found in notify manager", customer_id) return logbook.info("notify manager about adding new service to tariff {}", customer.tariff.name) from api.admin.user import UserApi service_edit_url = urljoin( request_base_url(), posixpath.join(UserApi.ADMIN_FRONTEND_PATH, "tariffs", str(customer.tariff.tariff_id), "services")) customer_url = urljoin( request_base_url(), posixpath.join(UserApi.ADMIN_FRONTEND_PATH, "index", str(customer.customer_id), "info")) subject, body = MessageTemplate.get_rendered_message( MessageTemplate.NEW_SERVICE_IN_TARIFF, language=preferred_language(), account=customer.email, flavor_name=flavor_name, tariff=customer.tariff.name, customer_url=customer_url, service_edit_url=service_edit_url) logbook.info("Sending email notification to delete data of {}", customer.email) send_email.delay(get_customers_manager(customer), subject, body)
def send_email_about_balance_modifying(customer, delta, currency, balance, comment): assert isinstance(delta, Decimal) subscription_info = customer.subscription_info()['billing'] if subscription_info['enable']: modifying_date = utcnow().datetime if delta > 0: template_id = MessageTemplate.CUSTOMER_RECHARGE else: template_id = MessageTemplate.CUSTOMER_WITHDRAW base_url = request_base_url() from api.cabinet.customer import CustomerApi url = urljoin( base_url, posixpath.join(CustomerApi.CABINET_FRONTEND_PATH, "transactions")) subject, body = MessageTemplate.get_rendered_message( template_id, language=customer.locale_language(), money={ 'money': abs(delta), 'currency': currency }, balance={ 'money': balance, 'currency': currency }, comment=comment, withdraw_date=modifying_date, transactions_url=url) send_email.delay(subscription_info['email'], subject, body)
def send_email_limit_notification(email, days, language): block_date = arrow.utcnow() + datetime.timedelta(days=days) subject, body = MessageTemplate.get_rendered_message( MessageTemplate.CUSTOMER_BALANCE_LIMIT, language=language, block_date=block_date.datetime) logbook.info("Sending email with balance limit notification to {}", email) send_email.delay(email, subject, body)
def send_email_hdd_notification(manager_email, block_date, account): subject, body = MessageTemplate.get_rendered_message(MessageTemplate.CUSTOMER_HDD_DELETE, language=preferred_language(), block_date=block_date, account=account) logbook.info("Sending email notification to delete data of {}", account) send_email.delay(manager_email, subject, body)
def send_email_hdd_notification(manager_email, block_date, account): subject, body = MessageTemplate.get_rendered_message( MessageTemplate.CUSTOMER_HDD_DELETE, language=preferred_language(), block_date=block_date, account=account) logbook.info("Sending email notification to delete data of {}", account) send_email.delay(manager_email, subject, body)
def send_email_with_news(subject, body): if subject.startswith(conf.devel.test_prefix): return customers = Customer.news_subscribers() subject, body = MessageTemplate.get_rendered_message(MessageTemplate.NEWS, language=preferred_language(), news_subject=subject, news_body=body) for customer in customers: send_email.delay(customer.subscription_info()['news']['email'], subject, body)
def send_email_os_credentials(email, name, password, tenant_id, language): subject, body = MessageTemplate.get_rendered_message(MessageTemplate.OS_CREDENTIALS, language=language, os_name=name, os_password=password, os_tenant=tenant_id, os_region=conf.openstack.region, os_horizon_url=conf.openstack.horizon_url, os_keystone_url=conf.openstack.customer_keystone_url) log.debug("Sending email with OpenStack credentials to {}", name) send_email.delay(email, subject, body)
def send_email_with_news(subject, body): if subject.startswith(conf.devel.test_prefix): return customers = Customer.news_subscribers() subject, body = MessageTemplate.get_rendered_message( MessageTemplate.NEWS, language=preferred_language(), news_subject=subject, news_body=body) for customer in customers: send_email.delay(customer.subscription_info()['news']['email'], subject, body)
def send_email_os_credentials(email, name, password, tenant_id, language): subject, body = MessageTemplate.get_rendered_message( MessageTemplate.OS_CREDENTIALS, language=language, os_name=name, os_password=password, os_tenant=tenant_id, os_region=conf.openstack.region, os_horizon_url=conf.openstack.horizon_url, os_keystone_url=conf.openstack.customer_keystone_url) log.debug("Sending email with OpenStack credentials to {}", name) send_email.delay(email, subject, body)
def send_email(self, send_to, send_cc=None, subject=None): """ Send email to specified email send_to with custom subject and body. :param Email send_to: email to send message to. :param list send_cc: list of emails for cc email field. :param str subject: custom subject for email. """ template_subject, body = MessageTemplate.get_rendered_message( MessageTemplate.SEND_EMAIL, language=preferred_language()) if subject is None: subject = template_subject send_email.delay(send_to, subject, body, cc=send_cc) return {}
def send_password_reset_email(email, base_url, for_new_user=False): from task.mail import send_email user = User.get_by_email(email, include_deleted=False) if user is None: raise errors.UserNotFound() token = PasswordResetToken.create(user) url = urljoin(base_url, posixpath.join(UserApi.ADMIN_FRONTEND_PATH, "set-password/{}".format(token.id))) template_id = MessageTemplate.NEW_USER if for_new_user else MessageTemplate.USER_PASSWORD_RESET url_name = "activate_url" if for_new_user else "password_reset_url" params = {url_name: url} subject, body = MessageTemplate.get_rendered_message(template_id, language=preferred_language(), user_name=user.name, **params) send_email.delay(email, subject, body) return user
def send_email_about_blocking(self, blocked, user_id): subscription_info = self.subscription_info()['status'] if subscription_info['enable']: block_date = utcnow().datetime if blocked: if user_id: template_id = MessageTemplate.CUSTOMER_BLOCKED_BY_MANAGER subject, body = MessageTemplate.get_rendered_message(template_id, language=self.locale_language(), block_date=block_date, email=self.email) else: template_id = MessageTemplate.CUSTOMER_BLOCKED recommended_payment = conf.customer.minimum_recommended_payment.get(self.tariff.currency) recommended_payment = {'money': Decimal(recommended_payment), 'currency': self.tariff.currency} subject, body = MessageTemplate.get_rendered_message(template_id, language=self.locale_language(), block_date=block_date, email=self.email, minimum_recomended_payment=recommended_payment) else: template_id = MessageTemplate.CUSTOMER_UNBLOCKED subject, body = MessageTemplate.get_rendered_message(template_id, language=self.locale_language()) send_email.delay(subscription_info['email'], subject, body)
def notify_managers_about_new_service_in_tariff(self, customer_id, flavor_name): customer = Customer.get_by_id(customer_id) if not customer: logbook.error("Customer {} not found in notify manager", customer_id) return logbook.info("notify manager about adding new service to tariff {}", customer.tariff.name) from api.admin.user import UserApi service_edit_url = urljoin(request_base_url(), posixpath.join(UserApi.ADMIN_FRONTEND_PATH, "tariffs", str(customer.tariff.tariff_id), "services")) customer_url = urljoin(request_base_url(), posixpath.join(UserApi.ADMIN_FRONTEND_PATH, "index", str(customer.customer_id), "info")) subject, body = MessageTemplate.get_rendered_message(MessageTemplate.NEW_SERVICE_IN_TARIFF, language=preferred_language(), account=customer.email, flavor_name=flavor_name, tariff=customer.tariff.name, customer_url=customer_url, service_edit_url=service_edit_url) logbook.info("Sending email notification to delete data of {}", customer.email) send_email.delay(get_customers_manager(customer), subject, body)
def send_email_auto_payment_processed(cls, customer, delta, currency, card_type, card_last_four, comment, accepted=True): assert isinstance(delta, Decimal) subscription_info = customer.subscription_info()['billing'] if subscription_info['enable']: modifying_date = utcnow().datetime if accepted: template_id = MessageTemplate.CUSTOMER_RECHARGE_AUTO else: template_id = MessageTemplate.CUSTOMER_RECHARGE_AUTO_REJECT base_url = request_base_url() from api.cabinet.customer import CustomerApi url = urljoin( base_url, posixpath.join(CustomerApi.CABINET_FRONTEND_PATH, "transactions")) subject, body = MessageTemplate.get_rendered_message( template_id, language=customer.locale_language(), money={ 'money': abs(delta), 'currency': currency }, withdraw_date=modifying_date, card_type=card_type, card_last_four=card_last_four, transactions_url=url, comment=comment) send_email.delay(subscription_info['email'], subject, body)
def customer_auto_report(customer_id, time_now): from model import Customer, ScheduledTask from task.mail import send_email customer = Customer.get_by_id(customer_id) if not customer: logbook.error("Can't find customer {} for report generation", customer_id) return report_task = ScheduledTask.get_by_customer(customer_id, Customer.AUTO_REPORT_TASK) if not report_task: logbook.error("Can't find auto report task for customer {}", customer_id) return logbook.debug("Start auto-report task for customer {}: {}", customer, report_task) report_task.start() previous_next_send = report_task.next_scheduled.replace(tzinfo=utc) if previous_next_send is None or previous_next_send > time_now: logbook.warning("Looks like report for customer {} already sent. Next send: {}, current time {}", customer, previous_next_send, time_now) report_task.completed(now=time_now) return report_begin = day_start(report_task.last or time_now) _, report_end = report_task.task_range(time_now, previous_interval=True) report_end = day_start(report_end) report_id = CustomerReportId(customer.customer_id, report_begin, report_end, conf.customer.report.type, conf.customer.report.format, customer.locale) report_file_generate(report_id) # report_file_generate closed session so we should initialize customer again customer = Customer.get_by_id(customer_id) report_task = ScheduledTask.get_by_customer(customer_id, Customer.AUTO_REPORT_TASK) report_cache = ReportCache() report = report_cache.get_report_aggregated(report_id) if not report or not report["tariffs"]: logbook.info("Report is empty for customer {}", customer) report_task.completed(now=time_now) return report_file = report_cache.get_report(report_id) if not report_file: logbook.error("Report generation failed") report_task.completed(False) return filename = Report.generate_file_name(customer.get_name(), report_begin, report_end, conf.customer.report.format) subject, body = MessageTemplate.get_rendered_message( MessageTemplate.CUSTOMER_AUTO_REPORT, customer.locale_language(), customer_name=customer.get_name(), currency=customer.tariff.currency, report_start=report_begin, report_end=report_end) subscription_info = customer.subscription_info()['billing'] if subscription_info["enable"]: send_email.delay(subscription_info["email"], subject, body, attachments=[(filename, report_file)]) report_task.completed(now=time_now) comment_fmt = "%s - %s" % (report_begin.strftime('%Y-%m-%d'), report_end.strftime('%Y-%m-%d')) for currency, amount in report["total"].items(): amount = Decimal(amount) customer.modify_balance(-amount, currency, None, comment_fmt) account = customer.get_account(currency) account.charge(-amount) db.session.commit()
def customer_auto_report(customer_id, time_now): from model import Customer, ScheduledTask from task.mail import send_email customer = Customer.get_by_id(customer_id) if not customer: logbook.error("Can't find customer {} for report generation", customer_id) return report_task = ScheduledTask.get_by_customer(customer_id, Customer.AUTO_REPORT_TASK) if not report_task: logbook.error("Can't find auto report task for customer {}", customer_id) return logbook.debug("Start auto-report task for customer {}: {}", customer, report_task) report_task.start() previous_next_send = report_task.next_scheduled.replace(tzinfo=utc) if previous_next_send is None or previous_next_send > time_now: logbook.warning( "Looks like report for customer {} already sent. Next send: {}, current time {}", customer, previous_next_send, time_now) report_task.completed(now=time_now) return report_begin = day_start(report_task.last or time_now) _, report_end = report_task.task_range(time_now, previous_interval=True) report_end = day_start(report_end) report_id = CustomerReportId(customer.customer_id, report_begin, report_end, conf.customer.report.type, conf.customer.report.format, customer.locale) report_file_generate(report_id) # report_file_generate closed session so we should initialize customer again customer = Customer.get_by_id(customer_id) report_task = ScheduledTask.get_by_customer(customer_id, Customer.AUTO_REPORT_TASK) report_cache = ReportCache() report = report_cache.get_report_aggregated(report_id) if not report or not report["tariffs"]: logbook.info("Report is empty for customer {}", customer) report_task.completed(now=time_now) return report_file = report_cache.get_report(report_id) if not report_file: logbook.error("Report generation failed") report_task.completed(False) return filename = Report.generate_file_name(customer.get_name(), report_begin, report_end, conf.customer.report.format) subject, body = MessageTemplate.get_rendered_message( MessageTemplate.CUSTOMER_AUTO_REPORT, customer.locale_language(), customer_name=customer.get_name(), currency=customer.tariff.currency, report_start=report_begin, report_end=report_end) subscription_info = customer.subscription_info()['billing'] if subscription_info["enable"]: send_email.delay(subscription_info["email"], subject, body, attachments=[(filename, report_file)]) report_task.completed(now=time_now) comment_fmt = "%s - %s" % (report_begin.strftime('%Y-%m-%d'), report_end.strftime('%Y-%m-%d')) for currency, amount in report["total"].items(): amount = Decimal(amount) customer.modify_balance(-amount, currency, None, comment_fmt) account = customer.get_account(currency) account.charge(-amount) db.session.commit()
def send_email_make_production(self): template_id = MessageTemplate.CUSTOMER_MAKE_PRODUCTION subject, body = MessageTemplate.get_rendered_message(template_id, language=self.locale_language()) send_email.delay(self.email, subject, body)