Skip to content

Commit 80b3507

Browse files
Adi8712S-tejkarthikpadarthiPrinceBujethia
authored
SA-2 [New-UI] - updated APIs for use with fusion-client (FusionIIIT#1717)
* chore: added linting * changes in feedbackAPI * feat: added update bill excel (kumkum020704#4) * fix: migrations * Fix: Fixed an api endpoint allowed method (kumkum020704#5) * Added New API for Student Records, Implement Checks in RebateAPI. (kumkum020704#6) * Change in feedbackAPI * New API and filter in RebateApi * Fix merge * fix: api cleanup --------- Co-authored-by: S-tej <[email protected]> Co-authored-by: Padarthi Karthik <[email protected]> Co-authored-by: Green Mansion <[email protected]>
1 parent 79b18a6 commit 80b3507

File tree

10 files changed

+283
-61
lines changed

10 files changed

+283
-61
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,5 @@ package-lock.json
7676

7777
.DS_Store
7878
**/generated.pdf
79+
80+
.ruff_cache/

.pre-commit-config.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v3.2.0
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- id: check-yaml
8+
- id: check-added-large-files
9+
- repo: https://github.com/astral-sh/ruff-pre-commit
10+
rev: v0.6.9
11+
hooks:
12+
- id: ruff
13+
args: [ --fix ]
14+
- id: ruff-format

FusionIIIT/Fusion/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from django.contrib import admin
2323
from django.contrib.auth import views as auth_views
2424

25+
from django.views.static import serve
26+
2527

2628
urlpatterns = [
2729
url(r'^', include('applications.globals.urls')),
@@ -62,4 +64,5 @@
6264
url(r'^recruitment/', include('applications.recruitment.urls')),
6365
url(r'^examination/', include('applications.examination.urls')),
6466
url(r'^otheracademic/', include('applications.otheracademic.urls')),
67+
url(r'^media/(?P<path>.*)$', serve, {"document_root": settings.MEDIA_ROOT},),
6568
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

FusionIIIT/applications/central_mess/api/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
url('deRegistrationRequestApi', views.DeregistrationRequestApi.as_view(), name='deRegistrationRequestApi'),
2929
url('deRegistrationApi', views.DeregistrationApi.as_view(), name='deRegistrationApi'),
3030
url('updatePaymentRequestApi', views.UpdatePaymentRequestApi.as_view(), name='updatePaymentRequestApi'),
31+
url('get_mess_balance_statusApi', views.Get_Mess_Balance_Status.as_view(), name='get_mess_balance_statusApi'),
3132
]

FusionIIIT/applications/central_mess/api/views.py

Lines changed: 197 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#APIs
2+
from datetime import date, datetime, timedelta
23
from django.db.models import F
34
from rest_framework.views import APIView
45
from rest_framework.response import Response
6+
# from FusionIIIT.notification.views import central_mess_notif
57
from .serializers import *
68
from django.shortcuts import get_object_or_404
79
from applications.central_mess.models import *
810
from django.contrib.auth.models import User
911
from applications.globals.models import ExtraInfo, HoldsDesignation, Designation
1012
from django.http import JsonResponse
1113

14+
today_g = datetime.datetime.now()
15+
1216
class FeedbackApi(APIView):
1317

1418
def get(self, request):
@@ -61,6 +65,29 @@ def put(self, request):
6165
feedback_request.save()
6266

6367
return Response({'status':200})
68+
69+
def delete(self, request):
70+
data = request.data
71+
student_id = data.get('student_id')
72+
mess = data.get('mess')
73+
feedback_type = data.get('feedback_type')
74+
description = data.get('description')
75+
fdate = data.get('fdate')
76+
77+
# Locate the feedback record
78+
feedback_request = get_object_or_404(
79+
Feedback,
80+
student_id=student_id,
81+
mess=mess,
82+
feedback_type=feedback_type,
83+
description=description,
84+
fdate=fdate,
85+
)
86+
87+
# Delete the feedback record
88+
feedback_request.delete()
89+
90+
return Response({'status': 200, 'message': 'Feedback deleted successfully.'})
6491

6592

6693
class MessinfoApi(APIView):
@@ -126,6 +153,7 @@ def post(self, request):
126153

127154
class Monthly_billApi(APIView):
128155
def get(self, request):
156+
129157
monthly_bill_obj = Monthly_bill.objects.all();
130158
serialized_obj = Monthly_billSerializer(monthly_bill_obj, many=True)
131159
return Response({'status':200, 'payload':serialized_obj.data})
@@ -168,31 +196,36 @@ def post(self, request):
168196

169197
class PaymentsApi(APIView):
170198
def get(self, request):
199+
username = get_object_or_404(User,username=request.user.username)
200+
idd = ExtraInfo.objects.get(user=username)
201+
student = Student.objects.get(id=idd.id)
171202
payments_obj = Payments.objects.all();
203+
payments_obj = payments_obj.filter(student_id=student)
172204
serialized_obj = PaymentsSerializer(payments_obj, many=True)
173205
return Response({'status':200, 'payload':serialized_obj.data})
174206

175-
def post(self, request):
176-
data = request.data
207+
# def post(self, request):
208+
# data = request.data
177209

178-
# sem = data['sem']
179-
# year = data['year']
180-
amount_paid = data['amount_paid']
210+
# # sem = data['sem']
211+
# # year = data['year']
212+
# amount_paid = data['amount_paid']
181213

182214

183-
username = get_object_or_404(User,username=request.user.username)
184-
idd = ExtraInfo.objects.get(user=username)
185-
student = Student.objects.get(id=idd.id)
215+
# username = get_object_or_404(User,username=request.user.username)
216+
# idd = ExtraInfo.objects.get(user=username)
217+
# student = Student.objects.get(id=idd.id)
186218

187219

188-
obj = Payments(
189-
student_id = student,
190-
# sem = sem,
191-
# year = year,
192-
amount_paid = amount_paid,
193-
)
194-
obj.save()
195-
return Response({'status':200})
220+
# obj = Payments(
221+
# student_id = student,
222+
# # sem = sem,
223+
# # year = year,
224+
# amount_paid = amount_paid,
225+
# )
226+
# obj.save()
227+
# return Response({'status':200})
228+
196229
class MenuApi(APIView):
197230
def get(self, request):
198231
menu_obj = Menu.objects.all();
@@ -201,6 +234,7 @@ def get(self, request):
201234

202235
def post(self, request):
203236
data = request.data
237+
print(data)
204238

205239
mess_option = data['mess_option']
206240
meal_time = data['meal_time']
@@ -224,17 +258,44 @@ def post(self, request):
224258
data = request.data
225259

226260
# student_id = data['mess_option']
261+
flag=1
227262
start_date = data['start_date']
228263
end_date = data['end_date']
229264
purpose = data['purpose']
230265
status = data['status']
266+
"""
267+
status:
268+
'2' -> approved
269+
'1' -> pending
270+
'0' -> declined
271+
"""
231272
app_date = data['app_date']
232273
leave_type = data['leave_type']
233274

275+
if (end_date < start_date):
276+
flag = 0
277+
return Response({'status': 3, 'message': 'Please check the dates'})
278+
234279
username = get_object_or_404(User,username=request.user.username)
235280
idd = ExtraInfo.objects.get(user=username)
236281
student = Student.objects.get(id=idd.id)
237282

283+
date_format = "%Y-%m-%d"
284+
b = datetime.datetime.strptime(str(start_date), date_format)
285+
d = datetime.datetime.strptime(str(end_date), date_format)
286+
287+
rebate_check = Rebate.objects.filter(student_id=student, status='2')
288+
for r in rebate_check:
289+
a = datetime.datetime.strptime(str(r.start_date), date_format)
290+
c = datetime.datetime.strptime(str(r.end_date), date_format)
291+
if ((b <= a and (d >= a and d <= c)) or (b >= a and (d >= a and d <= c))
292+
or (b <= a and (d >= c)) or ((b >= a and b <= c) and (d >= c))):
293+
flag = 0
294+
data = {
295+
'status': 3,
296+
'message': "Already applied for these dates",
297+
}
298+
return Response({'status': 3, 'message': 'Already applied for these dates'})
238299

239300
obj = Rebate(
240301
student_id = student,
@@ -245,6 +306,10 @@ def post(self, request):
245306
end_date= end_date,
246307
start_date = start_date
247308
)
309+
310+
if flag == 1:
311+
message = 'Your leave request has been accepted between dates ' + str(b.date()) + ' and ' + str(d.date())
312+
# central_mess_notif(request.user, student.id.user, 'leave_request', message)
248313
obj.save()
249314
return Response({'status':200})
250315

@@ -404,8 +469,7 @@ def get(self, request):
404469

405470
def post(self, request):
406471
data = request.data
407-
408-
472+
409473
start_date = data['start_date']
410474
end_date = data['end_date']
411475
status = data['status']
@@ -568,13 +632,11 @@ def post(self,request):
568632
return response
569633

570634
class Get_Reg_Records(APIView):
571-
572-
def post(self,request):
573-
student = request.data['student_id']
574-
reg_record = Reg_records.objects.filter(student_id=student)
575-
635+
def get(self,request):
636+
student_id = request.GET.get('student_id')
637+
reg_record = Reg_records.objects.filter(student_id=student_id)
576638
serialized_obj = reg_recordSerialzer(reg_record,many=True)
577-
return Response({'payload':serialized_obj.data})
639+
return Response({'payload':serialized_obj.data})
578640

579641

580642
class Get_Student_bill(APIView):
@@ -722,33 +784,38 @@ def put(self, request):
722784
print({'error': str(e)})
723785
return Response({'error': str(e)}, status=400)
724786

725-
class DeregistrationApi(APIView):
726-
def post(self, request):
727-
try:
728-
data = request.data
729-
print(data)
730-
student_id = data['student_id']
731-
end_date = data['end_date']
732-
733-
username = get_object_or_404(User, username=student_id)
734-
idd = ExtraInfo.objects.get(user=username)
735-
student = Student.objects.get(id=idd.id)
736-
737-
reg_main = Reg_main.objects.get(student_id=student)
738-
reg_main.current_mess_status = "Deregistered"
739-
reg_main.save()
740-
741-
reg_record = Reg_records.objects.filter(student_id=student).latest('start_date')
742-
reg_record.end_date = end_date
743-
reg_record.save()
744-
return Response({'status': 200})
745-
except Exception as e:
746-
print({'error': str(e)})
747-
return Response({'error': str(e)}, status=400)
787+
# class DeregistrationApi(APIView):
788+
# def post(self, request):
789+
# try:
790+
# data = request.data
791+
# print(data)
792+
# student_id = data['student_id']
793+
# end_date = data['end_date']
794+
795+
# username = get_object_or_404(User, username=student_id)
796+
# idd = ExtraInfo.objects.get(user=username)
797+
# student = Student.objects.get(id=idd.id)
798+
799+
# reg_main = Reg_main.objects.get(student_id=student)
800+
# reg_main.current_mess_status = "Deregistered"
801+
# reg_main.save()
802+
803+
# reg_record = Reg_records.objects.filter(student_id=student).latest('start_date')
804+
# reg_record.end_date = end_date
805+
# reg_record.save()
806+
# return Response({'status': 200})
807+
# except Exception as e:
808+
# print({'error': str(e)})
809+
# return Response({'error': str(e)}, status=400)
748810

749811
class UpdatePaymentRequestApi(APIView):
750812
def get(self, request):
751-
update_payment_requests = Update_Payment.objects.all()
813+
student_id = request.query_params.get('student_id')
814+
if student_id:
815+
update_payment_requests = Update_Payment.objects.filter(student_id=student_id)
816+
else:
817+
update_payment_requests = Update_Payment.objects.all()
818+
752819
serializer = UpdatePaymentRequestSerializer(update_payment_requests, many=True)
753820
return Response({'status': 200, 'payload': serializer.data})
754821

@@ -792,4 +859,85 @@ def put(self, request):
792859
return Response({'status': 200})
793860
except Exception as e:
794861
print({'error': str(e)})
795-
return Response({'error': str(e)}, status=400)
862+
return Response({'error': str(e)}, status=400)
863+
864+
from rest_framework.parsers import MultiPartParser, FormParser
865+
from rest_framework import status
866+
from openpyxl import load_workbook
867+
868+
class UpdateBillExcelAPI(APIView):
869+
parser_classes = (MultiPartParser, FormParser)
870+
871+
def post(self, request):
872+
if 'file' not in request.FILES:
873+
return Response({'error': 'No file provided'}, status=status.HTTP_400_BAD_REQUEST)
874+
875+
file = request.FILES['file']
876+
if not file.name.endswith(('.xlsx', '.xls')):
877+
return Response({'error': 'Invalid file format. Only .xlsx and .xls are allowed.'}, status=status.HTTP_400_BAD_REQUEST)
878+
879+
try:
880+
wb = load_workbook(file)
881+
sheet = wb.active
882+
flag = False
883+
884+
for row in sheet.iter_rows(min_row=2):
885+
student_id = str(row[0].value).upper()
886+
try:
887+
student = Student.objects.select_related('id', 'id__user', 'id__department').get(id=student_id)
888+
except Student.DoesNotExist:
889+
continue
890+
891+
month = str(row[1].value)
892+
year = row[2].value
893+
amt = row[3].value
894+
rebate_cnt = row[4].value
895+
rebate_amt = row[5].value
896+
total_amt = row[6].value
897+
try:
898+
bill = Monthly_bill.objects.get(student_id=student_id, month=month, year=year)
899+
reg_main = Reg_main.objects.get(student_id=student_id)
900+
reg_main.balance += bill.total_bill
901+
bill.amount = amt
902+
bill.rebate_count = rebate_cnt
903+
bill.rebate_amount = rebate_amt
904+
bill.total_bill = total_amt
905+
reg_main.balance -= total_amt
906+
907+
bill.save()
908+
reg_main.save()
909+
except Monthly_bill.DoesNotExist:
910+
bill = Monthly_bill(
911+
student_id=student,
912+
month=month,
913+
year=year,
914+
amount=amt,
915+
rebate_count=rebate_cnt,
916+
rebate_amount=rebate_amt,
917+
total_bill=total_amt
918+
)
919+
bill.save()
920+
921+
return Response({'message': 'File processed successfully'}, status=status.HTTP_200_OK)
922+
923+
except Exception as e:
924+
return Response({'error': f'An error occurred: {str(e)}'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
925+
926+
class Get_Mess_Balance_Status(APIView):
927+
def get(self, request):
928+
username = get_object_or_404(User,username=request.user.username)
929+
idd = ExtraInfo.objects.get(user=username)
930+
student_id = Student.objects.get(id=idd.id)
931+
try:
932+
mess_optn = Reg_main.objects.select_related('student_id','student_id__id','student_id__id__user','student_id__id__department').get(student_id=student_id)
933+
# y = Menu.objects.filter(mess_option=mess_optn.mess_option)
934+
current_rem_balance = mess_optn.balance
935+
current_mess_status = mess_optn.current_mess_status
936+
except:
937+
mess_optn={}
938+
mess_optn={'mess_option':'no-mess'}
939+
# y = Menu.objects.filter(mess_option="mess1")
940+
current_rem_balance = 0
941+
current_mess_status = 'Deregistered'
942+
943+
return Response({'payload': {'mess_option': mess_optn.mess_option, 'current_rem_balance': current_rem_balance, 'current_mess_status': current_mess_status}})

0 commit comments

Comments
 (0)