Skip to content

Commit

Permalink
chore: remove some admin code
Browse files Browse the repository at this point in the history
  • Loading branch information
AfaqShuaib09 committed Dec 23, 2024
1 parent ed502a7 commit 2c7fa2d
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 57 deletions.
56 changes: 0 additions & 56 deletions course_discovery/apps/tagging/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import csv
from io import TextIOWrapper

from django.contrib import admin, messages
from django.shortcuts import redirect, render
from django.template.response import TemplateResponse
from django.urls import reverse

from course_discovery.apps.course_metadata.models import Course
Expand All @@ -18,60 +16,6 @@ class VerticalFilterAdmin(admin.ModelAdmin):
list_display = ('name', 'is_active', 'description', 'slug',)
search_fields = ('name',)

def changelist_view(self, request, extra_context=None):
extra_context = extra_context or {}
extra_context['upload_csv_url'] = reverse('admin:verticalfilter_upload_csv')
return super().changelist_view(request, extra_context=extra_context)

def get_urls(self):
from django.urls import path
urls = super().get_urls()
custom_urls = [
path(
'upload-csv/',
self.admin_site.admin_view(self.upload_csv),
name='verticalfilter_upload_csv',
),
]
return custom_urls + urls

def upload_csv(self, request):
from django.shortcuts import redirect
if request.method == 'POST' and request.FILES.get('csv_file'):
csv_file = request.FILES['csv_file']
file_data = TextIOWrapper(csv_file, encoding='utf-8')
csv_reader = csv.reader(file_data)

# Skipping the header row
next(csv_reader)

created_count = 0
for row in csv_reader:
# Assuming the CSV structure is [name, description, is_active]
if row: # Skip empty rows
name, description, is_active = row
try:
# Create a VerticalFilter instance for each row
VerticalFilter.objects.create(
name=name,
description=description,
is_active=(is_active.lower() == 'true'), # Assuming 'true' or 'false' in CSV
)
created_count += 1
except Exception as e:
# Log the error or handle invalid rows
messages.error(request, f"Error processing row: {row}. Error: {e}")

messages.success(request, f"{created_count} VerticalFilter instances created successfully!")
return redirect('admin:tagging_verticalfilter_changelist')

return TemplateResponse(
request,
"admin/tagging/verticalfilter/upload_csv_form.html",
context={'opts': self.model._meta},
)


@admin.register(SubVericalFilter)
class SubVericalFilterAdmin(admin.ModelAdmin):
list_display = ('name', 'is_active', 'slug', 'description', 'vertical_filters')
Expand Down
1 change: 0 additions & 1 deletion course_discovery/apps/tagging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from course_discovery.apps.course_metadata.models import Course, Program


# Create your models here.
class VerticalFilter(TimeStampedModel):
name = models.CharField(max_length=255, unique=True)
slug = AutoSlugField(populate_from='name', max_length=255, unique=True)
Expand Down

0 comments on commit 2c7fa2d

Please sign in to comment.