--- /dev/null
+# -*- coding: utf-8 -*-
+import json
+
+from django.contrib.sites.models import Site
+from django.core.urlresolvers import reverse
+from django.utils.functional import lazy
+from piston.handler import BaseHandler
+from piston.utils import rc
+
+from catalogue.forms import LessonImportForm
+from catalogue.models import Lesson
+
+API_BASE = EDUMED_BASE = MEDIA_BASE = lazy(
+ lambda: u'https://' + Site.objects.get_current().domain, unicode)()
+
+
+class LessonDetails(object):
+ """Custom fields used for representing Lessons."""
+
+ @classmethod
+ def href(cls, lesson):
+ """ Returns an URI for a Lesson in the API. """
+ return API_BASE + reverse("api_lesson", args=[lesson.slug])
+
+ @classmethod
+ def url(cls, lesson):
+ """ Returns Lesson's URL on the site. """
+ return EDUMED_BASE + lesson.get_absolute_url()
+
+
+class LessonDetailHandler(BaseHandler, LessonDetails):
+ """ Main handler for Lesson objects.
+
+ Responsible for single Lesson details.
+ """
+ allowed_methods = ['GET']
+ fields = ['title', 'url']
+
+ def read(self, request, lesson):
+ """ Returns details of a lesson, identified by a slug. """
+ try:
+ return Lesson.objects.get(slug=lesson)
+ except Lesson.DoesNotExist:
+ return rc.NOT_FOUND
+
+
+class LessonsHandler(LessonDetailHandler):
+ allowed_methods = ('GET', 'POST')
+ model = Lesson
+ fields = ['href', 'title', 'url']
+
+ def create(self, request, *args, **kwargs):
+ if not request.user.has_perm('catalogue.add_lesson'):
+ return rc.FORBIDDEN
+
+ data = json.loads(request.POST.get('data'))
+ form = LessonImportForm(data)
+ if form.is_valid():
+ form.save()
+ return rc.CREATED
+ else:
+ return rc.NOT_FOUND
--- /dev/null
+# -*- coding: utf-8 -*-
+from piston.resource import Resource
+
+
+class CsrfExemptResource(Resource):
+ """A Custom Resource that is csrf exempt"""
+ def __init__(self, handler, authentication=None):
+ super(CsrfExemptResource, self).__init__(handler, authentication)
+ self.csrf_exempt = getattr(self.handler, 'csrf_exempt', True)
\ No newline at end of file
--- /dev/null
+# -*- coding: utf-8 -*-
+from django.conf.urls import patterns, url
+from django.views.decorators.csrf import csrf_exempt
+from piston.authentication import OAuthAuthentication, oauth_access_token
+from piston.resource import Resource
+
+from api import handlers
+from api.helpers import CsrfExemptResource
+
+auth = OAuthAuthentication(realm="Edukacja Medialna")
+
+lesson_list_resource = CsrfExemptResource(handler=handlers.LessonsHandler, authentication=auth)
+lesson_resource = Resource(handler=handlers.LessonDetailHandler)
+
+urlpatterns = patterns(
+ 'piston.authentication',
+ url(r'^oauth/request_token/$', 'oauth_request_token'),
+ url(r'^oauth/authorize/$', 'oauth_user_auth'),
+ url(r'^oauth/access_token/$', csrf_exempt(oauth_access_token)),
+)
+
+urlpatterns += patterns(
+ '',
+ # url(r'^$', TemplateView.as_view(template_name='api/main.html'), name='api'),
+
+ # objects details
+ url(r'^lessons/(?P<lesson>[a-z0-9-]+)/$', lesson_resource, name="api_lesson"),
+
+ # lessons
+ url(r'^lessons/$', lesson_list_resource, name='api_lesson_list'),
+)
--- /dev/null
+# -*- coding: utf-8 -*-
+from django.forms import Form, FileField, CharField, ValidationError
+
+from catalogue.models import Lesson
+
+
+class LessonImportForm(Form):
+ lesson_xml_file = FileField(required=False)
+ lesson_xml = CharField(required=False)
+
+ def clean(self):
+ from django.core.files.base import ContentFile
+
+ if not self.cleaned_data['lesson_xml_file']:
+ if self.cleaned_data['lesson_xml']:
+ self.cleaned_data['lesson_xml_file'] = \
+ ContentFile(self.cleaned_data['lesson_xml'].encode('utf-8'))
+ else:
+ raise ValidationError(u"Proszę dostarczyć XML.")
+ return super(LessonImportForm, self).clean()
+
+ def save(self, commit=True, **kwargs):
+ return Lesson.publish(self.cleaned_data['book_xml_file'])
'django_libravatar',
'sorl.thumbnail',
'subdomains',
+ 'piston',
'django.contrib.auth',
'django.contrib.contenttypes',
url(r'^forum/', include('pybb.urls', namespace='pybb')),
url(r'^kompetencje/', include('curriculum.urls')),
url(r'^wtem/', include('wtem.urls')),
+ url(r'^api/', include('api.urls')),
)
python-memcached
django-piwik
django-extensions
+django-piston==0.2.2.1.2
#pyScss
#git+git://github.com/Kronuz/pyScss.git@d8f4da23a3c87696a75b3830ed4ab49b75550a93#egg=pyScss
#TODO: pyScss support, for now just install sass