# Path: payway/orders/views/list.py
# class OrderListView(TemplateView):
#     template_name = 'orders/list.html'
# 
#     def get(self, request, *args, **kwargs):
#         orders_page = create_paginated_page(
#             query_set=Order.objects.filter(user=request.user).order_by("-id"),
#             page_number=request.GET.get('page') or 1,
#             objects_per_page=ORDERS_PER_PAGE
#         )
#         return self.render_to_response({'orders_page': orders_page})
# 
# Path: payway/orders/views/payment.py
# class OrderPaymentView(TemplateView):
#     template_name = 'orders/payment.html'
# 
#     def get(self, request, *args, **kwargs):
#         merchant = get_object_or_404(Merchant, uid=request.GET.get('merchant_uid'))
#         order = Order.objects.get_or_None(
#             merchant=merchant,
#             uid=request.GET.get('uid') or -1
#         )
#         order_form = OrderForm(data=request.GET, accounts=self.get_accounts_list(request))
#         return self.render_to_response({
#             'order_form': order_form,
#             'is_paid': order.is_paid if order else False,
#             'success_url': merchant.success_url,
#             'fail_url': merchant.fail_url,
#         })
# 
#     @transaction.commit_on_success
#     def post(self, request):
#         accounts = self.get_accounts_list(request)
#         order_form = OrderForm(data=request.POST, accounts=accounts)
# 
#         if order_form.is_valid():
#             account = get_object_or_404(Account, user=request.user, uid=order_form.cleaned_data['account'])
#             merchant = get_object_or_404(Merchant, uid=order_form.cleaned_data['merchant_uid'])
#             merchant_account = get_object_or_404(Account, user=merchant.user, uid=order_form.cleaned_data['merchant_account'])
# 
#             order_sum = Money(order_form.cleaned_data['sum'], account.currency)
#             order_uid = order_form.cleaned_data['uid']
# 
#             if order_sum <= account.get_balance():
#                 order, created = Order.objects.get_or_create(
#                     uid=order_uid,
#                     account=account,
#                     merchant=merchant,
#                     defaults={
#                         'uid': order_uid,
#                         'account': account,
#                         'sum': order_sum,
#                         'merchant': merchant,
#                         'description': order_form.cleaned_data['description']
#                     }
#                 )
#                 if not created:
#                     order.sum = order_sum
# 
#                 if not order.is_paid:
#                     response = MerchantHttpClient.notify(merchant, order)
#                     if RESPONSE_STATUS.OK not in response:
#                         order.set_paid(False)
#                         messages.error(request, _("Connection error. Merchant couldn't process order! Merchant response ") + response)
#                     else:
#                         account.transfer(order_sum, merchant_account)
#                         order.set_paid(True)
#                         messages.info(request, _("Order paid successfully"))
# 
#                     order.save()
#                     order_form.set_account_field_choices(self.get_accounts_list(request))
# 
#                 return self.render_to_response({
#                     'order_form': order_form,
#                     'is_paid': order.is_paid,
#                     'success_url': merchant.success_url,
#                     'fail_url': merchant.fail_url,
#                 })
# 
#             order_form.set_not_enough_money_error()
# 
#         return self.render_to_response({
#             'order_form': order_form,
#         })
# 
#     def get_accounts_list(self, request):
#         return get_list_or_404(Account, user=request.user)


from django.conf.urls.defaults import patterns, url
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import TemplateView
from payway.orders.views.list import OrderListView
from payway.orders.views.payment import OrderPaymentView
# -*- coding: UTF-8 -*-

__author__ = 'Razzhivin Alexander'
__email__ = 'admin@httpbots.com'


urlpatterns = patterns('',
    url(r'^payment/$', login_required(csrf_exempt(OrderPaymentView.as_view())), name='orders_payment'),

# Path: payway/orders/views/list.py
# class OrderListView(TemplateView):
#     template_name = 'orders/list.html'
# 
#     def get(self, request, *args, **kwargs):
#         orders_page = create_paginated_page(
#             query_set=Order.objects.filter(user=request.user).order_by("-id"),
#             page_number=request.GET.get('page') or 1,
#             objects_per_page=ORDERS_PER_PAGE
#         )
#         return self.render_to_response({'orders_page': orders_page})
# 
# Path: payway/orders/views/payment.py
# class OrderPaymentView(TemplateView):
#     template_name = 'orders/payment.html'
# 
#     def get(self, request, *args, **kwargs):
#         merchant = get_object_or_404(Merchant, uid=request.GET.get('merchant_uid'))
#         order = Order.objects.get_or_None(
#             merchant=merchant,
#             uid=request.GET.get('uid') or -1
#         )
#         order_form = OrderForm(data=request.GET, accounts=self.get_accounts_list(request))
#         return self.render_to_response({
#             'order_form': order_form,
#             'is_paid': order.is_paid if order else False,
#             'success_url': merchant.success_url,
#             'fail_url': merchant.fail_url,
#         })
# 
#     @transaction.commit_on_success
#     def post(self, request):
#         accounts = self.get_accounts_list(request)
#         order_form = OrderForm(data=request.POST, accounts=accounts)
# 
#         if order_form.is_valid():
#             account = get_object_or_404(Account, user=request.user, uid=order_form.cleaned_data['account'])
#             merchant = get_object_or_404(Merchant, uid=order_form.cleaned_data['merchant_uid'])
#             merchant_account = get_object_or_404(Account, user=merchant.user, uid=order_form.cleaned_data['merchant_account'])
# 
#             order_sum = Money(order_form.cleaned_data['sum'], account.currency)
#             order_uid = order_form.cleaned_data['uid']
# 
#             if order_sum <= account.get_balance():
#                 order, created = Order.objects.get_or_create(
#                     uid=order_uid,
#                     account=account,
#                     merchant=merchant,
#                     defaults={
#                         'uid': order_uid,
#                         'account': account,
#                         'sum': order_sum,
#                         'merchant': merchant,
#                         'description': order_form.cleaned_data['description']
#                     }
#                 )
#                 if not created:
#                     order.sum = order_sum
# 
#                 if not order.is_paid:
#                     response = MerchantHttpClient.notify(merchant, order)
#                     if RESPONSE_STATUS.OK not in response:
#                         order.set_paid(False)
#                         messages.error(request, _("Connection error. Merchant couldn't process order! Merchant response ") + response)
#                     else:
#                         account.transfer(order_sum, merchant_account)
#                         order.set_paid(True)
#                         messages.info(request, _("Order paid successfully"))
# 
#                     order.save()
#                     order_form.set_account_field_choices(self.get_accounts_list(request))
# 
#                 return self.render_to_response({
#                     'order_form': order_form,
#                     'is_paid': order.is_paid,
#                     'success_url': merchant.success_url,
#                     'fail_url': merchant.fail_url,
#                 })
# 
#             order_form.set_not_enough_money_error()
# 
#         return self.render_to_response({
#             'order_form': order_form,
#         })
# 
#     def get_accounts_list(self, request):
#         return get_list_or_404(Account, user=request.user)


# Path: payway/orders/urls.py
from django.conf.urls.defaults import patterns, url
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import TemplateView
from payway.orders.views.list import OrderListView
from payway.orders.views.payment import OrderPaymentView

# -*- coding: UTF-8 -*-

__author__ = 'Razzhivin Alexander'
__email__ = 'admin@httpbots.com'


urlpatterns = patterns('',
    url(r'^payment/$', login_required(csrf_exempt(OrderPaymentView.as_view())), name='orders_payment'),

    url(r'^list/$', login_required(OrderListView.as_view()), name='orders_list'),