Ejemplo n.º 1
0
    def _create_token(self, task):
        token = create_token(task)
        try:
            class_conf = settings.TASK_SETTINGS.get(
                self.task_type, settings.DEFAULT_TASK_SETTINGS)

            # will throw a key error if the token template has not
            # been specified
            email_conf = class_conf['emails']['token']
            send_stage_email(task, email_conf, token)
            return {'notes': ['created token']}, 200
        except KeyError as e:
            import traceback
            trace = traceback.format_exc()
            self.logger.critical(
                ("(%s) - Exception escaped! %s\nTrace: \n%s") %
                (timezone.now(), e, trace))
            notes = {
                'errors': [("Error: '%s' while sending " + "token. See task " +
                            "itself for details.") % e]
            }
            create_notification(task, notes, error=True)

            response_dict = {
                'errors': [
                    "Error: Something went wrong on the " +
                    "server. It will be looked into shortly."
                ]
            }
            return response_dict, 500
Ejemplo n.º 2
0
    def post(self, request, format=None):
        """
        Reissue a token for an approved task.

        Clears other tokens for it.
        """
        uuid = request.data.get('task', None)
        if uuid is None:
            return Response(
                {'errors': {
                    'task': [
                        "This field is required.",
                    ]
                }},
                status=400)
        try:
            if 'admin' in request.keystone_user['roles']:
                task = Task.objects.get(uuid=uuid)
            else:
                task = Task.objects.get(
                    uuid=uuid, project_id=request.keystone_user['project_id'])
        except Task.DoesNotExist:
            return Response({'errors': ['No task with this id.']}, status=404)

        if task.completed:
            return Response(
                {'errors': ['This task has already been completed.']},
                status=400)

        if task.cancelled:
            return Response({'errors': ['This task has been cancelled.']},
                            status=400)

        if not task.approved:
            return Response({'errors': ['This task has not been approved.']},
                            status=400)

        for token in task.tokens:
            token.delete()

        token = create_token(task)
        try:
            class_conf = settings.TASK_SETTINGS.get(
                task.task_type, settings.DEFAULT_TASK_SETTINGS)

            # will throw a key error if the token template has not
            # been specified
            email_conf = class_conf['emails']['token']
            send_stage_email(task, email_conf, token)
        except KeyError as e:
            return self._handle_task_error(e,
                                           task,
                                           "while sending token",
                                           return_response=True)
        return Response({'notes': ['Token reissued.']}, status=200)
Ejemplo n.º 3
0
    def post(self, request, format=None):
        """
        Reissue a token for an approved task.

        Clears other tokens for it.
        """
        uuid = request.data.get('task', None)
        if uuid is None:
            return Response({'task': [
                "This field is required.",
            ]},
                            status=400)
        try:
            if 'admin' in request.keystone_user['roles']:
                task = Task.objects.get(uuid=uuid)
            else:
                task = Task.objects.get(
                    uuid=uuid, project_id=request.keystone_user['project_id'])
        except Task.DoesNotExist:
            return Response({'errors': ['No task with this id.']}, status=404)

        if task.completed:
            return Response(
                {'errors': ['This task has already been completed.']},
                status=400)

        if task.cancelled:
            return Response({'errors': ['This task has been cancelled.']},
                            status=400)

        if not task.approved:
            return Response({'errors': ['This task has not been approved.']},
                            status=400)

        for token in task.tokens:
            token.delete()

        token = create_token(task)
        try:
            class_conf = settings.TASK_SETTINGS.get(
                task.task_type, settings.DEFAULT_TASK_SETTINGS)

            # will throw a key error if the token template has not
            # been specified
            email_conf = class_conf['emails']['token']
            send_stage_email(task, email_conf, token)
        except KeyError as e:
            notes = {
                'errors': [("Error: '%(error)s' while sending token. " +
                            "See registration itself for details.") % {
                                'error': e
                            }],
                'task':
                task.uuid
            }
            create_notification(task, notes)

            import traceback
            trace = traceback.format_exc()
            self.logger.critical(
                ("(%s) - Exception escaped!" + " %s\n Trace: \n%s") %
                (timezone.now(), e, trace))

            response_dict = {
                'errors': [
                    "Error: Something went wrong on the " +
                    "server. It will be looked into shortly."
                ]
            }
            return Response(response_dict, status=500)
        return Response({'notes': ['Token reissued.']}, status=200)
