Commit 34e4dd6d authored by Bruno Martin's avatar Bruno Martin
Browse files

move viewsets and serializers from legacy to

course.note app
parent 97ee60c9
from rest_framework import serializers
from .models import Note
from courses.models import Unit, Lesson, Course
class NoteSerializer(serializers.ModelSerializer):
class Meta:
model = Note
fields = ('id', 'text', 'content_type', 'object_id')
fields = (
'id',
'text',
'content_type',
'object_id',
)
class UnitNoteSerializer(serializers.ModelSerializer):
user_note = NoteSerializer()
class Meta:
model = Unit
fields = (
'id',
'title',
'video',
'position',
'user_note',
)
class LessonNoteSerializer(serializers.ModelSerializer):
units_notes = UnitNoteSerializer(many=True)
course = serializers.SlugRelatedField(slug_field='slug', read_only=True)
class Meta:
model = Lesson
fields = (
'id',
'name',
'position',
'slug',
'course',
'units_notes',
)
class CourseNoteSerializer(serializers.ModelSerializer):
lessons_notes = LessonNoteSerializer(many=True)
course_notes_number = serializers.IntegerField(required=False)
class Meta:
model = Course
fields = (
'id',
'slug',
'name',
'lessons_notes',
'course_notes_number',
)
# -*- coding: utf-8 -*-
from braces.views import LoginRequiredMixin
from django.shortcuts import get_object_or_404
from django.views.generic.base import TemplateView
from notes.models import Note
from notes.serializers import NoteSerializer
from core.models import Course
from django.http import HttpResponse
from django.contrib.contenttypes.models import ContentType
from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from courses.models import Course, Unit
from courses.notes.models import Note
from courses.notes.serializers import (
NoteSerializer,
UnitNoteSerializer,
LessonNoteSerializer,
CourseNoteSerializer,
)
class NotesViewSet(LoginRequiredMixin, viewsets.ModelViewSet):
class NotesViewSet(viewsets.ModelViewSet):
model = Note
serializer_class = NoteSerializer
filter_fields = ('content_type', 'object_id')
permission_classes = (IsAuthenticated,)
def perform_create(self, serializer):
# Only create a new note if there isn't one already for this user in this unit
......@@ -31,15 +42,58 @@ class NotesViewSet(LoginRequiredMixin, viewsets.ModelViewSet):
return Note.objects.filter(user=user)
class UserNotesView(LoginRequiredMixin, TemplateView):
template_name = 'notes.html'
class UserNotesViewSet(viewsets.ReadOnlyModelViewSet):
model = Course
queryset = Course.objects.all()
lookup_field = 'course'
permission_classes = (IsAuthenticated,)
def retrieve(self, request, *args, **kwargs):
user = self.request.user
if 'course' in self.kwargs:
course = get_object_or_404(Course, slug=self.kwargs['course'])
units = Unit.objects.filter(lesson__course=course, notes__user=user).exclude(notes__isnull=True)
lessons_dict = {}
for unit in units:
lesson = unit.lesson
if lesson.slug not in lessons_dict:
lessons_dict[lesson.slug] = lesson
lessons_dict[lesson.slug].units_notes = []
unit_type = ContentType.objects.get_for_model(unit)
note = get_object_or_404(Note, user=user, content_type__pk=unit_type.id, object_id=unit.id)
unit.user_note = note
lessons_dict[lesson.slug].units_notes.append(unit)
class CourseNotesView(LoginRequiredMixin, TemplateView):
template_name = 'course-notes.html'
results = []
for lesson in lessons_dict.values():
results.append(LessonNoteSerializer(lesson).data)
return Response(results)
def list(self, request, *args, **kwargs):
user = self.request.user
units = Unit.objects.filter(notes__user=user).exclude(notes__isnull=True)
courses = {}
for unit in units:
course = unit.lesson.course
lesson = unit.lesson
if course.slug not in courses:
courses[course.slug] = course
courses[course.slug].lessons_dict = {}
if lesson.slug not\
in courses[course.slug].lessons_dict:
courses[course.slug].lessons_dict[lesson.slug] = lesson
courses[course.slug].lessons_dict[lesson.slug].units_notes = []
unit_type = ContentType.objects.get_for_model(unit)
note = get_object_or_404(Note, user=user, content_type__pk=unit_type.id, object_id=unit.id)
unit.user_note = note
courses[course.slug].lessons_dict[lesson.slug].units_notes.append(unit)
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super(CourseNotesView, self).get_context_data(**kwargs)
context['course'] = get_object_or_404(Course, slug=self.kwargs['course_slug'])
return context
results = []
for course in courses.values():
course.lessons_notes = course.lessons_dict.values()
course.course_notes_number = Unit.objects.filter(lesson__course=course, notes__user=user).exclude(notes__isnull=True).count()
del course.lessons_dict
results.append(CourseNoteSerializer(course).data)
return Response(results)
......@@ -43,7 +43,10 @@ from courses.stats.views import (
UserActivitiesViewSet,
UserActionsViewSet
)
from courses.notes.views import (
NotesViewSet,
UserNotesViewSet,
)
router = routers.SimpleRouter(trailing_slash=False)
router.register(r'course', CourseViewSet, base_name='course')
......@@ -87,6 +90,10 @@ router.register(r'event', EventViewSet, base_name='event')
router.register(r'user-activities', UserActivitiesViewSet, base_name='user-activities')
router.register(r'actions-user', UserActionsViewSet, base_name='actions-user')
# Notes
router.register(r'note', NotesViewSet, base_name='note')
router.register(r'user_notes', UserNotesViewSet, base_name='user_notes')
app_name = 'courses'
urlpatterns = [
# Stats (Activity Stream related functionality)
......
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