import uuid import structlog from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models from django.utils.translation import gettext_lazy as _ from vbv_lernwelt.core.models import User from vbv_lernwelt.course.models import CourseSessionUser from vbv_lernwelt.notify.services import NotificationService logger = structlog.get_logger(__name__) class FeedbackIntegerField(models.IntegerField): def __init__(self, **kwargs): validators = kwargs.pop("validators", []) nullable = kwargs.pop("null", True) super().__init__( validators=validators + [MinValueValidator(1), MaxValueValidator(4)], null=nullable, **kwargs, ) class FeedbackResponse(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) feedback_user = models.ForeignKey(User, on_delete=models.CASCADE) class DiscoveredChoices(models.TextChoices): INTERNET = "I", _("Internet") LEAFLET = "L", _("Leaflet") NEWSPAPER = "N", _("Newspaper") RECOMMENDATION = "R", _("Personal recommendation") EVENT = "E", _("Public event") OTHER = "O", _("Other") class RatingChoices(models.IntegerChoices): ONE = 1, "1" TWO = 2, "2" THREE = 3, "3" FOUR = 4, "4" class PercentageChoices(models.IntegerChoices): TWENTY = 20, "20%" FOURTY = 40, "40%" SIXTY = 60, "60%" EIGHTY = 80, "80%" HUNDRED = 100, "100%" def save(self, *args, **kwargs): super(FeedbackResponse, self).save(*args, **kwargs) try: if self.submitted and not self.notification_sent: course_session_users = CourseSessionUser.objects.filter( role="EXPERT", course_session=self.course_session, expert=self.circle, ) for csu in course_session_users: NotificationService.send_new_feedback_notification( recipient=csu.user, feedback_response=self, ) self.notification_sent = True self.save() except Exception: logger.exception( "Failed to send feedback notification", exc_info=True, label="feedback_notification", ) data = models.JSONField(default=dict) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) submitted = models.BooleanField(default=False) notification_sent = models.BooleanField(default=False) circle = models.ForeignKey("learnpath.Circle", models.PROTECT) course_session = models.ForeignKey("course.CourseSession", models.CASCADE)