Ejemplo n.º 4
0
    def post(self, request, uuid, format=None):
        """
        Will approve the Task specified,
        followed by running the post_approve actions
        and if valid will setup and create a related token.
        """
        try:
            task = Task.objects.get(uuid=uuid)
        except Task.DoesNotExist:
            return Response({'errors': ['No task with this id.']}, status=404)

        try:
            if request.data.get('approved') is not True:
                return Response(
                    {'approved': ["this is a required boolean field."]},
                    status=400)
        except ParseError:
            return Response(
                {'approved': ["this is a required boolean field."]},
                status=400)

        if task.completed:
            return Response(
                {'errors': ['This task has already been completed.']},
                status=400)

        if task.cancelled:
            return Response({'errors': ['This task has been cancelled.']},
                            status=400)

        # we check that the task is valid before approving it:
        valid = True
        for action in task.actions:
            if not action.valid:
                valid = False

        if not valid:
            return Response(
                {
                    'errors': [
                        'Cannot approve an invalid task. ' +
                        'Update data and rerun pre_approve.'
                    ]
                },
                status=400)

        # We approve the task before running actions,
        # that way if something goes wrong we know if it was approved,
        # when it was approved, and who approved it last. Subsequent
        # reapproval attempts overwrite previous approved_by/on.
        task.approved = True
        task.approved_by = request.keystone_user
        task.approved_on = timezone.now()
        task.save()

        need_token = False
        valid = True

        actions = []

        for action in task.actions:
            act_model = action.get_action()
            actions.append(act_model)
            try:
                act_model.post_approve()
            except Exception as e:
                notes = {
                    'errors': [("Error: '%s' while approving task. " +
                                "See task itself for details.") % e],
                    'task':
                    task.uuid
                }
                create_notification(task, notes)

                import traceback
                trace = traceback.format_exc()
                self.logger.critical(
                    ("(%s) - Exception escaped! %s\n" + "Trace: \n%s") %
                    (timezone.now(), e, trace))

                return Response(notes, status=500)

            if not action.valid:
                valid = False
            if action.need_token:
                need_token = True

        if valid:
            if need_token:
                token = create_token(task)
                try:
                    class_conf = settings.TASK_SETTINGS.get(
                        task.task_type, settings.DEFAULT_TASK_SETTINGS)

                    # will throw a key error if the token template has not
                    # been specified
                    email_conf = class_conf['emails']['token']
                    send_stage_email(task, email_conf, token)
                    return Response({'notes': ['created token']}, status=200)
                except KeyError as e:
                    notes = {
                        'errors':
                        [("Error: '%s' while sending " + "token. See task " +
                          "itself for details.") % e],
                        'task':
                        task.uuid
                    }
                    create_notification(task, notes)

                    import traceback
                    trace = traceback.format_exc()
                    self.logger.critical(
                        ("(%s) - Exception escaped!" + " %s\n Trace: \n%s") %
                        (timezone.now(), e, trace))

                    response_dict = {
                        'errors': [
                            "Error: Something went wrong on the " +
                            "server. It will be looked into shortly."
                        ]
                    }
                    return Response(response_dict, status=500)
            else:
                for action in actions:
                    try:
                        action.submit({})
                    except Exception as e:
                        notes = {
                            'errors':
                            [("Error: '%s' while submitting " +
                              "task. See task " + "itself for details.") % e],
                            'task':
                            task.uuid
                        }
                        create_notification(task, notes)

                        import traceback
                        trace = traceback.format_exc()
                        self.logger.critical(
                            ("(%s) - Exception escaped!" + " %s\n Trace: \n%s")
                            % (timezone.now(), e, trace))

                        return Response(notes, status=500)

                task.completed = True
                task.completed_on = timezone.now()
                task.save()

                # Sending confirmation email:
                class_conf = settings.TASK_SETTINGS.get(
                    task.task_type, settings.DEFAULT_TASK_SETTINGS)
                email_conf = class_conf.get('emails',
                                            {}).get('completed', None)
                send_stage_email(task, email_conf)

                return Response({'notes': ["Task completed successfully."]},
                                status=200)
        return Response({'errors': ['actions invalid']}, status=400)
