#!/usr/bin/env python
# -*- coding:utf8 -*-
# Copyright (C) 2014 FinalsClub Foundation
-from itertools import chain
import json
-from django.core.exceptions import ObjectDoesNotExist
-from django.core.urlresolvers import reverse
-from django.forms.formsets import formset_factory
-from django.http import HttpResponseRedirect, HttpResponseBadRequest, HttpResponseNotFound, HttpResponse
-
-from django.views.generic import DetailView, FormView
-from django.views.generic.detail import SingleObjectMixin
-from django.views.generic.edit import FormMixin, ProcessFormView
-from karmaworld.apps.notes.models import Note
-from karmaworld.apps.quizzes.forms import KeywordForm
-from karmaworld.apps.quizzes.models import Quiz, ALL_QUESTION_CLASSES, Keyword, BaseQuizQuestion, \
- ALL_QUESTION_CLASSES_NAMES, MultipleChoiceQuestion, MultipleChoiceOption, TrueFalseQuestion, FlashCardQuestion
-from karmaworld.utils import ajax_pk_base, ajax_base
-
-
-class QuizView(DetailView):
- queryset = Quiz.objects.all()
- model = Quiz
- context_object_name = 'quiz' # name passed to template
- template_name = 'quizzes/quiz.html'
-
- def get_context_data(self, **kwargs):
- all_questions = []
- for cls in ALL_QUESTION_CLASSES:
- all_questions.append(
- [(cls.__name__, o) for o in cls.objects.filter(quiz=self.object)]
- )
-
- result_list = sorted(chain.from_iterable(all_questions),
- key=lambda o: o[1].timestamp,
- reverse=True)
-
- kwargs['questions'] = result_list
- kwargs['quiz'] = self.object
-
- return super(QuizView, self).get_context_data(**kwargs)
-
-
-class KeywordEditView(FormView):
- template_name = 'quizzes/keyword_edit.html'
- form_class = formset_factory(KeywordForm)
-
- def get(self, requests, *args, **kwargs):
- self.lookup_note()
- return super(KeywordEditView, self).get(requests, *args, **kwargs)
-
- def post(self, requests, *args, **kwargs):
- self.lookup_note()
- return super(KeywordEditView, self).post(requests, *args, **kwargs)
-
- def lookup_note(self):
- self.note = Note.objects.get(slug=self.kwargs['slug'])
-
- def get_success_url(self):
- return reverse('keyword_edit', args=(self.note.course.school.slug, self.note.course.slug, self.note.slug))
-
- def get_initial(self):
- existing_keywords = self.note.keyword_set.order_by('id')
- initial_data = [{'keyword': keyword.word, 'definition': keyword.definition, 'id': keyword.pk}
- for keyword in existing_keywords]
- return initial_data
-
- def get_context_data(self, **kwargs):
- kwargs['note'] = self.note
- kwargs['prototype_form'] = KeywordForm
- return super(KeywordEditView, self).get_context_data(**kwargs)
-
- def form_valid(self, formset):
- for form in formset:
- word = form['keyword'].data
- definition = form['definition'].data
- id = form['id'].data
- if word == '':
- continue
- try:
- keyword_object = Keyword.objects.get(id=id)
- except (ValueError, ObjectDoesNotExist):
- keyword_object = Keyword()
-
- keyword_object.note = self.note
- keyword_object.word = word
- keyword_object.definition = definition
- keyword_object.save()
-
- return super(KeywordEditView, self).form_valid(formset)
-
-
-def quiz_answer(request):
- """Handle an AJAX request checking if a quiz answer is correct"""
- if not (request.method == 'POST' and request.is_ajax()):
- # return that the api call failed
- return HttpResponseBadRequest(json.dumps({'status': 'fail', 'message': 'must be a POST ajax request'}),
- mimetype="application/json")
-
- try:
- question_type_str = request.POST['question_type']
- if question_type_str not in ALL_QUESTION_CLASSES_NAMES:
- raise Exception("Not a valid question type")
- question_type_class = ALL_QUESTION_CLASSES_NAMES[question_type_str]
- question = question_type_class.objects.get(id=request.POST['id'])
-
- correct = False
-
- if question_type_class is MultipleChoiceQuestion:
- answer = MultipleChoiceOption.objects.get(id=request.POST['answer'])
- if answer.question == question and answer.correct:
- correct = True
- elif question_type_class is TrueFalseQuestion:
- answer = request.POST['answer'] == 'true'
- correct = question.true == answer
-
- elif question_type_class is FlashCardQuestion:
- answer = request.POST['answer'].lower()
- correct = question.keyword_side.lower() == answer
-
- except Exception, e:
- return HttpResponseBadRequest(json.dumps({'status': 'fail',
- 'message': e.message,
- 'exception': e.__class__.__name__}),
- mimetype="application/json")
-
- return HttpResponse(json.dumps({'correct': correct}), mimetype="application/json")
+from django.core.exceptions import ObjectDoesNotExist
+from django.http import HttpResponseNotFound, HttpResponse, HttpResponseForbidden
+from karmaworld.apps.quizzes.models import Keyword
+from karmaworld.utils import ajax_base
def set_keyword(annotation_uri, keyword, definition, ranges):
from django.conf.urls import patterns, include, url
from django.views.generic.base import TemplateView
-from karmaworld.apps.courses.models import Course
from karmaworld.apps.courses.views import AboutView, flag_course, edit_course
from karmaworld.apps.courses.views import CourseDetailView
from karmaworld.apps.courses.views import CourseListView
NoteKeywordsView, edit_note, NoteDeleteView
from karmaworld.apps.moderation import moderator
from karmaworld.apps.document_upload.views import save_fp_upload
-from karmaworld.apps.quizzes.views import QuizView, KeywordEditView, quiz_answer, get_keywords_annotator, \
- set_delete_keyword_annotator, get_keywords_datatables
+from karmaworld.apps.quizzes.views import set_delete_keyword_annotator, get_keywords_annotator
from karmaworld.apps.users.views import ProfileView
-from ajax_select import urls as ajax_select_urls
-
# See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#hooking-adminsite-instances-into-your-urlconf
admin.autodiscover()
NoteView.as_view(), name='note_detail_pk'),
"""
-# See: https://docs.djangoproject.com/en/dev/topics/http/urls/
urlpatterns = patterns('',
## Administrative URLpatterns
# Admin panel and documentation:
url(r'^ajax/course/edit/(?P<pk>[\d]+)/$', edit_course, name='edit_course'),
url(r'^ajax/note/edit/(?P<pk>[\d]+)/$', edit_note, name='edit_note'),
- # Check if a quiz answer is correct
- url(r'^ajax/quiz/check/$', quiz_answer, name='quiz_answer'),
-
url(r'^ajax/annotations/annotations$', set_delete_keyword_annotator, name='set_keyword'),
url(r'^ajax/annotations/search/$', get_keywords_annotator, name='get_keywords'),
url(r'note/delete/$', NoteDeleteView.as_view(), name='note_delete'),
- # Quizzes
- url(r'^quiz/(?P<pk>[\d]+)/$',
- QuizView.as_view(), name='quiz'),
- url(r'^keywords/' + SLUG.format('') + '/$',
- KeywordEditView.as_view(), name='keyword_edit'),
-
url(r'^$', CourseListView.as_view(), name='home'),
)