Add competence listing functionality

This commit is contained in:
Gergely Polonkai 2015-01-05 16:13:08 +01:00 committed by Gergely Polonkai
parent 2a53041702
commit d2e56db0d9
7 changed files with 79 additions and 11 deletions

View File

@ -1,5 +1,5 @@
[ [
{% for comp in duck.duckcompetence_set.all %} {% for comp in comp_list %}
{ {
"name": "{{ comp.comp.name }}", "name": "{{ comp.comp.name }}",
"level": {{ comp.level }} "level": {{ comp.level }}

View File

@ -25,7 +25,10 @@ class ApiTest(TestCase):
user = User() user = User()
user.save() user.save()
self.duck = Duck(pk = 1, species = species, location = loc, donated_by = user) self.duck = Duck(
species = species,
location = loc,
donated_by = user)
self.duck.save() self.duck.save()
comp = Competence(name = 'test', added_by = user) comp = Competence(name = 'test', added_by = user)
@ -38,4 +41,4 @@ class ApiTest(TestCase):
response = self.client.get('/api/duck/1/competence.json') response = self.client.get('/api/duck/1/competence.json')
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context['duck'].duckcompetence_set.all()), 1) self.assertEqual(len(response.context['comp_list']), 1)

View File

@ -7,5 +7,5 @@ from . import views
urlpatterns = patterns( urlpatterns = patterns(
'', '',
url(r'^reverse.js$', cache_page(3600)(urls_js), name = 'js_reverse'), url(r'^reverse.js$', cache_page(3600)(urls_js), name = 'js_reverse'),
url(r'^duck/(?P<duck_id>\d+)/competence.json$', views.duck_comp_list) url(r'^duck/(?P<duck_id>\d+)/competence.json$', views.DuckCompListView.as_view(), name = 'complist'),
) )

View File

@ -1,12 +1,14 @@
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from django.views import generic
from booking.models import Duck from booking.models import Duck
def duck_comp_list(request, duck_id): class DuckCompListView(generic.ListView):
duck = get_object_or_404(Duck, pk = duck_id) template_name = 'api/duck_comp_list.json'
context_object_name = 'comp_list'
context = { def get_queryset(self):
'duck': duck duck_id = self.kwargs['duck_id']
} duck = get_object_or_404(Duck, pk = duck_id)
return render(request, 'api/duck_comp_list.json', context) return duck.duckcompetence_set.all()

View File

@ -0,0 +1,11 @@
.button {
background-color: #aaa;
display: inline;
padding: .2em;
margin: .1em;
cursor: pointer;
}
.no-close .ui-dialog-titlebar-close {
display: none;
}

View File

@ -13,7 +13,7 @@
Location: {{ duck.location }}<br> Location: {{ duck.location }}<br>
DPX: {{ duck.dpx }}<br> DPX: {{ duck.dpx }}<br>
<div class="button" id="duck-book-{{ duck.id }}">book-button</div> <div class="button" id="duck-book-{{ duck.id }}">book-button</div>
<div class="button" id="duck-complist-{{ duck.id }}">complist-button</div> <div class="button complist-button" id="duck-complist-{{ duck.id }}">Competence list</div>
<div class="button" id="duck-namesugg-{{ duck.id }}">namesugg-button</div> <div class="button" id="duck-namesugg-{{ duck.id }}">namesugg-button</div>
<div class="button" id="duck-adopt-{{ duck.id }}">adopt-button</div> <div class="button" id="duck-adopt-{{ duck.id }}">adopt-button</div>
</div> </div>
@ -21,3 +21,46 @@
<br class="clear"> <br class="clear">
{% endif %} {% endif %}
{% endblock %} {% endblock %}
{% block endscript %}
$('.complist-button').click(function() {
duck_id = $(this).attr('id').replace(/^duck-complist-/, '');
if (isNaN(duck_id) || (duck_id.trim() == '')) {
return;
}
var url = Urls['api:complist'](duck_id);
var dialog = $('<div style="display: none;"></div>').appendTo('body');
$.getJSON(url, function(data) {
var items = [];
$.each(data, function(key, item) {
items.push('<li class="competence-' + item.level + '">' + item.name + ' (' + item.level + ')</li>');
});
$('<ul/>', {
'class': 'complist',
'html': items.join('')
}).appendTo(dialog);
dialog.dialog({
title: 'Competence list',
dialogClass: 'no-close',
buttons: [
{
text: 'Close',
click: function() {
$(this).dialog('close');
}
}
],
close: function(event, ui) {
dialog.remove();
},
modal: true
});
});
});
{% endblock %}

View File

@ -1,9 +1,15 @@
{% load staticfiles %}
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>Rubber Duck Booking Tool</title> <title>Rubber Duck Booking Tool</title>
<link rel="stylesheet" type="text/css" href="{% static 'booking.css' %}">
<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.11.2/themes/ui-lightness/jquery-ui.css">
<script src="{% url 'api:js_reverse' %}" type="text/javascript"></script> <script src="{% url 'api:js_reverse' %}" type="text/javascript"></script>
<script src="//code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script>
</head> </head>
<body> <body>
<h1>Rubber Duck Booking Tool</h1> <h1>Rubber Duck Booking Tool</h1>
@ -22,5 +28,8 @@
<a href="{% url 'booking:vocabulary' %}">Vocabulary</a> <a href="{% url 'booking:vocabulary' %}">Vocabulary</a>
<a href="{% url 'booking:disclaimer' %}">Disclaimer</a> <a href="{% url 'booking:disclaimer' %}">Disclaimer</a>
{% block body %}{% endblock %} {% block body %}{% endblock %}
<script type="text/javascript">
{% block endscript %}{% endblock %}
</script>
</body> </body>
</html> </html>