-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from vaibhav1724:upload_projects
added function to upload projects from CSV file
- Loading branch information
Showing
3 changed files
with
63 additions
and
41 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
socbackend/accounts/migrations/0004_alter_userprofile_roll_number.py
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,19 @@ | ||
# Generated by Django 4.2.6 on 2024-11-08 20:34 | ||
|
||
import accounts.models | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('accounts', '0003_alter_userprofile_department'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='userprofile', | ||
name='roll_number', | ||
field=models.CharField(error_messages={'unique': 'A user with that roll number already exists.'}, help_text='Required. 9 characters or fewer.', max_length=9, unique=True, validators=[accounts.models.validate_roll_number], verbose_name='roll number'), | ||
), | ||
] |
83 changes: 42 additions & 41 deletions
83
socbackend/projects/management/commands/upload_projects.py
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,46 +1,47 @@ | ||
import csv | ||
import os | ||
from django.core.management.base import BaseCommand, CommandError | ||
from projects.models import Project | ||
from django.conf import settings | ||
|
||
class Command(BaseCommand): | ||
help = 'Upload project data from a CSV file' | ||
# Write your functions for uploading the projects from csv file.. | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('csv_file', type=str, help='The path to the CSV file to be processed') | ||
# Use projects.csv | ||
import os | ||
import uuid | ||
from django.core.files import File | ||
import pandas as pd | ||
from projects.models import Project | ||
|
||
def handle(self, *args, **options): | ||
csv_file_path = options['csv_file'] | ||
IMG_PROJECTS_PATH = os.path.join( | ||
os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))), | ||
'img' | ||
) | ||
|
||
if not os.path.exists(csv_file_path): | ||
raise CommandError(f"File '{csv_file_path}' does not exist.") | ||
def upload_projects(csv_file_path): | ||
data = pd.read_csv(csv_file_path) | ||
no_of_projects = Project.objects.all().__len__ | ||
if no_of_projects != data.__len__: | ||
Project.objects.all().delete() | ||
# Iterate through each row and save it to the Project model | ||
for index ,row in data.iterrows(): | ||
project = Project( | ||
title=row['title'], | ||
general_category=row.get('general_category', 'Others'), | ||
specific_category=row.get('specific_category', 'NA'), | ||
mentee_max=row['mentee_max'], | ||
mentor=row.get('mentor', 'NA'), | ||
co_mentor_info=row.get('co_mentor_info', 'NA'), | ||
description=row.get('description', 'NA'), | ||
timeline=row.get('timeline', 'NA'), | ||
checkpoints=row.get('checkpoints', 'NA'), | ||
prereuisites=row.get('prereuisites', 'NA'), | ||
banner_image=row.get('banner_image', None), | ||
banner_image_link=row.get('banner_image_link', None), | ||
) | ||
image_filename = row.get('banner_image') | ||
if pd.notna(image_filename): | ||
image_path = os.path.join(IMG_PROJECTS_PATH, image_filename) | ||
if os.path.exists(image_path): | ||
with open(image_path, 'rb') as image_file: | ||
project.banner_image.save(image_filename, File(image_file), save=False) | ||
|
||
with open(csv_file_path, newline='') as csvfile: | ||
reader = csv.DictReader(csvfile) | ||
for row in reader: | ||
try: | ||
project, created = Project.objects.update_or_create( | ||
project_id=row['project_id'], | ||
defaults={ | ||
'primary_mentor': row['primary_mentor'], | ||
'project_title': row['project_title'], | ||
'co_mentor_details': row['co_mentor_details'], | ||
'project_category_specific': row['project_category_specific'], | ||
'project_description': row['project_description'], | ||
'maximum_mentees': row['maximum_mentees'], | ||
'prereuisites': row['prereuisites'], | ||
'banner_image_path': row['banner_image_path'], | ||
'banner_image_url': row['banner_image_url'], | ||
'project_timeline': row['project_timeline'], | ||
'project_checkpoints': row['project_checkpoints'], | ||
'project_category_general': row['project_category_general'], | ||
} | ||
) | ||
if created: | ||
self.stdout.write(self.style.SUCCESS(f"Successfully created project {project.project_title}")) | ||
else: | ||
self.stdout.write(self.style.SUCCESS(f"Updated project {project.project_title}")) | ||
except Exception as e: | ||
self.stdout.write(self.style.ERROR(f"Error processing row {row}: {e}")) | ||
if not project.code: | ||
project.code = str(uuid.uuid4())[:8] | ||
|
||
no_of_projects = data.__len__ | ||
project.save() |
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