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.
242 lines
8.5 KiB
242 lines
8.5 KiB
# coding=utf-8
|
|
from django.db.models import Q
|
|
from django.http import Http404
|
|
from access.models import User
|
|
from lms.decors import api_decor
|
|
from courses.models import Course, Exam, Lesson, Homework
|
|
from finance.models import ServiceRequest, Bill, Price
|
|
from finance.tools import system_check_bill
|
|
from lms.settings import DOMAIN
|
|
|
|
|
|
@api_decor(without_auth=False, need_keys=['id', 'type'], method='POST', check_request=True)
|
|
def check_materials_access(request, context):
|
|
if request.user.is_authenticated() and request.user.is_admin:
|
|
context['code'] = '1'
|
|
return context
|
|
|
|
material = None
|
|
theme_type = 'B'
|
|
if request.POST['type'] == 'homework':
|
|
material = Homework.objects.get(id=request.POST['id'])
|
|
theme_type = material.theme.price_type
|
|
|
|
elif request.POST['type'] == 'exam':
|
|
material = Exam.objects.get(id=request.POST['id'])
|
|
theme_type = material.theme.price_type
|
|
|
|
elif request.POST['type'] == 'lesson':
|
|
material = Lesson.objects.get(id=request.POST['id'])
|
|
if material.free:
|
|
context['code'] = '1'
|
|
return context
|
|
theme_type = material.theme.price_type
|
|
|
|
if material and system_check_bill(material.token, request.user, theme_type=theme_type):
|
|
context['code'] = '1'
|
|
else:
|
|
context['code'] = '0'
|
|
context['response'] = material.token
|
|
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=True, method='POST', need_keys=['key'], check_request=True)
|
|
def send_self_bill(request, context):
|
|
context['code'] = '0'
|
|
if not request.POST.get('fname'):
|
|
context['response'] = u'Фамилия не указана'
|
|
return context
|
|
|
|
if not request.POST.get('name'):
|
|
context['response'] = u'Имя не указано'
|
|
return context
|
|
|
|
if not request.POST.get('oname'):
|
|
context['response'] = u'Очество не указано'
|
|
return context
|
|
|
|
if not request.POST.get('phone'):
|
|
context['response'] = u'Телефон не указан'
|
|
return context
|
|
|
|
if not request.POST.get('email'):
|
|
context['response'] = u'Электронный адрес не указан'
|
|
return context
|
|
|
|
try:
|
|
price = Price.objects.get(key=request.POST['key'])
|
|
except Price.DoesNotExist:
|
|
raise Http404
|
|
else:
|
|
context['code'] = '1'
|
|
user, c = User.objects.get_or_create(email=request.POST['email'])
|
|
if not user.phone:
|
|
user.phone = request.POST['phone']
|
|
if not user.name:
|
|
user.name = request.POST['name']
|
|
if not user.oname:
|
|
user.oname = request.POST['oname']
|
|
if not user.fname:
|
|
user.fname = request.POST['fname']
|
|
if c:
|
|
user.in_role = 'F'
|
|
user.save()
|
|
try:
|
|
bill = Bill.objects.get(service=price, price=price.cost, user=user)
|
|
except Bill.DoesNotExist:
|
|
bill = Bill.objects.create(service=price, price=price.cost, user=user, manager=User.objects.get(id=1325))
|
|
context['data'] = bill.gen_robokassa_url()
|
|
else:
|
|
if bill.status == 'F':
|
|
context['code'] = '0'
|
|
context['response'] = u'Счет уже оплачен <b>ID счета: %s</b>' % bill.id
|
|
else:
|
|
context['data'] = bill.gen_robokassa_url()
|
|
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=False, need_keys=['id'], method='GET', check_request=True)
|
|
def cancel_order(request, context):
|
|
try:
|
|
bill = Bill.objects.get(id=request.GET['id'])
|
|
except Bill.DoesNotExist:
|
|
pass
|
|
else:
|
|
bill.status = 'C'
|
|
bill.save()
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=False, need_keys=['id'], method='GET', check_request=True)
|
|
def sent_order(request, context):
|
|
try:
|
|
bill = Bill.objects.get(id=request.GET['id'])
|
|
except Bill.DoesNotExist:
|
|
context['data'] = DOMAIN
|
|
context['code'] = '0'
|
|
else:
|
|
if bill.status in ['F', 'H']:
|
|
context['code'] = '0'
|
|
context['response'] = u'Оплата счета не возможна'
|
|
else:
|
|
bill.status = 'P'
|
|
context['code'] = '1'
|
|
bill.save()
|
|
context['data'] = bill.get_face()
|
|
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=False, need_keys=['id', 'status'], method='GET', check_request=True)
|
|
def change_service_request_status(request, context):
|
|
s = ServiceRequest.objects.get(id=request.GET['id'])
|
|
s.status = request.GET['status']
|
|
s.manager = request.user
|
|
if request.GET.get('description'):
|
|
s.cancel_description = request.GET['description']
|
|
s.save()
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=False, need_keys=['id'], method='GET', check_request=True)
|
|
def check_service_request(request, context):
|
|
s = ServiceRequest.objects.get(id=request.GET['id'])
|
|
if s.manager and s.manager != request.user:
|
|
context['code'] = '0'
|
|
else:
|
|
context['code'] = '1'
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=False, need_keys=['course', 'user'], method='GET', check_request=True)
|
|
def check_bill(request, context):
|
|
if Bill.objects.filter(service__course=Course.objects.get(id=request.GET['course']),
|
|
user=User.objects.get(id=request.GET['user']), status='F').exists():
|
|
context['code'] = '1'
|
|
else:
|
|
context['code'] = '0'
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=False, need_keys=['type'], method='POST', check_request=True)
|
|
def create_service_request(request, context):
|
|
if request.POST.get('course'):
|
|
course = Course.objects.get(id=request.POST['course'])
|
|
if ServiceRequest.objects.filter(
|
|
Q(status='S', student=request.user, course=course) | Q(status='W', student=request.user,
|
|
course=course)).exists():
|
|
context['code'] = '0'
|
|
context['response'] = u'Вы уже отправляли запрос на покупку этого курса. Запрос еще не обработан.'
|
|
|
|
elif Bill.objects.filter(user=request.user, status='W', service__course=course).exists():
|
|
context['code'] = '0'
|
|
context['response'] = u'Счет на покупку этого курса уже был выставлен.'
|
|
|
|
else:
|
|
context['code'] = '1'
|
|
ServiceRequest.objects.create(student=request.user, course=course, _type=request.POST['type'])
|
|
context['data'] = {'course': course.title}
|
|
context['data']['phone'] = request.user.get_phone()
|
|
else:
|
|
context['code'] = '1'
|
|
ServiceRequest.objects.get_or_create(student=request.user, _type=request.POST['type'])
|
|
context['data'] = {'course': ''}
|
|
context['data']['phone'] = request.user.get_phone()
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=False, need_keys=['id'], method='POST', check_request=True)
|
|
def cancel_service_request(request, context):
|
|
bill = ServiceRequest.objects.get(id=request.POST['id'])
|
|
bill.status = 'B'
|
|
bill.save()
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=False, need_keys=['ID'], method='POST', check_request=True)
|
|
def remove_service_request(request, context):
|
|
try:
|
|
service_request = ServiceRequest.objects.get(id=request.POST['ID'])
|
|
except ServiceRequest.DoesNotExist:
|
|
context['code'] = '0'
|
|
context['response'] = u'Запрос не найден'
|
|
else:
|
|
if request.user.in_role in ['A', 'S', 'S2']:
|
|
service_request.delete()
|
|
context['code'] = '1'
|
|
else:
|
|
context['code'] = '0'
|
|
context['response'] = u'Не достаточно прав'
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=False, need_keys=['id'], method='GET', check_request=True)
|
|
def reply_bill(request, context):
|
|
try:
|
|
bill = Bill.objects.get(id=request.GET['id'])
|
|
except Bill.DoesNotExist:
|
|
context['code'] = '0'
|
|
else:
|
|
bill.status = 'W'
|
|
bill.save()
|
|
context['code'] = '1'
|
|
return context
|
|
|
|
|
|
@api_decor(without_auth=False)
|
|
def get_user_from_bill_id(request, context):
|
|
# Получение пользователя привязанного к счету
|
|
if request.user.in_role == 'S':
|
|
try:
|
|
bill = Bill.objects.get(id=request.GET['id'])
|
|
except Bill.DoesNotExist:
|
|
context['code'] = '0'
|
|
context['response'] = 'Счет с таким ID не найден'
|
|
else:
|
|
context['data'] = bill.get_face()
|
|
context['code'] = '1'
|
|
else:
|
|
raise Http404
|
|
return context
|
|
|