-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat : Added populate.py to populate dummy data for the db (#138)
- Loading branch information
1 parent
3f0d5d3
commit 12a76a0
Showing
12 changed files
with
131 additions
and
53 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
Binary file not shown.