Conversion_Kitchen_Code/kitchen_counter/institutional/views.py

1662 lines
76 KiB
Python
Raw Permalink Normal View History

2024-04-27 09:33:09 +00:00
# from rest_framework import viewsets
# from rest_framework.decorators import action
# from rest_framework.response import Response
# from .models import CorporateMember, CorporateUser, Transaction, CreditRequest, Payment, CreditRequestFromMember, Institute, AffiliatedStudent, UnaffiliatedStudent, WaitForInstituteStudent, CreditRequestStudent, CreditRequestInstitute
# from .serializers import (
# CorporateMemberSerializer, CorporateUserSerializer, TransactionSerializer,
# CreditRequestSerializer, PaymentSerializer, CreditRequestMemberSerializer,
# InstituteSerializer, AffiliatedStudentSerializer, UnaffiliatedStudentSerializer, WaitForInstituteStudentSerializer,CreditRequestInstituteSerializer, CreditRequestStudentSerializer
# )
# from django.http import HttpResponse
# from django.shortcuts import render
# from django.views import View
# from django.contrib.auth.models import User
# from django.conf import settings
# from datetime import datetime, timedelta
# from django.shortcuts import get_object_or_404
# from django.http import JsonResponse
# from django.db import transaction as db_transaction
# from django.db.models import Sum, Q
# from django.db import transaction
# from auto_email.views import sendmail
# from reportlab.lib.pagesizes import letter
# from reportlab.lib import colors
# from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
# from reportlab.lib.styles import getSampleStyleSheet
# from reportlab.lib.units import inch
# import json
# from django.utils import timezone
# DISCOUNT = settings.IM_DISCOUNT
# from MNF.settings import discount_limit_handler
from django.shortcuts import redirect
class CorporateReg(View):
def get(self, request, *args, **kwargs):
user = request.user
if CorporateMember.objects.filter(user=user).exists():
return redirect('/institute/')
elif CorporateUser.objects.filter(user= user).exists():
return redirect('/institute/')
return render(request, "institutional/intermidiate.html", context)
elif Institute.objects.filter(user= user).exists() or AffiliatedStudent.objects.filter(user= user).exists() or UnaffiliatedStudent.objects.filter(user= user).exists() or WaitForInstituteStudent.objects.filter(user= user).exists():
message = "You have already taken other type of membership so you can not take this membership!!"
context = {
'message': message,
}
return render(request, "institutional/intermidiate.html", context)
else:
key = settings.COUNTRY_KEY
context = {
'key': key,
}
return render(request, "institutional/corporateReg.html", context)
class InstituteReg(View):
def get(self, request, *args, **kwargs):
user = request.user
if Institute.objects.filter(user= user).exists():
return redirect('/institute/')
elif AffiliatedStudent.objects.filter(user= user).exists():
return redirect('/institute/')
elif CorporateMember.objects.filter(user=user).exists() or CorporateUser.objects.filter(user= user).exists() or UnaffiliatedStudent.objects.filter(user= user).exists() or WaitForInstituteStudent.objects.filter(user= user).exists():
message = "You have already taken other type of membership so you can not take this membership!!"
context = {
'message': message,
}
return render(request, "institutional/intermidiate.html", context)
else:
key = settings.COUNTRY_KEY
context = {
'key': key,
}
return render(request, "institutional/instituteReg.html", context)
class StudentReg(View):
def get(self, request, *args, **kwargs):
user = request.user
if WaitForInstituteStudent.objects.filter(user= user).exists():
return redirect('/institute/')
elif UnaffiliatedStudent.objects.filter(user= user).exists():
return redirect('/institute/')
elif CorporateMember.objects.filter(user=user).exists() or CorporateUser.objects.filter(user= user).exists() or AffiliatedStudent.objects.filter(user= user).exists() or Institute.objects.filter(user= user).exists():
message = "You have already taken other type of membership so you can not take this membership!!"
context = {
'message': message,
}
return render(request, "institutional/intermidiate.html", context)
else:
key = settings.COUNTRY_KEY
institutes = Institute.objects.all()
context = {
'key': key,
'institutes': institutes,
}
return render(request, "institutional/studentReg.html", context)
class AffliliatedStudentReg(View):
def get(self, request, *args, **kwargs):
key = settings.COUNTRY_KEY
institutes = Institute.objects.all()
context = {
'key': key,
'institutes': institutes,
}
return render(request, "institutional/affstudentReg.html", context)
class Main(View):
def get(self, request, *args, **kwargs):
user = request.user
total_payable_of_users_this_month = total_payable_of_users_last_month = total_payable_of_students_last_month = total_payable_of_students_this_month = 0.0
credit_requests = CreditRequestFromMember.objects.filter(ctype="corporate_membership")
credit_requests_institute = CreditRequestInstitute.objects.filter(ctype="institute_membership")
if user.is_superuser:
context ={
'credit_requests': credit_requests,
'credit_requests_institute': credit_requests_institute,
}
return render(request, 'institutional/AdminDashboard.html', context)
if CorporateMember.objects.filter(user=user).exists() or CorporateUser.objects.filter(user= user).exists() or Institute.objects.filter(user= user).exists() or AffiliatedStudent.objects.filter(user= user).exists() or UnaffiliatedStudent.objects.filter(user= user).exists() or WaitForInstituteStudent.objects.filter(user= user).exists():
if CorporateMember.objects.filter(user=user).exists():
print("Phase-1")
if CorporateMember.objects.filter(user= user, is_approved="pending").exists():
print("Phase-2")
return render(request, 'institutional/application_under_review.html')
elif CorporateMember.objects.filter(user= user, is_approved="approved").exists():
userdetails = CorporateMember.objects.get(user= user, is_approved="approved")
associated_corporate_users = CorporateUser.objects.filter(this_user_admin=userdetails)
for auser in associated_corporate_users:
total_payable_of_users_this_month += float(auser.used_credit_point)
for auser in associated_corporate_users:
total_payable_of_users_last_month += float(auser.pending_payment)
total_payable_of_member_last_month = float(userdetails.pending_payment)
total_payable_of_member_this_month = float(userdetails.used_credit_point)
input_dt = datetime.now()
next_month = input_dt.replace(day=28) + timedelta(days=4)
print("Phase-3")
validity_time = next_month - timedelta(days=next_month.day)
credit_requests = CreditRequest.objects.filter(request_to=userdetails)
total_payable = 0.0
total_payable += float(userdetails.used_credit_point)
corporate_users = CorporateUser.objects.filter(this_user_admin = userdetails)
for i in corporate_users:
total_payable += float(i.used_credit_point)
context = {
'userdetails': userdetails,
'associated_corporate_users': associated_corporate_users,
'validity_time': validity_time,
'credit_requests': credit_requests,
'last_month_payment': (total_payable_of_member_last_month + total_payable_of_users_last_month),
'payment_till_now': (total_payable_of_member_this_month + total_payable_of_users_this_month + total_payable_of_member_last_month + total_payable_of_users_last_month),
'total_payable': total_payable,
}
print("Phase-4")
return render(request, 'institutional/CorporateMemberDashboard.html', context)
else:
application = CorporateMember.objects.get(user=user)
message = "Your Registration request for Corporate Membership is under review."
application_type = "CorporateMembership"
print("Phase-5")
if CorporateUser.objects.filter(user=user).exists():
print("Phase-1")
if CorporateUser.objects.filter(user= user, is_approved="banned").exists():
print("Phase-2")
return render(request, 'institutional/corporateUserBan.html')
if CorporateUser.objects.filter(user= user, is_approved="active").exists():
userdetails = CorporateUser.objects.get(user= user, is_approved="active")
input_dt = datetime.now()
next_month = input_dt.replace(day=28) + timedelta(days=4)
validity_time = next_month - timedelta(days=next_month.day)
transactions = Transaction.objects.filter(user=user)
print("Phase-3")
credit_requests_send = CreditRequest.objects.filter(request_from=userdetails)
context = {
'userdetails': userdetails,
'validity_time': validity_time,
'credit_requests_send': credit_requests_send,
'transactions': transactions,
}
print("Phase-4")
return render(request, 'institutional/CorporateUserDashboard.html', context)
if Institute.objects.filter(user=user).exists():
print("Phase-1")
if Institute.objects.filter(user= user, is_approved="pending").exists():
print("Phase-2")
return render(request, 'institutional/application_under_review.html')
elif Institute.objects.filter(user= user, is_approved="approved").exists():
userdetails = Institute.objects.get(user= user, is_approved="approved")
affiliated_student = AffiliatedStudent.objects.filter(institute=userdetails)
for auser in affiliated_student:
total_payable_of_students_this_month += float(auser.used_credit_point)
for auser in affiliated_student:
total_payable_of_students_last_month += float(auser.pending_payment)
total_payable_of_institute_last_month = float(userdetails.pending_payment)
total_payable_of_institute_this_month = float(userdetails.used_credit_point)
input_dt = datetime.now()
next_month = input_dt.replace(day=28) + timedelta(days=4)
print("Phase-3")
validity_time = next_month - timedelta(days=next_month.day)
credit_requests = CreditRequestStudent.objects.filter(request_to=userdetails)
total_payable = 0
total_payable += float(userdetails.used_credit_point)
affiliated_students = AffiliatedStudent.objects.filter(institute = userdetails)
for i in affiliated_students:
total_payable += float(i.used_credit_point)
context = {
'total_payable': total_payable,
'userdetails': userdetails,
'affiliated_student': affiliated_student,
'validity_time': validity_time,
'credit_requests': credit_requests,
'last_month_payment': (total_payable_of_institute_last_month + total_payable_of_students_last_month),
'payment_till_now': (total_payable_of_institute_this_month + total_payable_of_students_this_month + total_payable_of_institute_last_month + total_payable_of_students_last_month),
}
print("Phase-4")
return render(request, 'institutional/InstituteDashboard.html', context)
else:
application = Institute.objects.get(user=user)
message = "Your Registration request for Institute Membership is under review."
application_type = "InstituteMembership"
print("Phase-5")
if AffiliatedStudent.objects.filter(user=user).exists():
print("Phase-1")
if AffiliatedStudent.objects.filter(user= user, is_approved="pending").exists():
print("Phase-2")
return render(request, 'institutional/application_under_review.html')
elif AffiliatedStudent.objects.filter(user= user, is_approved="approved").exists():
userdetails = AffiliatedStudent.objects.get(user= user, is_approved="approved")
credit_requests = CreditRequestStudent.objects.filter(request_from=userdetails)
transactions = Transaction.objects.filter(user=user)
print("Phase-3")
context = {
'userdetails': userdetails,
'credit_requests': credit_requests,
'transactions': transactions,
}
print("Phase-4")
return render(request, 'institutional/AffiliatedStudentDashboard.html', context)
else:
application = AffiliatedStudent.objects.get(user=user)
message = "Your Registration request for Affiliated Student Membership is under review."
application_type = "AffiliatedStudentMembership"
print("Phase-5")
return render(request, 'institutional/corporateUserBan.html')
if UnaffiliatedStudent.objects.filter(user=user).exists():
print("Phase-1")
if UnaffiliatedStudent.objects.filter(user= user, is_approved="pending").exists():
print("Phase-2")
return render(request, 'institutional/application_under_review.html')
elif UnaffiliatedStudent.objects.filter(user= user, is_approved="approved").exists():
userdetails = UnaffiliatedStudent.objects.get(user= user, is_approved="approved")
context = {
'userdetails': userdetails,
}
print("Phase-3")
return render(request, 'institutional/UnaffiliatedStudentDashboard.html', context)
else:
application = UnaffiliatedStudent.objects.get(user=user)
message = "Your Registration request for Unaffiliated Student Membership is under review."
application_type = "UnaffiliatedStudentMembership"
print("Phase-4")
if WaitForInstituteStudent.objects.filter(user=user).exists():
print("Phase-1")
if WaitForInstituteStudent.objects.filter(user= user, is_approved="pending").exists():
print("Phase-2")
return render(request, 'institutional/application_under_review.html')
elif WaitForInstituteStudent.objects.filter(user= user, is_approved="approved").exists():
userdetails = WaitForInstituteStudent.objects.get(user= user, is_approved="approved")
institute_registered = Institute.objects.filter(institute_email= userdetails.institute_email).exists()
print("Phase-3")
context = {
'userdetails': userdetails,
'institute_registered': institute_registered,
}
print("Phase-4")
return render(request, 'institutional/WaitForInstituteStudentDashboard.html', context)
else:
application = WaitForInstituteStudent.objects.get(user=user)
message = "Your Registration request for Wait For Institute Student Membership is under review."
application_type = "WaitForInstituteStudentMembership"
print("Phase-5")
context = {
'message': message,
'application': application,
'application_type': application_type,
}
print("Phase-6")
return render(request, 'institutional/Reject_Application.html', context)
message = "You have not Taken any membership yet!! Please Take any membership to view the Member Dashboard."
context = {
'message': message,
}
return render(request, "institutional/intermidiate.html", context)
class CorporateMemberViewSet(viewsets.ViewSet):
def list(self, request):
queryset = CorporateMember.objects.all()
serializer = CorporateMemberSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
member = CorporateMember.objects.get(pk=pk)
serializer = CorporateMemberSerializer(member)
return Response(serializer.data)
def create(self, request):
serializer = CorporateMemberSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
sendmail(to_email=[request.data.get("contact_person_email")], email_code="IP1")
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
def update(self, request, pk=None):
member = CorporateMember.objects.get(pk=pk)
serializer = CorporateMemberSerializer(member, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def partial_update(self, request, pk=None):
member = CorporateMember.objects.get(pk=pk)
serializer = CorporateMemberSerializer(member, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
if request.data.get("is_approved") == "rejected":
sendmail(to_email=[member.contact_person_email], email_code="IP3")
if request.data.get("is_approved") == "approved":
sendmail(to_email=[member.contact_person_email], email_code="IP2")
return Response(serializer.data)
return Response(serializer.errors, status=400)
def destroy(self, request, pk=None):
member = CorporateMember.objects.get(pk=pk)
member.delete()
return Response(status=204)
class CorporateUserViewSet(viewsets.ViewSet):
def list(self, request):
queryset = CorporateUser.objects.all()
serializer = CorporateUserSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
user = CorporateUser.objects.get(pk=pk)
serializer = CorporateUserSerializer(user)
return Response(serializer.data)
def create(self, request):
serializer = CorporateUserSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
em = CorporateMember.objects.get(corporate_member_uuid = request.data.get("this_user_admin"))
sendmail(to_email=[em.contact_person_email],email_code="IP4")
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
def update(self, request, pk=None):
user = CorporateUser.objects.get(pk=pk)
serializer = CorporateUserSerializer(user, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def partial_update(self, request, pk=None):
user = CorporateUser.objects.get(pk=pk)
serializer = CorporateUserSerializer(user, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
if request.data.get("is_approved") == 'active' :
sendmail(to_email=[user.contact_person_email], email_code="IP15")
if request.data.get("is_approved") == 'banned' :
sendmail(to_email=[user.contact_person_email], email_code="IP14")
if (request.data.get("services_access_premise_pool") == user.services_access_premise_pool):
sendmail(to_email=[user.contact_person_email], email_code="IP11")
return Response(serializer.data)
return Response(serializer.errors, status=400)
def destroy(self, request, pk=None):
user = CorporateUser.objects.get(pk=pk)
user.delete()
sendmail(to_email=[user.contact_person_email], email_code="IP3")
return Response(status=204)
class TransactionViewSet(viewsets.ViewSet):
def list(self, request):
queryset = Transaction.objects.all()
serializer = TransactionSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
transaction = Transaction.objects.get(pk=pk)
serializer = TransactionSerializer(transaction)
return Response(serializer.data)
def create(self, request):
serializer = TransactionSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
def update(self, request, pk=None):
transaction = Transaction.objects.get(pk=pk)
serializer = TransactionSerializer(transaction, data=request.data)
if serializer.is_valid():
serializer.save()
#
return Response(serializer.data)
return Response(serializer.errors, status=400)
def partial_update(self, request, pk=None):
transaction = Transaction.objects.get(pk=pk)
serializer = TransactionSerializer(transaction, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
#
return Response(serializer.data)
return Response(serializer.errors, status=400)
def destroy(self, request, pk=None):
transaction = Transaction.objects.get(pk=pk)
transaction.delete()
return Response(status=204)
class CreditRequestViewSet(viewsets.ViewSet):
def list(self, request):
queryset = CreditRequest.objects.all()
serializer = CreditRequestSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
credit_request = CreditRequest.objects.get(pk=pk)
serializer = CreditRequestSerializer(credit_request)
return Response(serializer.data)
def create(self, request):
serializer = CreditRequestSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
useremail = CorporateUser.objects.get(corporate_user_uuid = request.data.get("request_from"))
sendmail(to_email=[useremail.contact_person_email], email_code="IP5")
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
def update(self, request, pk=None):
credit_request = CreditRequest.objects.get(pk=pk)
serializer = CreditRequestSerializer(credit_request, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def partial_update(self, request, pk=None):
credit_request = CreditRequest.objects.get(pk=pk)
serializer = CreditRequestSerializer(credit_request, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def destroy(self, request, pk=None):
credit_request = CreditRequest.objects.get(pk=pk)
credit_request.delete()
sendmail(to_email=[credit_request.request_from.contact_person_email], email_code="IP7")
return Response(status=204)
class CreditRequestMemberViewSet(viewsets.ViewSet):
def list(self, request):
queryset = CreditRequestFromMember.objects.all()
serializer = CreditRequestMemberSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
credit_request = CreditRequestFromMember.objects.get(pk=pk)
serializer = CreditRequestMemberSerializer(credit_request)
return Response(serializer.data)
def create(self, request):
serializer = CreditRequestMemberSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
useremail = CorporateMember.objects.get(corporate_member_uuid = request.data.get("request_from"))
sendmail(to_email=[useremail.contact_person_email], email_code="IP8")
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
def update(self, request, pk=None):
credit_request = CreditRequestFromMember.objects.get(pk=pk)
serializer = CreditRequestMemberSerializer(credit_request, data=request.data)
if serializer.is_valid():
serializer.save()
#
return Response(serializer.data)
return Response(serializer.errors, status=400)
def partial_update(self, request, pk=None):
credit_request = CreditRequestFromMember.objects.get(pk=pk)
serializer = CreditRequestMemberSerializer(credit_request, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def destroy(self, request, pk=None):
credit_request = CreditRequestFromMember.objects.get(pk=pk)
credit_request.delete()
sendmail(to_email=[credit_request.request_from.contact_person_email], email_code="IP10")
return Response(status=204)
class PaymentViewSet(viewsets.ViewSet):
def list(self, request):
queryset = Payment.objects.all()
serializer = PaymentSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
payment = Payment.objects.get(pk=pk)
serializer = PaymentSerializer(payment)
return Response(serializer.data)
def create(self, request):
serializer = PaymentSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
def update(self, request, pk=None):
payment = Payment.objects.get(pk=pk)
serializer = PaymentSerializer(payment, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def partial_update(self, request, pk=None):
payment = Payment.objects.get(pk=pk)
serializer = PaymentSerializer(payment, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def destroy(self, request, pk=None):
payment = Payment.objects.get(pk=pk)
payment.delete()
return Response(status=204)
class InstituteViewSet(viewsets.ViewSet):
def list(self, request):
queryset = Institute.objects.all()
serializer = InstituteSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
institute = Institute.objects.get(pk=pk)
serializer = InstituteSerializer(institute)
return Response(serializer.data)
def create(self, request):
serializer = InstituteSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
sendmail(to_email=[request.data.get("institute_email")], email_code="IP16")
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
def update(self, request, pk=None):
institute = Institute.objects.get(pk=pk)
serializer = InstituteSerializer(institute, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def partial_update(self, request, pk=None):
institute = Institute.objects.get(pk=pk)
serializer = InstituteSerializer(institute, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
if request.data.get("is_approved") == "rejected":
sendmail(to_email=[institute.admin_email], email_code="IP18")
if request.data.get("is_approved") == "approved":
sendmail(to_email=[institute.admin_email], email_code="IP17")
return Response(serializer.data)
return Response(serializer.errors, status=400)
def destroy(self, request, pk=None):
institute = Institute.objects.get(pk=pk)
institute.delete()
return Response(status=204)
class AffiliatedStudentViewSet(viewsets.ViewSet):
def list(self, request):
queryset = AffiliatedStudent.objects.all()
serializer = AffiliatedStudentSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
affiliated_student = AffiliatedStudent.objects.get(pk=pk)
serializer = AffiliatedStudentSerializer(affiliated_student)
return Response(serializer.data)
def create(self, request):
serializer = AffiliatedStudentSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
sendmail(to_email=[request.data.get("student_institute_email")], email_code="IP30")
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
def update(self, request, pk=None):
affiliated_student = AffiliatedStudent.objects.get(pk=pk)
serializer = AffiliatedStudentSerializer(affiliated_student, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def partial_update(self, request, pk=None):
affiliated_student = AffiliatedStudent.objects.get(pk=pk)
serializer = AffiliatedStudentSerializer(affiliated_student, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
if request.data.get("is_approved") == 'approved' :
sendmail(to_email=[affiliated_student.student_institute_email], email_code="IP28")
if request.data.get("is_approved") == 'rejected' :
sendmail(to_email=[affiliated_student.student_institute_email], email_code="IP27")
return Response(serializer.data)
return Response(serializer.errors, status=400)
def destroy(self, request, pk=None):
affiliated_student = AffiliatedStudent.objects.get(pk=pk)
affiliated_student.delete()
return Response(status=204)
class UnaffiliatedStudentViewSet(viewsets.ViewSet):
def list(self, request):
queryset = UnaffiliatedStudent.objects.all()
serializer = UnaffiliatedStudentSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
unaffiliated_student = UnaffiliatedStudent.objects.get(pk=pk)
serializer = UnaffiliatedStudentSerializer(unaffiliated_student)
return Response(serializer.data)
def create(self, request):
serializer = UnaffiliatedStudentSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
sendmail(to_email=[request.data.get("student_institute_email")], email_code="IP34")
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
def update(self, request, pk=None):
unaffiliated_student = UnaffiliatedStudent.objects.get(pk=pk)
serializer = UnaffiliatedStudentSerializer(unaffiliated_student, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def partial_update(self, request, pk=None):
unaffiliated_student = UnaffiliatedStudent.objects.get(pk=pk)
serializer = UnaffiliatedStudentSerializer(unaffiliated_student, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
if request.data.get("is_approved") == 'approved' :
sendmail(to_email=[unaffiliated_student.email], email_code="IP35")
if request.data.get("is_approved") == 'rejected' :
sendmail(to_email=[unaffiliated_student.email], email_code="IP36")
return Response(serializer.data)
return Response(serializer.errors, status=400)
def destroy(self, request, pk=None):
unaffiliated_student = UnaffiliatedStudent.objects.get(pk=pk)
unaffiliated_student.delete()
return Response(status=204)
class WaitForInstituteStudentViewSet(viewsets.ViewSet):
def list(self, request):
queryset = WaitForInstituteStudent.objects.all()
serializer = WaitForInstituteStudentSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
wait_for_institute_student = WaitForInstituteStudent.objects.get(pk=pk)
serializer = WaitForInstituteStudentSerializer(wait_for_institute_student)
return Response(serializer.data)
def create(self, request):
serializer = WaitForInstituteStudentSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
sendmail(to_email=[request.data.get("student_institute_email")], email_code="IP37")
sendmail(to_email=[request.data.get("institute_email")], email_code="IP40")
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
def update(self, request, pk=None):
wait_for_institute_student = WaitForInstituteStudent.objects.get(pk=pk)
serializer = WaitForInstituteStudentSerializer(wait_for_institute_student, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def partial_update(self, request, pk=None):
wait_for_institute_student = WaitForInstituteStudent.objects.get(pk=pk)
serializer = WaitForInstituteStudentSerializer(wait_for_institute_student, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
if request.data.get("is_approved") == 'approved' :
sendmail(to_email=[wait_for_institute_student.email], email_code="IP38")
if request.data.get("is_approved") == 'rejected' :
sendmail(to_email=[wait_for_institute_student.email], email_code="IP39")
return Response(serializer.data)
return Response(serializer.errors, status=400)
def destroy(self, request, pk=None):
wait_for_institute_student = WaitForInstituteStudent.objects.get(pk=pk)
wait_for_institute_student.delete()
return Response(status=204)
class CreditRequestStudentViewSet(viewsets.ViewSet):
def list(self, request):
queryset = CreditRequestStudent.objects.all()
serializer = CreditRequestStudentSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
credit_request = CreditRequestStudent.objects.get(pk=pk)
serializer = CreditRequestStudentSerializer(credit_request)
return Response(serializer.data)
def create(self, request):
serializer = CreditRequestStudentSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
Afem = AffiliatedStudent.objects.get(affiliated_student_uuid = request.data.get("request_from"))
sendmail(to_email=[Afem.student_institute_email], email_code="IP20")
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
def update(self, request, pk=None):
credit_request = CreditRequestStudent.objects.get(pk=pk)
serializer = CreditRequestStudentSerializer(credit_request, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def partial_update(self, request, pk=None):
credit_request = CreditRequestStudent.objects.get(pk=pk)
serializer = CreditRequestStudentSerializer(credit_request, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
#
return Response(serializer.data)
return Response(serializer.errors, status=400)
def destroy(self, request, pk=None):
credit_request = CreditRequestStudent.objects.get(pk=pk)
credit_request.delete()
sendmail(to_email=[credit_request.request_from.student_institute_email], email_code="IP21")
return Response(status=204)
class CreditRequestInstituteViewSet(viewsets.ViewSet):
def list(self, request):
queryset = CreditRequestInstitute.objects.all()
serializer = CreditRequestInstituteSerializer(queryset, many=True)
return Response(serializer.data)
def retrieve(self, request, pk=None):
credit_request = CreditRequestInstitute.objects.get(pk=pk)
serializer = CreditRequestInstituteSerializer(credit_request)
return Response(serializer.data)
def create(self, request):
serializer = CreditRequestInstituteSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
em = Institute.objects.get(institute_uuid=request.data.get("request_from"))
sendmail(to_email=[em.admin_email], email_code="IP22")
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
def update(self, request, pk=None):
credit_request = CreditRequestInstitute.objects.get(pk=pk)
serializer = CreditRequestInstituteSerializer(credit_request, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def partial_update(self, request, pk=None):
credit_request = CreditRequestInstitute.objects.get(pk=pk)
serializer = CreditRequestInstituteSerializer(credit_request, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=400)
def destroy(self, request, pk=None):
credit_request = CreditRequestInstitute.objects.get(pk=pk)
credit_request.delete()
sendmail(to_email=[credit_request.request_from.institute_email], email_code="IP23")
return Response(status=204)
def get_user_transactions(request, user_id):
user = get_object_or_404(User, id=user_id)
transactions = Transaction.objects.filter(user=user)
# Create a list to hold the serialized transaction data
serialized_transactions = []
# Serialize each transaction and add it to the list
for transaction in transactions:
serialized_transaction = {
'transaction_uuid': str(transaction.transaction_uuid),
'user': transaction.user.username,
'date': transaction.date.isoformat(),
'user_type': transaction.user_type,
'service_type': transaction.service_type,
'amount': str(transaction.amount),
}
serialized_transactions.append(serialized_transaction)
# Return the serialized transactions as JSON response
return JsonResponse(serialized_transactions, safe=False)
def check_corporate_user_exists(request):
if request.method == 'POST':
name = request.POST.get('name')
email = request.POST.get('email')
# Perform the logic to check if a user with the provided name and email exists
user_exists = User.objects.filter(email=email).exists()
if user_exists:
corporate_user_exists = CorporateUser.objects.filter(contact_person_email=email).exists()
if corporate_user_exists:
response_data = {
'userExists': 'corporate_user'
}
return JsonResponse(response_data)
else:
user = User.objects.get(email=email)
response_data = {
'userExists': 'normal_user',
'userId': user.pk
}
return JsonResponse(response_data)
else:
response_data = {
'userExists': 'not_user'
}
return JsonResponse(response_data)
return JsonResponse({'error': 'Invalid request method'})
def check_domain_available(request):
if request.method == 'POST':
domain_name = request.POST.get('domainname')
# Perform the logic to check if a user with the provided name and email exists
user_exists = Institute.objects.filter(domain_name=domain_name).exists()
response_data = {
'userExists': True
}
if user_exists:
institute = Institute.objects.filter(domain_name=domain_name).exists()
if institute:
response_data = {
'userExists': False
}
return JsonResponse(response_data)
return JsonResponse({'error': 'Invalid request method'})
def make_transaction(user, service_type, service_id, amount):
user = User.objects.get(pk=user)
print("User Found")
print(service_type)
if CorporateUser.objects.filter(user=user).exists():
user_type = 'corporate_user'
elif CorporateMember.objects.filter(user=user).exists():
user_type = 'corporate_member'
elif Institute.objects.filter(user=user).exists():
user_type = 'institute'
elif AffiliatedStudent.objects.filter(user=user).exists():
user_type = 'student'
else:
user_type = ''
if CorporateUser.objects.filter(user=user).exists():
print("Corporate User")
cuser = CorporateUser.objects.get(user=user)
print("cuser")
cuadmin = cuser.this_user_admin
print('cuadmin')
print(cuser.services_access_subtitle)
print(cuser.services_access_viewers_lounge)
print(cuser.services_access_project_center)
print(cuser.services_access_conversion)
print(cuser.services_access_script_builder)
print(cuser.services_access_script_builder)
if cuser.is_approved != "active":
return False
if float(cuser.available_credit_points) <= float(amount):
return False
if service_type == 'Subtitle':
print("--------------1")
print(cuser.services_access_subtitle)
if cuser.services_access_subtitle == False:
return False
if service_type == 'Viewers Lounge':
print("--------------2")
print(cuser.services_access_viewers_lounge)
if cuser.services_access_viewers_lounge == False:
return False
if service_type == 'Project Center':
print("--------------3")
print(cuser.services_access_project_center)
if cuser.services_access_project_center == False:
return False
if service_type == 'Conversion':
print("--------------4")
print(cuser.services_access_conversion)
if cuser.services_access_conversion == False:
return False
if service_type == 'Narration':
print("--------------5")
print(cuser.services_access_narration)
if cuser.services_access_narration == False:
return False
if service_type == 'Script Builder':
print("--------------6")
print(cuser.services_access_script_builder)
if cuser.services_access_script_builde == Falser:
return False
if service_type == 'Premise Pool':
print("--------------7")
print(cuser.services_access_premise_pool)
if cuser.services_access_premise_pool == False:
return False
try:
with db_transaction.atomic():
transaction = Transaction.objects.create(
user=user,
user_type=user_type,
service_type=service_type,
service_id=service_id,
amount=amount,
date=timezone.now()
)
print('transaction added')
cuser.available_credit_points = float(cuser.available_credit_points) - float(amount)
print("user available_credit_points")
cuser.used_credit_point = float(cuser.used_credit_point) + float(amount)
print("user used_credit_point")
cuser.pending_payment = float(cuser.pending_payment) + float(amount)
print("user pending_payment")
cuadmin.pending_payment = float(cuadmin.pending_payment) + float(amount)
print("user admin pending_payment")
cuser.save()
cuadmin.save()
return True
except:
return False
elif CorporateMember.objects.filter(user=user).exists():
print("Corporate Member")
member = CorporateMember.objects.get(user=user)
print('member')
if member.is_approved != "approved":
return False
if float(member.available_credit_points) <= float(amount):
return False
try:
with db_transaction.atomic():
transaction = Transaction.objects.create(
user=user,
user_type=user_type,
service_type=service_type,
service_id=service_id,
amount=amount,
date=timezone.now()
)
print('transaction added')
member.available_credit_points = float(member.available_credit_points) - float(amount)
print('member available_credit_points')
member.used_credit_point = float(member.used_credit_point) + float(amount)
print('member used_credit_point')
member.pending_payment = float(member.pending_payment) + float(amount)
print('member pending_payment')
member.save()
return True
except:
return False
elif AffiliatedStudent.objects.filter(user=user).exists():
print('AffiliatedStudent')
astudent = AffiliatedStudent.objects.get(user=user)
print('astudent')
asinstitute = astudent.institute
print('asinstitute')
if astudent.is_approved != "approved":
return False
if float(astudent.available_credit_points) <= float(amount):
return False
try:
with db_transaction.atomic():
transaction = Transaction.objects.create(
user=user,
user_type=user_type,
service_type=service_type,
service_id=service_id,
amount=amount,
date=timezone.now()
)
print('transaction added')
astudent.available_credit_points = float(astudent.available_credit_points) - float(amount)
print('astudent available_credit_points')
astudent.used_credit_point = float(astudent.used_credit_point) + float(amount)
print('astudent used_credit_point')
astudent.pending_payment = float(astudent.pending_payment) + float(amount)
print('astudent pending_payment')
asinstitute.pending_payment = float(asinstitute.pending_payment) + float(amount)
print('asinstitute pending_payment')
astudent.save()
asinstitute.save()
return True
except:
return False
elif Institute.objects.filter(user=user).exists():
print('Institute')
institute = Institute.objects.get(user=user)
print('institute')
if institute.is_approved != "approved":
return False
if float(institute.available_credit_points) <= float(amount):
return False
try:
with db_transaction.atomic():
transaction = Transaction.objects.create(
user=user,
user_type=user_type,
service_type=service_type,
service_id=service_id,
amount=amount,
date=timezone.now()
)
print('transaction added')
institute.available_credit_points = float(institute.available_credit_points) - float(amount)
print('institute available_credit_points')
institute.used_credit_point = float(institute.used_credit_point) + float(amount)
print('institute used_credit_point')
institute.pending_payment = float(institute.pending_payment) + float(amount)
print('institute pending_payment')
institute.save()
return True
except:
return False
else:
return False
def run_credit_transfer(request):
# Filter corporate members and users whose join date is before the first day of the next month
corporate_members = CorporateMember.objects.all()
corporate_users = CorporateUser.objects.all()
# Transfer used_credit_points to pending_payment for corporate members
with transaction.atomic():
for member in corporate_members:
member.pending_payment += float(member.used_credit_point)
member.used_credit_point = 0.0
member.credit_limit = member.requested_credit_point
member.available_credit_points = member.requested_credit_point
member.save()
# Transfer used_credit_points to pending_payment for corporate users
with transaction.atomic():
for user in corporate_users:
user.pending_payment += float(user.used_credit_point)
user.used_credit_point = 0.0
user.save()
institutes = Institute.objects.all()
astudents = AffiliatedStudent.objects.all()
# Transfer used_credit_points to pending_payment for Institute
with transaction.atomic():
for institute in institutes:
institute.pending_payment += float(institute.used_credit_point)
institute.used_credit_point = 0.0
institute.credit_limit = institute.requested_credit_point
institute.available_credit_points = institute.requested_credit_point
institute.save()
# Transfer used_credit_points to pending_payment for Affiliated Students
with transaction.atomic():
for astudent in astudents:
astudent.pending_payment += float(astudent.used_credit_point)
astudent.used_credit_point = 0.0
astudent.save()
return HttpResponse("Credit transfer completed successfully.")
def get_discount(user_id, amount):
user = User.objects.get(pk=user_id)
payable_after_s_discount = amount - ((amount*50)/100)
if UnaffiliatedStudent.objects.filter(user=user).exists():
user = UnaffiliatedStudent.objects.get(user=user)
if float(user.free_credits) >= float(payable_after_s_discount):
return 100
if float(user.free_credits) != 0.0:
return ((float(user.free_credits)/amount)*100) + 50
return discount_limit_handler(DISCOUNT["student_discount"])
elif WaitForInstituteStudent.objects.filter(user=user).exists():
user = WaitForInstituteStudent.objects.get(user=user)
if float(user.free_credits) >= float(payable_after_s_discount):
return 100
if float(user.free_credits) != 0.00:
return ((float(user.free_credits)/amount)*100) + 50
return discount_limit_handler(DISCOUNT["student_discount"])
elif AffiliatedStudent.objects.filter(user=user).exists():
user = AffiliatedStudent.objects.get(user=user)
if float(user.free_credits) >= float(payable_after_s_discount):
return 100
if float(user.free_credits) != 0:
return ((float(user.free_credits)/amount)*100) + 50
return discount_limit_handler(DISCOUNT["student_discount"])
else:
return 0
def after_student_discount(user_id, amount):
user = User.objects.get(pk=user_id)
if UnaffiliatedStudent.objects.filter(user=user).exists():
user = UnaffiliatedStudent.objects.get(user=user)
user.free_credits -= amount
user.save()
return True
elif WaitForInstituteStudent.objects.filter(user=user).exists():
user = WaitForInstituteStudent.objects.get(user=user)
user.free_credits -= amount
user.save()
return True
elif AffiliatedStudent.objects.filter(user=user).exists():
user = AffiliatedStudent.objects.get(user=user)
user.free_credits -= amount
user.save()
return True
else:
return False
def check_institute_exists(request):
if request.method == 'POST':
email = request.POST.get('email')
# Perform the logic to check if a user with the provided name and email exists
institute = Institute.objects.filter(institute_email=email).exists()
if institute:
response_data = {
'userExists': True,
'uuid': Institute.objects.get(institute_email=email).institute_uuid,
}
return JsonResponse(response_data)
else:
response_data = {
'userExists': False,
}
return JsonResponse(response_data)
return JsonResponse({'error': 'Invalid request method'})
def update_transactions(user_id, amount):
user = User.objects.get(pk=user_id)
total_payable_of_users_this_month = total_payable_of_users_last_month = total_payable_of_member_last_month = total_payable_of_member_this_month = last_month_payment = payment_till_now = 0
if CorporateMember.objects.filter(user=user).exists():
userdetails = CorporateMember.objects.get(user= user, is_approved="approved")
associated_corporate_users = CorporateUser.objects.filter(this_user_admin=userdetails)
for auser in associated_corporate_users:
total_payable_of_users_this_month += float(auser.used_credit_point)
for auser in associated_corporate_users:
total_payable_of_users_last_month += float(auser.pending_payment)
total_payable_of_member_last_month = float(userdetails.pending_payment)
total_payable_of_member_this_month = float(userdetails.used_credit_point)
last_month_payment= float(total_payable_of_member_last_month + total_payable_of_users_last_month)
payment_till_now = float(total_payable_of_member_this_month + total_payable_of_users_this_month + total_payable_of_member_last_month + total_payable_of_users_last_month)
if amount == payment_till_now:
print(amount, last_month_payment)
print("Paid all payments of the last month!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
member = CorporateMember.objects.get(user = user)
# Calculate the start and end dates of the last month
today = datetime.now()
last_month_end_date = today.replace(day=1) - timedelta(days=1)
last_month_start_date = last_month_end_date.replace(day=1)
# Get the associated CorporateUsers
associated_users = CorporateUser.objects.filter(this_user_admin=member)
# Create a list of user IDs for the associated CorporateUsers
associated_user_ids = list(associated_users.values_list('user__id', flat=True))
# Add the CorporateMember's user ID to the list
associated_user_ids.append(member.user.id)
print(associated_user_ids)
last_month_transactions = Transaction.objects.filter(
Q(user__id__in=associated_user_ids) &
Q(date__gte=last_month_start_date, date__lte=today) &
Q(status='pending')
)
for last_month_transaction in last_month_transactions:
print(last_month_transaction.transaction_uuid)
#last_month_transactions.update(status='paid')
print(amount, payment_till_now)
print("Paid all payments till now!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
return True
elif amount == last_month_payment:
print(amount, last_month_payment)
print("Paid all payments of the last month!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
member = CorporateMember.objects.get(user = user)
# Calculate the start and end dates of the last month
today = datetime.now()
last_month_end_date = today.replace(day=1) - timedelta(days=1)
last_month_start_date = last_month_end_date.replace(day=1)
# Get the associated CorporateUsers
associated_users = CorporateUser.objects.filter(this_user_admin=member)
# Create a list of user IDs for the associated CorporateUsers
associated_user_ids = list(associated_users.values_list('user__id', flat=True))
# Add the CorporateMember's user ID to the list
associated_user_ids.append(member.user.id)
print(associated_user_ids)
last_month_transactions = Transaction.objects.filter(
Q(user__id__in=associated_user_ids) &
Q(date__gte=last_month_start_date, date__lte=last_month_end_date) &
Q(status='pending')
)
for last_month_transaction in last_month_transactions:
print(last_month_transaction.transaction_uuid)
#last_month_transactions.update(status='paid')
return True
else:
print(amount, last_month_payment, payment_till_now)
print("Something went wrong")
return False
return False
def open_bill_corporate(request):
if request.method == 'POST':
total_payable = 0.0
uuid = request.POST['uuid']
corporate_member = CorporateMember.objects.get(corporate_member_uuid = uuid)
total_payable += float(corporate_member.used_credit_point)
corporate_users = CorporateUser.objects.filter(this_user_admin = corporate_member)
for i in corporate_users:
total_payable += float(i.used_credit_point)
transactions = Transaction.objects.filter(user=corporate_member.user, status='pending')
corporate_users_transactions = {}
for i in corporate_users:
user_transactions = Transaction.objects.filter(user=i.user, status='pending')
corporate_users_transactions[i.corporate_user_uuid] = {
'name': i.name,
'transactions': user_transactions
}
discount_percentage = 0
discount_value = 0.0
if total_payable <= 5000.00:
discount_percentage = 10
discount_value = round(float((total_payable*10)/100),2)
elif total_payable > 5000.00 and total_payable <= 25000.00:
discount_percentage = 20
discount_value = round(float((total_payable*20)/100),2)
else:
discount_percentage = 30
discount_value = round(float((total_payable*30)/100),2)
final_payable = round(float(total_payable) - float(discount_value),2)
context={
'corporate_member': corporate_member,
'corporate_users': corporate_users,
'total_payable': total_payable,
'transactions': transactions,
'corporate_users_transactions': corporate_users_transactions,
'discount_percentage': discount_percentage,
'discount_value': discount_value,
'final_payable': final_payable,
}
return render(request, 'institutional/corporate_bill.html', context=context)
def payment_received_corporate_member(request):
if request.method == 'POST':
try:
data = json.loads(request.body)
uuid = data.get('uuid')
if uuid:
corporate_member = CorporateMember.objects.get(corporate_member_uuid=uuid)
corporate_member.used_credit_point = 0
corporate_member.available_credit_points = corporate_member.requested_credit_point
corporate_member.save()
# Update status for corporate member's transactions
member_transactions = Transaction.objects.filter(user=corporate_member.user, status='pending')
member_transactions.update(status='paid')
# Update status for corporate users' transactions
corporate_users = corporate_member.corporateuser_set.all()
for user in corporate_users:
user.used_credit_point = 0
user.available_credit_points = user.credit_limit
user.save()
user_transactions = Transaction.objects.filter(user=user.user, status='pending')
user_transactions.update(status='paid')
sendmail(to_email=[corporate_member.contact_person_email], email_code="IP12")
return JsonResponse({'message': 'Transactions updated successfully.'})
else:
return JsonResponse({'message': 'UUID is missing or invalid.'}, status=400)
except json.JSONDecodeError:
return JsonResponse({'message': 'Invalid JSON data.'}, status=400)
def open_bill_institute(request):
if request.method == 'POST':
total_payable = 0.0
uuid = request.POST['uuid']
institute = Institute.objects.get(institute_uuid = uuid)
total_payable += float(institute.used_credit_point)
affiliated_students = AffiliatedStudent.objects.filter(institute = institute)
for i in affiliated_students:
total_payable += float(i.used_credit_point)
transactions = Transaction.objects.filter(user=institute.user, status='pending')
affiliated_student_transactions = {}
for i in affiliated_students:
user_transactions = Transaction.objects.filter(user=i.user, status='pending')
affiliated_student_transactions[i.affiliated_student_uuid] = {
'name': i.name,
'transactions': user_transactions
}
discount_percentage = 0
discount_value = 0.0
if total_payable <= 5000.00:
discount_percentage = 5
discount_value = round(float((total_payable*5)/100),2)
elif total_payable > 5000.00 and total_payable <= 25000.00:
discount_percentage = 10
discount_value = round(float((total_payable*10)/100),2)
else:
discount_percentage = 15
discount_value = round(float((total_payable*15)/100),2)
final_payable = round(float(total_payable) - float(discount_value),2)
context={
'institute': institute,
'affiliated_students': affiliated_students,
'total_payable': total_payable,
'transactions': transactions,
'affiliated_student_transactions': affiliated_student_transactions,
'discount_percentage': discount_percentage,
'discount_value': discount_value,
'final_payable': final_payable,
}
return render(request, 'institutional/institute_bill.html', context=context)
def payment_received_institute_member(request):
if request.method == 'POST':
try:
data = json.loads(request.body)
uuid = data.get('uuid')
if uuid:
institute = Institute.objects.get(institute_uuid=uuid)
institute.used_credit_point = 0
institute.available_credit_points = institute.requested_credit_point
institute.save()
# Update status for corporate member's transactions
member_transactions = Transaction.objects.filter(user=institute.user, status='pending')
member_transactions.update(status='paid')
# Update status for corporate users' transactions
affiliated_students = institute.affiliatedstudent_set.all()
for user in affiliated_students:
user.used_credit_point = 0
user.available_credit_points = user.credit_limit
user.save()
user_transactions = Transaction.objects.filter(user=user.user, status='pending')
user_transactions.update(status='paid')
sendmail(to_email=[institute.admin_email], email_code="IP25")
return JsonResponse({'message': 'Transactions updated successfully.'})
else:
return JsonResponse({'message': 'UUID is missing or invalid.'}, status=400)
except json.JSONDecodeError:
return JsonResponse({'message': 'Invalid JSON data.'}, status=400)
def deactivate_expired_accounts():
today = timezone.now().date()
# Deactivate UnaffiliatedStudent accounts
unaffiliated_students_to_deactivate = UnaffiliatedStudent.objects.filter(is_approved='approved', end_date=today)
for student in unaffiliated_students_to_deactivate:
student.is_approved = 'rejected' # Set to 'rejected' to deactivate the account
student.save()
# Deactivate WaitForInstituteStudent accounts
wait_for_institute_students_to_deactivate = WaitForInstituteStudent.objects.filter(is_approved='approved', end_date=today)
for student in wait_for_institute_students_to_deactivate:
student.is_approved = 'rejected' # Set to 'rejected' to deactivate the account
student.save()
def deactivate_accounts_with_pending_payments():
# Deactivate CorporateMember accounts with pending_payment > 0
corporate_members_to_deactivate = CorporateMember.objects.filter(is_approved='approved', pending_payment__gt=0)
for member in corporate_members_to_deactivate:
# Deactivate CorporateUser accounts associated with this Corporate Member
corporate_users_to_deactivate = CorporateUser.objects.filter(is_approved='active', this_user_admin=member)
for user in corporate_users_to_deactivate:
user.is_approved = 'banned' # Set to 'banned' to deactivate the account
user.save()
member.is_approved = 'rejected' # Set to 'rejected' to deactivate the account
member.save()
# Deactivate Institute accounts with pending_payment > 0
institute_members_to_deactivate = InstituteMember.objects.filter(is_approved='approved', pending_payment__gt=0)
for member in institute_members_to_deactivate:
# Deactivate Affiliated Students accounts associated with this Institute Member
affiliated_student_to_deactivate = AffiliatedStudentMember.objects.filter(is_approved='active', institute=member)
for user in affiliated_student_to_deactivate:
user.is_approved = 'rejected' # Set to 'banned' to deactivate the account
user.save()
member.is_approved = 'rejected' # Set to 'rejected' to deactivate the account
member.save()
def send_deactivation_reminder_emails():
today = timezone.now().date()
# Calculate the deactivation dates for each reminder
deactivation_dates = [
today + timedelta(days=3),
today + timedelta(days=15),
today + timedelta(days=30),
]
for deactivation_date in deactivation_dates:
# Deactivate UnaffiliatedStudent accounts
unaffiliated_students_to_remind = UnaffiliatedStudent.objects.filter(
is_approved='approved',
end_date=deactivation_date
)
for student in unaffiliated_students_to_remind:
if deactivation_date == today + timedelta(days=3):
email_code = "IP44" # 3 days prior
elif deactivation_date == today + timedelta(days=15):
email_code = "IP49" # 15 days prior
elif deactivation_date == today + timedelta(days=30):
email_code = "IP54" # 30 days prior
else:
email_code = "IP" # Default email code if none of the conditions match
sendmail(to_email=[student.student_institute_email], email_code=email_code)
# Deactivate WaitForInstituteStudent accounts
wait_for_institute_students_to_remind = WaitForInstituteStudent.objects.filter(
is_approved='approved',
end_date=deactivation_date
)
for student in wait_for_institute_students_to_remind:
if deactivation_date == today + timedelta(days=3):
email_code = "IP45" # 3 days prior
elif deactivation_date == today + timedelta(days=15):
email_code = "IP50" # 15 days prior
elif deactivation_date == today + timedelta(days=30):
email_code = "IP55" # 30 days prior
else:
email_code = "IP" # Default email code if none of the conditions match
sendmail(to_email=[student.student_institute_email], email_code=email_code)
# Deactivate AffiliatedStudent accounts
affiliated_students_to_remind = AffiliatedStudent.objects.filter(
is_approved='approved',
end_date=deactivation_date
)
for student in affiliated_students_to_remind:
if deactivation_date == today + timedelta(days=3):
email_code = "IP43" # 3 days prior
elif deactivation_date == today + timedelta(days=15):
email_code = "IP48" # 15 days prior
elif deactivation_date == today + timedelta(days=30):
email_code = "IP53" # 30 days prior
else:
email_code = "IP" # Default email code if none of the conditions match
sendmail(to_email=[student.student_institute_email], email_code=email_code)
# Deactivate CorporateMember accounts with pending_payment > 0
corporate_members_to_remind = CorporateMember.objects.filter(
is_approved='approved',
pending_payment__gt=0
)
for member in corporate_members_to_remind:
if deactivation_date == today + timedelta(days=3):
email_code = "IP42" # 3 days prior
elif deactivation_date == today + timedelta(days=15):
email_code = "IP47" # 15 days prior
elif deactivation_date == today + timedelta(days=30):
email_code = "IP52" # 30 days prior
else:
email_code = "IP" # Default email code if none of the conditions match
sendmail(to_email=[member.contact_person_email], email_code=email_code)
# Deactivate Institute accounts with pending_payment > 0
institute_members_to_remind = InstituteMember.objects.filter(
is_approved='approved',
pending_payment__gt=0
)
for member in institute_members_to_remind:
if deactivation_date == today + timedelta(days=3):
email_code = "IP41" # 3 days prior
elif deactivation_date == today + timedelta(days=15):
email_code = "IP46" # 15 days prior
elif deactivation_date == today + timedelta(days=30):
email_code = "IP51" # 30 days prior
else:
email_code = "IP" # Default email code if none of the conditions match
sendmail(to_email=[member.institute_email], email_code=email_code)
def IM_Tut(request):
return render(request,"institutional/itutorial.html")
def C_Tut(request):
return render(request,"institutional/ctutorial.html")
def S_Tut(request):
return render(request,"institutional/stutorial.html")
def C_demo(request):
return render(request,"institutional/Corporate_demo.html")
def S_demo(request):
return render(request,"institutional/Student_demo.html")
def IM_demo(request):
return render(request,"institutional/intitution_demo.html")
def after_student_discount1(user_id, amount):
user = User.objects.get(pk=user_id)
print("amount neha:",amount)
payment={}
if UnaffiliatedStudent.objects.filter(user=user).exists():
user = UnaffiliatedStudent.objects.get(user=user)
free_credits = user.free_credits
if float(free_credits) > amount:
user.free_credits = float(free_credits) - amount
user.save()
payment["free_credits_student"] = user.free_credits
elif float(free_credits) < amount:
print("in elif")
free_credits = amount - float(free_credits)
user.free_credits = 0.00
user.save()
payment["free_credits_student"] = free_credits
else:
payment["free_credits_student"] = amount
return True,payment
elif WaitForInstituteStudent.objects.filter(user=user).exists():
user = WaitForInstituteStudent.objects.get(user=user)
free_credits = user.free_credits
payment={}
if float(free_credits) > amount:
user.free_credits = float(free_credits) - amount
user.save()
payment["free_credits_student"] = user.free_credits
elif float(free_credits) < amount:
print("in elif")
free_credits = amount - float(free_credits)
user.free_credits = 0.00
user.save()
payment["free_credits_student"] = free_credits
else:
payment["free_credits_student"] = amount
return True,payment
elif AffiliatedStudent.objects.filter(user=user).exists():
user = AffiliatedStudent.objects.get(user=user)
free_credits = user.free_credits
payment={}
if float(free_credits) > amount:
user.free_credits = float(free_credits) - amount
user.save()
payment["free_credits_student"] = user.free_credits
elif float(free_credits) < amount:
print("in elif")
free_credits = amount - float(free_credits)
user.free_credits = 0.00
user.save()
payment["free_credits_student"] = free_credits
else:
payment["free_credits_student"] = amount
return True,payment
else:
return False