You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

194 lines
7.0 KiB

from django.conf import urls, settings
from django.contrib import messages
from django.db.models import Sum
from django.http import HttpResponse, JsonResponse, HttpResponseForbidden
from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import DetailView, CreateView
from django.views.generic.base import View
from pprint import pprint, pformat
from .forms import WithDrawForm, TmpCheckOrderForm, TmpPaymentAvisoForm
from .models import InvoiceHistory, WithDraw, Transaction
from users.mixins import CheckForUserMixin
from users.models import User
class ScoreDetailView(CheckForUserMixin, DetailView):
model = User
template_name = 'score-detail.html'
context_object_name = 'user_score'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
user_score_balance = InvoiceHistory.objects.filter(user=self.get_object()).aggregate(Sum('sum'))
context['user_score_balance'] = user_score_balance['sum__sum'] or 0
context['form'] = WithDrawForm
return context
class ScoreView(View):
template_name = 'score-detail.html'
def dispatch(self, request, *args, **kwargs):
if request.user.is_authenticated() and request.user.is_customer():
return super().dispatch(request, *args, **kwargs)
else:
return HttpResponseForbidden('403 Forbidden')
def get(self, request, *args, **kwargs):
# transaction = Transaction.objects.create(customer=request.user)
user_score = get_object_or_404(User.customer_objects, pk=kwargs.get('pk'))
return render(request, self.template_name, {
# 'transaction': transaction,
'YANDEX_MONEY': settings.YANDEX_MONEY,
'user_score': user_score,
})
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
user_score_balance = InvoiceHistory.objects.filter(user=self.get_object()).aggregate(Sum('sum'))
context['user_score_balance'] = user_score_balance['sum__sum'] or 0
context['form'] = WithDrawForm
return context
class AjaxableResponseMixin(object):
def form_invalid(self, form):
response = super().form_invalid(form)
if self.request.is_ajax():
return JsonResponse(form.errors, status=400)
else:
return response
def form_valid(self, form):
# import code; code.interact(local=dict(globals(), **locals()))
response = super(AjaxableResponseMixin, self).form_valid(form)
if self.request.is_ajax():
messages.info(self.request, 'Ваша заявка на вывод средств принята!')
data = {
'pk': self.object.pk,
'status': 'ok',
}
return JsonResponse(data)
else:
return response
# class WithDrawCreate(AjaxableResponseMixin, CreateView):
# model = WithDraw
# form_class = WithDrawForm
# success_url = '/'
class WithDrawCreate(CreateView):
model = WithDraw
form_class = WithDrawForm
def form_valid(self, form):
if self.request.is_ajax():
self.object = form.save()
messages.info(self.request, 'Ваша заявка на вывод средств принята!')
data = {
'pk': self.object.pk,
'status': 'ok',
}
return JsonResponse(data)
return super().form_valid(form)
def form_invalid(self, form):
if self.request.is_ajax():
return JsonResponse(form.errors, status=400)
return super().form_invalid(form)
# Yandex Money ------------------------------------------------
class TmpCheckOrderView(View):
form_class = TmpCheckOrderForm
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
trans = form.cleaned_data.get('transaction')
if form.is_valid() and not trans.is_void():
res = """<?xml version="1.0" encoding="utf-8"?>
<checkOrderResponse
performedDatetime="{date}"
code="0"
invoiceId="{invoice_id}"
shopId="{shop_id}"/>
""".format(
date=timezone.now().isoformat(),
invoice_id=form.cleaned_data.get('invoiceId'),
shop_id=form.cleaned_data.get('shopId'),
)
return HttpResponse(res, content_type='application/xml')
else:
res = """<?xml version="1.0" encoding="utf-8"?>
<checkOrderResponse
performedDatetime="{date}"
code="1"
invoiceId="{invoice_id}"
shopId="{shop_id}"
message="Check order, validation error"
techMessage="Check order, validation error"/>
""".format(
date=timezone.now().isoformat(),
invoice_id=form.cleaned_data.get('invoiceId'),
shop_id=form.cleaned_data.get('shopId'),
)
# return HttpResponse(res, content_type='application/xml')
return HttpResponse('<pre>{msg}</pre>'.format(msg=pformat(form.errors))) # Debug
class TmpPaymentAvisoView(View):
form_class = TmpPaymentAvisoForm
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
res = """<?xml version="1.0" encoding="utf-8"?>
<paymentAvisoResponse
performedDatetime="{date}"
code="0"
invoiceId="{invoice_id}"
shopId="{shop_id}"/>
""".format(
date=timezone.now().isoformat(),
invoice_id=form.cleaned_data.get('invoiceId'),
shop_id=form.cleaned_data.get('shopId'),
)
return HttpResponse(res, content_type='application/xml')
else:
res = """<?xml version="1.0" encoding="utf-8"?>
<paymentAvisoResponse
performedDatetime="{date}"
code="1"
message="Payment aviso, validation error"
techMessage="Payment aviso, validation error"/>
""".format(date=timezone.now().isoformat())
# return HttpResponse(res, content_type='application/xml')
return HttpResponse('<pre>{msg}</pre>'.format(msg=pformat(form.errors))) # Debug