class DefaultDecisionConfigurationSerializer(serializers.ModelSerializer): user = fields.HiddenField(default=serializers.CurrentUserDefault()) configuration = fields.CharField() def validate(self, attrs): # Get configuration conf_str = attrs.pop('configuration') try: configuration = GetConfiguration( user_conf=json.loads(conf_str)).for_json() except Exception as e: logger.exception(e) raise exceptions.ValidationError( {'configuration': _('The configuration has wrong format')}) # Save configuration file conf_db = file_get_or_create( json.dumps(configuration, indent=2, sort_keys=True, ensure_ascii=False), 'configuration.json', JobFile) attrs['file_id'] = conf_db.id return attrs def to_representation(self, instance): if isinstance(instance, self.Meta.model): return {'pk': instance.pk} return {} class Meta: model = DefaultDecisionConfiguration fields = ('user', 'configuration')
class CreateDecisionSerializer(serializers.ModelSerializer): operator = fields.HiddenField(default=serializers.CurrentUserDefault()) files = DecisionFilesField() configuration = fields.CharField(required=False) def validate(self, attrs): conf_data = validate_configuration(attrs.pop('configuration', None)) attrs.update(conf_data) return attrs def create(self, validated_data): assert 'job_id' in validated_data, 'Wrong serializer usage' job_files = validated_data.pop('files') instance = super().create(validated_data) FileSystem.objects.bulk_create(list(FileSystem(decision=instance, **fkwargs) for fkwargs in job_files)) decision_status_changed(instance) return instance def update(self, instance, validated_data): raise RuntimeError('Update method is not supported for this serializer') def to_representation(self, instance): if isinstance(instance, self.Meta.model): return {'url': reverse('jobs:decision', args=[instance.pk])} return super(CreateDecisionSerializer, self).to_representation(instance) class Meta: model = Decision fields = ('title', 'operator', 'files', 'configuration')
class RestartDecisionSerializer(serializers.ModelSerializer): operator = fields.HiddenField(default=serializers.CurrentUserDefault()) configuration = fields.CharField() def __clear_related_objects(self, instance): Report.objects.filter(decision=instance).delete() AttrFile.objects.filter(decision=instance).delete() AdditionalSources.objects.filter(decision=instance).delete() CompareDecisionsInfo.objects.filter( Q(decision1=instance) | Q(decision2=instance)).delete() DecisionCache.objects.filter(decision=instance).delete() Task.objects.filter(decision=instance).delete() def validate(self, attrs): conf_data = validate_configuration(attrs['operator'], attrs.pop('configuration')) attrs.update(conf_data) attrs['status'] = DECISION_STATUS[1][0] attrs['start_date'] = now() int_fields = ('tasks_total', 'tasks_pending', 'tasks_processing', 'tasks_finished', 'tasks_error', 'tasks_cancelled', 'solutions') null_fields = ('error', 'finish_date', 'total_sj', 'failed_sj', 'solved_sj', 'expected_time_sj', 'start_sj', 'finish_sj', 'gag_text_sj', 'total_ts', 'failed_ts', 'solved_ts', 'expected_time_ts', 'start_ts', 'finish_ts', 'gag_text_ts') for field_name in int_fields: attrs[field_name] = 0 for field_name in null_fields: attrs[field_name] = None return attrs def create(self, validated_data): raise NotImplementedError( 'Create method is not supported for this serializer') def update(self, instance, validated_data): self.__clear_related_objects(instance) instance = super(RestartDecisionSerializer, self).update(instance, validated_data) decision_status_changed(instance) return instance def to_representation(self, instance): return {'url': reverse('jobs:decision', args=[instance.pk])} class Meta: model = Decision fields = ('operator', 'configuration')
class SchedulerUserSerializer(serializers.ModelSerializer): user = fields.HiddenField(default=serializers.CurrentUserDefault()) def create(self, validated_data): try: instance = SchedulerUser.objects.get(user=validated_data['user']) validated_data.pop('user') return self.update(instance, validated_data) except SchedulerUser.DoesNotExist: return super().create(validated_data) class Meta: model = SchedulerUser fields = ('user', 'login', 'password')
class CreateJobSerializer(serializers.ModelSerializer): author = fields.HiddenField(default=serializers.CurrentUserDefault()) user_roles = UserRoleSerializer(many=True, default=[]) def create(self, validated_data): user_roles = validated_data.pop('user_roles') instance = super().create(validated_data) UserRole.objects.bulk_create(list(UserRole(job=instance, **rkwargs) for rkwargs in user_roles)) return instance def update(self, instance, validated_data): raise RuntimeError('Update method is not supported for this serializer') def to_representation(self, instance): if isinstance(instance, self.Meta.model): return {'url': reverse('jobs:decision-create', args=[instance.pk])} return super(CreateJobSerializer, self).to_representation(instance) class Meta: model = Job fields = ('preset', 'name', 'global_role', 'user_roles', 'author')
class NewStudentSerializer(serializers.ModelSerializer): curator = fields.HiddenField(default=serializers.CurrentUserDefault()) class Meta: model = CuratorStudent fields = ('curator', 'student')