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

Feat : Added populate.py to populate dummy data for the db #138

Merged
merged 10 commits into from
May 21, 2024
Empty file.
Empty file.
40 changes: 40 additions & 0 deletions applications/adminportal/management/commands/populate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.core.management.base import BaseCommand
from scripts.add_batch import add_batch
from scripts.add_data import add_data
from scripts.add_degree import add_degree
from scripts.add_pass import add_pass


class Command(BaseCommand):
help = 'Populates the database with initial data'

def handle(self, *args, **options):
self.stdout.write("Starting the population process")


try :
add_batch()
self.stdout.write(self.style.SUCCESS("Batches added successfully"))
except :
self.stdout.write(self.style.ERROR("An error occured while adding batches"))

try :
add_degree()
self.stdout.write(self.style.SUCCESS("Degrees added successfully"))
except :
self.stdout.write(self.style.ERROR("An error occured while adding degrees"))

try :
add_data()
self.stdout.write(self.style.SUCCESS("Data added successfully"))
except :
self.stdout.write(self.style.ERROR("An error occured while adding data"))

try :
add_pass()
self.stdout.write(self.style.SUCCESS("Passwords added successfully"))
except :
self.stdout.write(self.style.ERROR("An error occured while adding passwords"))
# ... call other functions ...

self.stdout.write(self.style.SUCCESS("Population process completed successfully"))
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ vine==5.1.0
wcwidth==0.2.13
webencodings==0.5.1
wrapt==1.16.0
xlrd==1.2.0
zipp==0.6.0
openpyxl==3.0.9
garg3133 marked this conversation as resolved.
Show resolved Hide resolved

Binary file removed scripts/acc.xlsx
Binary file not shown.
16 changes: 13 additions & 3 deletions scripts/add_batch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "AlumniConnect.settings.development")
django.setup()

garg3133 marked this conversation as resolved.
Show resolved Hide resolved
from applications.alumniprofile.models import Batch

for num in range(2009, 2023):
b1 = Batch(batch = num)
b1.save()
def add_batch() :
try :
batch_list = [Batch(batch = year) for year in range(2000, 2024)]
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
Batch.objects.bulk_create(batch_list, ignore_conflicts=True)
print("Batch added successfully")
except Exception as e :
print(f'An error occured: {e}')
raise
78 changes: 63 additions & 15 deletions scripts/add_data.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,65 @@
import datetime, xlrd
from applications.alumniprofile.models import Profile, Batch
from openpyxl import load_workbook
from django.contrib.auth.models import User
from applications.alumniprofile.models import Profile, Batch
from django.db import transaction

def reg_no_gen(degree_, spec_, year):
degree = {"B.Tech": "1", "B.Des": '2', "M.Tech": '3', "M.Des": '4', "PhD": '5'}
spec = {"NA": '00', "CSE": "01", "ECE": "02", "ME": "03", "MT": "04", "NS": "05", "DS": "06"}
last_reg_no = Profile.objects.filter(year_of_admission=year).order_by('user__date_joined').last()
new_reg_no = (int(str(last_reg_no.reg_no)[-4:]) + 1) if last_reg_no else 1
return degree[degree_] + spec[spec_] + str(year)[2:] + str(convert_int(new_reg_no, 4))
garg3133 marked this conversation as resolved.
Show resolved Hide resolved

def convert_int(number, decimals):
return str(number).zfill(decimals)

def add_data() :

location = "scripts/data_acc.xlsx"
wb = load_workbook(location)
sheet = wb.active

print("Adding data")
with transaction.atomic():
try:
for row in sheet.iter_rows(min_row=2, values_only=True):
roll_no, name, email, sex, dob, programme, branch, batch_year,year_of_admission = row

roll_no = str(int(roll_no))
email = str(email)

# User creation logic
user, user_created = User.objects.get_or_create(username=roll_no, defaults={'email': email})
garg3133 marked this conversation as resolved.
Show resolved Hide resolved
if user_created:
user.set_password(roll_no)
user.save()

