-
Notifications
You must be signed in to change notification settings - Fork 1
hidden user score #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
hidden user score #155
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4581fc8
now ordering by hidden score, that's calculated by how much user had …
VeryBigSad 297abce
add ordering score to admin + user signals are now in a separate file
VeryBigSad 52d7696
fix tests
VeryBigSad 6a4790f
fix tests again
VeryBigSad 7d94a3e
save the user model after ordering score calculated
VeryBigSad 66e98dc
fix infinite recursion for user updates
VeryBigSad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
users/migrations/0037_alter_customuser_options_customuser_ordering_score.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
|
|
@@ -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) | ||
|
|
@@ -105,6 +107,30 @@ class CustomUser(AbstractUser): | |
|
|
||
| objects = CustomUserManager() | ||
|
|
||
| def calculate_ordering_score(self) -> int: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [] | ||
|
|
@@ -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): | ||
|
|
@@ -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 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
мб переименовать в hidden_score