diff --git a/generic/urls.py b/generic/urls.py index 75cc2b83..5e782177 100755 --- a/generic/urls.py +++ b/generic/urls.py @@ -1,6 +1,13 @@ from django.urls import path, include from . import views +from rapids import views as rapid_views + +from rest_framework import routers + +router = routers.DefaultRouter() +router.register(r'rapids/exams', rapid_views.ExamViewSet, basename="rapid-exams") + app_name = "generic" urlpatterns = [ @@ -11,7 +18,11 @@ urlpatterns = [ path("examination/ajax/get_examination_id", views.get_examination_id, name="get_examination_id"), - path("exam_json_edit", + path("generic_exam_json_edit", views.generic_exam_json_edit, name="generic_exam_json_edit"), + + + path('api/', include(router.urls)), + path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) ] diff --git a/generic/views.py b/generic/views.py index b6cc64f5..9f156fe2 100644 --- a/generic/views.py +++ b/generic/views.py @@ -85,7 +85,9 @@ def generic_exam_json_edit(request): question_ids = json.loads(request.POST.get("add_exam_questions")) question_objects = Question.objects.filter(pk__in=question_ids) - exam.exam_questions.add(question_objects) + for i in question_ids: + exam.exam_questions.add(i) + #exam.exam_questions.add(question_objects) exam.save() data = {"status": "success"} return JsonResponse(data, status=200) diff --git a/rad/settings.py b/rad/settings.py index fcd84822..3c427c03 100644 --- a/rad/settings.py +++ b/rad/settings.py @@ -51,6 +51,7 @@ INSTALLED_APPS = [ 'debug_toolbar', "tagulous", "dbbackup", + "rest_framework", ] MIDDLEWARE = [ diff --git a/rad/urls.py b/rad/urls.py index 8bda858b..3425925e 100644 --- a/rad/urls.py +++ b/rad/urls.py @@ -38,6 +38,7 @@ urlpatterns = [ # Global url that registers RTS compatible exams path("exam/json/", views.active_exams, name="active_exams"), path("exam/submit", views.exam_submit, name="global_exam_answers_submit"), + #path('', include('generic.urls')), ] diff --git a/rapids/forms.py b/rapids/forms.py index 617e5816..ee17e8ca 100755 --- a/rapids/forms.py +++ b/rapids/forms.py @@ -70,6 +70,8 @@ class RapidCreationDefaultForm(ModelForm): class RapidForm(ModelForm): + + exams = ModelMultipleChoiceField(required=False, queryset=Exam.objects.all()) class Media: # Django also includes a few javascript files necessary # for the operation of this form element. You need to @@ -106,6 +108,39 @@ class RapidForm(ModelForm): choices=Rapid.LATERALITY_CHOICES, required=True, widget=RadioSelect() ) + if kwargs.get("instance"): + # We get the 'initial' keyword argument or initialize it + # as a dict if it didn't exist. + initial = kwargs.setdefault("initial", {}) + # The widget for a ModelMultipleChoiceField expects + # a list of primary key for the selected data. + initial["exams"] = [t.pk for t in kwargs["instance"].exams.all()] + + ModelForm.__init__(self, *args, **kwargs) + + def save(self, commit=True): + # Get the unsaved Pizza instance + instance = ModelForm.save(self, False) + + # Prepare a 'save_m2m' method for the form, + old_save_m2m = self.save_m2m + + def save_m2m(): + old_save_m2m() + # This is where we actually link the pizza with toppings + instance.exams.clear() + for exam in self.cleaned_data["exams"]: + instance.exams.add(exam) + + self.save_m2m = save_m2m + + # Do we need to save all changes now? + # if commit: + instance.save() + self.save_m2m() + + return instance + class Meta: model = Rapid # fields = ['due_back'] diff --git a/rapids/serializers.py b/rapids/serializers.py new file mode 100644 index 00000000..5e5585fa --- /dev/null +++ b/rapids/serializers.py @@ -0,0 +1,11 @@ +# serializers.py +from rest_framework import serializers, permissions + + +from .models import Exam + +class ExamSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Exam + fields = ('name', 'id', 'active', "publish_results") + permission_classes = [permissions.IsAuthenticated] diff --git a/rapids/templates/rapids/question_display_block.html b/rapids/templates/rapids/question_display_block.html index 03dde6a1..3baa3ad1 100755 --- a/rapids/templates/rapids/question_display_block.html +++ b/rapids/templates/rapids/question_display_block.html @@ -16,6 +16,11 @@ {% endfor %} +
+ Exams: {% for exam in question.exams.all %} + {{ exam.name }}, + {% endfor %} +

Feedback: {{ question.feedback }}

Author(s): {% for author in question.author.all %} {{author}}, {% endfor %}

diff --git a/rapids/templates/rapids/view.html b/rapids/templates/rapids/view.html index 91e9becf..b86daa17 100755 --- a/rapids/templates/rapids/view.html +++ b/rapids/templates/rapids/view.html @@ -8,44 +8,78 @@

Filter Rapids

-
- {{ filter.form }} - -
+
+ {{ filter.form }} + +
{% render_table table %} - + +