Commit 2d3c6516 authored by Marten Kenbeek's avatar Marten Kenbeek
Browse files

Added registered actions to the backend.

parent 9bce8563
# Generated by Django 2.1.2 on 2018-10-16 11:34
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('courses', '0002_auto_20180918_1154'),
]
operations = [
migrations.CreateModel(
name='RegisteredAction',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100)),
('date', models.DateField()),
('description', models.TextField()),
('course', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, related_name='registered_actions', to='courses.Course', to_field='course_id')),
],
),
]
......@@ -8,3 +8,18 @@ class Course(models.Model):
def __str__(self):
return f"{self.course_name}"
class RegisteredAction(models.Model):
course = models.ForeignKey(
"Course",
related_name="registered_actions",
on_delete=models.DO_NOTHING,
to_field="course_id",
)
title = models.CharField(max_length=100)
date = models.DateField()
description = models.TextField()
def __str__(self):
return f"{self.title}"
from django.urls import path
from rest_framework.routers import DefaultRouter
from courses.views import CourseViewSet
from courses.views import CourseViewSet, RegisteredActionViewSet
app_name = "courses-api"
router = DefaultRouter()
router.register("courses", CourseViewSet)
router.register("actions", RegisteredActionViewSet)
urlpatterns = router.urls
from rest_framework import serializers
from courses.models import Course
from courses.models import Course, RegisteredAction
class CourseSerializer(serializers.ModelSerializer):
......@@ -8,3 +8,9 @@ class CourseSerializer(serializers.ModelSerializer):
model = Course
fields = ["pk", "course_id", "course_slug", "course_name"]
extra_kwargs = {"course_id": {"validators": []}}
class RegisteredActionSerializer(serializers.ModelSerializer):
class Meta:
model = RegisteredAction
fields = ["pk", "course_id", "title", "date", "description"]
from rest_framework.permissions import IsAdminUser
from rest_framework.permissions import IsAdminUser, IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from courses.models import Course
from courses.serializers import CourseSerializer
from courses.models import Course, RegisteredAction
from courses.serializers import CourseSerializer, RegisteredActionSerializer
class CourseViewSet(ModelViewSet):
queryset = Course.objects.all()
serializer_class = CourseSerializer
permission_classes = [IsAdminUser]
class RegisteredActionViewSet(ModelViewSet):
queryset = RegisteredAction.objects.all()
serializer_class = RegisteredActionSerializer
permission_classes = [IsAuthenticated]
def get_queryset(self):
return super().get_queryset().filter(course__user=self.request.user)
......@@ -10,7 +10,7 @@ from rest_framework.test import APIClient
from users.models import User
from .courses.factories import CourseFactory
from .courses.factories import CourseFactory, RegisteredActionFactory
from .users.factories import AccessTokenFactory, UserFactory
register(UserFactory)
......@@ -25,6 +25,7 @@ register(
register(UserFactory, "_teacher", email="teacher@example.com", role=User.TEACHER)
register(UserFactory, "qdt", email="qdt@example.com", role=User.QDT)
register(CourseFactory)
register(RegisteredActionFactory)
@pytest.fixture
......
from datetime import date
import factory
from django.utils.crypto import get_random_string
from factory.django import DjangoModelFactory
......@@ -10,3 +12,13 @@ class CourseFactory(DjangoModelFactory):
course_id = factory.LazyFunction(get_random_string)
course_slug = "capstone-recommender-systems"
course_name = "Capstone Recommender Systems"
class RegisteredActionFactory(DjangoModelFactory):
class Meta:
model = "courses.RegisteredAction"
course = factory.SubFactory(CourseFactory)
title = "Updated quiz 1"
date = date(2017, 7, 16)
description = "Changed the second question in quiz 1 to make it clearer"
from datetime import date
import pytest
from courses.models import Course
from courses.models import Course, RegisteredAction
@pytest.mark.django_db
......@@ -25,3 +27,20 @@ def test_course_str(course):
assert (
str(course) == "Capstone Recommender Systems"
), "incorrect string representation"
@pytest.mark.django_db
def test_can_create_registered_action(course):
action = RegisteredAction.objects.create(
course=course,
title="Updated quiz 2",
date=date(2018, 4, 19),
description="Removed the last question from quiz 2",
)
assert action.pk is not None, "action has no pk"
RegisteredAction.objects.get(pk=action.pk)
@pytest.mark.django_db
def test_registered_action_str(registered_action):
assert str(registered_action) == "Updated quiz 1", "incorrect string representation"
import pytest
from courses.serializers import CourseSerializer
from courses.serializers import CourseSerializer, RegisteredActionSerializer
@pytest.mark.django_db
def test_can_serializer_course(course):
def test_can_serialize_course(course):
assert CourseSerializer(course).data == {
"pk": course.pk,
"course_id": course.course_id,
"course_slug": "capstone-recommender-systems",
"course_name": "Capstone Recommender Systems",
}
@pytest.mark.django_db
def test_can_serialize_registered_action(registered_action):
assert RegisteredActionSerializer(registered_action).data == {
"course_id": registered_action.course_id,
"date": "2017-07-16",
"description": "Changed the second question in quiz 1 to make it clearer",
"pk": registered_action.pk,
"title": "Updated quiz 1",
}
......@@ -14,3 +14,21 @@ def test_can_view_course(admin_api_client, course):
"course_slug": "capstone-recommender-systems",
"course_name": "Capstone Recommender Systems",
}, "view did not return correct data"
@pytest.mark.django_db
def test_can_view_registered_action(teacher, teacher_api_client, registered_action):
teacher.courses.add(registered_action.course)
response = teacher_api_client.get(
reverse(
"courses-api:registeredaction-detail", kwargs={"pk": registered_action.pk}
)
)
assert response.status_code == 200, "could not get registered_action detail"
assert response.data == {
"course_id": registered_action.course_id,
"date": "2017-07-16",
"description": "Changed the second question in quiz 1 to make it clearer",
"pk": registered_action.pk,
"title": "Updated quiz 1",
}, "view did not return correct data"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment