import
This commit is contained in:
@@ -1316,3 +1316,219 @@ class AuthViewSet(viewsets.GenericViewSet):
|
||||
message='Password has been reset successfully',
|
||||
request=request
|
||||
)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# CSV Import/Export Views for Admin
|
||||
# ============================================================================
|
||||
|
||||
from django.http import HttpResponse, JsonResponse
|
||||
from django.contrib.admin.views.decorators import staff_member_required
|
||||
from django.views.decorators.http import require_http_methods
|
||||
import csv
|
||||
import io
|
||||
|
||||
|
||||
@staff_member_required
|
||||
@require_http_methods(["GET"])
|
||||
def industry_csv_template(request):
|
||||
"""Download CSV template for Industry import"""
|
||||
response = HttpResponse(content_type='text/csv')
|
||||
response['Content-Disposition'] = 'attachment; filename="industry_template.csv"'
|
||||
|
||||
writer = csv.writer(response)
|
||||
writer.writerow(['name', 'description', 'is_active'])
|
||||
writer.writerow(['Technology', 'Technology industry', 'true'])
|
||||
writer.writerow(['Healthcare', 'Healthcare and medical services', 'true'])
|
||||
|
||||
return response
|
||||
|
||||
|
||||
@staff_member_required
|
||||
@require_http_methods(["POST"])
|
||||
def industry_csv_import(request):
|
||||
"""Import industries from CSV"""
|
||||
if not request.FILES.get('csv_file'):
|
||||
return JsonResponse({'success': False, 'error': 'No CSV file provided'}, status=400)
|
||||
|
||||
csv_file = request.FILES['csv_file']
|
||||
decoded_file = csv_file.read().decode('utf-8')
|
||||
io_string = io.StringIO(decoded_file)
|
||||
reader = csv.DictReader(io_string)
|
||||
|
||||
created = 0
|
||||
updated = 0
|
||||
errors = []
|
||||
|
||||
from django.utils.text import slugify
|
||||
|
||||
for row_num, row in enumerate(reader, start=2):
|
||||
try:
|
||||
is_active = row.get('is_active', 'true').lower() in ['true', '1', 'yes']
|
||||
slug = slugify(row['name'])
|
||||
|
||||
industry, created_flag = Industry.objects.update_or_create(
|
||||
name=row['name'],
|
||||
defaults={
|
||||
'slug': slug,
|
||||
'description': row.get('description', ''),
|
||||
'is_active': is_active
|
||||
}
|
||||
)
|
||||
if created_flag:
|
||||
created += 1
|
||||
else:
|
||||
updated += 1
|
||||
except Exception as e:
|
||||
errors.append(f"Row {row_num}: {str(e)}")
|
||||
|
||||
return JsonResponse({
|
||||
'success': True,
|
||||
'created': created,
|
||||
'updated': updated,
|
||||
'errors': errors
|
||||
})
|
||||
|
||||
|
||||
@staff_member_required
|
||||
@require_http_methods(["GET"])
|
||||
def industrysector_csv_template(request):
|
||||
"""Download CSV template for IndustrySector import"""
|
||||
response = HttpResponse(content_type='text/csv')
|
||||
response['Content-Disposition'] = 'attachment; filename="industrysector_template.csv"'
|
||||
|
||||
writer = csv.writer(response)
|
||||
writer.writerow(['name', 'industry', 'description', 'is_active'])
|
||||
writer.writerow(['Software Development', 'Technology', 'Software and app development', 'true'])
|
||||
writer.writerow(['Healthcare IT', 'Healthcare', 'Healthcare information technology', 'true'])
|
||||
|
||||
return response
|
||||
|
||||
|
||||
@staff_member_required
|
||||
@require_http_methods(["POST"])
|
||||
def industrysector_csv_import(request):
|
||||
"""Import industry sectors from CSV"""
|
||||
if not request.FILES.get('csv_file'):
|
||||
return JsonResponse({'success': False, 'error': 'No CSV file provided'}, status=400)
|
||||
|
||||
csv_file = request.FILES['csv_file']
|
||||
decoded_file = csv_file.read().decode('utf-8')
|
||||
io_string = io.StringIO(decoded_file)
|
||||
reader = csv.DictReader(io_string)
|
||||
|
||||
created = 0
|
||||
updated = 0
|
||||
errors = []
|
||||
|
||||
from django.utils.text import slugify
|
||||
|
||||
for row_num, row in enumerate(reader, start=2):
|
||||
try:
|
||||
is_active = row.get('is_active', 'true').lower() in ['true', '1', 'yes']
|
||||
slug = slugify(row['name'])
|
||||
|
||||
# Find industry by name
|
||||
try:
|
||||
industry = Industry.objects.get(name=row['industry'])
|
||||
except Industry.DoesNotExist:
|
||||
errors.append(f"Row {row_num}: Industry '{row['industry']}' not found")
|
||||
continue
|
||||
|
||||
sector, created_flag = IndustrySector.objects.update_or_create(
|
||||
name=row['name'],
|
||||
industry=industry,
|
||||
defaults={
|
||||
'slug': slug,
|
||||
'description': row.get('description', ''),
|
||||
'is_active': is_active
|
||||
}
|
||||
)
|
||||
if created_flag:
|
||||
created += 1
|
||||
else:
|
||||
updated += 1
|
||||
except Exception as e:
|
||||
errors.append(f"Row {row_num}: {str(e)}")
|
||||
|
||||
return JsonResponse({
|
||||
'success': True,
|
||||
'created': created,
|
||||
'updated': updated,
|
||||
'errors': errors
|
||||
})
|
||||
|
||||
|
||||
@staff_member_required
|
||||
@require_http_methods(["GET"])
|
||||
def seedkeyword_csv_template(request):
|
||||
"""Download CSV template for SeedKeyword import"""
|
||||
response = HttpResponse(content_type='text/csv')
|
||||
response['Content-Disposition'] = 'attachment; filename="seedkeyword_template.csv"'
|
||||
|
||||
writer = csv.writer(response)
|
||||
writer.writerow(['keyword', 'industry', 'sector', 'volume', 'difficulty', 'intent', 'is_active'])
|
||||
writer.writerow(['python programming', 'Technology', 'Software Development', '10000', '45', 'Informational', 'true'])
|
||||
writer.writerow(['medical software', 'Healthcare', 'Healthcare IT', '5000', '60', 'Commercial', 'true'])
|
||||
|
||||
return response
|
||||
|
||||
|
||||
@staff_member_required
|
||||
@require_http_methods(["POST"])
|
||||
def seedkeyword_csv_import(request):
|
||||
"""Import seed keywords from CSV"""
|
||||
if not request.FILES.get('csv_file'):
|
||||
return JsonResponse({'success': False, 'error': 'No CSV file provided'}, status=400)
|
||||
|
||||
csv_file = request.FILES['csv_file']
|
||||
decoded_file = csv_file.read().decode('utf-8')
|
||||
io_string = io.StringIO(decoded_file)
|
||||
reader = csv.DictReader(io_string)
|
||||
|
||||
created = 0
|
||||
updated = 0
|
||||
errors = []
|
||||
|
||||
for row_num, row in enumerate(reader, start=2):
|
||||
try:
|
||||
is_active = row.get('is_active', 'true').lower() in ['true', '1', 'yes']
|
||||
|
||||
# Find industry and sector by name
|
||||
try:
|
||||
industry = Industry.objects.get(name=row['industry'])
|
||||
except Industry.DoesNotExist:
|
||||
errors.append(f"Row {row_num}: Industry '{row['industry']}' not found")
|
||||
continue
|
||||
|
||||
try:
|
||||
sector = IndustrySector.objects.get(name=row['sector'], industry=industry)
|
||||
except IndustrySector.DoesNotExist:
|
||||
errors.append(f"Row {row_num}: Sector '{row['sector']}' not found in industry '{row['industry']}'")
|
||||
continue
|
||||
|
||||
keyword, created_flag = SeedKeyword.objects.update_or_create(
|
||||
keyword=row['keyword'],
|
||||
industry=industry,
|
||||
sector=sector,
|
||||
defaults={
|
||||
'volume': int(row.get('volume', 0)),
|
||||
'difficulty': int(row.get('difficulty', 0)),
|
||||
'intent': row.get('intent', 'Informational'),
|
||||
'is_active': is_active
|
||||
}
|
||||
)
|
||||
if created_flag:
|
||||
created += 1
|
||||
else:
|
||||
updated += 1
|
||||
except Exception as e:
|
||||
errors.append(f"Row {row_num}: {str(e)}")
|
||||
|
||||
return JsonResponse({
|
||||
'success': True,
|
||||
'created': created,
|
||||
'updated': updated,
|
||||
'errors': errors
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user