# This file is part of k4ever, a point-of-sale system
#   Contact............  <k4ever@lists.someserver.de>
#   Website............  http://k4ever.someserver.de/
#   Bug tracker........  http://k4ever.someserver.de/report
#
# Licensed under GNU Affero General Public License v3 or later

from django.conf.urls.defaults import *
from piston.resource import Resource
from piston.authentication import HttpBasicAuthentication
from api2.authentication import DjangoAuthentication, MultiAuthentication
from api2.handlers import *
from api2.decorators import fix_mime
import piston.utils

# piston does not understand mimetypes with charsets, HACK: fix content_type
piston.utils.Mimer.content_type = fix_mime(piston.utils.Mimer.content_type)

class CsrfExemptResource(Resource):
	""" Except a :class:`Resource` from djangos CSRF-Framework.

		This idea is taken from
		http://www.robertshady.com/content/creating-very-basic-api-using-python-django-and-piston
	"""
	
	def __init__(self, handler, authentication = None):
		super( CsrfExemptResource, self ).__init__( handler, authentication )
		self.csrf_exempt = getattr( self.handler, 'csrf_exempt', True )

# build authenticatiooors
basicAuth = HttpBasicAuthentication(realm="Freitagsrundenkassensystemapi")
djangoAuth = DjangoAuthentication()
multiAuth = MultiAuthentication([basicAuth, djangoAuth])
ad = {'authentication': multiAuth}

buyableItemRes    = CsrfExemptResource(handler=BuyableItemHandler,     **ad)
buyableTypeRes    = CsrfExemptResource(handler=BuyableTypeHandler,     **ad)
historyRes = CsrfExemptResource(handler=HistoryHandler,         **ad)
imgSizesRes  = CsrfExemptResource(handler=ImgSizesHandler, **ad)
imgThumbRes = CsrfExemptResource(handler=ImgThumbHandler, **ad)

transactionTransactRes = CsrfExemptResource(handler=TransactionTransactHandler, **ad)
transactionTypeRes     = CsrfExemptResource(handler=TransactionTypeHandler, **ad)
transactionVirtualRes  = CsrfExemptResource(handler=TransactionVirtualHandler, **ad)
accountBalanceRes      = CsrfExemptResource(handler=AccountBalanceHandler, **ad)

authBlobRes = CsrfExemptResource(handler=AuthBlobHandler, **ad)
authUserRes = CsrfExemptResource(handler=AuthUserHandler, **ad)
configRes   = CsrfExemptResource(handler=ConfigHandler, **ad)


urlpatterns = patterns('',
	url(r'buyable/item/?$', buyableItemRes),
	url(r'buyable/item/(?P<itemId>\d+)/?$', buyableItemRes),
	url(r'buyable/item/bulkbuy/?$', buyableItemRes, {'bulkBuy': True}),
	url(r'buyable/types/?$', buyableTypeRes),
	url(r'buyable/history/?$', historyRes),
	url(r'buyable/img/sizes/?$', imgSizesRes),
	url(r'buyable/img/thumb/(?P<itemId>\d+)/(?P<xSize>\d+)x(?P<ySize>\d+)/?$', imgThumbRes),
	
	url(r'account/transactions/transact/?$', transactionTransactRes),
	url(r'account/transactions/types/?$', transactionTypeRes),
	url(r'account/transactions/virtual/?$', transactionVirtualRes),
	url(r'account/balance/?$', accountBalanceRes),
	
	url(r'auth/blob/?$', authBlobRes),
	url(r'auth/user/?$', authUserRes),
	url(r'config/?$', configRes),
)

