def mutate(self, info, **kwargs): template_instance = get_model_object(StockCountTemplate, 'id', kwargs.get('template_id')) template_fields = stock_counts.get_template_fields(**kwargs) stock_tempalate = stock_counts.add_fields_to_template( template_instance, **template_fields) to_email = stock_tempalate.assigned_users.values_list('email', flat=True) email_stock_template = 'email_alerts/stocks/stock_email.html' subject = 'Stock counts template updated' context = { 'schedule_time': datetime.strftime(stock_tempalate.schedule_time.start_date, "%d-%b-%Y"), 'by': str(info.context.user.email), 'template_type': 'Stock counts', 'small_text_detail': 'Hello, stock counts schedule has \ been modified' } params = {'model': StockCountTemplate} with SaveContextManager(stock_tempalate, **params) as stock_tempalate: send_mail = SendMail(email_stock_template, context, subject, to_email) send_mail.send() message = SUCCESS_RESPONSES["edit_success"].format( "Stock count template") return EditStockCountTemplate(stock_template=stock_tempalate, success=message)
def _send_single_email(email, detail): context = {'detail': detail} mail = SendMail( to_email=[email], subject='Supplier Order Form Email', template='email_alerts/orders/supplier_order_email.html', context=context) mail.send()
def mutate(self, info, email, password, mobile_number): mobile_number = mobile_number.replace(" ", "") validate_fields = ValidateUser().validate_user_fields( email, password, mobile_number) try: user = User.objects.create_user(**validate_fields) role = get_model_object(Role, 'name', 'Master Admin') user.set_password(password) user.role = role user.save() # account verification token = account_activation_token.make_token(user) uid = urlsafe_base64_encode(force_bytes(user.pk)) to_email = [user.email] email_verify_template = \ 'email_alerts/authentication/verification_email.html' subject = 'Account Verification' context = { 'template_type': 'Verify your email', 'small_text_detail': 'Thank you for ' 'creating a HealthID account. ' 'Please verify your email ' 'address to set up your account.', 'email': user.email, 'domain': DOMAIN, 'uid': uid, 'token': token, } send_mail = SendMail(email_verify_template, context, subject, to_email) send_mail.send() registration_message =\ AUTH_SUCCESS_RESPONSES["registration_success"] verification_message = AUTH_SUCCESS_RESPONSES["email_verification"] success = [registration_message + verification_message] verification_link = f"{DOMAIN}/healthid/activate/{uid}/{token}" return RegisterUser(success=success, user=user, verification_link=verification_link) except Exception as e: errors = ["Something went wrong: {}".format(e)] return RegisterUser(errors=errors)
def mutate(self, info, **kwargs): template_instance = StockCountTemplate() template_fields = stock_counts.get_template_fields(**kwargs) with SaveContextManager(template_instance) as stock_tempalate: stock_tempalate = stock_counts.add_fields_to_template( template_instance, **template_fields) event = Event( start_date=template_fields['start_date'], end_date=template_fields['end_date'], event_title=f'Stock Count Reminder', description=f'Stock reminder count on auto scheduling', event_type=template_fields['event_type']) event.save() stock_tempalate.schedule_time = event stock_tempalate.created_by = info.context.user stock_tempalate.unique = True try: stock_tempalate.save() except IntegrityError: raise IntegrityError( STOCK_ERROR_RESPONSES['template_integrity_error']) to_email = stock_tempalate.assigned_users.values_list('email', flat=True) email_stock_template = 'email_alerts/stocks/stock_email.html' subject = 'Stock counts alert' context = { 'schedule_time': datetime.strftime(stock_tempalate.schedule_time.start_date, "%d-%b-%Y"), 'by': str(info.context.user.email), 'template_type': 'Stock counts', 'small_text_detail': 'Hello, you have been assigned to carry' ' out stock counts' } send_mail = SendMail(email_stock_template, context, subject, to_email) send_mail.send() if stock_tempalate.interval: message = SUCCESS_RESPONSES["with_interval"].format( "Template with interval {} days".format( stock_tempalate.interval)) else: message = SUCCESS_RESPONSES["creation_success"].format( "Stock count template") return CreateStockCountTemplate(stock_template=stock_tempalate, success=message)
def generate_stock_counts_notifications(): '''Method to generate notifications ''' today = datetime.now() count_date = today + timedelta(days=1) stock_templates = StockCountTemplate.objects.filter( schedule_time__start_date__range=(today, count_date)) if stock_templates: for template in stock_templates: assigned_users = template.assigned_users.all() to_email = assigned_users.values_list('email', flat=True) email_stock_template = 'email_alerts/stocks/stock_email.html' subject = 'Stock counts alert' date = datetime.strftime(template.schedule_time.start_date, "%d-%b-%Y") messg = f'Reminder for the scheduled stock count on {date}' context = { 'schedule_time': date, 'template_type': 'Stock counts notification', 'small_text_detail': messg } send_mail = SendMail(email_stock_template, context, subject, to_email) thread = threading.Thread(target=notify_users( send_mail, assigned_users, messg), daemon=True) thread.start()
def schedule_templates(template): """ Creates a new record based on the template passed as an argument Arguments: template {instance} -- an instance of the stock template """ new_start_date = template.schedule_time.start_date + \ timedelta(days=template.interval) new_end_date = template.schedule_time.end_date + \ timedelta(days=template.interval) template_instance = StockCountTemplate() event_type = EventType.objects.get(id=template.schedule_time.event_type.id) with SaveContextManager(template_instance) as stock_template: stock_template.outlet = template.outlet stock_template.end_on = template.end_on stock_template.interval = template.interval stock_template.created_by = template.created_by stock_template.scheduled = template.scheduled event = Event(start_date=new_start_date, end_date=new_end_date, event_title=f'Stock Count Reminder', description=f'Stock reminder count on auto scheduling', event_type=event_type) event.save() stock_template.schedule_time = event stock_template.save() stock_template.products.set(template.get_products()) stock_template.assigned_users.set(template.get_assigned_users()) stock_template.designated_users.set(template.get_designated_users()) to_email = stock_template.assigned_users.values_list('email', flat=True) email_stock_template = 'email_alerts/stocks/stock_email.html' subject = 'Stock counts alert' context = { 'schedule_time': datetime.strftime(stock_template.schedule_time.start_date, "%d-%b-%Y"), 'by': str(template.created_by.email), 'template_type': 'Stock counts', 'small_text_detail': 'Hello, you have been assigned to carry' ' out stock counts' } send_mail = SendMail(email_stock_template, context, subject, to_email) send_mail.send()
def mutate(self, info, **kwargs): stock_tempalate = get_model_object( StockCountTemplate, 'id', kwargs.get('template_id')) to_email = stock_tempalate.assigned_users.values_list( 'email', flat=True) email_stock_template = 'email_alerts/stocks/stock_email.html' subject = 'Stock counts template canceled' context = { 'schedule_time': datetime.strftime( stock_tempalate.schedule_time.start_date, "%d-%b-%Y"), 'by': str(info.context.user.email), 'template_type': 'Stock counts', 'small_text_detail': 'Hello, stock counts has been canceled' } send_mail = SendMail(email_stock_template, context, subject, to_email) stock_tempalate.hard_delete() send_mail.send() message = SUCCESS_RESPONSES[ "deletion_success"].format("Stock template") return DeleteStockCountTemplate(success=message)
def send_manager_email(products, managers_email): """ send manager an email copy Args: products (list): Outlet's product. managers_email (str): Email belonging to an outlet's manager Returns: None """ mail_context = { 'products': products, } mail = SendMail( to_email=[managers_email], subject='Inventory Notification', template='email_alerts/reorder_email.html', context=mail_context ) mail.send()
def mutate(self, info, email): if email.strip() == "": blank_email = AUTH_ERROR_RESPONSES["password_reset_blank_email"] raise GraphQLError(blank_email) user = User.objects.filter(email=email).first() if user is None: invalid_email = AUTH_ERROR_RESPONSES["password_reset_blank_email"] raise GraphQLError(invalid_email) token = account_activation_token.make_token(user) uid = urlsafe_base64_encode(force_bytes(user.pk)) user_firstname = user.first_name if not user_firstname: user_firstname = "User" # Send email to the user to_email = [user.email] email_verify_template = \ 'email_alerts/authentication/password_reset_email.html' subject = 'Account Verification' context = { 'template_type': 'Password reset requested ' 'for your HealthID Account.', 'small_text_detail': '', 'name': user_firstname, 'email': email, 'domain': DOMAIN, 'uid': uid, 'token': token, } send_mail = SendMail(email_verify_template, context, subject, to_email) send_mail.send() reset_link = "{}/healthid/password_reset/{}/{}".format( DOMAIN, uid, token) success = AUTH_SUCCESS_RESPONSES["password_reset_link_success"] return ResetPassword(reset_link=reset_link, success=success)
def mail_receipt(receipt_id): """ Sends a digital copy of a sales receipt to the customer Arguments: receipt_id: ID of receipt to send Returns: None """ receipt = get_model_object(Receipt, 'id', receipt_id) sale_details = SaleDetail.objects.filter(sale_id=receipt.sale.id) receipt_product_info = [{ 'product_name': get_model_object(Product, 'id', sold_item_detail.product_id).product_name, 'quantity_bought': sold_item_detail.quantity, 'price': sold_item_detail.price, 'discount': sold_item_detail.discount } for sold_item_detail in sale_details] if not receipt.sale.customer: raise AttributeError(RECEIPT_ERROR_RESPONSES['mailer_no_customer']) if not receipt.sale.customer.email: raise AttributeError(RECEIPT_ERROR_RESPONSES['mailer_no_email']) mail_context = { 'receipt': receipt, 'receipt_product_info': receipt_product_info, 'sale': receipt.sale } mail = SendMail( to_email=[receipt.sale.customer.email], subject=f"Purchase receipt from {receipt.sale.outlet.name}", template='email_alerts/receipts/sale_receipt.html', context=mail_context) mail.send()