-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodels.py
More file actions
54 lines (43 loc) · 1.97 KB
/
models.py
File metadata and controls
54 lines (43 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
is_teacher = models.BooleanField(default=False, null=False)
is_student = models.BooleanField(default=True, null=False)
is_imported = models.BooleanField(default=False, null=False)
rating = models.DecimalField(max_digits=6, decimal_places=2, default=1200.00)
accept_ranked_matches = models.BooleanField(
default=True, help_text="Allow any user to request ranked matches against you"
)
accept_unranked_matches = models.BooleanField(
default=True, help_text="Allow any user to request unranked matches against you"
)
@property
def has_management_permission(self) -> bool:
return self.is_teacher or self.is_staff or self.is_superuser
@property
def short_name(self):
return (
f"{self.first_name} {self.last_name} ({self.username})"
if self.first_name or self.last_name
else self.username
)
def get_social_auth(self):
return self.social_auth.get(provider="ion")
def save(self, *args, **kwargs):
existing_user = User.objects.filter(username=self.username, is_imported=True).first()
if existing_user:
from ..games.models import (
Submission, # cannot import at top of file b/c Submission references User (circular import)
)
Submission.objects.filter(user=existing_user).update(user=self)
existing_user.delete()
super().save(*args, **kwargs)
def __str__(self):
return self.username
class RatingHistory(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="rating_history")
rating = models.DecimalField(max_digits=6, decimal_places=2)
changed_at = models.DateTimeField(auto_now_add=True)
match = models.ForeignKey("games.Match", null=True, on_delete=models.SET_NULL)
class Meta:
ordering = ["changed_at"]