def mail_time_summary(): users = [] for item in InvoiceUser.objects.all(): if item.mail_time_summary and item.user.email: users.append(item.user) for user in users: logger.info('mail_time_summary: {}'.format(user.username)) report = time_summary(user, days=1) message = '' for d, summary in report.items(): message = message + '\n\n{}, total time {}'.format( d.strftime('%d/%m/%Y %A'), summary['format_total'], ) for ticket in summary['tickets']: message = message + '\n{}: {}, {} ({})'.format( ticket['pk'], ticket['contact'], ticket['description'], ticket['format_minutes'], ) queue_mail_message( user, [user.email], 'Time Summary for {}'.format(timezone.now().strftime('%d/%m/%Y')), message, ) if users: process_mail.delay()
def pay_later_view(request, pk): payment = Payment.objects.get(pk=pk) _check_perm(request, payment) payment.check_can_pay payment.set_pay_later() queue_mail_template( payment, payment.mail_template_name, payment.mail_template_context(), ) _send_notification_email(payment, request) process_mail.delay() return HttpResponseRedirect(payment.url)
def form_valid(self, form): selection = form.cleaned_data['send_email'] use_template = selection == 'template' with transaction.atomic(): result = super().form_valid(form) if use_template: context = { self.object.email: { "SUBJECT": "Re: " + self.object.subject, "BODY": self.object.description, "DATE": self.object.created.strftime("%d-%b-%Y %H:%M:%S"), }, } queue_mail_template( self.object, 'enquiry_acknowledgement', context, ) else: attachments = [] if self.object.document: attachments.append(self.object.document.file.name) queue_mail_message( self.object, [self.object.email], self.object.subject, self.object.description, attachments=attachments, ) transaction.on_commit(lambda: process_mail.delay()) return result
def save(self, commit=True): instance = super(EnquiryForm, self).save(commit) if commit: email_addresses = [n.email for n in Notify.objects.all()] if email_addresses: queue_mail_message( instance, email_addresses, 'Enquiry from {}'.format(instance.name), self._email_message(instance), ) process_mail.delay() else: logging.error( "Enquiry app cannot send email notifications. " "No email addresses set-up in 'mail.models.Notify'" ) return instance
def form_valid(self, form): """Process the payment. Note: We do NOT update 'self.object' """ self.object = form.save(commit=False) token = form.cleaned_data['token'] slug = form.cleaned_data['action'] action = CheckoutAction.objects.get(slug=slug) checkout = None try: with transaction.atomic(): checkout = Checkout.objects.create_checkout( action, self.object, self.request.user ) # do stripe stuff outside of a transaction so we have some chance # of diagnosing progress if an exception is thrown. if not action.invoice: if not token: raise CheckoutError( "No checkout 'token' for: '{}'".format(self.object) ) self._form_valid_stripe(checkout, token) with transaction.atomic(): if action.invoice: self._form_valid_invoice(checkout, form) checkout.success() checkout.notify(self.request) #self.object.checkout_mail(action) url = self.object.checkout_success_url(checkout.pk) process_mail.delay() except CheckoutError as e: logger.error(e) if checkout: with transaction.atomic(): checkout.fail() url = self.object.checkout_fail_url(checkout.pk) # PJK TODO remove temp # raise return HttpResponseRedirect(url)
def form_valid(self, form): """Process the payment. Note: We do NOT update 'self.object' """ self.object = form.save(commit=False) token = form.cleaned_data['token'] slug = form.cleaned_data['action'] action = CheckoutAction.objects.get(slug=slug) checkout = None try: with transaction.atomic(): checkout = Checkout.objects.create_checkout( action, self.object, self.request.user) # do stripe stuff outside of a transaction so we have some chance # of diagnosing progress if an exception is thrown. if not action.invoice: if not token: raise CheckoutError("No checkout 'token' for: '{}'".format( self.object)) self._form_valid_stripe(checkout, token) with transaction.atomic(): if action.invoice: self._form_valid_invoice(checkout, form) checkout.success() checkout.notify(self.request) #self.object.checkout_mail(action) url = self.object.checkout_success_url(checkout.pk) process_mail.delay() except CheckoutError as e: logger.error(e) if checkout: with transaction.atomic(): checkout.fail() url = self.object.checkout_fail_url(checkout.pk) # PJK TODO remove temp # raise return HttpResponseRedirect(url)
def form_valid(self, form): self.object = form.save(commit=False) # Create the charge on Stripe's servers - this will charge the user's card token = form.cleaned_data['stripeToken'] self.object.save_token(token) # Set your secret key: remember to change this to your live secret key # in production. See your keys here https://manage.stripe.com/account stripe.api_key = settings.STRIPE_SECRET_KEY try: customer_id = self._init_stripe_customer( self.object.name, self.object.email, token ) stripe.Charge.create( amount=self.object.total_as_pennies(), # amount in pennies, again currency=CURRENCY, customer=customer_id, description=', '.join(self.object.description), ) with transaction.atomic(): self.object.set_paid() queue_mail_template( self.object, self.object.mail_template_name, self.object.mail_template_context(), ) _send_notification_email(self.object, self.request) process_mail.delay() result = super(StripeFormViewMixin, self).form_valid(form) except stripe.CardError as e: self.object.set_payment_failed() self._log_card_error(e, self.object.pk) result = HttpResponseRedirect(self.object.url_failure) except stripe.StripeError as e: self.object.set_payment_failed() self._log_stripe_error(e, 'payment: {}'.format(self.object.pk)) result = HttpResponseRedirect(self.object.url_failure) return result
def save(self, **kwargs): result = super().save(**kwargs) email = self.cleaned_data["email"] body = ['email address: {}'.format(email)] active_users = get_user_model().objects.filter(email__iexact=email) if active_users: for user in active_users: body.append(" User name: {}".format(user.username)) if user.is_active and user.has_usable_password(): body.append( " - Password reset email sent. " "(user is active and has a usable password)." ) else: body.append( " - Password reset email has NOT been sent. " ) if not user.is_active: body.append(" - (user is NOT active).") if not user.has_usable_password(): body.append( " - (user does NOT have a usable password)." ) else: body.append( "There are no users on the system with this email address." ) email_addresses = [n.email for n in Notify.objects.all()] if email_addresses: queue_mail_message( PasswordResetAudit.objects.audit(email), email_addresses, "Password reset request from {}".format(email), '\n'.join(body), ) transaction.on_commit(lambda: process_mail.delay()) else: logging.error( "Cannot send email notification of password request. " "No email addresses set-up in 'mail.models.Notify'" ) return result