Skip to content

Commit

Permalink
improve and clean the rebate view(Error Prone) (#106)
Browse files Browse the repository at this point in the history
Everything looks good.
  • Loading branch information
mittal-ishaan authored Dec 23, 2024
1 parent f1b7efa commit 644032e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 104 deletions.
26 changes: 17 additions & 9 deletions home/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,33 @@ def create_bill(sender, instance, created, **kwargs):


@receiver(post_save, sender=Rebate)
def direct_update_bill(sender, instance, created, **kwargs):
def direct_update_bill(sender, instance: Rebate, created, **kwargs):
logger.info("Signal called for Creating Short Rebate")
try:
if created:
email = instance.email
student = instance.email
allocation = instance.allocation_id
start_date = instance.start_date
end_date = instance.end_date
days = count(start_date, end_date)
save_short_bill(
email, allocation.period, days, allocation.high_tea, allocation.caterer
student,
allocation.period,
days,
allocation.high_tea,
allocation.caterer,
)
rebate_mail(
instance.start_date, instance.end_date, instance.approved, email.email
instance.start_date, instance.end_date, instance.approved, student.email
)
logger.info("Saved")
except Exception as e:
logger.error(e)


@receiver(pre_save, sender=Rebate)
def update_short_bill(sender, instance, **kwargs):
logger.info("Signal called for Short Rebate")
def update_short_bill(sender, instance: Rebate, **kwargs):
logger.info("Signal called for Updating Short Rebate")
try:
old_instance = Rebate.objects.get(pk=instance.pk)
if old_instance.approved != instance.approved:
Expand All @@ -67,8 +72,10 @@ def update_short_bill(sender, instance, **kwargs):
start_date = instance.start_date
end_date = instance.end_date
days = count(start_date, end_date)
if days < 0:
raise ValueError("Days are negative")
logger.info(old_instance.approved, instance.approved)
if instance.approved is True and days > 0:
if instance.approved is True:
save_short_bill(
email,
allocation.period,
Expand All @@ -94,10 +101,11 @@ def update_short_bill(sender, instance, **kwargs):


@receiver(pre_save, sender=LongRebate)
def update_long_bill(sender, instance, **kwargs):
def update_long_bill(sender, instance: LongRebate, **kwargs):
logger.info("Signal called for Updating Long Rebate")
try:
old_instance = LongRebate.objects.get(pk=instance.pk)
logger.info(old_instance.approved, instance.approved)
logger.info(instance.approved, instance.reason)
if (
old_instance.approved != instance.approved
or old_instance.reason != instance.reason
Expand Down
2 changes: 1 addition & 1 deletion home/utils/month.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def fill_periods(email, start_date, end_date):
days_per_period.append((period, days_in_period))
current_date = current_date + timedelta(days=days_in_period)

if current_date <= end_date and (end_date - current_date).days + 1 > 0:
if current_date <= end_date:
days_per_period.append((current_date, end_date))

for period_no, days in days_per_period:
Expand Down
2 changes: 1 addition & 1 deletion home/utils/rebate_bills_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)


def save_short_bill(student, period, days, high_tea, caterer):
def save_short_bill(student: Student, period: Period, days, high_tea, caterer):
rebate = StudentBills.objects.get(email=student, semester=period.semester)
print(caterer, student)
catererBill = CatererBills.objects.get(caterer=caterer, semester=period.semester)
Expand Down
177 changes: 84 additions & 93 deletions home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,117 +151,108 @@ def rebate(request):
:template:`rebateForm.html`
"""
text = ""
list = []
message = ""
period_obj = None
allocation_id = None
allocation = None
try:
student = Student.objects.filter(email__iexact=str(request.user.email))
try:
for period in Period.objects.all():
if period.end_date > date.today() + timedelta(1):
period_obj = period
try:
allocation_id = Allocation.objects.get(
email__email__iexact=str(request.user.email), period=period
)
break
except:
continue
key = str(allocation_id.student_id)
except Exception as e:
logger.error(e)
key = "You are not allocated for current period, please contact the dining warden to allocate you to a caterer"
student = Student.objects.get(email__iexact=request.user.email)
except Student.DoesNotExist:
key = "Signed in account does not does not have any allocation ID"
message = "Signed in account does not have any allocation ID"
return render(request, "rebateForm.html", {"text": message})

try:
period_obj = next(
period
for period in Period.objects.all()
if period.end_date > date.today() + timedelta(1)
)
allocation = Allocation.objects.get(
email__iexact=request.user.email, period=period_obj
)
except Exception as e:
logger.error(e)
message = "You are not allocated for current period, please contact the dining warden to allocate you to a caterer"
if request.method == "POST" and request.user.is_authenticated:
if not period_obj or not allocation_id:
text = "You are not allocated for current period, please contact the dining warden to allocate you to a caterer"
request.session["text"] = text
if not period_obj or not allocation:
message = "You are not allocated for current period, please contact the dining warden to allocate you to a caterer"
request.session["text"] = message
return redirect(request.path)

try:
start_date = parse_date(request.POST["start_date"])
end_date = parse_date(request.POST["end_date"])
rebate_days = ((end_date - start_date).days) + 1
before_rebate_days = (start_date - date.today()).days
try:
student = Student.objects.filter(
email__iexact=str(request.user.email)
).first()
period = period_obj.Sno
period_start = period_obj.start_date
period_end = period_obj.end_date
if rebate_days > 7:
text = "Max no of days for rebate is 7"
elif before_rebate_days < 2:
text = "Form needs to be filled atleast 2 days prior the comencement of leave."
elif not is_not_duplicate(student, start_date, end_date):
text = "You have already applied for rebate during this duration"
elif 0 < rebate_days < 2:
text = "Min no of days for rebate is 2"
else:
additional_text = ""
if not period_start <= start_date <= period_end:
short_left_rebate = LeftShortRebate(
email=student.email,
start_date=start_date,
end_date=end_date,
date_applied=date.today(),
)

if rebate_days > 7:
message = "Max no of days for rebate is 7"
elif before_rebate_days < 2:
message = "Form needs to be filled atleast 2 days prior the comencement of leave."
elif not is_not_duplicate(student, start_date, end_date):
message = "You have already applied for rebate during this duration"
elif 0 < rebate_days < 2:
message = "Min no of days for rebate is 2"
else:
additional_message = ""
if not period_obj.start_date <= start_date <= period_obj.end_date:
short_left_rebate = LeftShortRebate(
email=student.email,
start_date=start_date,
end_date=end_date,
date_applied=date.today(),
)
short_left_rebate.save()
message = "You have successfully submitted the rebate, it will get added to your bills in the next period."
upper_cap_check = -1
elif not period_obj.start_date <= end_date <= period_obj.end_date:
short_left_rebate = LeftShortRebate(
email=student.email,
start_date=period_obj.end_date + timedelta(days=1),
end_date=end_date,
date_applied=date.today(),
)
end_date = period_obj.end_date
upper_cap_check = max_days_rebate(
student, start_date, period_obj.end_date, period_obj
)
if upper_cap_check < 0:
short_left_rebate.save()
text = "You have successfully submitted the rebate, it will get added to your bills in the next period."
upper_cap_check = -1
elif not period_start <= end_date <= period_end:
short_left_rebate = LeftShortRebate(
email=student.email,
start_date=period_end + timedelta(days=1),
end_date=end_date,
date_applied=date.today(),
)
end_date = period_end
upper_cap_check = max_days_rebate(
student, start_date, period_end, period_obj
)
if upper_cap_check < 0:
short_left_rebate.save()
additional_text = " Note: The days after the current period end date will be added to your bills in the next period."
else:
upper_cap_check = max_days_rebate(
student, start_date, end_date, period_obj
)
if upper_cap_check >= 0:
text = (
"You can only apply for max 8 days in a period. Days left for this period: "
+ str(upper_cap_check)
)
elif text == "":
r = Rebate(
email=student,
allocation_id=allocation_id,
start_date=start_date,
end_date=end_date,
approved=True,
)
r.save()
text = "You have successfully submitted the rebate. Thank You! You shall recieve a confirmation mail, If not please contact the Dining Warden."
if additional_text:
text += additional_text
elif not text:
text = "Your rebate application has been rejected due to non-compliance of the short term rebate rules"
except Allocation.DoesNotExist:
text = "Email ID does not match with the allocation ID"
additional_message = " Note: The days after the current period end date will be added to your bills in the next period."
else:
upper_cap_check = max_days_rebate(
student, start_date, end_date, period_obj
)
if upper_cap_check >= 0:
message = (
"You can only apply for max 8 days in a period. Days left for this period: "
+ str(upper_cap_check)
)
elif not message:
rebate = Rebate(
email=student,
allocation_id=allocation,
start_date=start_date,
end_date=end_date,
approved=True,
)
rebate.save()
message = "You have successfully submitted the rebate. Thank You! You shall recieve a confirmation mail, If not please contact the Dining Warden."
if additional_message:
message += additional_message
elif not message:
message = "Your rebate application has been rejected due to non-compliance of the short term rebate rules"
except Exception as e:
logger.error(e)
text = (
message = (
"Ohh No! an unknown ERROR occured, Please inform about it immediatly to the Dining Wadern. Possible Error: "
+ key
+ str(e)
)
request.session["text"] = text
request.session["text"] = message
return redirect(request.path)
text = request.session.get("text", "")
if text != "":
del request.session["text"]
context = {"text": text, "key": key, "list": list}
context = {"text": text, "key": allocation.student_id}
return render(request, "rebateForm.html", context)


Expand Down Expand Up @@ -336,7 +327,7 @@ def addLongRebateBill(request):
text = "Long Term rebate added Successfully"
except Exception as e:
text = "An error occurred while processing your form submission. If you're submitting an application, try compressing it before resubmitting. If the issue persists, please report it to the admin."
print(e)
logger.error(e)
except Exception as e:
logger.error(e)
text = "Email ID does not exist in the database. Please login using the correct email ID"
Expand Down

0 comments on commit 644032e

Please sign in to comment.