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.
 
 
 
 
 
 

184 lines
6.6 KiB

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Sum
from django.http import HttpResponse, JsonResponse
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 users.models import User
from .forms import WithDrawForm, TmpCheckOrderForm, TmpPaymentAvisoForm
from .models import InvoiceHistory, WithDraw, Transaction
class ScoreDetailView(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(LoginRequiredMixin, View):
template_name = 'score-detail.html'
def get(self, request, *args, **kwargs):
transaction = Transaction.objects.create(customer=request.user, type='add')
user_score = get_object_or_404(User.objects, pk=kwargs.get('pk'))
current_sum_info = InvoiceHistory.objects.filter(user=user_score).aggregate(Sum('sum'))
user_score_balance = current_sum_info['sum__sum'] or 0
return render(request, self.template_name, {
'transaction': transaction,
'YANDEX_MONEY': settings.YANDEX_MONEY,
'user_score': user_score,
'user_score_balance': user_score_balance,
})
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():
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():
transaction = form.cleaned_data.get('transaction_id')
transaction.complete = True
transaction.sum = form.cleaned_data.get('orderSumAmount')
transaction.save()
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