Skip to content

Commit

Permalink
cleaning and correcting the code
Browse files Browse the repository at this point in the history
  • Loading branch information
mittal-ishaan committed Dec 20, 2023
1 parent 0968c3f commit 915350a
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 332 deletions.
111 changes: 18 additions & 93 deletions home/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
from django.contrib import admin
from django.http import HttpResponse
import datetime
from django.utils.translation import gettext_lazy as _
from .utils.django_email_server import rebate_mail,caterer_mail, long_rebate_query_mail
from .utils.rebate_bills_saver import save_short_bill, save_long_bill, update_bills
Expand Down Expand Up @@ -147,22 +148,6 @@ class about_Admin(admin.ModelAdmin):
)


# @admin.register(ShortRebate)
# class about_Admin(admin.ModelAdmin):
# model = ShortRebate
# fieldsets = (
# (
# None,
# {
# "fields": (
# "desc",
# ),
# "description": "%s" % SHORT_REBATE_DESC_TEXT,
# },
# ),
# )


@admin.register(Caterer)
class about_Admin(admin.ModelAdmin):
model = Caterer
Expand Down Expand Up @@ -259,47 +244,6 @@ class about_Admin(admin.ModelAdmin):
)


# @admin.register(Allocation)
# class about_Admin(ImportExportMixin, admin.ModelAdmin):
# resource_class = AllocationResource
# model = Allocation
# search_fields = ("student_id", "month", "caterer_name", "high_tea")
# list_filter = ("month", "caterer_name", "high_tea")
# list_display = ("student_id", "month", "caterer_name", "high_tea")
# fieldsets = (
# (
# None,
# {
# "fields": (
# "roll_no",
# "month",
# "student_id",
# "caterer_name",
# "high_tea",
# "first_pref",
# "second_pref",
# "third_pref",
# ),
# "description": "%s" % ALLOCATION_DESC_TEXT,
# },
# ),
# )
# actions = ["export_as_csv"]

# def export_as_csv(self, request, queryset):
# """
# Export action available in the admin page
# """
# resource = AllocationResource()
# dataset = resource.export(queryset)
# response = HttpResponse(dataset.csv, content_type="text/csv")
# response["Content-Disposition"] = 'attachment; filename="allocation.csv"'
# return response

# export_as_csv.short_description = "Export Allocation details to CSV"



@admin.register(Student)
class about_Admin(ImportExportMixin, admin.ModelAdmin):
resource_class = StudentResource
Expand Down Expand Up @@ -399,7 +343,7 @@ class about_Admin(ImportExportModelAdmin, admin.ModelAdmin):
},
),
)
actions = ["export_as_csv", "disapprove", "approve","send_mail"]
actions = ["export_as_csv", "disapprove", "approve","send_mail","clean"]

@admin.action(description="Disapprove the students")
def disapprove(self, request, queryset):
Expand Down Expand Up @@ -439,6 +383,21 @@ def send_mail(self, request, queryset):
for obj in queryset:
long_rebate_query_mail(obj.start_date, obj.end_date, obj.email.email)

# @admin.action(description="Clean left long rebate data")
# def clean(self, request, queryset):
# """
# Clean left long rebate data
# """
# for obj in queryset:
# if(obj.approved==True):
# if(obj.end_date>datetime.date(2023,12,7)):
# print(obj.end_date)
# obj.approved=False
# obj.save()
# print(obj.approved)
# obj.approved=True
# obj.save()

@admin.register(Rebate)
class about_Admin(ImportExportModelAdmin, admin.ModelAdmin):
resource_class = RebateResource
Expand Down Expand Up @@ -524,7 +483,6 @@ def export_as_csv(self, request, queryset):
# sender=form.instance.__class__, instance=form.instance, created=change
# )


def unregister_student(obj):
for caterer in Caterer.objects.all():
if caterer.student_limit > 0:
Expand Down Expand Up @@ -949,20 +907,7 @@ def correct_bills(self,request,queryset):
export_as_csv.short_description = "Export Allocation details to CSV"


# @admin.register(PeriodAutumn22)
# class about_Admin(admin.ModelAdmin):
# list_display = ("Sno", "start_date", "end_date")
# model = PeriodAutumn22
# fieldsets = (
# (None,{"fields": ("Sno", "start_date", "end_date")},),)

# @admin.register(PeriodSpring23)
# class about_Admin(admin.ModelAdmin):
# list_display = ("Sno", "start_date", "end_date")
# model = PeriodSpring23
# fieldsets = (
# (None,{"fields": ("Sno", "start_date", "end_date")},),)


@admin.register(Period)
class about_Admin(admin.ModelAdmin):
list_display = ("Sno", "start_date", "end_date", "semester")
Expand All @@ -983,26 +928,6 @@ class about_Admin(admin.ModelAdmin):
# "description": "%s" % CATERER_BILL_DESC_TEXT,
}

# @admin.register(CatererBillsAutumn22)
# class about_Admin(ImportExportModelAdmin, admin.ModelAdmin):
# resource_class = CatererBillsResource
# model = CatererBillsAutumn22
# fieldsets = ((None,caterer_bill_fields,),)
# list_display = ("__str__", "period1_bills", "period2_bills", "period3_bills", "period4_bills", "period5_bills", "period6_bills",)
# search_fields = ("caterer__name",)
# actions = ["export_as_csv"]

