Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
fix: track worksheet badges (#1829)
Browse files Browse the repository at this point in the history
* fix: track worksheet usage

* fix: track worksheet badges

* make user nullable

* remove unused args

* Merge branch 'worksheet_usage' into worksheet_badges

* remove unused args

* silence pylint unused arg error

* silence pylint unused arg error

* fix: worksheet usage

* merge

* simplify

* relock

* Merge branch 'worksheet_usage' into worksheet_badges

* merge from master

* testy signals

* pytest config
  • Loading branch information
SKairinos authored Nov 14, 2023
1 parent 8cc5023 commit e7dfe13
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ __pycache__/
*.so

# Editors & workspaces
.vscode/
**.vscode/
# .vscode/
# **.vscode/
*.code-workspace

# Distribution / packaging
Expand Down
29 changes: 29 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Django Server",
"type": "python",
"request": "launch",
"django": true,
"justMyCode": false,
"program": "${workspaceFolder}/example_project/manage.py",
"args": [
"runserver",
"localhost:8000"
]
},
{
"name": "Pytest",
"type": "python",
"request": "test",
"justMyCode": false,
"presentation": {
"hidden": true
}
}
]
}
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": [
"./aimmo"
]
}
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ black = "*"
codeforlife-portal = "*"
django-import-export = "==3.3.1"
django-test-migrations = "==1.2.0"
pytest = "~=6.2"
pytest = "*"
pytest-cov = "*"
pytest-django = "==4.5.2"
pytest-pythonpath = "*"
Expand Down
30 changes: 30 additions & 0 deletions aimmo/migrations/0036_worksheetbadge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 3.2.23 on 2023-11-10 11:35

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('aimmo', '0035_worksheetusage'),
]

operations = [
migrations.CreateModel(
name='WorksheetBadge',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('worksheet_id', models.IntegerField()),
('badge_id', models.IntegerField()),
('created_at', models.DateTimeField(default=django.utils.timezone.now)),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
],
options={
'unique_together': {('user', 'worksheet_id', 'badge_id')},
},
),
]
39 changes: 38 additions & 1 deletion aimmo/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
import re
import secrets
import typing as t

from common.models import Class, Teacher
from common.models import Class, Teacher, UserProfile
from django.contrib.auth.models import User
from django.db import models
from django.dispatch import receiver
Expand Down Expand Up @@ -205,6 +206,42 @@ class WorksheetUsage(models.Model):
created_at = models.DateTimeField(default=timezone.now)


class WorksheetBadge(models.Model):
user = models.ForeignKey(User, null=True, blank=True, on_delete=models.SET_NULL)
worksheet_id = models.IntegerField()
badge_id = models.IntegerField()
created_at = models.DateTimeField(default=timezone.now)

class Meta:
unique_together = ("user", "worksheet_id", "badge_id")


@receiver(models.signals.pre_save, sender=UserProfile)
def create_worksheet_badge(instance: UserProfile, **_kwargs):
aimmo_badges: t.Optional[str] = instance.aimmo_badges
if aimmo_badges is None:
return

if instance.pk is not None:
old_instance: UserProfile = UserProfile.objects.get(pk=instance.pk)
if old_instance.aimmo_badges == instance.aimmo_badges:
return

for badge in aimmo_badges.split(","):
match = re.match(r"(\d+):(\d+)", badge)
if not match:
continue

worksheet_badge = {
"user": instance.user,
"worksheet_id": match.group(1),
"badge_id": match.group(2),
}

if not WorksheetBadge.objects.filter(**worksheet_badge).exists():
WorksheetBadge.objects.create(**worksheet_badge)


# TODO: Replace with a ModelSerializer
class GameSerializer(serializers.Serializer):
name = serializers.CharField(max_length=100, required=False)
Expand Down
45 changes: 45 additions & 0 deletions aimmo/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from common.models import UserProfile
from django.contrib.auth.models import User
from django.test import TestCase

from ..models import WorksheetBadge


class TestSignals(TestCase):
def test_pre_save___user_profile__create_worksheet_badge(self):
user = User.objects.create_user(
"JDoe",
"[email protected]",
"password",
)

user_profile = UserProfile.objects.create(user=user)
assert not WorksheetBadge.objects.exists()

def assert_worksheet_badges():
worksheet_badges = [
WorksheetBadge(
user=user,
worksheet_id=1,
badge_id=1,
),
WorksheetBadge(
user=user,
worksheet_id=1,
badge_id=2,
),
]
assert len(worksheet_badges) == WorksheetBadge.objects.count()
for worksheet_badge, expected_worksheet_badge in zip(WorksheetBadge.objects.all(), worksheet_badges):
assert (
worksheet_badge.user == expected_worksheet_badge.user
and worksheet_badge.worksheet_id == expected_worksheet_badge.worksheet_id
and worksheet_badge.badge_id == expected_worksheet_badge.badge_id
)

user_profile.aimmo_badges = "1:1,1:2"
user_profile.save()
assert_worksheet_badges()

user_profile.save()
assert_worksheet_badges()

0 comments on commit e7dfe13

Please sign in to comment.