Пример #1
0
    def handle(self, *args, **options):  # pylint:disable=too-many-branches
        username = options[self.UserModel.USERNAME_FIELD].strip()
        password = options['password'].strip()
        email = options['email'].strip()
        force = to_bool(options['force'])
        is_superuser = to_bool(options['is_superuser'])

        try:
            username = self.username_field.clean(username, None)
        except exceptions.ValidationError as e:
            raise CommandError('; '.join(e.messages))

        try:
            self.email_field.clean(email, None)
        except exceptions.ValidationError as e:
            raise CommandError('; '.join(e.messages))

        try:
            self.UserModel.objects.get_by_natural_key(username)
        except self.UserModel.DoesNotExist:
            pass
        else:
            _logger.info("Info: Username %s is already taken. Will not recreate user.", username)
            return

        try:
            self.UserModel.objects.get(email=email)
        except self.UserModel.DoesNotExist:
            pass
        except exceptions.MultipleObjectsReturned:
            raise CommandError("Error: That %s is already taken." % email)
        else:
            raise CommandError("Error: That %s is already taken." % email)

        if not username:
            raise CommandError("Error: Blank username aren't allowed.")

        if not password:
            raise CommandError("Error: Blank passwords aren't allowed.")

        if not email:
            raise CommandError("Error: Blank email aren't allowed.")

        user_data = {
            self.UserModel.USERNAME_FIELD: username,
            'email': email,
        }

        self.validate_password(password=password, user_data=user_data, force=force)
        user_data['password'] = password

        if is_superuser:
            self.UserModel.objects.create_superuser(**user_data)
        else:
            self.UserModel.objects.create_user(**user_data)

        if options['verbosity'] >= 1:
            self.stdout.write("{} created successfully.".format(
                'Superuser' if is_superuser else 'User'))
Пример #2
0
 def filter_queryset(self, queryset):
     independent = to_bool(self.request.query_params.get(
         'independent', None),
                           handle_none=True,
                           exception=ValidationError)
     group_id = self.request.query_params.get('group', None)
     if independent and group_id:
         raise ValidationError(
             'You cannot filter for independent experiments and '
             'group experiments at the same time.')
     queryset = queryset.filter(project=self.project)
     if independent:
         queryset = queryset.filter(experiment_group__isnull=True)
     if group_id:
         group = self.get_group(project=self.project, group_id=group_id)
         if group.is_study:
             queryset = queryset.filter(experiment_group=group)
         elif group.is_selection:
             queryset = group.selection_experiments.all()
         else:
             raise ValidationError('Invalid group.')
     auditor.record(event_type=PROJECT_EXPERIMENTS_VIEWED,
                    instance=self.project,
                    actor_id=self.request.user.id,
                    actor_name=self.request.user.username)
     return super().filter_queryset(queryset=queryset)
Пример #3
0
    def post(self, request, *args, **kwargs):
        ttl = self.request.data.get(RedisTTL.TTL_KEY)
        if ttl:
            try:
                ttl = RedisTTL.validate_ttl(ttl)
            except ValueError:
                raise ValidationError('ttl must be an integer.')

        auditor.record(event_type=self.event_type,
                       instance=self.job,
                       actor_id=self.request.user.id,
                       actor_name=self.request.user.username)

        description = None
        config = None
        update_code_reference = False
        if 'config' in request.data:
            spec = validate_job_spec_config(
                [self.job.specification.parsed_data, request.data['config']], raise_for_rest=True)
            config = spec.parsed_data
        if 'update_code' in request.data:
            try:
                update_code_reference = to_bool(request.data['update_code'])
            except TypeError:
                raise ValidationError('update_code should be a boolean')
        if 'description' in request.data:
            description = request.data['description']
        new_obj = self.clone(obj=self.job,
                             config=config,
                             update_code_reference=update_code_reference,
                             description=description)
        if ttl:
            RedisTTL.set_for_job(job_id=new_obj.id, value=ttl)
        serializer = self.get_serializer(new_obj)
        return Response(status=status.HTTP_201_CREATED, data=serializer.data)
Пример #4
0
 def post(self, request, *args, **kwargs):
     obj = self.get_object()
     pending = request.data.get('pending')
     pending = to_bool(pending) if pending is not None else False
     auditor.record(event_type=EXPERIMENT_GROUP_STOPPED_TRIGGERED,
                    instance=obj,
                    actor_id=request.user.id,
                    actor_name=request.user.username,
                    pending=pending)
     if pending:
         celery_app.send_task(
             SchedulerCeleryTasks.EXPERIMENTS_GROUP_STOP_EXPERIMENTS,
             kwargs={
                 'experiment_group_id': obj.id,
                 'pending': pending,
                 'message': 'User stopped pending experiments'
             },
             countdown=conf.get('GLOBAL_COUNTDOWN'))
     else:
         celery_app.send_task(SchedulerCeleryTasks.EXPERIMENTS_GROUP_STOP,
                              kwargs={
                                  'experiment_group_id': obj.id,
                                  'message': 'User stopped experiment group'
                              },
                              countdown=conf.get('GLOBAL_COUNTDOWN'))
     return Response(status=status.HTTP_200_OK)
