def process_run_request( self, operation_type: str, request: http.HttpRequest, action: models.Action, prev_url: str, ) -> http.HttpResponse: """Process a request (GET or POST).""" payload = SessionPayload(request.session, initial_values={ 'action_id': action.id, 'operation_type': operation_type, 'prev_url': prev_url, 'post_url': reverse('action:run_done') }) form = self.run_form_class( request.POST or None, columns=action.workflow.columns.filter(is_key=True), action=action, form_info=payload) if request.method == 'POST' and form.is_valid(): return self.process_run_post(request, action, payload) # Render the form return render( request, self.run_template, { 'action': action, 'num_msgs': action.get_rows_selected(), 'form': form, 'valuerange': range(2) })
def _send_confirmation_message( user, action: models.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(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(ontask_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 context = { 'num_messages': nmsgs, 'email_sent_datetime': str(now), 'filter_present': cfilter is not None, 'num_rows': action.workflow.nrows, 'subject': str(ontask_settings.NOTIFICATION_SUBJECT), 'body': text_content, 'from_email': str(ontask_settings.NOTIFICATION_SENDER), 'to_email': [user.email] } action.log(user, models.Log.ACTION_EMAIL_NOTIFY, **context) # Send email out try: send_mail(str(ontask_settings.NOTIFICATION_SUBJECT), text_content, str(ontask_settings.NOTIFICATION_SENDER), [user.email], html_message=html_content) except Exception as exc: raise Exception( _('Error when sending the notification: {0}').format(str(exc)), )
def run_json_list_action( req: HttpRequest, workflow: Workflow, action: Action, ) -> HttpResponse: """Request data to send JSON objects. Form asking for token and export wf :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 = JSONListPayload({'action_id': action.id}) # Create the form to ask for the email subject and other information form = JSONListActionForm(req.POST or None, form_info=action_info) if req.method == 'POST' and form.is_valid(): # Log the event log_item = Log.objects.register( req.user, Log.SCHEDULE_JSON_EXECUTE, action.workflow, { 'action': action.name, 'action_id': action.id, 'exported_workflow': action_info['export_wf'], 'status': 'Preparing to execute', 'target_url': action.target_url }) # Update the last_execution_log action.last_executed_log = log_item action.save() # Send the objects run_task.delay(req.user.id, log_item.id, action_info.get_store()) # Reset object to carry action info throughout dialogs set_action_payload(req.session) req.session.save() # Successful processing. return render(req, 'action/action_done.html', { 'log_id': log_item.id, 'download': action_info['export_wf'] }) # Render the form return render( req, 'action/request_json_list_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 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, form_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: WSGIRequest, 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:canvas_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, form_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()) continue_url = 'action:item_filter' else: continue_url = 'action:canvas_email_done' # Check for the CANVAS token and proceed to the continue_url return canvas_get_or_set_oauth_token(req, action_info['target_url'], continue_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() })