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.
69 lines
2.3 KiB
69 lines
2.3 KiB
from django.http import JsonResponse
|
|
from django.contrib import messages
|
|
from django.db.models import Sum
|
|
from django.views.generic import DetailView, CreateView
|
|
from users.models import User
|
|
from users.mixins import CheckForUserMixin
|
|
from .models import InvoiceHistory, WithDraw
|
|
from .forms import WithDrawForm
|
|
|
|
|
|
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 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)
|
|
|