1 # -*- coding: utf-8 -*-
4 from django.contrib.sites.models import Site
5 from django.core.urlresolvers import reverse
6 from django.utils.functional import lazy
7 from piston.handler import BaseHandler
8 from piston.utils import rc
10 from catalogue.forms import LessonImportForm
11 from catalogue.models import Lesson
13 API_BASE = EDUMED_BASE = MEDIA_BASE = lazy(
14 lambda: u'https://' + Site.objects.get_current().domain, unicode)()
17 class LessonDetails(object):
18 """Custom fields used for representing Lessons."""
21 def href(cls, lesson):
22 """ Returns an URI for a Lesson in the API. """
23 return API_BASE + reverse("api_lesson", args=[lesson.slug])
27 """ Returns Lesson's URL on the site. """
28 return EDUMED_BASE + lesson.get_absolute_url()
31 class LessonDetailHandler(BaseHandler, LessonDetails):
32 """ Main handler for Lesson objects.
34 Responsible for single Lesson details.
36 allowed_methods = ['GET']
37 fields = ['title', 'url']
39 def read(self, request, lesson):
40 """ Returns details of a lesson, identified by a slug. """
42 return Lesson.objects.get(slug=lesson)
43 except Lesson.DoesNotExist:
47 class LessonsHandler(LessonDetailHandler):
48 allowed_methods = ('GET', 'POST')
50 fields = ['href', 'title', 'url']
52 def create(self, request, *args, **kwargs):
53 if not request.user.has_perm('catalogue.add_lesson'):
56 data = json.loads(request.POST.get('data'))
57 form = LessonImportForm(data)