Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Optional
- from django.db import models
- from django.utils import timezone
- from django.forms import ModelForm
- from django.contrib.contenttypes.models import ContentType
- from django.contrib.contenttypes.fields import GenericForeignKey
- from .user import User
- from crm.models import Project
- from crm.models import get_base_record_for_project
- from misc.generic_models.meta_mixin import MetaMixin
- from misc.views.clean_form_errors import clean_form_errors
- from misc.generic_models.generic_model import GenericModel
- class SubmissionRequestManager(models.Manager):
- @staticmethod
- def submission_request_create_or_update(
- data: dict, instance: Optional['SubmissionRequest'] = None, user: Optional['User'] = None
- ) -> dict:
- """Выполняет создание или изменение экземпляра ConnectingUserToAccess."""
- connecting_user_to_access_form = SubmissionRequest(data=data, instance=instance)
- is_valid = connecting_user_to_access_form.is_valid()
- if is_valid:
- instance = connecting_user_to_access_form.save()
- return {
- 'is_ok': is_valid,
- 'errors': clean_form_errors(connecting_user_to_access_form.errors),
- 'obj': instance
- }
- @staticmethod
- def response_to_request(obj: 'SubmissionRequest', user: 'User') -> dict:
- """Выполняет перевод в архив экземпляра SubmissionRequest."""
- errors = {}
- is_ok = True
- if not obj:
- errors = {'obj': SubmissionRequest.ErrorHandler.FAIL_TO_FIND_OBJ}
- is_ok = False
- # :TODOO Поменять на метод ответа
- return {'is_ok': is_ok, 'errors': errors, 'obj': obj}
- @staticmethod
- def submission_request_delete(obj: 'SubmissionRequest', user: Optional['User'] = None) -> dict:
- if not obj:
- error = SubmissionRequest.ErrorHandler.FAIL_TO_FIND_OBJ
- return {'is_ok': False, 'error': error, 'obj': None}
- else:
- obj.delete()
- return {'is_ok': True, 'error': '', 'obj': obj}
- class SubmissionRequest(GenericModel):
- class ErrorHandler:
- """Статусы ошибок"""
- FAIL_TO_FIND_OBJ = 'Объект не найден.'
- class Status(models.TextChoices):
- """Статус набора данных."""
- REQUEST_COMPLETED = 'request_completed', 'Запрос выполнен'
- IN_PROCESS = 'in_process', 'В процессе'
- DENIED = 'denied', 'Отказано'
- meta_tags = [MetaMixin.ModelTags.UserData]
- _id_meta = [MetaMixin.FieldTags.PKField, MetaMixin.FieldTags.NotForLog]# Мета данные поля id
- user = models.ForeignKey(
- User,
- verbose_name='Сотрудник запрашиваемый доступы или имущество',
- related_name='submission_request_user',
- on_delete=models.CASCADE,
- null=False,
- blank=False,
- )
- _user_meta = [MetaMixin.FieldTags.UserField]
- project = models.ForeignKey(
- Project, verbose_name='проект',
- on_delete=models.PROTECT,
- default=get_base_record_for_project,
- blank=True,
- )
- _project_meta = [MetaMixin.FieldTags.ProjectField]
- comment_user = models.TextField(verbose_name='Комментарий запрашиваемого запрос', null=True, blank=True)
- comment_administration = models.TextField(verbose_name='Комментарий админестратора', null=True, blank=True)
- status = models.CharField('Статус данных', max_length=18, choices=Status.choices, default=Status.IN_PROCESS)
- content_type = models.ForeignKey(ContentType, verbose_name='Объект запроса', on_delete=models.CASCADE)
- object_id = models.PositiveIntegerField()
- content_object = GenericForeignKey('content_type', 'object_id')
- supervisor = models.ForeignKey(
- User,
- verbose_name='Ответственный сотрудник',
- related_name='submission_request_supervisor',
- on_delete=models.CASCADE,
- null=False,
- blank=False,
- )
- objects = SubmissionRequestManager()
- def __str__(self):
- return str(self)
- __repr__ = __str__
- class Meta:
- verbose_name = 'Запрос на подпись (Имущество и доступы)'
- verbose_name_plural = 'Запрос на подпись (Имущество и доступы)'
- ordering = ['-id']
- class SubmissionRequestForm(ModelForm):
- class Meta:
- model = SubmissionRequest
- fields = '__all__'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement