Skip to content

Commit

Permalink
add rest interface and ansible app for questions app
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Apr 15, 2016
1 parent a40d6f4 commit c0e9444
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 1 deletion.
66 changes: 66 additions & 0 deletions apps/questions/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from rest_framework import serializers

from .models import *


class CatalogQuestionSerializer(serializers.ModelSerializer):

tag = serializers.SerializerMethodField()

class Meta:
model = Question
fields = ('id', 'text', 'tag')

def get_tag(self, obj):
return obj.attribute.tag


class CatalogQuestionEntitiesSerializer(serializers.ModelSerializer):

text = serializers.CharField(source='question.text', read_only=True)
questions = CatalogQuestionSerializer(source='questionset.questions', many=True, read_only=True)
tag = serializers.SerializerMethodField()

class Meta:
model = QuestionEntity
fields = ('id', 'title', 'text', 'is_set', 'questions', 'tag')

def get_tag(self, obj):
if obj.is_set:
return obj.questionset.attributeset.tag
else:
return obj.question.attribute.tag


class CatalogSubsectionSerializer(serializers.ModelSerializer):

entities = CatalogQuestionEntitiesSerializer(many=True, read_only=True)

class Meta:
model = Subsection
fields = ('id', 'title', 'entities')


class CatalogSectionSerializer(serializers.ModelSerializer):

subsections = CatalogSubsectionSerializer(many=True, read_only=True)

class Meta:
model = Section
fields = ('id', 'title', 'subsections')


class CatalogSerializer(serializers.ModelSerializer):

sections = CatalogSectionSerializer(many=True, read_only=True)

class Meta:
model = Catalog
fields = ('id', 'title', 'sections')


class CatalogListSerializer(serializers.ModelSerializer):

class Meta:
model = Catalog
fields = ('id', 'title')
57 changes: 57 additions & 0 deletions apps/questions/static/questions/js/questions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var app = angular.module('questions', ['select-by-number']);

// customizations for Django integration
app.config(['$httpProvider', '$interpolateProvider', function($httpProvider, $interpolateProvider) {
// use {$ and $} as tags for angular
$interpolateProvider.startSymbol('{$');
$interpolateProvider.endSymbol('$}');

// set Django's CSRF Cookie and Header
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
}]);

app.factory('QuestionsService', ['$http', '$timeout', function($http, $timeout) {

var catalogs_url = '/questions/api/catalogs/';

service = {
values: {},
errors: {},
catalogs: []
};

function fetchCatalogs() {
$http.get(catalogs_url).success(function(response) {
service.catalogs = response;
service.catalogId = response[0].id;
fetchCatalog();
});
}

function fetchCatalog() {
var url = catalogs_url + service.catalogId + '/';
$http.get(url)
.success(function(response) {
service.catalog = response;
});
}

service.init = function() {
fetchCatalogs();
};

service.changeCatalog = function() {
fetchCatalog();
};

return service;

}]);

app.controller('QuestionsController', ['$scope', 'QuestionsService', function($scope, QuestionsService) {

$scope.service = QuestionsService;
$scope.service.init();

}]);
154 changes: 154 additions & 0 deletions apps/questions/templates/questions/questions.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{% extends 'core/page.html' %}
{% load staticfiles %}
{% load compress %}
{% load i18n %}
{% load core_tags %}

{% block head %}
<script type="text/javascript" src="{% static 'angular/angular.min.js' %}" ></script>
<script type="text/javascript" src="{% static 'core/js/select-by-number.js' %}" ></script>
<script type="text/javascript" src="{% static 'questions/js/questions.js' %}" ></script>
{% endblock %}

{% block bodyattr %} ng-app="questions" ng-controller="QuestionsController" {% endblock %}

{% block sidebar %}

<h3>{% trans 'Catalog' %}</h3>

<select class="form-control" ng-model="service.catalogId" ng-change="service.changeCatalog()" by-number>
<option ng-repeat="catalog in service.catalogs" value="{$ catalog.id $}">{$ catalog.title $}</option>
</select>

<h3>{% trans 'Filter' %}</h3>

<div class="input-group">
<input type="text" class="form-control" ng-model="service.filter"></input>
<span class="input-group-btn" ng-click="service.filter = ''">
<button class="btn btn-default">
<span class="glyphicon glyphicon-remove"></span>
</button>
</span>
</div>

<h3>{% trans 'Options' %}</h3>

<ul class="list-unstyled">
<li>
<a href="" ng-click="service.openModal('catalog','update')">
{% trans 'Update catalog details' %}
</a>
</li>
<li>
<a href="" ng-click="service.openModal('catalog','delete')">
{% trans 'Delete catalog' %}
</a>
</li>
</ul>

