Skip to content

Commit 16bb809

Browse files
improve and clean the rebate view(Error Prone) (#106)
Everything looks good.
1 parent c1ad4de commit 16bb809

4 files changed

Lines changed: 103 additions & 104 deletions

File tree

home/signals.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,33 @@ def create_bill(sender, instance, created, **kwargs):
3737

3838

3939
@receiver(post_save, sender=Rebate)
40-
def direct_update_bill(sender, instance, created, **kwargs):
40+
def direct_update_bill(sender, instance: Rebate, created, **kwargs):
41+
logger.info("Signal called for Creating Short Rebate")
4142
try:
4243
if created:
43-
email = instance.email
44+
student = instance.email
4445
allocation = instance.allocation_id
4546
start_date = instance.start_date
4647
end_date = instance.end_date
4748
days = count(start_date, end_date)
4849
save_short_bill(
49-
email, allocation.period, days, allocation.high_tea, allocation.caterer
50+
student,
51+
allocation.period,
52+
days,
53+
allocation.high_tea,
54+
allocation.caterer,
5055
)
5156
rebate_mail(
52-
instance.start_date, instance.end_date, instance.approved, email.email
57+
instance.start_date, instance.end_date, instance.approved, student.email
5358
)
5459
logger.info("Saved")
5560
except Exception as e:
5661
logger.error(e)
5762

5863

5964
@receiver(pre_save, sender=Rebate)
60-
def update_short_bill(sender, instance, **kwargs):
61-
logger.info("Signal called for Short Rebate")
65+
def update_short_bill(sender, instance: Rebate, **kwargs):
66+
logger.info("Signal called for Updating Short Rebate")
6267
try:
6368
old_instance = Rebate.objects.get(pk=instance.pk)
6469
if old_instance.approved != instance.approved:
@@ -67,8 +72,10 @@ def update_short_bill(sender, instance, **kwargs):
6772
start_date = instance.start_date
6873
end_date = instance.end_date
6974
days = count(start_date, end_date)
75+
if days < 0:
76+
raise ValueError("Days are negative")
7077
logger.info(old_instance.approved, instance.approved)
71-
if instance.approved is True and days > 0:
78+
if instance.approved is True:
7279
save_short_bill(
7380
email,
7481
allocation.period,
@@ -94,10 +101,11 @@ def update_short_bill(sender, instance, **kwargs):
94101

95102

96103
@receiver(pre_save, sender=LongRebate)
97-
def update_long_bill(sender, instance, **kwargs):
104+
def update_long_bill(sender, instance: LongRebate, **kwargs):
105+
logger.info("Signal called for Updating Long Rebate")
98106
try:
99107
old_instance = LongRebate.objects.get(pk=instance.pk)
100-
logger.info(old_instance.approved, instance.approved)
108+
logger.info(instance.approved, instance.reason)
101109
if (
102110
old_instance.approved != instance.approved
103111
or old_instance.reason != instance.reason

home/utils/month.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def fill_periods(email, start_date, end_date):
2828
days_per_period.append((period, days_in_period))
2929
current_date = current_date + timedelta(days=days_in_period)
3030

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

3434
for period_no, days in days_per_period:

home/utils/rebate_bills_saver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
)
1313

1414

15-
def save_short_bill(student, period, days, high_tea, caterer):
15+
def save_short_bill(student: Student, period: Period, days, high_tea, caterer):
1616
rebate = StudentBills.objects.get(email=student, semester=period.semester)
1717
print(caterer, student)
1818
catererBill = CatererBills.objects.get(caterer=caterer, semester=period.semester)

home/views.py

Lines changed: 84 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -151,117 +151,108 @@ def rebate(request):
151151
152152
:template:`rebateForm.html`
153153
"""
154-
text = ""
155-
list = []
154+
message = ""
156155
period_obj = None
157-
allocation_id = None
156+
allocation = None
158157
try:
159-
student = Student.objects.filter(email__iexact=str(request.user.email))
160-
try:
161-
for period in Period.objects.all():
162-
if period.end_date > date.today() + timedelta(1):
163-
period_obj = period
164-
try:
165-
allocation_id = Allocation.objects.get(
166-
email__email__iexact=str(request.user.email), period=period
167-
)
168-
break
169-
except:
170-
continue
171-
key = str(allocation_id.student_id)
172-
except Exception as e:
173-
logger.error(e)
174-
key = "You are not allocated for current period, please contact the dining warden to allocate you to a caterer"
158+
student = Student.objects.get(email__iexact=request.user.email)
175159
except Student.DoesNotExist:
176-
key = "Signed in account does not does not have any allocation ID"
160+
message = "Signed in account does not have any allocation ID"
161+
return render(request, "rebateForm.html", {"text": message})
162+
163+
try:
164+
period_obj = next(
165+
period
166+
for period in Period.objects.all()
167+
if period.end_date > date.today() + timedelta(1)
168+
)
169+
allocation = Allocation.objects.get(
170+
email__iexact=request.user.email, period=period_obj
171+
)
172+
except Exception as e:
173+
logger.error(e)
174+
message = "You are not allocated for current period, please contact the dining warden to allocate you to a caterer"
177175
if request.method == "POST" and request.user.is_authenticated:
178-
if not period_obj or not allocation_id:
179-
text = "You are not allocated for current period, please contact the dining warden to allocate you to a caterer"
180-
request.session["text"] = text
176+
if not period_obj or not allocation:
177+
message = "You are not allocated for current period, please contact the dining warden to allocate you to a caterer"
178+
request.session["text"] = message
181179
return redirect(request.path)
180+
182181
try:
183182
start_date = parse_date(request.POST["start_date"])
184183
end_date = parse_date(request.POST["end_date"])
185184
rebate_days = ((end_date - start_date).days) + 1
186185
before_rebate_days = (start_date - date.today()).days
187-
try:
188-
student = Student.objects.filter(
189-
email__iexact=str(request.user.email)
190-
).first()
191-
period = period_obj.Sno
192-
period_start = period_obj.start_date
193-
period_end = period_obj.end_date
194-
if rebate_days > 7:
195-
text = "Max no of days for rebate is 7"
196-
elif before_rebate_days < 2:
197-
text = "Form needs to be filled atleast 2 days prior the comencement of leave."
198-
elif not is_not_duplicate(student, start_date, end_date):
199-
text = "You have already applied for rebate during this duration"
200-
elif 0 < rebate_days < 2:
201-
text = "Min no of days for rebate is 2"
202-
else:
203-
additional_text = ""
204-
if not period_start <= start_date <= period_end:
205-
short_left_rebate = LeftShortRebate(
206-
email=student.email,
207-
start_date=start_date,
208-
end_date=end_date,
209-
date_applied=date.today(),
210-
)
186+
187+
if rebate_days > 7:
188+
message = "Max no of days for rebate is 7"
189+
elif before_rebate_days < 2:
190+
message = "Form needs to be filled atleast 2 days prior the comencement of leave."
191+
elif not is_not_duplicate(student, start_date, end_date):
192+
message = "You have already applied for rebate during this duration"
193+
elif 0 < rebate_days < 2:
194+
message = "Min no of days for rebate is 2"
195+
else:
196+
additional_message = ""
197+
if not period_obj.start_date <= start_date <= period_obj.end_date:
198+
short_left_rebate = LeftShortRebate(
199+
email=student.email,
200+
start_date=start_date,
201+
end_date=end_date,
202+
date_applied=date.today(),
203+
)
204+
short_left_rebate.save()
205+
message = "You have successfully submitted the rebate, it will get added to your bills in the next period."
206+
upper_cap_check = -1
207+
elif not period_obj.start_date <= end_date <= period_obj.end_date:
208+
short_left_rebate = LeftShortRebate(
209+
email=student.email,
210+
start_date=period_obj.end_date + timedelta(days=1),
211+
end_date=end_date,
212+
date_applied=date.today(),
213+
)
214+
end_date = period_obj.end_date
215+
upper_cap_check = max_days_rebate(
216+
student, start_date, period_obj.end_date, period_obj
217+
)
218+
if upper_cap_check < 0:
211219
short_left_rebate.save()
212-
text = "You have successfully submitted the rebate, it will get added to your bills in the next period."
213-
upper_cap_check = -1
214-
elif not period_start <= end_date <= period_end:
215-
short_left_rebate = LeftShortRebate(
216-
email=student.email,
217-
start_date=period_end + timedelta(days=1),
218-
end_date=end_date,
219-
date_applied=date.today(),
220-
)
221-
end_date = period_end
222-
upper_cap_check = max_days_rebate(
223-
student, start_date, period_end, period_obj
224-
)
225-
if upper_cap_check < 0:
226-
short_left_rebate.save()
227-
additional_text = " Note: The days after the current period end date will be added to your bills in the next period."
228-
else:
229-
upper_cap_check = max_days_rebate(
230-
student, start_date, end_date, period_obj
231-
)
232-
if upper_cap_check >= 0:
233-
text = (
234-
"You can only apply for max 8 days in a period. Days left for this period: "
235-
+ str(upper_cap_check)
236-
)
237-
elif text == "":
238-
r = Rebate(
239-
email=student,
240-
allocation_id=allocation_id,
241-
start_date=start_date,
242-
end_date=end_date,
243-
approved=True,
244-
)
245-
r.save()
246-
text = "You have successfully submitted the rebate. Thank You! You shall recieve a confirmation mail, If not please contact the Dining Warden."
247-
if additional_text:
248-
text += additional_text
249-
elif not text:
250-
text = "Your rebate application has been rejected due to non-compliance of the short term rebate rules"
251-
except Allocation.DoesNotExist:
252-
text = "Email ID does not match with the allocation ID"
220+
additional_message = " Note: The days after the current period end date will be added to your bills in the next period."
221+
else:
222+
upper_cap_check = max_days_rebate(
223+
student, start_date, end_date, period_obj
224+
)
225+
if upper_cap_check >= 0:
226+
message = (
227+
"You can only apply for max 8 days in a period. Days left for this period: "
228+
+ str(upper_cap_check)
229+
)
230+
elif not message:
231+
rebate = Rebate(
232+
email=student,
233+
allocation_id=allocation,
234+
start_date=start_date,
235+
end_date=end_date,
236+
approved=True,
237+
)
238+
rebate.save()
239+
message = "You have successfully submitted the rebate. Thank You! You shall recieve a confirmation mail, If not please contact the Dining Warden."
240+
if additional_message:
241+
message += additional_message
242+
elif not message:
243+
message = "Your rebate application has been rejected due to non-compliance of the short term rebate rules"
253244
except Exception as e:
254245
logger.error(e)
255-
text = (
246+
message = (
256247
"Ohh No! an unknown ERROR occured, Please inform about it immediatly to the Dining Wadern. Possible Error: "
257-
+ key
248+
+ str(e)
258249
)
259-
request.session["text"] = text
250+
request.session["text"] = message
260251
return redirect(request.path)
261252
text = request.session.get("text", "")
262253
if text != "":
263254
del request.session["text"]
264-
context = {"text": text, "key": key, "list": list}
255+
context = {"text": text, "key": allocation.student_id}
265256
return render(request, "rebateForm.html", context)
266257

267258

@@ -336,7 +327,7 @@ def addLongRebateBill(request):
336327
text = "Long Term rebate added Successfully"
337328
except Exception as e:
338329
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."
339-
print(e)
330+
logger.error(e)
340331
except Exception as e:
341332
logger.error(e)
342333
text = "Email ID does not exist in the database. Please login using the correct email ID"

0 commit comments

Comments
 (0)