diff --git a/AlumniConnect/management/__init__.py b/AlumniConnect/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/AlumniConnect/management/commands/__init__.py b/AlumniConnect/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/AlumniConnect/management/commands/populate.py b/AlumniConnect/management/commands/populate.py new file mode 100644 index 00000000..447ede20 --- /dev/null +++ b/AlumniConnect/management/commands/populate.py @@ -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.")) diff --git a/AlumniConnect/settings/common.py b/AlumniConnect/settings/common.py index 60be5331..db4df0d5 100644 --- a/AlumniConnect/settings/common.py +++ b/AlumniConnect/settings/common.py @@ -36,7 +36,8 @@ 'applications.chapter', 'ckeditor', 'ckeditor_uploader', - 'tempus_dominus' + 'tempus_dominus', + 'AlumniConnect', ] MIDDLEWARE = [ diff --git a/requirements.txt b/requirements.txt index 37eea0a3..5c50964c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/scripts/acc.xlsx b/scripts/acc.xlsx deleted file mode 100644 index bf15a7b7..00000000 Binary files a/scripts/acc.xlsx and /dev/null differ diff --git a/scripts/add_batch.py b/scripts/add_batch.py index 4e7543c7..a8518066 100644 --- a/scripts/add_batch.py +++ b/scripts/add_batch.py @@ -1,5 +1,10 @@ from applications.alumniprofile.models import Batch -for num in range(2009, 2023): - b1 = Batch(batch = num) - b1.save() \ No newline at end of file +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 diff --git a/scripts/add_data.py b/scripts/add_data.py index c2902bf7..48f13724 100644 --- a/scripts/add_data.py +++ b/scripts/add_data.py @@ -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 \ No newline at end of file diff --git a/scripts/add_degree.py b/scripts/add_degree.py index a78ac71f..faa4b60a 100644 --- a/scripts/add_degree.py +++ b/scripts/add_degree.py @@ -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 \ No newline at end of file diff --git a/scripts/add_pass.py b/scripts/add_pass.py index a4d81491..653250db 100644 --- a/scripts/add_pass.py +++ b/scripts/add_pass.py @@ -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 diff --git a/scripts/add_reg.py b/scripts/add_reg.py deleted file mode 100644 index 26734395..00000000 --- a/scripts/add_reg.py +++ /dev/null @@ -1,24 +0,0 @@ -from applications.alumniprofile.models import Profile -from AlumniConnect.views import reg_no_gen,convert_int - -print(Profile.objects.all().order_by('user__date_joined')) - -# def reg_no_gen(degree_, spec_, year): -# print(Profile.objects.all().order_by('user__date_joined')) -# print(Profile.objects.all().order_by('user__date_joined')) -# 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)) - -# def convert_int(number,decimals) : -# return str(number).zfill(decimals) -for p in Profile.objects.all().order_by('user__date_joined'): - # p.is_verified=True - #p.save() - p.reg_no = reg_no_gen(p.programme, p.branch, p.year_of_admission, p.user.date_joined) - - #p.reg_no = 0 - print(p.name, p.reg_no, int(p.reg_no)) - p.save() \ No newline at end of file diff --git a/scripts/data_acc.xlsx b/scripts/data_acc.xlsx new file mode 100644 index 00000000..0a442770 Binary files /dev/null and b/scripts/data_acc.xlsx differ