4848)
4949from .utils .django_email_server import long_rebate_query_mail
5050from .utils .month import fill_periods , map_periods_to_long_rebate
51- from .utils .rebate_bills_saver import fix_all_bills , save_long_bill , update_bills
51+ from .utils .rebate_bills_saver import fix_all_bills , save_long_bill
5252
5353# Customising the heading and title of the admin page
5454admin .site .site_header = "Dining Website Admin Page"
@@ -906,8 +906,7 @@ def email(self, obj):
906906 def name (self , obj ):
907907 return obj .email .name if obj .email else ""
908908
909- actions = ["export_as_csv" , "correct_bills" , "fix_issue" , "fix_period" ]
910-
909+ actions = ["export_as_csv" , "fix_duplicates" , "shift_caterer" ]
911910 def export_as_csv (self , request , queryset ):
912911 """
913912 Export action available in the admin page
@@ -918,33 +917,54 @@ def export_as_csv(self, request, queryset):
918917 response ["Content-Disposition" ] = 'attachment; filename="allocation.csv"'
919918 return response
920919
921- def correct_bills (self , request , queryset ):
920+ def fix_duplicates (self , request , queryset ):
921+ emails = []
922922 for obj in queryset :
923- if obj .period == Period .objects .get (
924- Sno = 5 , semester = Semester .objects .get (name = "Autumn 2023" )
925- ):
926- update_bills (obj .email , obj )
927- obj .save ()
928- if obj .period == Period .objects .get (
929- Sno = 4 , semester = Semester .objects .get (name = "Autumn 2023" )
930- ):
931- update_bills (obj .email , obj )
932- obj .save ()
933-
934- def fix_issue (self , request , queryset ):
923+ emails .append (obj .email .email )
935924 for obj in queryset :
936- caterer = Caterer .objects .get (name = obj .first_pref )
937- print (caterer )
938- obj .caterer = caterer
939- obj .save ()
925+ period = Period .objects .get (
926+ Sno = 3 , semester = Semester .objects .get (name = "Spring 2025" )
927+ )
928+ allocations = Allocation .objects .filter (
929+ email__email = obj .email .email , period = period
930+ )
931+ if len (allocations ) > 1 :
932+ for allocation in allocations [1 :]:
933+ allocation .delete ()
940934
941- def fix_period (self , request , queryset ):
942- for obj in queryset :
943- if obj .period is None :
944- obj .period = Period .objects .get (
945- Sno = 3 , semester = Semester .objects .get (name = "Spring 2025" )
946- )
947- obj .save ()
935+ def shift_caterer (self , request , queryset ):
936+ caterers = Caterer .objects .all ()
937+ student_limit = {}
938+
939+ for caterer in caterers :
940+ period = Period .objects .get (
941+ Sno = 3 , semester = Semester .objects .get (name = "Spring 2025" )
942+ )
943+ allocations = Allocation .objects .filter (
944+ caterer = caterer , period = period
945+ ).order_by ("registration_time" )
946+ student_limit [caterer .name ] = [
947+ caterer .student_limit - len (allocations ),
948+ allocations ,
949+ ]
950+
951+ for caterer in caterers :
952+ limit = caterer .student_limit
953+ count = student_limit [caterer .name ][0 ]
954+ allocations = student_limit [caterer .name ][1 ]
955+ if count < 0 :
956+ for allocation in allocations [limit :]:
957+ second_pref = allocation .second_pref
958+ if student_limit [second_pref ][0 ] > 0 :
959+ allocation .caterer = Caterer .objects .get (name = second_pref )
960+ allocation .save ()
961+ student_limit [second_pref ][0 ] -= 1
962+ else :
963+ third_pref = allocation .third_pref
964+ if student_limit [third_pref ][0 ] > 0 :
965+ allocation .caterer = Caterer .objects .get (name = third_pref )
966+ allocation .save ()
967+ student_limit [third_pref ][0 ] -= 1
948968
949969 export_as_csv .short_description = "Export Allocation details to CSV"
950970
0 commit comments