api2 beginning, minor changes

This commit is contained in:
seba 2011-09-28 10:51:32 +02:00
parent ad792b6747
commit c7ad4be6e4
13 changed files with 180 additions and 5 deletions

View File

@ -17,3 +17,6 @@ Nice-to-haf:
Konrad:
Abmeldenutton rechts oder rot?
- die liste der zu einkaufenden items ist doof :(
- /store/history/ ist noch kaputt + zeit unformatiert
- /transaction/ sehen die gemachten transactions noch nicht so cool aus

72
devel/api Normal file
View File

@ -0,0 +1,72 @@
API
kaufen
produkte
auflisten
alle, nach gruppe
produktgruppen auflisten
suchen nach namen
hasDeposit falls kein attribut
beliebteste produkte (allgemein, nur vom user)
kaufen eines items (mit oder ohne deposit)
pfand zurückgeben
letzte einkäufe
transaction
auflisten zahlarten (bar, ueberweisung, ...)
einzahlen
auszahlen
letzte einzahlungen
api auth stuff
if plugin identifies by authblob
getUsersByAuthBlob (oderso)
else
authenticateUser(user, authblob)
listAuthBlobs (fuer alle user, die das plugin erlaubt haben)
canSuAsUser (noch ordentlich zu benennen)
cool wäre:
irgendwann letzte Änderung der produktliste speichern
=== REST LIKE API STARTS HERE ===
buyable/
item/
GET (=list)
""" get a specific item or a full (group) item list """
group item belonging to group
id item with id
POST (=buy)
""" buy an item"
id [REQ] buy item with id
deposit
types/
GET (=list)
""" list all types (groups) which an item can belong to """
transaction/
transact/
GET (=list)
""" list your transactions """
num list $num entries
POST (=pay)
""" actually transact money """
amount [REQ] amount to add to your account
type [REQ] type of transaction (id)
types/
GET (=list)
""" list all available transaction types"
auth/
blob/
GET (=get)
""" return authblob if allowed or auth if str given """
blob blob to get user from / auth user with, returns User or NULL
POST
""" set authblob if allowed """
wget -q -O- --auth-no-challenge --http-user=seba -http-password=foobar23 http://devcat.someserver.de:13805/api2/buyable/item/foo

0
k4ever/api2/__init__.py Normal file
View File

27
k4ever/api2/handlers.py Normal file
View File

@ -0,0 +1,27 @@
from piston.handler import BaseHandler
from k4ever.buyable.models import *
from k4ever.transaction.models import *
class BuyableItemHandler(BaseHandler):
allowed_methods = ('GET', 'POST')
#fields = ('id', 'description')
model = Buyable
exclude = ('_state',)
#def read(self, request):
# return Buyable.objects.get(id=1)
class BuyableTypeHandler(BaseHandler):
allowed_methods = ('GET',)
model = BuyableType
class TransactionTransactHandler(BaseHandler):
allowed_methods = ('GET', 'POST')
model = Transaction
class TransactionTypeHandler(BaseHandler):
allowed_methods = ('GET',)
model = TransactionType
class AuthBlobHandler(BaseHandler):
pass

3
k4ever/api2/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

23
k4ever/api2/tests.py Normal file
View File

@ -0,0 +1,23 @@
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".
Replace these with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.failUnlessEqual(1 + 1, 2)
__test__ = {"doctest": """
Another way to test that 1 + 1 is equal to 2.
>>> 1 + 1 == 2
True
"""}

26
k4ever/api2/urls.py Normal file
View File

@ -0,0 +1,26 @@
from django.conf.urls.defaults import *
from piston.resource import Resource
from piston.authentication import HttpBasicAuthentication
from api2.handlers import *
auth = HttpBasicAuthentication(realm="Freitagsrundenkassensystemapi")
ad = {'authentication': auth}
buyableItemRes = Resource(handler=BuyableItemHandler, **ad)
buyableTypeRes = Resource(handler=BuyableTypeHandler, **ad)
transactionTransactRes = Resource(handler=TransactionTransactHandler, **ad)
transactionTypeRes = Resource(handler=TransactionTypeHandler, **ad)
authBlobRes = Resource(handler=AuthBlobHandler, **ad)
urlpatterns = patterns('',
url(r'buyable/item/', buyableItemRes),
url(r'buyable/types/', buyableTypeRes),
url(r'transaction/transact/', transactionTransactRes),
url(r'transaction/types/', transactionTypeRes),
url(r'auth/blob/', authBlobRes),
)

1
k4ever/api2/views.py Normal file
View File

@ -0,0 +1 @@
# Create your views here.

View File

@ -19,6 +19,9 @@ class Buyable(models.Model):
description = models.TextField()
buyableType = models.ManyToManyField(BuyableType)
def hasDeposit(self):
return self.deposit > 0
def createPurchase(self, isDeposit=False):
p = Purchase()
if isDeposit:
@ -31,7 +34,11 @@ class Buyable(models.Model):
return p
def __unicode__(self):
return "%s (%s EUR/%s Pfand)" % (self.name, self.price, self.deposit)
item = "%s (%.2f EUR" % (self.name, self.price)
if self.hasDeposit():
item += "/%.2f Pfand" % self.deposit
item += ")"
return item
class Order(models.Model):
user = models.ForeignKey(User)

View File

@ -2,7 +2,12 @@
{% block "content" %}
{% if item %}
You got the item {{ item }}.Buy <a href="/store/buy/{{ item.id }}/">it!</a> <a href="/store/buy/{{ item.id }}/with/deposit/">it+deposit!</a> <a href="/store/buy/{{ item.id }}/only/deposit/"> only deposit!</a>
You got the item {{ item }}.
Buy <a href="/store/buy/{{ item.id }}/">it!</a>
{% if item.hasDeposit %}
<a href="/store/buy/{{ item.id }}/with/deposit/">it+deposit!</a>
<a href="/store/buy/{{ item.id }}/only/deposit/"> only deposit!</a>
{% endif %}
{% else %}
No item found :(
{% endif %}

View File

@ -2,6 +2,7 @@
{% block "content" %}
{% for item in items %}
<img src="{{ MEDIA_URL }}{{ item.image }}">{{ item }} <a href="/store/show/{{ item.id }}">{{ item.name }}</a>
<img src="{{ MEDIA_URL }}{{ item.image }}">{{ item }}
<a href="/store/show/{{ item.id }}">{{ item.name }}</a>
{% endfor %}
{% endblock %}

View File

@ -25,6 +25,12 @@ table, caption, tbody, tfoot, thead, tr, th, td {
vertical-align: baseline;
}
/* added by seba because nobody else did this. */
.content {
margin: 5px;
}
/* end of addition */
body {
line-height: 1;
}

View File

@ -14,6 +14,7 @@ urlpatterns = patterns('',
# user stuff? go to main
(r'^$', 'main.views.startpage'),
(r'^api/', include('api.urls')),
(r'^api2/', include('api2.urls')),
(r'^user/', include('main.urls')),
(r'^transaction/', include('transaction.urls')),
(r'^store/', include('buyable.urls')),