Add completion app

This commit is contained in:
Daniel Egger 2022-06-08 16:19:37 +02:00
parent ff23e3b4f7
commit 08e5672623
12 changed files with 192 additions and 1 deletions

View File

@ -2,9 +2,10 @@
Base settings to build other settings files upon. Base settings to build other settings files upon.
""" """
import logging import logging
from pathlib import Path
import structlog import structlog
from environs import Env from environs import Env
from pathlib import Path
from vbv_lernwelt.core.utils import structlog_add_app_info from vbv_lernwelt.core.utils import structlog_add_app_info
@ -103,6 +104,7 @@ LOCAL_APPS = [
"vbv_lernwelt.simpletodo", "vbv_lernwelt.simpletodo",
"vbv_lernwelt.sso", "vbv_lernwelt.sso",
"vbv_lernwelt.learnpath", "vbv_lernwelt.learnpath",
"vbv_lernwelt.completion",
] ]
# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps # https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

View File

@ -42,6 +42,7 @@ urlpatterns = [
path('documents/', include(wagtaildocs_urls)), path('documents/', include(wagtaildocs_urls)),
path('pages/', include(wagtail_urls)), path('pages/', include(wagtail_urls)),
path('learnpath/', include("vbv_lernwelt.learnpath.urls")), path('learnpath/', include("vbv_lernwelt.learnpath.urls")),
path('api/completion/', include("vbv_lernwelt.completion.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG: if settings.DEBUG:
# Static file serving when using Gunicorn + Uvicorn for local web socket development # Static file serving when using Gunicorn + Uvicorn for local web socket development

View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class CompletionConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'vbv_lernwelt.completion'

View File

@ -0,0 +1,66 @@
# Generated by Django 3.2.13 on 2022-06-08 13:52
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='UserCircleCompletion',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('circle_key', models.UUIDField()),
('json_data', models.JSONField(blank=True, default=dict)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='LearningUnitQuestionCompletion',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('question_key', models.UUIDField()),
('circle_key', models.UUIDField()),
('completed', models.BooleanField(default=True)),
('json_data', models.JSONField(blank=True, default=dict)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='LearningContentCompletion',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('learning_content_key', models.UUIDField()),
('circle_key', models.UUIDField()),
('completed', models.BooleanField(default=True)),
('json_data', models.JSONField(blank=True, default=dict)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.AddConstraint(
model_name='usercirclecompletion',
constraint=models.UniqueConstraint(fields=('user', 'circle_key'), name='unique_user_circle_completion'),
),
migrations.AddConstraint(
model_name='learningunitquestioncompletion',
constraint=models.UniqueConstraint(fields=('user', 'question_key'), name='unique_user_question_key'),
),
migrations.AddConstraint(
model_name='learningcontentcompletion',
constraint=models.UniqueConstraint(fields=('user', 'learning_content_key'), name='unique_user_learning_content_key'),
),
]

View File

@ -0,0 +1,59 @@
from django.db import models
from django.db.models import UniqueConstraint
from vbv_lernwelt.core.models import User
class LearningContentCompletion(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
learning_content_key = models.UUIDField()
circle_key = models.UUIDField()
completed = models.BooleanField(default=True)
json_data = models.JSONField(default=dict, blank=True)
class Meta:
constraints = [
UniqueConstraint(
fields=['user', 'learning_content_key', ],
name='unique_user_learning_content_key'
)
]
class LearningUnitQuestionCompletion(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
question_key = models.UUIDField()
circle_key = models.UUIDField()
completed = models.BooleanField(default=True)
json_data = models.JSONField(default=dict, blank=True)
class Meta:
constraints = [
UniqueConstraint(
fields=['user', 'question_key', ],
name='unique_user_question_key'
)
]
class UserCircleCompletion(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
circle_key = models.UUIDField()
json_data = models.JSONField(default=dict, blank=True)
class Meta:
constraints = [
UniqueConstraint(fields=['user', 'circle_key'], name='unique_user_circle_completion')
]

View File

@ -0,0 +1,28 @@
from rest_framework.test import APITestCase, APIClient
from vbv_lernwelt.core.create_default_users import create_default_users
from vbv_lernwelt.core.models import User
from vbv_lernwelt.learnpath.models import LearningContent
from vbv_lernwelt.learnpath.tests.create_default_learning_path import create_default_learning_path
from vbv_lernwelt.learnpath.tests.test_create_default_learning_path import create_locales_for_wagtail
class CompletionApiTestCase(APITestCase):
@classmethod
def setUpClass(cls) -> None:
super(CompletionApiTestCase, cls).setUpClass()
create_locales_for_wagtail()
create_default_users()
create_default_learning_path()
def setUp(self) -> None:
self.user = User.objects.get(username='student')
self.client.login(username='student', password='student')
def test_completeLearningContent_works(self):
learning_content_key = LearningContent.objects.get(title='Einleitung Circle "Anlayse"').translation_key
response = self.client.post(f'/api/completion/complete_learning_content/', {
'learning_content_key': learning_content_key
})
print(response.content)
self.assertEqual(response.status_code, 201)

View File

@ -0,0 +1,7 @@
from django.urls import path
from vbv_lernwelt.completion.views import complete_learning_content
urlpatterns = [
path(r"complete_learning_content/", complete_learning_content, name="complete_learning_content"),
]

View File

@ -0,0 +1,19 @@
from rest_framework.decorators import api_view
from rest_framework.response import Response
from vbv_lernwelt.completion.models import LearningContentCompletion
from vbv_lernwelt.learnpath.models import LearningContent
@api_view(['POST'])
def complete_learning_content(request):
learning_content_key = request.data.get('learning_content_key')
learning_content = LearningContent.objects.get(translation_key=learning_content_key)
LearningContentCompletion.objects.create(
user=request.user,
learning_content_key=learning_content_key,
circle_key=learning_content.get_parent().translation_key,
)
return Response(status=201)