Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added script for alloting rooms to students. #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions institute/management/commands/allotrooms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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
from django.conf import settings


class Command(BaseCommand):
help = "Allot rooms to students given the Roll No. Usage: python manage.py allotrooms <filename>.csv"

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(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"])
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)))