shaping out functionality

master
Konstantin Martini 8 years ago
parent e0c901d456
commit d2f3f3029c
  1. 8
      matecnt/models.py
  2. 5
      matecnt/urls.py
  3. 103
      matecnt/views.py
  4. 3
      matemat/urls.py
  5. 6
      templates/checkout.html
  6. 27
      templates/drink.html
  7. 2
      templates/drinks.html
  8. 27
      templates/index.html
  9. 27
      templates/user.html

@ -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)

@ -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<code>\d+)$', 'matecnt.views.user'),
(r'drinks/.*$', 'matecnt.views.drinks'),
(r'drink/(?P<code>\d+)$', 'matecnt.views.drink'),
(r'checkout', 'matecnt.views.checkout')
)

@ -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)

@ -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')),
)

@ -3,11 +3,7 @@
{% block content %}
<h1>{{ heading }}</h1>
{% for drink in drinks %}
<h2>
<a href="/drink/{{ drink.code }}">
{{ drink.name }}
</a>
{{drinker.name}} spends {{drink.prize}} units on {{drink.name}}.
</h2>
{% endfor %}
{% endblock %}

@ -0,0 +1,27 @@
{% extends "base.html" %}
{% block content %}
<h1>{{ heading }}</h1>
<h2>
Name: {{drink.name}}<br />
Code: {{drink.code}}<br />
Prize: {{drink.prize}}<br />
</h2>
<h2>
Have one of these for {{drink.prize}} credits:
</h2>
{% for drinker in drinkers %}
<h2>
<form action="/mate/checkout" method="POST">
{% csrf_token %}
<input type="text" name="count" value="1" size="3" />
<input type="submit" name="submit" value="{{ drinker.name }}" />
<input type="hidden" name="drinker" value="{{ drinker.code }}"/>
<input type="hidden" name="drink" value="{{ drink.code }}" />
</form>
</h2>
{% endfor %}
{% endblock %}

@ -5,7 +5,7 @@
{% for drink in drinks %}
<h2>
<a href="/drink/{{ drink.code }}">
<a href="/mate/drink/{{ drink.code }}">
{{ drink.name }}
</a>
</h2>

@ -0,0 +1,27 @@
{% extends "base.html" %}
{% block content %}
<h1>{{ heading }}</h1>
<hr />
<h2>
User List:<br />
</h2>
{% for drinker in drinkers %}
<h2>
<a href="/mate/user/{{ drinker.code }}">
{{ drinker.name }}
</a>
</h2>
{% endfor %}
<hr />
<h2>
<a href="/mate/drinks/">
Drink list
</a>
</h2>
<hr />
{% endblock %}

@ -0,0 +1,27 @@
{% extends "base.html" %}
{% block content %}
<h1>{{ heading }}</h1>
<h2>
Name: {{drinker.name}}<br />
Code: {{drinker.code}}<br />
Credit: {{drinker.credit}}<br />
</h2>
<h2>
have a drink:
</h2>
{% for drink in drinks %}
<h2>
<form action="/mate/checkout" method="POST">
{% csrf_token %}
<input type="text" name="count" value="1" size="3" />
<input type="submit" name="submit" value="{{ drink.name }} ({{drink.prize}} credits)" />
<input type="hidden" name="drinker" value="{{ drinker.code }}"/>
<input type="hidden" name="drink" value="{{ drink.code }}" />
</form>
</h2>
{% endfor %}
{% endblock %}
Loading…
Cancel
Save