Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-D-Akbar committed Jan 21, 2025
1 parent fed0152 commit 91b6f81
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<option value="">-- Select Sub-Vertical --</option>
<option value="">-- Select SubVertical --</option>
{% for sub_vertical in sub_verticals %}
<option value={{ sub_vertical.slug }} >{{ sub_vertical.name }}</option>
{% endfor %}
35 changes: 13 additions & 22 deletions course_discovery/apps/tagging/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.shortcuts import render, get_object_or_404, redirect
from django.core.paginator import Paginator
from django.http import JsonResponse
from course_discovery.apps.tagging.models import Vertical, SubVertical, CourseVertical, Course
from django.http import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.http import HttpResponse

from course_discovery.apps.tagging.models import Course, CourseVertical, SubVertical, Vertical


def course_detail(request, uuid):
Expand All @@ -19,22 +19,20 @@ def course_detail(request, uuid):
sub_vertical = SubVertical.objects.filter(slug=sub_vertical_slug).first() if sub_vertical_slug else None

if sub_vertical and sub_vertical.vertical != vertical:
if request.htmx:
html = render_to_string("partials/message.html", {
"error": "Sub-vertical does not belong to the selected vertical."
}, request)
return HttpResponse(html, status=200)
html = render_to_string("partials/message.html", {
"error": "Sub-vertical does not belong to the selected vertical."
}, request)
return HttpResponse(html, status=200)

CourseVertical.objects.update_or_create(
course=course,
defaults={"vertical": vertical, "sub_vertical": sub_vertical}
)

if request.htmx:
html = render_to_string("partials/message.html", {
"success": "Vertical and Sub-Vertical assigned successfully."
}, request)
return HttpResponse(html, status=200)
html = render_to_string("partials/message.html", {
"success": "Vertical and Sub-Vertical assigned successfully."
}, request)
return HttpResponse(html, status=200)

return render(request, "tagging/course_detail.html", {
"course": course,
Expand All @@ -54,10 +52,6 @@ def load_subverticals(request):
return JsonResponse({"html": html})


from django.http import HttpResponse
from django.template.loader import render_to_string
from .models import Course

def course_list(request):
search_query = request.GET.get('search', '')
sort_by = request.GET.get('sort', 'title')
Expand All @@ -75,16 +69,14 @@ def course_list(request):
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)

# If HTMX request, return just the HTML fragment for the table and pagination
if request.headers.get('HX-Request'):
html = render_to_string('partials/course_table.html', {
'courses': page_obj,
'current_sort': sort_by.lstrip('-'),
'current_direction': direction,
})
return HttpResponse(html) # Return raw HTML, not wrapped in JsonResponse
return HttpResponse(html)

# Normal response if not an HTMX request
return render(request, "tagging/course_list.html", {
"courses": page_obj,
"current_sort": sort_by.lstrip('-'),
Expand All @@ -108,7 +100,6 @@ def vertical_list(request):
})



def subvertical_list(request):
"""
Renders a list of all SubVerticals with their parent Verticals and assigned Courses.
Expand Down

0 comments on commit 91b6f81

Please sign in to comment.