52 lines
1.9 KiB
Python
52 lines
1.9 KiB
Python
from core.apps.evaluation.choices.history import EvaluationEventType
|
|
|
|
|
|
class EvaluationHistoryService:
|
|
"""
|
|
AutoEvaluation va QuickEvaluation uchun history yozuvlarini yaratuvchi servis.
|
|
Faqat shu servis orqali yoziladi — frontend va boshqa kodlar bu sinfdan foydalanadi.
|
|
"""
|
|
|
|
@staticmethod
|
|
def _build_actor(user) -> dict:
|
|
if user is None:
|
|
return {"id": None, "full_name": "Tizim", "role": "system"}
|
|
full_name = ""
|
|
if hasattr(user, "get_full_name"):
|
|
full_name = user.get_full_name().strip()
|
|
if not full_name and hasattr(user, "phone"):
|
|
full_name = str(user.phone)
|
|
return {
|
|
"id": user.id,
|
|
"full_name": full_name or str(user),
|
|
"role": getattr(user, "role", "user"),
|
|
}
|
|
|
|
@classmethod
|
|
def log_auto(cls, auto_evaluation, event_type: EvaluationEventType, actor=None, meta: dict = None):
|
|
from core.apps.evaluation.models import AutoevaluationhistoryModel
|
|
|
|
actor_data = cls._build_actor(actor)
|
|
AutoevaluationhistoryModel.objects.create(
|
|
auto_evaluation=auto_evaluation,
|
|
event_type=event_type,
|
|
actor_id=actor_data["id"],
|
|
actor_full_name=actor_data["full_name"],
|
|
actor_role=actor_data["role"],
|
|
meta=meta or {},
|
|
)
|
|
|
|
@classmethod
|
|
def log_quick(cls, quick_evaluation, event_type: EvaluationEventType, actor=None, meta: dict = None):
|
|
from core.apps.evaluation.models import QuickevaluationhistoryModel
|
|
|
|
actor_data = cls._build_actor(actor)
|
|
QuickevaluationhistoryModel.objects.create(
|
|
quick_evaluation=quick_evaluation,
|
|
event_type=event_type,
|
|
actor_id=actor_data["id"],
|
|
actor_full_name=actor_data["full_name"],
|
|
actor_role=actor_data["role"],
|
|
meta=meta or {},
|
|
)
|