Skip to content

Commit

Permalink
Feat : Added populate.py to populate dummy data for the db (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
Priyansh61 authored May 21, 2024
1 parent 3f0d5d3 commit 12a76a0
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 53 deletions.
Empty file.
Empty file.
34 changes: 34 additions & 0 deletions AlumniConnect/management/commands/populate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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")

operations = [
(add_batch, "Batches added successfully", "An error occured while adding batches"),
(add_degree, "Degrees added successfully", "An error occured while adding degrees"),
(add_data, "Data added successfully", "An error occured while adding data"),
(add_pass, "Passwords added successfully", "An error occured while adding passwords")
]

error_occured = False

for operation, success_message, error_message in operations:
try:
operation()
self.stdout.write(self.style.SUCCESS(success_message))
except:
error_occured = True
self.stdout.write(self.style.ERROR(error_message))

if error_occured:
self.stdout.write(self.style.ERROR("Some data could not be added. Please check the error messages above."))
else:
self.stdout.write(self.style.SUCCESS("Population process completed successfully."))
3 changes: 2 additions & 1 deletion AlumniConnect/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
'applications.chapter',
'ckeditor',
'ckeditor_uploader',
'tempus_dominus'
'tempus_dominus',
'AlumniConnect',
]

MIDDLEWARE = [
Expand Down
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
amqp==5.2.0
astroid==2.1.0
async-timeout==4.0.3
backports.zoneinfo==0.2.1
beautifulsoup4==4.6.0
billiard==4.2.0
cachetools==3.1.0
Expand All @@ -12,7 +13,7 @@ click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.3.0
colorama==0.4.4
defusedxml==0.5.0
defusedxml==0.7.1
Django==2.2.28
django-allauth==0.32.0
django-anymail==6.0
Expand All @@ -29,6 +30,7 @@ django-semanticui-forms==1.6.5
django-tempus-dominus==5.1.2.13
django-widget-tweaks==1.4.3
easy-thumbnails==2.6
et-xmlfile==1.1.0
google-auth==1.6.3
html5lib==1.0b10
httplib2==0.12.3
Expand All @@ -42,6 +44,7 @@ Markdown==3.0.1
mccabe==0.6.1
more-itertools==8.0.2
oauthlib==3.0.1
openpyxl==3.1.2
pilkit==3.0
Pillow==9.5.0
prompt-toolkit==3.0.43
Expand Down
Binary file removed scripts/acc.xlsx
Binary file not shown.
11 changes: 8 additions & 3 deletions scripts/add_batch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
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(2009, 2024)]
Batch.objects.bulk_create(batch_list, ignore_conflicts=True)
print("Batch added successfully")
except Exception as e :
print(f'An error occured: {e}')
raise
75 changes: 60 additions & 15 deletions scripts/add_data.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,62 @@
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
from AlumniConnect.views import reg_no_gen

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)
first_name, last_name = name.split(' ', 1)


# User creation logic
user, user_created = User.objects.get_or_create(
username=roll_no,
first_name=first_name,
last_name=last_name,
defaults={'email': email})
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.

0 comments on commit 12a76a0

Please sign in to comment.