From 609eb1a159f4cfcd2a25035cf59e5477d5ad4b1e Mon Sep 17 00:00:00 2001 From: Challenger Date: Thu, 8 Jun 2023 23:58:11 +0530 Subject: [PATCH] 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") +