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 *

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)

transactionTransactRes = CsrfExemptResource(handler=TransactionTransactHandler, **ad)
transactionTypeRes     = CsrfExemptResource(handler=TransactionTypeHandler, **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'account/transactions/transact/?$', transactionTransactRes),
	url(r'account/transfers/transfer/?$', transactionTransactRes),
	url(r'account/transactions/types/?$', transactionTypeRes),
	url(r'account/transfers/types/?$', transactionTypeRes),
	url(r'account/balance/?$', accountBalanceRes),
	
	url(r'auth/blob/?$', authBlobRes),
	url(r'auth/user/?$', authUserRes),
	url(r'config/?$', configRes),
)

