Advertisement
Mochinov

Untitled

May 17th, 2022
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. from datetime import timedelta
  2. from typing import Optional
  3.  
  4. from django.db import models
  5. from django.utils import timezone
  6.  
  7. from crm.models import Project
  8. from core.models.crm_settings import CrmSettings
  9. from crm.models import get_base_record_for_project
  10. from crm_call.models import CallData
  11. from misc.generic_models.generic_model import GenericModel
  12. from misc.generic_models.meta_mixin import MetaMixin
  13. from users.models import User
  14.  
  15.  
  16. def default_listen_date_to():
  17. return timezone.now() + timedelta(minutes=1440)
  18.  
  19.  
  20. class Listen(GenericModel):
  21. """Используется для хранения информации о прослушке звонков."""
  22.  
  23. # TODO: В будущем планируется изменить на unique=False (у одного звонка будет несколько слушующих)
  24. call_data = models.ForeignKey(CallData, verbose_name='Запись звонка', on_delete=models.CASCADE, unique=True,
  25. related_name='to_listen')
  26. user_to_listen = models.ForeignKey(User, verbose_name='Сотрудник', related_name='to_listen',
  27. on_delete=models.SET_NULL, null=True, blank=True)
  28. listen_date_to = models.DateTimeField('Прослушать до', default=default_listen_date_to)
  29.  
  30.  
  31. meta_tags = [MetaMixin.ModelTags.UserData]
  32.  
  33. _id_meta = [MetaMixin.FieldTags.PKField, MetaMixin.FieldTags.NotForLog]# Мета данные поля id
  34.  
  35.  
  36. _user_meta = [MetaMixin.FieldTags.UserField]
  37.  
  38.  
  39. project = models.ForeignKey(
  40. Project,
  41. verbose_name='проект',
  42. on_delete=models.PROTECT,
  43. default=get_base_record_for_project,
  44. blank=True,
  45. )
  46. _project_meta = [MetaMixin.FieldTags.ProjectField]
  47.  
  48. class Meta:
  49. verbose_name = 'Прослушка'
  50. verbose_name_plural = 'Прослушка'
  51.  
  52. @staticmethod
  53. def get_default_listen_date_to(user: Optional[User] = None):
  54. project_id = (user.project_id if user else None) or get_base_record_for_project()
  55.  
  56. crm_settings, is_created = CrmSettings.objects.get_or_create(project_id=project_id)
  57. return timezone.now() + timedelta(minutes=crm_settings.time_to_evaluate_calls)
  58.  
  59. def get_call_data_str(self):
  60. """Возвращает строку таблицы CallData"""
  61.  
  62. if self.call_data:
  63. return str(self.call_data)
  64.  
  65. ---------------------------
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. def get_user_to_listen_field(self, obj) -> dict:
  74. """Определяет возвращаемое значение для поля user_to_listen."""
  75.  
  76. value = GenericSerializer.DEFAULT_EMPTY
  77. verbose = GenericSerializer.DEFAULT_EMPTY
  78.  
  79. is_readonly = False
  80.  
  81. if obj.to_listen.exists() and obj.to_listen.all().first().user_to_listen:
  82. value = obj.to_listen.all().first().user_to_listen.id
  83. verbose = obj.to_listen.all().first().user_to_listen.get_full_name()
  84.  
  85. if obj.qualitymaps.all():
  86. is_readonly = True
  87. value = 'Звонок прослушан'
  88. verbose = 'Звонок прослушан'
  89.  
  90. return self.get_table_field(field_name='user_to_listen_field', value=value, instance=obj, verbose=verbose,
  91. is_readonly=is_readonly)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement