Skip to content

Commit

Permalink
Merge pull request #18 from vaibhav1724:upload_projects
Browse files Browse the repository at this point in the history
added function to upload projects from CSV file
  • Loading branch information
Pratyaksh2309 authored Nov 23, 2024
2 parents a93c88d + e120259 commit 1748e05
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 41 deletions.
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 socbackend/projects/management/commands/upload_projects.py
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()
2 changes: 2 additions & 0 deletions socbackend/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from accounts.models import UserProfile
from rest_framework.permissions import AllowAny
import logging
from projects.management.commands.upload_projects import upload_projects

logger = logging.getLogger(__name__)
# from .serializers import (
Expand Down Expand Up @@ -109,6 +110,7 @@ def delete(self, request):

class BasicProjectListView(generics.ListAPIView):
permission_classes = []
upload_projects("./projects.csv")
queryset = Project.objects.all()
serializer_class = BasicProjectSerializer

Expand Down

0 comments on commit 1748e05

Please sign in to comment.