# Batch creation logic
batch, created = Batch.objects.get_or_create(batch=int(batch_year))

# Profile creation logic
profile, profile_created = Profile.objects.get_or_create(
user=user,
defaults={
'email': email,
'roll_no': roll_no,
'name': name,
'sex': sex,
'programme': programme,
'branch': branch,
'batch': batch, # use the batch instance directly
'date_of_birth': dob, # Assuming dob is already a datetime object
"year_of_admission" : year_of_admission
}
)

if profile_created:
profile.reg_no = reg_no_gen(programme, branch, batch_year)
profile.save()
print(profile.name, profile.year_of_admission, profile.reg_no)



loc = "acc.xlsx"
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)

for row in range(1, sheet.nrows):
vals = sheet.row_values(row)
a1 = sheet.cell_value(rowx=row, colx=4)
u1 = User(username=str(int(vals[0])), email=str(sheet.cell(row, 2).value))
u1.set_password(str(int(vals[0])))
u1.save()
p1 = Profile(user=u1, email=str(sheet.cell(row, 2).value), roll_no=int(vals[0]), name=vals[1], sex=vals[3], programme=vals[5], branch=vals[6], batch=Batch(int(vals[7])) , date_of_birth=datetime.datetime(*xlrd.xldate_as_tuple(a1, wb.datemode)))

p1.save()
except Exception as e :
print(f'An error occured: {e}')
raise
12 changes: 9 additions & 3 deletions scripts/add_degree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

degrees = ['Class 10th','Class 12th','Phd','M.Tech','B.Tech','M.Des','B.Des','MAcc', 'MAS', 'MEcon', 'MArch', 'MASc', 'MA', 'MAT', 'MA', 'MBus', 'MBA', 'MBI', 'MChem', 'MCom', 'MCA', 'MCJ', 'MDes', 'MDiv', 'MEcon', 'MEd', 'MEnt', 'MEng', 'MEM', 'LLM Eur', 'MFin', 'Master of Quantitative Finance', 'MFA', 'MHA', 'MHS', 'MH', 'MILR', 'MIS', 'MISM', 'MSIT', 'MJ', 'LLM', 'MSL', 'MArch', 'MLitt', 'MA', 'MLIS', 'MM', 'MM', 'OT', 'MPharm', 'MPhil', 'MPhys', 'MPS', 'MPA', 'MPAff', 'MPH', 'MPP', 'MRb', 'R', 'STM', 'MSM', 'MSc', 'MSE', 'MFin', 'HRD', 'MSMIS', 'MSIS', 'MSIT', 'MSN', 'MSPM', 'MSc', 'MSL', 'SCM', 'MST', 'MSW', 'MSSc', 'ChM', 'MSt', 'ThM', 'MTS', 'MVSC']

for degree in degrees:
deg = Degree(degree=degree)
deg.save()
def add_degree() :
print("Adding degree")
try :
degree_list = [Degree(degree = degree) for degree in degrees]
Degree.objects.bulk_create(degree_list, ignore_conflicts=True)
print("Degree added successfully")
except Exception as e :
print(f'An error occured: {e}')
raise
20 changes: 14 additions & 6 deletions scripts/add_pass.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
from django.contrib.auth.models import User
from applications.alumniprofile.models import Profile

users = Profile.objects.all()
for user in users:
print(user.reg_no)
#user.user.is_active = True
user.user.set_password(user.reg_no)
user.user.save()

def add_pass() :
print("Adding password")
try :
profiles = Profile.objects.all()
users_to_update = []
for profile in profiles:
profile.user.set_password(profile.reg_no)
users_to_update.append(profile.user)
User.objects.bulk_update(users_to_update, ['password'])
print("Password added successfully")
except Exception as e :
print(f'An error occured: {e}')
raise
24 changes: 0 additions & 24 deletions scripts/add_reg.py

This file was deleted.

Binary file added scripts/data_acc.xlsx
Binary file not shown.