# def export_as_csv(self, request, queryset):
# """
# Export action available in the admin page
# """
# resource = CatererBillsResource()
# dataset = resource.export(queryset)
# response = HttpResponse(dataset.csv, content_type="text/csv")
# response["Content-Disposition"] = 'attachment; filename="caterer_bills.csv"'
# return response

# export_as_csv.short_description = "Export Caterer Bills details to CSV"

# @admin.register(CatererBillsSpring23)
# class about_Admin(ImportExportModelAdmin, admin.ModelAdmin):
Expand Down
130 changes: 130 additions & 0 deletions home/depreciated/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from ..models import *
from django.contrib.admin.views.decorators import staff_member_required
import pandas as pd
import io
from django.shortcuts import redirect
from django.shortcuts import render

@staff_member_required(redirect_field_name="/", login_url="/")
def allocation(request):
"""
Display the Rebate Form Page :model:`home.models.students`.
*Template:*
:template:`home/allocation.html`
Gets the data from the allocation csv,
parses each row and allocates each student an allocation ID and caterer for that month.
Which can be then exported in the admin page.
CSV should be imported from /allocation/ url only
This form can only be accessed by the Institute's admin
"""
messages = ""
if (
request.method == "POST"
and request.user.is_authenticated
and request.user.is_staff
):
try:
file = request.FILES["csv"]
file_extension = file.name.split('.')[-1].lower()
if file_extension == 'csv':
csv_data = pd.read_csv(io.StringIO(file.read().decode('utf-8')))
elif file_extension == 'xlsx':
csv_data = pd.read_excel(file, engine='openpyxl')
print(csv_data.head())

for record in csv_data.to_dict(orient="records"):
try:
if 'First Preference' in csv_data.columns:
first_pref = str(record["First Preference"]).capitalize()
caterer1 = Caterer.objects.get(name=first_pref)
else:
first_pref =None
if 'Second Preference' in csv_data.columns:
second_pref = str(record["Second Preference"]).capitalize()
caterer2 = Caterer.objects.get(name=second_pref)
else:
second_pref=None
if 'Third Preference' in csv_data.columns:
third_pref = str(record["Third Preference"]).capitalize()
caterer3 = Caterer.objects.get(name=third_pref)
else:
third_pref=None

# getting period object
# if 'Period' in csv_data.columns and 'Semester' in csv_data.columns:
# period = str(record["Period"]).capitalize()
# semester = str(record["Semester"]).capitalize()
# period_obj = Period.objects.get(Sno=period, semester=Semester.objects.get(name=semester))
# else:
# period_obj = Period.objects.filter().last()
period_obj = Period.objects.filter().last()

# getting high tea
high_tea = record["High Tea"]
jain = record["Jain"]
print(high_tea)
if(high_tea=="Yes" or high_tea==True or high_tea=="TRUE"):
high_tea=True
else:
high_tea=False

student = Student.objects.filter(name=record["Name"], hostel=record["Hostel"]).first()
if(student==None):
messages+=str(record["Email"])
print(student)

if caterer1.student_limit>0:
caterer1.student_limit-=1
caterer1.save(update_fields=["student_limit"])
caterer=caterer1
elif caterer2 and caterer2.student_limit>0:
caterer2.student_limit-=1
caterer2.save(update_fields=["student_limit"])
caterer=caterer2
elif caterer3 and caterer3.student_limit>0:
caterer3.student_limit-=1
caterer3.save(update_fields=["student_limit"])
caterer=caterer3
student_id = str(caterer.name[0])
if high_tea == True:
student_id += "H"
else:
student_id+="NH"
student_id += str(caterer.student_limit)
allocation = Allocation(
email=student,
student_id=student_id,
period=period_obj,
caterer=caterer,
high_tea=high_tea,
jain=jain,
first_pref=first_pref,
second_pref=second_pref,
third_pref=third_pref,
)
allocation.save()
student_bill = StudentBills.objects.get_or_create(email=student, semester=period_obj.semester)
UnregisteredStudent.objects.filter(email__iexact=student.email).delete()
except Exception as e:
print(e)
messages += "Form submitted. Please check the admin page."
except Exception as e:
print(e)
messages = "Invalid CSV file"
request.session["messages"] = messages
return redirect(request.path)
messages = request.session.get("messages", "")
if(messages!=""):
del request.session["messages"]
period_obj = Period.objects.filter().last()
caterer_list = []
for caterer in Caterer.objects.filter(visible=True).all():
caterer_high_tea = Allocation.objects.filter(caterer=caterer, high_tea=True,period=period_obj).count()
caterer_total = Allocation.objects.filter(caterer=caterer,period=period_obj).count()
caterer_left = caterer.student_limit
caterer_list.append([caterer.name,caterer_high_tea,caterer_total,caterer_left])
context = {"messages": messages,"list": caterer_list}
return render(request, "admin/allocation.html", context)
29 changes: 0 additions & 29 deletions home/fields.py

This file was deleted.

Loading

0 comments on commit 915350a

Please sign in to comment.