From d2f3f3029c6463c5f9ddc8b14243db7810cefc89 Mon Sep 17 00:00:00 2001 From: Konstantin Martini Date: Sat, 3 Jan 2015 02:49:29 +0100 Subject: [PATCH] shaping out functionality --- matecnt/models.py | 8 ++-- matecnt/urls.py | 5 +- matecnt/views.py | 103 ++++++++++++++++++++++++++++++++++------ matemat/urls.py | 3 ++ templates/checkout.html | 6 +-- templates/drink.html | 27 +++++++++++ templates/drinks.html | 2 +- templates/index.html | 27 +++++++++++ templates/user.html | 27 +++++++++++ 9 files changed, 182 insertions(+), 26 deletions(-) create mode 100644 templates/drink.html create mode 100644 templates/index.html create mode 100644 templates/user.html diff --git a/matecnt/models.py b/matecnt/models.py index 49512ff..91cf38b 100644 --- a/matecnt/models.py +++ b/matecnt/models.py @@ -3,8 +3,8 @@ from django.db import models class Drinker(models.Model): - def __unicode__(self): - return unicode(self.name) + def __str__(self): + return '<%r %s>' % (self.name, self.code) name = models.CharField(max_length=123) @@ -15,8 +15,8 @@ class Drinker(models.Model): class Drink(models.Model): - def __unicode__(self): - return unicode(self.name) + def __str__(self): + return '<%r %s>' % (self.name, self.code) name = models.CharField(max_length=123) diff --git a/matecnt/urls.py b/matecnt/urls.py index 2645267..e6b1f2a 100644 --- a/matecnt/urls.py +++ b/matecnt/urls.py @@ -6,6 +6,9 @@ urlpatterns = patterns('', # url(r'^blog/', include('blog.urls')), #(r'.*$', 'matecnt.views.overview'), - (r'users/.*$', 'matecnt.views.users'), + (r'index$', 'matecnt.views.index'), + (r'user/(?P\d+)$', 'matecnt.views.user'), (r'drinks/.*$', 'matecnt.views.drinks'), + (r'drink/(?P\d+)$', 'matecnt.views.drink'), + (r'checkout', 'matecnt.views.checkout') ) diff --git a/matecnt/views.py b/matecnt/views.py index 66255f2..7d759cb 100644 --- a/matecnt/views.py +++ b/matecnt/views.py @@ -1,27 +1,100 @@ -#from django.shortcuts import render +from django.shortcuts import render, redirect, get_object_or_404 from django.http import HttpResponse +from django.http import HttpResponseRedirect from django.template import RequestContext, loader from matecnt.models import Drinker, Drink -def users(request): - #drinkers = Drinker.objects.all() - drinkers = Drinker.objects.order_by('credit')[:5] - template = loader.get_template('users.html') +# Helpers + +def get_users(): + return Drinker.objects.order_by('credit') + +def get_user(code): + return get_object_or_404(Drinker, code=code) + +def get_drink(code): + return get_object_or_404(Drink, code=code) + +def get_drinks(): + return Drink.objects.order_by('prize') + + +# Views + +def index(request): + ctx = { + 'heading': 'Welcome to YAMatemat', + 'drinkers': get_users(), + } + return render(request, 'index.html', ctx) + + +def user(request, code=None): + + if not code: + if 'user' in request.REQUEST: + code = request.REQUEST['user'] + else: + return redirect('/mate/users/') + + user = get_user(code) + + ctx = { + 'heading': 'User %s' % (user.name), + 'drinker': user, + 'drinks': get_drinks, + } + return render(request, 'user.html', ctx) + + +def drink(request, code=None): + + if not code: + if 'drink' in request.REQUEST: + code = request.REQUEST['drink'] + else: + return redirect('/mate/drinks/') + + drink = get_drink(code) + + + ctx = { + 'heading': 'Drink %s' % (drink.name), + 'drink': drink, + 'drinkers': get_users(), + } + return render(request, 'drink.html', ctx) - context = RequestContext(request, { - 'heading': 'User list', - 'drinkers': drinkers, - }) - return HttpResponse(template.render(context)) def drinks(request): - drinks = Drink.objects.order_by('prize') - template = loader.get_template('drinks.html') + drinks = get_drinks() - context = RequestContext(request, { + ctx = { 'heading': 'Drink list', 'drinks': drinks, - }) - return HttpResponse(template.render(context)) + } + return render(request, 'drinks.html', ctx) + + +def checkout(request): + + #if not request.method == 'POST': + #raise RuntimeError('only POST allowed') + + try: + user = request.REQUEST['drinker'] + drink = request.REQUEST['drink'] + except KeyError as ex: + return HttpResponse(status=400) + + count = request.REQUEST.get('count', 1) + + ctx = { + 'heading': 'Codes scanned: user=%r drink=%r amount=%s' % (user, drink, count), + 'drinker': get_user(user), + 'drink': get_drink(drink), + } + return render(request, 'checkout.html', ctx) + diff --git a/matemat/urls.py b/matemat/urls.py index 3fb4936..b07ade5 100644 --- a/matemat/urls.py +++ b/matemat/urls.py @@ -1,9 +1,12 @@ from django.conf.urls import patterns, include, url from django.contrib import admin +from django.views.generic import RedirectView + from matecnt import urls as matecnt_urls urlpatterns = patterns('', url(r'^mate/', include(matecnt_urls)), url(r'^admin/', include(admin.site.urls)), + (r'^.*$', RedirectView.as_view(url='/mate/index')), ) diff --git a/templates/checkout.html b/templates/checkout.html index 363300e..4b4f0d3 100644 --- a/templates/checkout.html +++ b/templates/checkout.html @@ -3,11 +3,7 @@ {% block content %}

{{ heading }}

-{% for drink in drinks %}

- - {{ drink.name }} - + {{drinker.name}} spends {{drink.prize}} units on {{drink.name}}.

-{% endfor %} {% endblock %} diff --git a/templates/drink.html b/templates/drink.html new file mode 100644 index 0000000..fb4e51a --- /dev/null +++ b/templates/drink.html @@ -0,0 +1,27 @@ +{% extends "base.html" %} + +{% block content %} +

{{ heading }}

+ +

+ Name: {{drink.name}}
+ Code: {{drink.code}}
+ Prize: {{drink.prize}}
+

+ +

+ Have one of these for {{drink.prize}} credits: +

+ +{% for drinker in drinkers %} +

+
+ {% csrf_token %} + + + + +
+

+{% endfor %} +{% endblock %} diff --git a/templates/drinks.html b/templates/drinks.html index 363300e..224bcef 100644 --- a/templates/drinks.html +++ b/templates/drinks.html @@ -5,7 +5,7 @@ {% for drink in drinks %}

- + {{ drink.name }}

diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..f2513fe --- /dev/null +++ b/templates/index.html @@ -0,0 +1,27 @@ +{% extends "base.html" %} + +{% block content %} +

{{ heading }}

+
+ +

+ User List:
+

+{% for drinker in drinkers %} +

+ + {{ drinker.name }} + +

+{% endfor %} + +
+ +

+ + Drink list + +

+ +
+{% endblock %} diff --git a/templates/user.html b/templates/user.html new file mode 100644 index 0000000..de8c777 --- /dev/null +++ b/templates/user.html @@ -0,0 +1,27 @@ +{% extends "base.html" %} + +{% block content %} +

{{ heading }}

+ +

+ Name: {{drinker.name}}
+ Code: {{drinker.code}}
+ Credit: {{drinker.credit}}
+

+ +

+ have a drink: +

+ +{% for drink in drinks %} +

+
+ {% csrf_token %} + + + + +
+

+{% endfor %} +{% endblock %}