Commit a6934054 authored by Matheus Miranda's avatar Matheus Miranda
Browse files

Merge branch 'develop' into 'master'

add my notes download feature

See merge request !74
parents 997c0771 95160877
......@@ -3,10 +3,11 @@ 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 rest_framework.decorators import action
from docx import Document
from courses.models import Course, Unit
from courses.notes.models import Note
......@@ -49,6 +50,30 @@ class UserNotesViewSet(viewsets.ReadOnlyModelViewSet):
lookup_field = 'course'
permission_classes = (IsAuthenticated,)
@staticmethod
def get_user_notes_by_course(user):
"""
Return the user notes organized by course
"""
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)
return courses
def retrieve(self, request, *args, **kwargs):
user = self.request.user
if 'course' in self.kwargs:
......@@ -73,22 +98,7 @@ class UserNotesViewSet(viewsets.ReadOnlyModelViewSet):
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)
courses = self.get_user_notes_by_course(user)
results = []
for course in courses.values():
......@@ -97,3 +107,28 @@ class UserNotesViewSet(viewsets.ReadOnlyModelViewSet):
del course.lessons_dict
results.append(CourseNoteSerializer(course).data)
return Response(results)
@action(detail=False, methods=['get'])
def download(self, request):
courses = self.get_user_notes_by_course(self.request.user)
document = Document()
# TODO Translate this
document.add_heading('AEA Notes', 0)
for course in courses.values():
document.add_heading(course.name, level=1)
for lesson in course.lessons_dict.values():
document.add_heading(lesson.name, level=2)
for unit in lesson.units_notes:
document.add_heading(unit.title, level=3)
document.add_paragraph(unit.user_note.text)
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
response['Content-Disposition'] = 'attachment; filename=my-notes.docx'
document.save(response)
return response
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