Skip to content

Commit

Permalink
Update Django to 5.0, use db_default in psychopass
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonejt committed Dec 5, 2023
1 parent ca08f53 commit 129cf71
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 42 deletions.
16 changes: 8 additions & 8 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion community/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TestCommunityView(APITestCase):

def setUp(self) -> None:
self.url = "/community"
self.user = User.objects.create_superuser(username="discord")
self.user = User.objects.create_superuser(username=get_random_string(10), email=None, password=None)
self.token = Token.objects.create(user=self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.token.key}")
self.community = Community.objects.create(
Expand Down
4 changes: 2 additions & 2 deletions dominator/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TestMemberDominatorView(APITestCase):

def setUp(self) -> None:
self.url = "/dominator/member"
self.user = User.objects.create_superuser(username="discord")
self.user = User.objects.create_superuser(username=get_random_string(10), email=None, password=None)
self.token = Token.objects.create(user=self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.token.key}")
self.community = Community.objects.create(
Expand Down Expand Up @@ -65,7 +65,7 @@ class TestMessageDominatorView(APITestCase):

def setUp(self) -> None:
self.url = "/dominator/message"
self.user = User.objects.create_superuser(username="discord")
self.user = User.objects.create_superuser(username=get_random_string(10), email=None, password=None)
self.token = Token.objects.create(user=self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.token.key}")
self.community = Community.objects.create(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Generated by Django 5.0 on 2023-12-05 00:36

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('psychopass', '0003_remove_communitypsychopass_community_id_and_more'),
]

operations = [
migrations.AlterField(
model_name='userpsychopass',
name='identity_attack',
field=models.FloatField(db_default=models.Value(0.5)),
),
migrations.AlterField(
model_name='userpsychopass',
name='insult',
field=models.FloatField(db_default=models.Value(0.5)),
),
migrations.AlterField(
model_name='userpsychopass',
name='messages',
field=models.PositiveIntegerField(db_default=models.Value(0)),
),
migrations.AlterField(
model_name='userpsychopass',
name='profanity',
field=models.FloatField(db_default=models.Value(0.5)),
),
migrations.AlterField(
model_name='userpsychopass',
name='psycho_hazard',
field=models.BooleanField(db_default=models.Value(False)),
),
migrations.AlterField(
model_name='userpsychopass',
name='severe_toxicity',
field=models.FloatField(db_default=models.Value(0.5)),
),
migrations.AlterField(
model_name='userpsychopass',
name='sexually_explicit',
field=models.FloatField(db_default=models.Value(0.5)),
),
migrations.AlterField(
model_name='userpsychopass',
name='threat',
field=models.FloatField(db_default=models.Value(0.5)),
),
migrations.AlterField(
model_name='userpsychopass',
name='toxicity',
field=models.FloatField(db_default=models.Value(0.5)),
),
]
35 changes: 9 additions & 26 deletions psychopass/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ class UserPsychoPass(models.Model):
platform = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
user_id = models.CharField(max_length=20, unique=True)

messages = models.PositiveBigIntegerField(default=0)
psycho_hazard = models.BooleanField(default=False)
messages = models.PositiveIntegerField(db_default=0)
psycho_hazard = models.BooleanField(db_default=False)

toxicity = models.FloatField(default=0.5)
severe_toxicity = models.FloatField(default=0.5)
identity_attack = models.FloatField(default=0.5)
insult = models.FloatField(default=0.5)
threat = models.FloatField(default=0.5)
profanity = models.FloatField(default=0.5)
sexually_explicit = models.FloatField(default=0.5)
toxicity = models.FloatField(db_default=0.5)
severe_toxicity = models.FloatField(db_default=0.5)
identity_attack = models.FloatField(db_default=0.5)
insult = models.FloatField(db_default=0.5)
threat = models.FloatField(db_default=0.5)
profanity = models.FloatField(db_default=0.5)
sexually_explicit = models.FloatField(db_default=0.5)

class Meta:
verbose_name = "Psycho-Pass"
Expand All @@ -29,23 +29,6 @@ class Meta:
def __str__(self) -> str:
return f"{self.platform.username}/{self.user_id} ({self.id})"

def ingest_message(self, scores: dict) -> None:
self.toxicity = self.update_score(
scores["TOXICITY"], self.toxicity, self.messages)
self.severe_toxicity = self.update_score(
scores["SEVERE_TOXICITY"], self.severe_toxicity, self.messages)
self.identity_attack = self.update_score(
scores["IDENTITY_ATTACK"], self.identity_attack, self.messages)
self.insult = self.update_score(
scores["INSULT"], self.insult, self.messages)
self.threat = self.update_score(
scores["THREAT"], self.threat, self.messages)
self.profanity = self.update_score(
scores["PROFANITY"], self.profanity, self.messages)
self.sexually_explicit = self.update_score(
scores["SEXUALLY_EXPLICIT"], self.sexually_explicit, self.messages)
self.messages = max(0, min(500, self.messages+1))

def crime_coefficient(self) -> int:
base = 100
base += (self.toxicity - 0.5) * 500
Expand Down
6 changes: 3 additions & 3 deletions psychopass/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TestUserPsychoPassView(APITestCase):

def setUp(self) -> None:
self.url = "/psychopass/user"
self.user = User.objects.create_superuser(username="discord")
self.user = User.objects.create_superuser(username=get_random_string(10), email=None, password=None)
self.token = Token.objects.create(user=self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.token.key}")
self.psycho_pass = UserPsychoPass.objects.create(
Expand Down Expand Up @@ -53,7 +53,7 @@ class TestCommunityPsychoPassView(APITestCase):

def setUp(self) -> None:
self.url = "/psychopass/community"
self.user = User.objects.create_superuser(username="discord")
self.user = User.objects.create_superuser(username=get_random_string(10), email=None, password=None)
self.token = Token.objects.create(user=self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.token.key}")
self.community = Community.objects.create(
Expand Down Expand Up @@ -93,7 +93,7 @@ class TestIngestMessage(APITestCase):

def setUp(self) -> None:
self.url = "/psychopass/message"
self.user = User.objects.create_superuser(username="discord")
self.user = User.objects.create_superuser(username=get_random_string(10), email=None, password=None)
self.token = Token.objects.create(user=self.user)
self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.token.key}")
self.community = Community.objects.create(
Expand Down
20 changes: 18 additions & 2 deletions psychopass/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,24 @@ def ingest_message(request: Request) -> Response:
Community, platform=request.user, community_id=request.data.get("communityID"))
community_psycho_pass = get_object_or_404(
CommunityPsychoPass, community=community)

psycho_pass.ingest_message(request.data.get("attributeScores"))

attribute_scores = request.data["attributeScores"]
psycho_pass.toxicity = psycho_pass.update_score(
attribute_scores["TOXICITY"], psycho_pass.toxicity, psycho_pass.messages)
psycho_pass.severe_toxicity = psycho_pass.update_score(
attribute_scores["SEVERE_TOXICITY"], psycho_pass.severe_toxicity, psycho_pass.messages)
psycho_pass.identity_attack = psycho_pass.update_score(
attribute_scores["IDENTITY_ATTACK"], psycho_pass.identity_attack, psycho_pass.messages)
psycho_pass.insult = psycho_pass.update_score(
attribute_scores["INSULT"], psycho_pass.insult, psycho_pass.messages)
psycho_pass.threat = psycho_pass.update_score(
attribute_scores["THREAT"], psycho_pass.threat, psycho_pass.messages)
psycho_pass.profanity = psycho_pass.update_score(
attribute_scores["PROFANITY"], psycho_pass.profanity, psycho_pass.messages)
psycho_pass.sexually_explicit = psycho_pass.update_score(
attribute_scores["SEXUALLY_EXPLICIT"], psycho_pass.sexually_explicit, psycho_pass.messages)
psycho_pass.messages = max(0, min(500, psycho_pass.messages+1))

psycho_pass.save()
community_psycho_pass.users.add(psycho_pass)
community_psycho_pass.save()
Expand Down

0 comments on commit 129cf71

Please sign in to comment.