From 609eb1a159f4cfcd2a25035cf59e5477d5ad4b1e Mon Sep 17 00:00:00 2001 From: Challenger Date: Thu, 8 Jun 2023 23:58:11 +0530 Subject: [PATCH 1/4] Added add_students script to add sample students --- CDC_Backend/scripts/add_students.py | 59 +++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 CDC_Backend/scripts/add_students.py diff --git a/CDC_Backend/scripts/add_students.py b/CDC_Backend/scripts/add_students.py new file mode 100644 index 0000000..1602a58 --- /dev/null +++ b/CDC_Backend/scripts/add_students.py @@ -0,0 +1,59 @@ +import random +from django.db.utils import IntegrityError +from APIs.models import Student + +# To run this script run the following command: +# python manage.py runscript add_students --script-args + +def run(*args): + + # Throw error if invalid number of arguments passed + if not args: + raise ValueError("Invalid number of arguments passed") + + if args[0] in ("manual", "man") : + if not args: + raise ValueError("Invalid number of arguments passed") + + for i in range(1, int(args[1])+1): + student = Student.objects.create( + id = input("Enter id: "), + roll_no = input("Enter roll_no: "), + name = input("Enter name: "), + branch = input("Enter branch: "), + phone_number = input("Enter phone_number: "), + cpi = input("Enter cpi: "), + degree = input("Enter degree: "), + batch = input("Enter batch: ") + + ) + student.save() + + elif args[0] in ("auto", "automatic"): + if not args: + raise ValueError("Invalid number of arguments passed") + + for i in range(1, int(args[1])+1): + try: + student = Student.objects.create( + id = i-1, + roll_no = 220010000 + i, + name = "Student " + str(i), + branch = random.choice(["CSE", "EE", "MECH", "CHEM", "CIVIL", "EP", "BSMS", "MNC"]), + phone_number = random.randint(1000000000, 9999999999), + cpi = random.random()*10, + degree = "bTech", + batch = random.randint(2018, 2023) + ) + student.save() + except IntegrityError: + pass + + elif args[0] in ("del", "delete"): + # delete students with name starting with Student + s = Student.objects.filter(name__startswith="Student") + s.delete() + + else: + raise ValueError("Invalid argument passed") + From 70711888ea563790b4b4958ef3f8f1ae465e011d Mon Sep 17 00:00:00 2001 From: Challenger Date: Fri, 9 Jun 2023 00:14:56 +0530 Subject: [PATCH 2/4] Using constants from APIs/constants.py now --- CDC_Backend/scripts/add_students.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/CDC_Backend/scripts/add_students.py b/CDC_Backend/scripts/add_students.py index 1602a58..e540204 100644 --- a/CDC_Backend/scripts/add_students.py +++ b/CDC_Backend/scripts/add_students.py @@ -2,9 +2,12 @@ import random from django.db.utils import IntegrityError from APIs.models import Student +from APIs.constants import BRANCHES, BATCH_CHOICES, DEGREE_CHOICES + # To run this script run the following command: # python manage.py runscript add_students --script-args + def run(*args): # Throw error if invalid number of arguments passed @@ -39,11 +42,11 @@ def run(*args): id = i-1, roll_no = 220010000 + i, name = "Student " + str(i), - branch = random.choice(["CSE", "EE", "MECH", "CHEM", "CIVIL", "EP", "BSMS", "MNC"]), + branch = random.choice(BRANCHES), phone_number = random.randint(1000000000, 9999999999), cpi = random.random()*10, - degree = "bTech", - batch = random.randint(2018, 2023) + degree = random.choice(DEGREE_CHOICES)[0], + batch = random.choice(BATCH_CHOICES)[0], ) student.save() except IntegrityError: From 1da13d787f844912fcd77cf83941e3f3ffee1f17 Mon Sep 17 00:00:00 2001 From: Challenger Date: Fri, 9 Jun 2023 18:38:01 +0530 Subject: [PATCH 3/4] Changed the format to get input --- CDC_Backend/scripts/add_students.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/CDC_Backend/scripts/add_students.py b/CDC_Backend/scripts/add_students.py index e540204..35503f6 100644 --- a/CDC_Backend/scripts/add_students.py +++ b/CDC_Backend/scripts/add_students.py @@ -17,17 +17,24 @@ def run(*args): if args[0] in ("manual", "man") : if not args: raise ValueError("Invalid number of arguments passed") - + + print("id\troll_no\tname\tbranch\tphone_number\tcpi\tdegree\tbatch") for i in range(1, int(args[1])+1): + details = input() + details = details.split(",") + details = [i.strip() for i in details] + details = [int(details[0]), int(details[1]), details[2], details[3], + int(details[4]), float(details[5]), details[6], details[7]] + student = Student.objects.create( - id = input("Enter id: "), - roll_no = input("Enter roll_no: "), - name = input("Enter name: "), - branch = input("Enter branch: "), - phone_number = input("Enter phone_number: "), - cpi = input("Enter cpi: "), - degree = input("Enter degree: "), - batch = input("Enter batch: ") + id = details[0], + roll_no = details[1], + name = details[2], + branch = details[3], + phone_number = details[4], + cpi = details[5], + degree = details[6], + batch = details[7], ) student.save() From f0a02ec2d022f68e44f6c2cd5af4f7fd8fabcc6c Mon Sep 17 00:00:00 2001 From: Challenger Date: Fri, 9 Jun 2023 18:55:55 +0530 Subject: [PATCH 4/4] Updated checks for script arguments --- CDC_Backend/scripts/add_students.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CDC_Backend/scripts/add_students.py b/CDC_Backend/scripts/add_students.py index 35503f6..107669c 100644 --- a/CDC_Backend/scripts/add_students.py +++ b/CDC_Backend/scripts/add_students.py @@ -15,7 +15,8 @@ def run(*args): raise ValueError("Invalid number of arguments passed") if args[0] in ("manual", "man") : - if not args: + + if len(args) != 2 or not args[1].isdigit() or int(args[1]) < 1: raise ValueError("Invalid number of arguments passed") print("id\troll_no\tname\tbranch\tphone_number\tcpi\tdegree\tbatch") @@ -40,7 +41,7 @@ def run(*args): student.save() elif args[0] in ("auto", "automatic"): - if not args: + if len(args) != 2 or not args[1].isdigit() or int(args[1]) < 1: raise ValueError("Invalid number of arguments passed") for i in range(1, int(args[1])+1): @@ -61,7 +62,7 @@ def run(*args): elif args[0] in ("del", "delete"): # delete students with name starting with Student - s = Student.objects.filter(name__startswith="Student") + s = Student.objects.filter(name__startswith="Stundent") s.delete() else: