Here is another hack, but I'm sure I can win any prize like Sam did, isn't 
it? :)

Use `publish_date` field as default sort, if you want want some kind of 
order, you can control. My specific case was to shuffle all the product 
position once in a while.

Here is my settings.py config for default sort

SHOP_PRODUCT_SORT_OPTIONS = (
>     (u'Recently published', u'-publish_date'),
>     (u'Recently added', u'-date_added'),
>     (u'Highest rated', u'-rating_average'),
>     (u'Least expensive', u'unit_price'),
>     (u'Most expensive', u'-unit_price')
> )


And here is my product shuffle/randomization view procedure.

@user_passes_test(lambda u: u.is_superuser)
> def clp_randomize_publish_date(request, category_id, delta_day):
>     """"
>     Shuffle/randomize the position of each product in the given category.
>     Default sort order for products is publish_date, therefore picking 
> random
>     publish dates for products will change their initial position in 
> category page.
>     Parameters:
>     - `category_id`: use 0, to perform action on every cartridge product 
> in the system,
>       otherwise specify exact category id.
>     - `delta_day`: time delta in days, to establish the date range (now - 
> delta_day) to now
>     """
>     now = 
> datetime.datetime.utcnow().replace(tzinfo=timezone.get_default_timezone())
>     end = now - datetime.timedelta(days=int(delta_day))
>     if int(category_id) > 0:
>         category = get_object_or_404(Category, id=int(category_id))
>         products = category.product_set.all()
>         category_title = category.title
>     else:
>         products = Product.objects.all()
>         category_title = "all"
>     context = {
>         "delta_day": delta_day,
>         "now": now,
>         "end": end,
>         "product_count": len(products),
>         "category_title": category_title,
>     }
>     if request.POST:
>         for p in products:
>             seed()
>             random_date = now - datetime.timedelta(days=randint(1, 
> int(delta_day)))
>             p.publish_date = random_date
>             p.save()
>         context["done"] = True
>     else:
>         context["done"] = False
>     return render(request, 
> 'shop/maintenance/clp_randomize_publish_date.html', context)


Hope this might help someone. 

-- 
You received this message because you are subscribed to the Google Groups 
"Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to mezzanine-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to