diff --git a/apps/course/models.py b/apps/course/models.py index b716d53b..bc7717b6 100644 --- a/apps/course/models.py +++ b/apps/course/models.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) diff --git a/apps/course/views.py b/apps/course/views.py index 1d961625..5fd8b577 100644 --- a/apps/course/views.py +++ b/apps/course/views.py @@ -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): diff --git a/project/views.py b/project/views.py index c8400f14..57435b56 100644 --- a/project/views.py +++ b/project/views.py @@ -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(),