Ejemplo n.º 5
0
    def post(self, request, uuid, format=None):
        """
        Will approve the Task specified,
        followed by running the post_approve actions
        and if valid will setup and create a related token.
        """
        try:
            task = Task.objects.get(uuid=uuid)
        except Task.DoesNotExist:
            return Response({'errors': ['No task with this id.']}, status=404)

        try:
            if request.data.get('approved') is not True:
                return Response(
                    {'approved': ["this is a required boolean field."]},
                    status=400)
        except ParseError:
            return Response(
                {'approved': ["this is a required boolean field."]},
                status=400)

        if task.completed:
            return Response(
                {'errors': ['This task has already been completed.']},
                status=400)

        if task.cancelled:
            return Response({'errors': ['This task has been cancelled.']},
                            status=400)

        # we check that the task is valid before approving it:
        valid = True
        for action in task.actions:
            if not action.valid:
                valid = False

        if not valid:
            return Response(
                {
                    'errors': [
                        'Cannot approve an invalid task. '
                        'Update data and rerun pre_approve.'
                    ]
                },
                status=400)

        if task.approved:
            # Expire previously in use tokens
            Token.objects.filter(task=task.uuid).delete()

        # We approve the task before running actions,
        # that way if something goes wrong we know if it was approved,
        # when it was approved, and who approved it last. Subsequent
        # reapproval attempts overwrite previous approved_by/on.
        task.approved = True
        task.approved_by = request.keystone_user
        task.approved_on = timezone.now()
        task.save()

        need_token = False
        valid = True

        actions = []

        for action in task.actions:
            act_model = action.get_action()
            actions.append(act_model)
            try:
                act_model.post_approve()
            except Exception as e:
                return self._handle_task_error(e,
                                               task,
                                               "while approving task",
                                               return_response=True)

            if not action.valid:
                valid = False
            if action.need_token:
                need_token = True

        if valid:
            if need_token:
                token = create_token(task)
                try:
                    class_conf = settings.TASK_SETTINGS.get(
                        task.task_type, settings.DEFAULT_TASK_SETTINGS)

                    # will throw a key error if the token template has not
                    # been specified
                    email_conf = class_conf['emails']['token']
                    send_stage_email(task, email_conf, token)
                    return Response({'notes': ['created token']}, status=200)
                except KeyError as e:
                    return self._handle_task_error(e,
                                                   task,
                                                   "while sending token",
                                                   return_response=True)
            else:
                for action in actions:
                    try:
                        action.submit({})
                    except Exception as e:
                        return self._handle_task_error(e,
                                                       task,
                                                       "while submitting task",
                                                       return_response=True)

                task.completed = True
                task.completed_on = timezone.now()
                task.save()

                # Sending confirmation email:
                class_conf = settings.TASK_SETTINGS.get(
                    task.task_type, settings.DEFAULT_TASK_SETTINGS)
                email_conf = class_conf.get('emails',
                                            {}).get('completed', None)
                send_stage_email(task, email_conf)

                return Response({'notes': ["Task completed successfully."]},
                                status=200)
        return Response({'errors': ['actions invalid']}, status=400)