<ul class="list-unstyled">
<li>
<a href="" ng-click="service.openModal('catalog','create')">
{% trans 'Create new catalog' %}
</a>
</li>
<li>
<a href="" ng-click="service.openModal('section','create')">
{% trans 'Create new section' %}
</a>
</li>
<li>
<a href="" ng-click="service.openModal('subsection','create')">
{% trans 'Create new subsection' %}
</a>
</li>
<li>
<a href="" ng-click="service.openModal('questionset','create')">
{% trans 'Create new questionset' %}
</a>
</li>
<li>
<a href="" ng-click="service.openModal('question','create')">
{% trans 'Create new question' %}
</a>
</li>
</ul>

{% endblock %}

{% block page %}

<h2>{% trans 'Questions' %}</h2>

<h3>{$ service.catalog.title $}</h3>

<div ng-repeat="section in service.catalog.sections">

<h4>{$ section.title $}</h4>

<div ng-repeat="subsection in section.subsections">

<h5>{$ subsection.title $}</h5>

<div ng-repeat="entity in subsection.entities">

<div class="panel panel-default" ng-show="entity.is_set">

<div class="panel-heading">
<strong>{% trans 'Question set' %}</strong> {$ entity.title $}
<em ng-show="entity.is_collection">collection</em>
<div class="pull-right">
<code>{$ entity.tag $}</code>
<a href="" class="small glyphicon glyphicon-plus"
ng-show="entity.is_set"
ng-click="service.openModal('question', 'create', entity)"></a>
<a href="" class="small glyphicon glyphicon-pencil"
ng-click="service.openModal('questionset', 'update', entity)"></a>
<a href="" class="small glyphicon glyphicon-trash"
ng-click="service.openModal('questionset', 'delete', entity)"></a>
</div>
</div>

<ul class="list-group" ng-show="entity.questions">
<li class="list-group-item" ng-repeat="question in entity.questions">
<span>{$ question.text $}</span>
<em ng-show="question.is_collection">collection</em>
<div class="pull-right">
<code>{$ question.tag $}</code>
<a href="" class="small glyphicon glyphicon-pencil"
ng-click="service.openModal('update', question, entity)"></a>
<a href="" class="small glyphicon glyphicon-trash"
ng-click="service.openModal('delete', question, entity)"></a>
</div>
</li>
</ul>

</div>

<div class="panel panel-default" ng-hide="entity.is_set">

<div class="panel-heading">
<strong>{% trans 'Question' %}</strong> {$ entity.title $}
<em ng-show="entity.is_collection">collection</em>
<div class="pull-right">
<code>{$ entity.tag $}</code>
<a href="" class="small glyphicon glyphicon-pencil"
ng-click="service.openModal('question', 'update', entity)"></a>
<a href="" class="small glyphicon glyphicon-trash"
ng-click="service.openModal('question', 'delete', entity)"></a>
</div>
</div>

<ul class="list-group">
<li class="list-group-item">
<span>{$ entity.text $}</span>
<code class="pull-right">{$ entity.tag $}</code>
</li>
</ul>

</div>
</div>
</div>
</div>

{% endblock %}
12 changes: 11 additions & 1 deletion apps/questions/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from django.conf.urls import url
from django.conf.urls import url, include
from django.utils.translation import ugettext_lazy as _

from rest_framework import routers

from .views import *

router = routers.DefaultRouter()
router.register(r'catalogs', CatalogViewSet, base_name='catalog')

urlpatterns = [

url(_(r'^catalogs/$'), catalogs, name='catalogs'),
url(_(r'^catalogs/(?P<pk>[0-9]+)/$'), catalog, name='catalog'),

Expand Down Expand Up @@ -31,4 +37,8 @@
url(_(r'^questionsets/(?P<pk>[0-9]+)/update$'), QuestionSetUpdateView.as_view(), name='questionset_update'),
url(_(r'^questionsets/(?P<pk>[0-9]+)/delete$'), QuestionSetDeleteView.as_view(), name='questionset_delete'),
url(_(r'^questionsets/(?P<pk>[0-9]+)/create-question/$'), QuestionSetCreateQuestionView.as_view(), name='questionset_create_question'),


url(r'^$', questions, name='questions'),
url(r'^api/', include(router.urls)),
]
18 changes: 18 additions & 0 deletions apps/questions/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import render, get_object_or_404

from rest_framework import viewsets

from apps.core.views import ProtectedCreateView, ProtectedUpdateView, ProtectedDeleteView

from .models import *
from .serializers import *


def catalogs(request):
Expand Down Expand Up @@ -164,3 +167,18 @@ def form_valid(self, form):
form.instance.subsection = self.questionset.subsection
form.instance.questionset = self.questionset
return super(QuestionSetCreateQuestionView, self).form_valid(form)


def questions(request):
return render(request, 'questions/questions.html')


class CatalogViewSet(viewsets.ModelViewSet):

queryset = Catalog.objects.all()

def get_serializer_class(self):
if self.action == 'list':
return CatalogListSerializer
else:
return CatalogSerializer

0 comments on commit c0e9444

Please sign in to comment.