|
|
|
|
|
|
|
|
|
|
|
from django.db.models.signals import post_save |
|
|
from django.dispatch import receiver |
|
|
from django.conf import settings |
|
|
|
|
|
from apps.users.models import User, UserProfile |
|
|
from apps.forum.models import Question, Answer |
|
|
from apps.bookings.models import Booking |
|
|
from apps.mentors.models import MentorProfile |
|
|
|
|
|
@receiver(post_save, sender=User) |
|
|
def create_user_profile(sender, instance, created, **kwargs): |
|
|
"""Créer un profil par défaut pour chaque nouvel utilisateur""" |
|
|
if created: |
|
|
|
|
|
if not instance.profiles.exists(): |
|
|
UserProfile.objects.create( |
|
|
user=instance, |
|
|
name=instance.email.split('@')[0], |
|
|
is_current=True |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Question) |
|
|
def handle_question_points(sender, instance, created, **kwargs): |
|
|
"""Attribution de points pour nouvelle question""" |
|
|
if created: |
|
|
instance.author.add_points( |
|
|
settings.GAMIFICATION_POINTS['QUESTION_POSTED'], |
|
|
'question_posted' |
|
|
) |
|
|
|
|
|
|
|
|
from apps.gamification.services import BadgeService |
|
|
BadgeService.check_and_award_badges(instance.author) |
|
|
|
|
|
@receiver(post_save, sender=Answer) |
|
|
def handle_answer_points(sender, instance, created, **kwargs): |
|
|
"""Attribution de points pour nouvelle réponse""" |
|
|
if created: |
|
|
instance.author.add_points( |
|
|
settings.GAMIFICATION_POINTS['ANSWER_POSTED'], |
|
|
'answer_posted' |
|
|
) |
|
|
|
|
|
|
|
|
from apps.gamification.services import BadgeService |
|
|
BadgeService.check_and_award_badges(instance.author) |
|
|
|
|
|
@receiver(post_save, sender=MentorProfile) |
|
|
def handle_mentor_verification(sender, instance, created, **kwargs): |
|
|
"""Gérer la vérification du mentor (Role + Points)""" |
|
|
if instance.is_verified: |
|
|
|
|
|
if instance.user.role != 'MENTOR': |
|
|
instance.user.role = 'MENTOR' |
|
|
instance.user.save(update_fields=['role']) |
|
|
|
|
|
|
|
|
from apps.gamification.models import UserPointsHistory |
|
|
if not UserPointsHistory.objects.filter(user=instance.user, reason='become_mentor').exists(): |
|
|
instance.user.add_points( |
|
|
settings.GAMIFICATION_POINTS['BECOME_MENTOR'], |
|
|
'become_mentor' |
|
|
) |
|
|
|
|
|
|
|
|
from apps.gamification.services import BadgeService |
|
|
BadgeService.check_and_award_badges(instance.user) |
|
|
|
|
|
@receiver(post_save, sender=Booking) |
|
|
def handle_booking_completion(sender, instance, created, **kwargs): |
|
|
"""Attribution de points pour session complétée""" |
|
|
if not created and instance.status == 'COMPLETED': |
|
|
|
|
|
instance.student.add_points( |
|
|
settings.GAMIFICATION_POINTS['BOOKING_COMPLETED'], |
|
|
'booking_completed' |
|
|
) |
|
|
|
|
|
|
|
|
if hasattr(instance.mentor, 'mentor_profile'): |
|
|
profile = instance.mentor.mentor_profile |
|
|
profile.total_sessions += 1 |
|
|
profile.save(update_fields=['total_sessions']) |
|
|
|
|
|
|