From 7d8987d12666d3c8fb9c834e5242639d07835b4c Mon Sep 17 00:00:00 2001 From: eswarclynn Date: Sun, 8 Nov 2020 19:11:47 +0530 Subject: [PATCH 1/2] Added script for alloting rooms to students. --- institute/management/commands/allotrooms.py | 61 +++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 institute/management/commands/allotrooms.py diff --git a/institute/management/commands/allotrooms.py b/institute/management/commands/allotrooms.py new file mode 100644 index 00000000..b94499e6 --- /dev/null +++ b/institute/management/commands/allotrooms.py @@ -0,0 +1,61 @@ +import os, csv, traceback +from django.core.exceptions import ValidationError +from django.core.management.base import BaseCommand, CommandError +from institute.models import Block, Student +from django.conf import settings + + +class Command(BaseCommand): + help = "Imports Students from given CSV file to Student Model." + + def get_file_path(self, file_name): + return os.path.join(settings.BASE_DIR, "data", file_name) + + def get_floor_from_letter(self, letter): + if letter == "G": + return "Ground" + elif letter == "F": + return "First" + elif letter == "S": + return "Second" + else: + raise ValidationError("Only Ground(G), First(F) and Second(S) floors accepted!") + + + def add_arguments(self, parser): + parser.add_argument("file_name", nargs="+", type=str) + + def handle(self, *args, **options): + file_path = self.get_file_path(options["file_name"][0]) + try: + with open(file_path) as csv_file: + csv_reader = csv.DictReader(csv_file, delimiter=",") + self.stdout.write(self.style.SUCCESS("Reading: {}".format(file_path))) + + created, rejected = [0, 0] + for data in csv_reader: + try: + student = Student.objects.get(roll_no = data["id"]) + roomdetail = student.roomdetail + roomdetail.room_no = int(data["Room no"]) + roomdetail.floor = self.get_floor_from_letter(data["floor"]) + roomdetail.block = (Block.objects.filter(name__contains = data["block name"]) | Block.objects.filter(name__contains = data["block name"].capitalize())).first() + roomdetail.full_clean() + roomdetail.save() + created += 1 + + except Student.DoesNotExist: + print("Invalid Student ID: {}".format(data["id"])) + rejected += 1 + except Exception as e: + if data["id"]: + print("Error while inserting student - {}: {}".format(data["id"], e.messages)) + # traceback.print_exc() + else: + pass + rejected += 1 + + except Exception as e: + raise CommandError("Error: {}".format(e)) + + self.stdout.write(self.style.SUCCESS('Successfully imported {} students. Rejected {} students.'.format(created, rejected))) \ No newline at end of file From 2c604f649bc39b9891fef52bad653a75ba4c98cf Mon Sep 17 00:00:00 2001 From: eswarclynn Date: Tue, 10 Nov 2020 17:20:42 +0530 Subject: [PATCH 2/2] Query students by roll no. or registration no. --- institute/management/commands/allotrooms.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/institute/management/commands/allotrooms.py b/institute/management/commands/allotrooms.py index b94499e6..a2f2f7ce 100644 --- a/institute/management/commands/allotrooms.py +++ b/institute/management/commands/allotrooms.py @@ -1,4 +1,5 @@ import os, csv, traceback +from django.db.models import Q from django.core.exceptions import ValidationError from django.core.management.base import BaseCommand, CommandError from institute.models import Block, Student @@ -6,7 +7,7 @@ class Command(BaseCommand): - help = "Imports Students from given CSV file to Student Model." + help = "Allot rooms to students given the Roll No. Usage: python manage.py allotrooms .csv" def get_file_path(self, file_name): return os.path.join(settings.BASE_DIR, "data", file_name) @@ -35,7 +36,7 @@ def handle(self, *args, **options): created, rejected = [0, 0] for data in csv_reader: try: - student = Student.objects.get(roll_no = data["id"]) + student = Student.objects.get(Q(regd_no = data["id"]) | Q(roll_no = data["id"])) roomdetail = student.roomdetail roomdetail.room_no = int(data["Room no"]) roomdetail.floor = self.get_floor_from_letter(data["floor"])