diff --git a/aimmo/migrations/0037_worksheetusage_loaded_at.py b/aimmo/migrations/0037_worksheetusage_loaded_at.py new file mode 100644 index 000000000..38d5dd130 --- /dev/null +++ b/aimmo/migrations/0037_worksheetusage_loaded_at.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.23 on 2023-11-10 16:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('aimmo', '0036_worksheetbadge'), + ] + + operations = [ + migrations.AddField( + model_name='worksheetusage', + name='loaded_at', + field=models.DateTimeField(blank=True, null=True), + ), + ] diff --git a/aimmo/models.py b/aimmo/models.py index 170625d62..8aa1c81f6 100755 --- a/aimmo/models.py +++ b/aimmo/models.py @@ -204,6 +204,7 @@ class WorksheetUsage(models.Model): klass = models.ForeignKey(Class, null=True, blank=True, on_delete=models.SET_NULL) worksheet_id = models.IntegerField() created_at = models.DateTimeField(default=timezone.now) + loaded_at = models.DateTimeField(null=True, blank=True) class WorksheetBadge(models.Model): diff --git a/aimmo/views.py b/aimmo/views.py index d0fcc85e7..403cb136d 100644 --- a/aimmo/views.py +++ b/aimmo/views.py @@ -9,6 +9,7 @@ from django.db import transaction from django.http import Http404, HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, JsonResponse from django.shortcuts import get_object_or_404 +from django.utils import timezone from django.views.decorators.csrf import ensure_csrf_cookie from rest_framework import mixins, status, viewsets from rest_framework.authentication import BasicAuthentication, SessionAuthentication @@ -67,6 +68,19 @@ def badges(request, id): avatar = Avatar.objects.create(game=game, owner=request.user) avatar_user_profile = UserProfile.objects.get(user=avatar.owner) + worksheet_usage = ( + WorksheetUsage.objects.filter( + user=request.user, + klass=game.game_class, + worksheet_id=game.worksheet_id, + ) + .order_by("created_at") + .last() + ) + if worksheet_usage and not worksheet_usage.loaded_at: + worksheet_usage.loaded_at = timezone.now() + worksheet_usage.save() + if request.method == "POST": earned_badges = request.POST["badges"]