def run_email_action( req: HttpRequest, workflow: Workflow, action: Action, ) -> HttpResponse: """Request data to send emails. Form asking for subject line, email column, etc. :param req: HTTP request (GET) :param workflow: workflow being processed :param action: Action being run :return: HTTP response """ # Get the payload from the session, and if not, use the given one action_info = get_or_set_action_info(req.session, EmailPayload, initial_values={ 'action_id': action.id, 'prev_url': reverse('action:run', kwargs={'pk': action.id}), 'post_url': reverse('action:email_done') }) # Create the form to ask for the email subject and other information form = EmailActionForm(req.POST or None, column_names=[ col.name for col in workflow.columns.filter(is_key=True) ], action=action, action_info=action_info) # Request is a POST and is valid if req.method == 'POST' and form.is_valid(): if action_info['confirm_items']: # Add information to the session object to execute the next pages action_info['button_label'] = ugettext('Send') action_info['valuerange'] = 2 action_info['step'] = 2 set_action_payload(req.session, action_info.get_store()) return redirect('action:item_filter') # Go straight to the final step. return run_email_done(req, action_info=action_info, workflow=workflow) # Render the form return render( req, 'action/request_email_data.html', { 'action': action, 'num_msgs': action.get_rows_selected(), 'form': form, 'valuerange': range(2) })
def run_canvas_email_action( req: HttpRequest, workflow: Workflow, action: Action, ) -> HttpResponse: """Request data to send JSON objects. Form asking for subject, item column (contains ids to select unique users), confirm items (add extra step to drop items), export workflow and target_rul (if needed). :param req: HTTP request (GET) :param workflow: workflow being processed :param action: Action begin run :return: HTTP response """ # Get the payload from the session, and if not, use the given one action_info = get_or_set_action_info(req.session, CanvasEmailPayload, initial_values={ 'action_id': action.id, 'prev_url': reverse('action:run', kwargs={'pk': action.id}), 'post_url': reverse('action:email_done') }) # Create the form to ask for the email subject and other information form = CanvasEmailActionForm( req.POST or None, column_names=[ col.name for col in workflow.columns.filter(is_key=True) ], action=action, action_info=action_info) if req.method == 'POST' and form.is_valid(): # Request is a POST and is valid if action_info['confirm_items']: # Create a dictionary in the session to carry over all the # information to execute the next pages action_info['button_label'] = ugettext('Send') action_info['valuerange'] = 2 action_info['step'] = 2 set_action_payload(req.session, action_info.get_store()) return redirect('action:item_filter') # Go straight to the token request step return canvas_get_or_set_oauth_token(req, action_info['target_url']) # Render the form return render( req, 'action/request_canvas_email_data.html', { 'action': action, 'num_msgs': action.get_rows_selected(), 'form': form, 'valuerange': range(2), 'rows_all_false': action.get_row_all_false_count() })
def send_confirmation_message( user, action: Action, nmsgs: int, ) -> None: """Send the confirmation message. :param user: Destination email :param action: Action being considered :param nmsgs: Number of messages being sent :return: """ # Creating the context for the confirmation email now = datetime.datetime.now(pytz.timezone(ontask_settings.TIME_ZONE)) cfilter = action.get_filter() context = { 'user': user, 'action': action, 'num_messages': nmsgs, 'email_sent_datetime': now, 'filter_present': cfilter is not None, 'num_rows': action.workflow.nrows, 'num_selected': action.get_rows_selected(), } # Create template and render with context try: html_content = Template( str(getattr(settings, 'NOTIFICATION_TEMPLATE')), ).render(Context(context)) text_content = strip_tags(html_content) except TemplateSyntaxError as exc: raise Exception( _('Syntax error in notification template ({0})').format( str(exc)), ) # Log the event Log.objects.register( user, Log.ACTION_EMAIL_NOTIFY, action.workflow, { 'user': user.id, 'action': action.id, 'num_messages': nmsgs, 'email_sent_datetime': str(now), 'filter_present': cfilter is not None, 'num_rows': action.workflow.nrows, 'subject': str(getattr(settings, 'NOTIFICATION_SUBJECT')), 'body': text_content, 'from_email': str(getattr(settings, 'NOTIFICATION_SENDER')), 'to_email': [user.email] }, ) # Send email out try: send_mail(str(getattr(settings, 'NOTIFICATION_SUBJECT')), text_content, str(getattr(settings, 'NOTIFICATION_SENDER')), [user.email], html_message=html_content) except Exception as exc: raise Exception( _('Error when sending the notification: {0}').format(str(exc)), )