Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class CustomUserAdmin(admin.ModelAdmin):
"city",
)

readonly_fields = ("ordering_score",)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

мб переименовать в hidden_score


def save_model(self, request, obj, form, change):
# if user_type changed, then delete all related fields
if change:
Expand Down
3 changes: 3 additions & 0 deletions users/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ class UsersConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "users"
verbose_name = "Пользователи"

def ready(self):
import users.signals # noqa: F401
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 4.1.3 on 2023-07-03 20:59

from django.db import migrations, models


def set_up_ordering_score(apps, schema_editor):
CustomUser = apps.get_model("users", "CustomUser")

for user in CustomUser.objects.all():
user.ordering_score = user.calculate_ordering_score()
user.save()


class Migration(migrations.Migration):

dependencies = [
("users", "0036_userlink"),
]

operations = [
migrations.AddField(
model_name="customuser",
name="ordering_score",
field=models.PositiveIntegerField(default=0, editable=False),
),
migrations.AlterModelOptions(
name="customuser",
options={
"ordering": ["-ordering_score"],
"verbose_name": "Пользователь",
"verbose_name_plural": "Пользователи",
},
),
migrations.RunPython(set_up_ordering_score)
]
46 changes: 31 additions & 15 deletions users/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver

from users.constants import (
ADMIN,
Expand Down Expand Up @@ -69,6 +67,10 @@ class CustomUser(AbstractUser):
default=get_default_user_type,
)

ordering_score = models.PositiveIntegerField(
default=0,
editable=False,
)
patronymic = models.CharField(max_length=255, null=True, blank=True)
key_skills = models.CharField(max_length=512, null=True, blank=True)
avatar = models.URLField(null=True, blank=True)
Expand Down Expand Up @@ -105,6 +107,30 @@ class CustomUser(AbstractUser):

objects = CustomUserManager()

def calculate_ordering_score(self) -> int:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вынести куда нибудь в service.py

"""
Calculate ordering score of the user, e.g. how full their profile is.

Returns:
int: ordering score of the user.
"""
score = 0
if self.avatar:
score += 10
if self.key_skills:
score += 7
if self.about_me:
score += 6
if self.region:
score += 4
if self.city:
score += 4
if self.organization:
score += 6
if self.speciality:
score += 7
return score

def get_project_chats(self) -> list:
collaborations = self.collaborations.all()
projects = []
Expand All @@ -124,6 +150,9 @@ def __str__(self) -> str:
class Meta:
verbose_name = "Пользователь"
verbose_name_plural = "Пользователи"
# order by count of fields inputted, like avatar, key_skills, about_me, etc.
# first show users with all fields inputted, then with 1 field inputted, etc.
ordering = ["-ordering_score"]


class UserAchievement(models.Model):
Expand Down Expand Up @@ -311,19 +340,6 @@ def __str__(self):
return f"Investor<{self.id}> - {self.user.first_name} {self.user.last_name}"


@receiver(post_save, sender=CustomUser)
def create_or_update_user_types(sender, instance, created, **kwargs):
if created:
if instance.user_type == CustomUser.MEMBER:
Member.objects.create(user=instance)
elif instance.user_type == CustomUser.MENTOR:
Mentor.objects.create(user=instance)
elif instance.user_type == CustomUser.EXPERT:
Expert.objects.create(user=instance)
elif instance.user_type == CustomUser.INVESTOR:
Investor.objects.create(user=instance)


class UserLink(models.Model):
"""
UserLink model
Expand Down
22 changes: 22 additions & 0 deletions users/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.db.models.signals import post_save
from django.dispatch import receiver

from users.models import CustomUser, Member, Mentor, Expert, Investor


@receiver(post_save, sender=CustomUser)
def create_or_update_user_types(sender, instance, created, **kwargs):
if created:
if instance.user_type == CustomUser.MEMBER:
Member.objects.create(user=instance)
elif instance.user_type == CustomUser.MENTOR:
Mentor.objects.create(user=instance)
elif instance.user_type == CustomUser.EXPERT:
Expert.objects.create(user=instance)
elif instance.user_type == CustomUser.INVESTOR:
Investor.objects.create(user=instance)
# check that the change wasn't about ordering scores
current_ordering_score = instance.calculate_ordering_score()
if instance.ordering_score != current_ordering_score:
instance.ordering_score = current_ordering_score
instance.save()