Пример #5
0
    def handle(self, *args, **options):
        log_sleep_interval = options['log_sleep_interval']
        persist = to_bool(options['persist'])
        node = self.get_node_or_wait(log_sleep_interval)
        self.stdout.write("Started a new resources monitor with, "
                          "log sleep interval: `{}` and persist: `{}`".format(
                              log_sleep_interval, persist),
                          ending='\n')
        containers = {}
        while True:
            try:
                if node:
                    monitor.run(containers, node, persist)
            except redis.exceptions.ConnectionError as e:
                monitor.logger.warning(
                    "Redis connection is probably already closed %s\n", e)
            except Exception as e:
                monitor.logger.exception("Unhandled exception occurred %s\n",
                                         e)

            time.sleep(log_sleep_interval)
            try:
                if node:
                    node.refresh_from_db()
                else:
                    node = self.get_node()
            except (InterfaceError, ProgrammingError, OperationalError) as e:
                monitor.logger.exception(
                    "Database connection is probably already closed %s\n", e)
                return
Пример #6
0
 def post(self, request, *args, **kwargs):
     if self.project.has_notebook:
         commit = request.data.get('commit')
         commit = to_bool(commit) if commit is not None else True
         try:
             if commit:
                 # Commit changes
                 git.commit(self.project.repo.path, request.user.email, request.user.username)
             else:
                 # Reset changes
                 git.undo(self.project.repo.path)
         except FileNotFoundError:
             # Git probably was not found
             pass
         celery_app.send_task(
             SchedulerCeleryTasks.PROJECTS_NOTEBOOK_STOP,
             kwargs={
                 'project_name': self.project.unique_name,
                 'project_uuid': self.project.uuid.hex,
                 'notebook_job_name': self.project.notebook.unique_name,
                 'notebook_job_uuid': self.project.notebook.uuid.hex,
                 'update_status': True
             })
         auditor.record(event_type=NOTEBOOK_STOPPED_TRIGGERED,
                        instance=self.project.notebook,
                        target='project',
                        actor_id=self.request.user.id,
                        actor_name=self.request.user.username,
                        countdown=1)
     elif self.project.notebook and self.project.notebook.is_running:
         self.project.notebook.set_status(status=ExperimentLifeCycle.STOPPED,
                                          message='Notebook was stopped')
     return Response(status=status.HTTP_200_OK)
Пример #7
0
    def get_serializer_class(self):
        if self.create_serializer_class and self.request.method.lower() == 'post':
            return self.create_serializer_class

        metrics_only = to_bool(self.request.query_params.get('metrics', None),
                               handle_none=True,
                               exception=ValidationError)
        if metrics_only:
            return self.metrics_serializer_class

        declarations_only = to_bool(self.request.query_params.get('declarations', None),
                                    handle_none=True,
                                    exception=ValidationError)
        if declarations_only:
            return self.declarations_serializer_class

        return self.serializer_class
Пример #8
0
 def handle(self, *args, **options):
     sleep_interval = options['sleep_interval']
     persist = to_bool(options['persist'])
     node = self.get_node_or_wait(sleep_interval)
     self.stdout.write("Started a new resources monitor with, "
                       "log sleep interval: `{}` and persist: `{}`".format(
                           sleep_interval, persist),
                       ending='\n')
     self.discover(node=node, sleep_interval=sleep_interval)
Пример #9
0
 def handle_code(self, request):
     commit = request.data.get('commit')
     commit = to_bool(commit) if commit is not None else True
     if commit and conf.get(NOTEBOOKS_MOUNT_CODE) and self.project.has_repo:
         # Commit changes
         git.commit(self.project.repo.path, request.user.email,
                    request.user.username)
     else:
         # Reset changes
         git.undo(self.project.repo.path)
Пример #10
0
    def post(self, request, *args, **kwargs):
        ttl = self.request.data.get(RedisTTL.TTL_KEY)
        if ttl:
            try:
                ttl = RedisTTL.validate_ttl(ttl)
            except ValueError:
                raise ValidationError('ttl must be an integer.')

        obj = self.get_object()
        auditor.record(event_type=self.event_type,
                       instance=obj,
                       actor_id=self.request.user.id,
                       actor_name=self.request.user.username)

        description = None
        config = None
        declarations = None
        update_code_reference = False
        if 'config' in request.data:
            spec = validate_experiment_spec_config(
                [obj.specification.parsed_data, request.data['config']],
                raise_for_rest=True)
            config = spec.parsed_data
            declarations = spec.declarations
        if 'update_code' in request.data:
            update_code_reference = to_bool(request.data['update_code'],
                                            handle_none=True,
                                            exception=ValidationError)
        if 'description' in request.data:
            description = request.data['description']
        new_obj = self.clone(obj=obj,
                             config=config,
                             declarations=declarations,
                             update_code_reference=update_code_reference,
                             description=description)
        if ttl:
            RedisTTL.set_for_experiment(experiment_id=new_obj.id, value=ttl)
        serializer = self.get_serializer(new_obj)
        return Response(status=status.HTTP_201_CREATED, data=serializer.data)
Пример #11
0
def _indepenent_condition(queryset, params, negation):
    params = to_list(params)
    if len(params) == 1 and to_bool(params[0]) is True:
        return queryset.filter(experiment_group__isnull=True)
    return queryset
Пример #12
0
def _indepenent_condition(queryset: Any, params: Union[str, Iterable], negation: bool) -> Any:
    params = to_list(params)
    if len(params) == 1 and to_bool(params[0]) is True:
        return queryset.filter(experiment_group__isnull=True)
    return queryset