Skip to content

Commit 58823b3

Browse files
author
mattfreire
committed
assignment list and detail views added
1 parent 174ae28 commit 58823b3

File tree

20 files changed

+542
-4
lines changed

20 files changed

+542
-4
lines changed

api/__init__.py

Whitespace-only changes.

api/admin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.contrib import admin
2+
3+
from .models import Choice, Question, Assignment, GradedAssignment
4+
5+
admin.site.register(Choice)
6+
admin.site.register(Question)
7+
admin.site.register(Assignment)
8+
admin.site.register(GradedAssignment)

api/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class ApiConfig(AppConfig):
5+
name = 'api'

api/assignments/urls.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from rest_framework.routers import DefaultRouter
2+
from api.views import AssignmentViewSet
3+
4+
router = DefaultRouter()
5+
router.register(r'', AssignmentViewSet, base_name='assignments')
6+
urlpatterns = router.urls

api/graded_assignments/urls.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from rest_framework.routers import DefaultRouter
2+
# from .views import UserViewSet
3+
4+
router = DefaultRouter()
5+
# router.register(r'', UserViewSet, base_name='users')
6+
urlpatterns = router.urls

api/models.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from django.db import models
2+
from users.models import User
3+
4+
5+
class Assignment(models.Model):
6+
title = models.CharField(max_length=50)
7+
teacher = models.ForeignKey(User, on_delete=models.CASCADE)
8+
9+
def __str__(self):
10+
return self.title
11+
12+
13+
class GradedAssignment(models.Model):
14+
student = models.ForeignKey(User, on_delete=models.CASCADE)
15+
assignment = models.ForeignKey(
16+
Assignment, on_delete=models.SET_NULL, blank=True, null=True)
17+
grade = models.FloatField()
18+
19+
def __str__(self):
20+
return self.student.username
21+
22+
23+
class Choice(models.Model):
24+
title = models.CharField(max_length=50)
25+
26+
def __str__(self):
27+
return self.title
28+
29+
30+
class Question(models.Model):
31+
question = models.CharField(max_length=200)
32+
choices = models.ManyToManyField(Choice)
33+
answer = models.ForeignKey(
34+
Choice, on_delete=models.CASCADE, related_name='answer')
35+
assignment = models.ForeignKey(
36+
Assignment, on_delete=models.CASCADE, related_name='questions')
37+
order = models.SmallIntegerField()
38+
39+
def __str__(self):
40+
return self.question

api/serializers.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from rest_framework import serializers
2+
3+
from .models import Assignment, Question
4+
5+
6+
class StringSerializer(serializers.StringRelatedField):
7+
def to_internal_value(self, value):
8+
return value
9+
10+
11+
class QuestionSerializer(serializers.ModelSerializer):
12+
choices = StringSerializer(many=True)
13+
14+
class Meta:
15+
model = Question
16+
fields = ('id', 'choices', 'question', 'order')
17+
18+
19+
class AssignmentSerializer(serializers.ModelSerializer):
20+
questions = serializers.SerializerMethodField()
21+
teacher = StringSerializer(many=False)
22+
23+
class Meta:
24+
model = Assignment
25+
fields = ('__all__')
26+
27+
def get_questions(self, obj):
28+
questions = QuestionSerializer(obj.questions.all(), many=True).data
29+
return questions

api/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

api/views.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from rest_framework import viewsets
2+
3+
from .models import Assignment
4+
from .serializers import AssignmentSerializer
5+
6+
7+
class AssignmentViewSet(viewsets.ModelViewSet):
8+
serializer_class = AssignmentSerializer
9+
queryset = Assignment.objects.all()

home/settings/base.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'rest_framework',
2525
'rest_framework.authtoken',
2626

27+
'api',
2728
'users'
2829
]
2930

@@ -69,12 +70,10 @@
6970

7071
REST_FRAMEWORK = {
7172
'DEFAULT_PERMISSION_CLASSES': (
72-
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
73+
'rest_framework.permissions.IsAuthenticated',
7374
),
7475
'DEFAULT_AUTHENTICATION_CLASSES': (
7576
'rest_framework.authentication.TokenAuthentication',
76-
'rest_framework.authentication.SessionAuthentication',
77-
'rest_framework.authentication.BasicAuthentication',
7877
),
7978
}
8079

0 commit comments

Comments
 (0)