def page(request): """ Ajax view in order to enable infinite scroll on images view. """ if 'category' in request.GET and request.GET['category']: category = get_object_or_404(Category, pk=int(request.GET['category'])) images = category.image_set.all() # TODO fold this ordering into the above query images = sorted(images, key=lambda im: VoteCount.get_vote_count(image_id=im.pk, cat_id=category.pk), reverse=True) p = Paginator(images, IMAGES_PER_PAGE) else: p = Paginator(Image.objects.order_by("-created"), IMAGES_PER_PAGE) page_num = request.GET['page'] page = p.page(page_num) images = [] for im in page.object_list: categories = [] for cat in im.categories.all(): has_voted_up = has_voted_down = "" if request.user.is_authenticated(): has_voted_up = Vote.check_user_vote_type(user=request.user, image=im.pk, category=cat.pk, score=1) has_voted_down = Vote.check_user_vote_type(user=request.user, image=im.pk, category=cat.pk, score=-1) categories.append({ "pk": cat.pk, "title": cat.title, "has_voted_up": has_voted_up, "has_voted_down": has_voted_down, "votes": VoteCount.get_vote_count(image_id=im.pk, cat_id=cat.pk) }) images.append({ "pk": im.pk, "caption": im.caption, "url": im.url, "source": im.source, "categories": categories }) return json_response({ "status": "ok", "images": images, "has_more_pages": page.has_next() })
def list(request, category): category = get_object_or_404(Category, pk=int(category)) images = category.image_set.all() # TODO fold this ordering into the above query images = sorted(images, key=lambda im: VoteCount.get_vote_count(image_id=im.pk, cat_id=category.pk), reverse=True) p = Paginator(images, IMAGES_PER_PAGE) # index will always show first page of images page = p.page(1) categories = Category.objects.all() return render_to_response("index.html", { 'page': page, 'categories': categories, 'category': category }, context_instance = RequestContext(request) )
def list(request, category): category = get_object_or_404(Category, pk=int(category)) images = category.image_set.all() # TODO fold this ordering into the above query images = sorted(images, key=lambda im: VoteCount.get_vote_count( image_id=im.pk, cat_id=category.pk), reverse=True) p = Paginator(images, IMAGES_PER_PAGE) # index will always show first page of images page = p.page(1) categories = Category.objects.all() return render_to_response("index.html", { 'page': page, 'categories': categories, 'category': category }, context_instance=RequestContext(request))
def page(request): """ Ajax view in order to enable infinite scroll on images view. """ if 'category' in request.GET and request.GET['category']: category = get_object_or_404(Category, pk=int(request.GET['category'])) images = category.image_set.all() # TODO fold this ordering into the above query images = sorted(images, key=lambda im: VoteCount.get_vote_count( image_id=im.pk, cat_id=category.pk), reverse=True) p = Paginator(images, IMAGES_PER_PAGE) else: p = Paginator(Image.objects.order_by("-created"), IMAGES_PER_PAGE) page_num = request.GET['page'] page = p.page(page_num) images = [] for im in page.object_list: categories = [] for cat in im.categories.all(): has_voted_up = has_voted_down = "" if request.user.is_authenticated(): has_voted_up = Vote.check_user_vote_type(user=request.user, image=im.pk, category=cat.pk, score=1) has_voted_down = Vote.check_user_vote_type(user=request.user, image=im.pk, category=cat.pk, score=-1) categories.append({ "pk": cat.pk, "title": cat.title, "has_voted_up": has_voted_up, "has_voted_down": has_voted_down, "votes": VoteCount.get_vote_count(image_id=im.pk, cat_id=cat.pk) }) images.append({ "pk": im.pk, "caption": im.caption, "url": im.url, "source": im.source, "categories": categories }) return json_response({ "status": "ok", "images": images, "has_more_pages": page.has_next() })
def vote_count(image, category): return VoteCount.get_vote_count(image_id=image, cat_id=category)