|
|
|
|
@ -1,11 +1,40 @@ |
|
|
|
|
from django.contrib.auth.decorators import login_required |
|
|
|
|
from django.http import JsonResponse |
|
|
|
|
from django.views.generic import ListView |
|
|
|
|
from django.template import loader |
|
|
|
|
|
|
|
|
|
from .models import Course |
|
|
|
|
from django.views.generic import View, ListView |
|
|
|
|
from django.views.decorators.csrf import csrf_exempt |
|
|
|
|
from django.views.decorators.http import require_http_methods |
|
|
|
|
from .models import Course, Like |
|
|
|
|
from .filters import CourseFilter |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_required |
|
|
|
|
@csrf_exempt |
|
|
|
|
@require_http_methods(['POST']) |
|
|
|
|
def likes(request, course_id): |
|
|
|
|
try: |
|
|
|
|
course = Course.objects.prefetch_related('likes').get(id=course_id) |
|
|
|
|
except Course.DoesNotExist: |
|
|
|
|
return JsonResponse({ |
|
|
|
|
'success': False, |
|
|
|
|
'errors': ['Course with id f{cource_id} not found'] |
|
|
|
|
}) |
|
|
|
|
else: |
|
|
|
|
course_user_likes = course.likes.filter(user=request.user) |
|
|
|
|
if course_user_likes.exists(): |
|
|
|
|
is_liked = False |
|
|
|
|
course_user_likes.delete() |
|
|
|
|
else: |
|
|
|
|
course.likes.add(Like.objects.create(user=request.user)) |
|
|
|
|
is_liked = True |
|
|
|
|
count = course.likes.count() |
|
|
|
|
return JsonResponse({ |
|
|
|
|
"success": True, |
|
|
|
|
"likes_count": count, |
|
|
|
|
"is_liked": is_liked, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CoursesView(ListView): |
|
|
|
|
model = Course |
|
|
|
|
context_object_name = 'course_items' |
|
|
|
|
|