from django.db import models
from users.models import User
[docs]
class Score(models.Model):
"""
Stores the computed compatibility score between a mentee and a mentor.
Each row represents a unique mentee–mentor pairing. Scores are used by
the matching algorithm to rank and assign mentors to mentees. The
``(mentee_email, mentor_email)`` pair is enforced as unique at the
database level.
Attributes:
mentee_email (EmailField): Email address of the mentee in the pairing.
mentor_email (EmailField): Email address of the mentor in the pairing.
score (FloatField): Computed compatibility score for this pairing.
created_at (DateTimeField): Timestamp when the score was first calculated.
updated_at (DateTimeField): Timestamp of the most recent score update.
"""
mentee_email = models.EmailField(default='')
mentor_email = models.EmailField(default='')
score = models.FloatField(default=0.0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
db_table = 'scores'
unique_together = [['mentee_email', 'mentor_email']]
indexes = [
models.Index(fields=['mentee_email', 'mentor_email']),
models.Index(fields=['score']),
]
def __str__(self):
"""
Return a human-readable representation of the score record.
:returns: A string in the form ``'<mentee_email> - <mentor_email>: <score>'``.
:rtype: str
"""
return f"{self.mentee_email} - {self.mentor_email}: {self.score}"
@property
def mentee(self):
"""
Retrieve the :class:`~users.models.User` instance for the mentee.
Performs a live database lookup by ``mentee_email`` on every access.
:returns: The ``User`` record whose email matches ``mentee_email``.
:rtype: users.models.User
:raises users.models.User.DoesNotExist: If no user with ``mentee_email``
exists in the database.
Example::
>>> score.mentee.firstName
'Jane'
"""
return User.objects.get(email=self.mentee_email)
@property
def mentor(self):
"""
Retrieve the :class:`~users.models.User` instance for the mentor.
Performs a live database lookup by ``mentor_email`` on every access.
:returns: The ``User`` record whose email matches ``mentor_email``.
:rtype: users.models.User
:raises users.models.User.DoesNotExist: If no user with ``mentor_email``
exists in the database.
Example::
>>> score.mentor.firstName
'John'
"""
return User.objects.get(email=self.mentor_email)