LIL-595 Сортировка при выводе курсов на сайте

remotes/origin/hotfix/LIL-661
gzbender 8 years ago
parent d261388942
commit 4d06d62f06
  1. 19
      apps/course/models.py
  2. 15
      apps/course/views.py
  3. 2
      project/views.py

@ -1,4 +1,5 @@
import arrow
from random import shuffle
from uuid import uuid4
from django.db import models
from django.core.exceptions import ValidationError
@ -148,6 +149,24 @@ class Course(BaseModel, DeactivatedMixin):
def count_videos_in_lessons(self):
return Video.objects.filter(lesson__in=self.lessons.all()).count()
@classmethod
def shuffle(cls, qs, order_is_featured = True):
if order_is_featured:
featured = []
other = []
for c in list(qs):
if c.is_featured:
featured.append(c)
else:
other.append(c)
shuffle(featured)
shuffle(other)
return featured + other
else:
all = list(qs)
shuffle(all)
return all
class Category(models.Model):
title = models.CharField('Название категории', max_length=100)

@ -1,4 +1,3 @@
from random import shuffle
from paymentwall import Pingback
from django.contrib.auth import get_user_model
@ -276,19 +275,7 @@ class CoursesView(ListView):
context = super().get_context_data()
filtered = CourseFilter(self.request.GET)
context.update(filtered.data)
# Иммитация order_by('?'), чтобы можно было использовать пагинацию
# если что, сделать свой QuerySet для Course с методом, который возвращает такой список
course_items = context.get('course_items')
featured = []
other = []
for ci in course_items:
if ci.is_featured:
featured.append(ci)
else:
other.append(ci)
shuffle(featured)
shuffle(other)
context['course_items'] = featured + other
context['course_items'] = Course.shuffle(context.get('course_items'))
return context
def get_template_names(self):

@ -74,7 +74,7 @@ class IndexView(TemplateView):
'online': online,
'online_coming_soon': online_coming_soon,
'school_schedule': school_schedule,
'course_items': Course.objects.filter(status=Course.PUBLISHED)[:6],
'course_items': Course.shuffle(Course.objects.filter(status=Course.PUBLISHED)[:6]),
'is_purchased': school_payment_exists,
'min_school_price': SchoolSchedule.objects.aggregate(Min('month_price'))['month_price__min'],
'school_schedules': SchoolSchedule.objects.all(),

Loading…
Cancel
Save