def topic_detail(request, topic_id): topic = get_object_or_404(FoodTopic, pk=topic_id) foods = make_paginator(request, topic.foods.all()) return render(request, 'topic/detail.tpl', { 'topic': topic, 'foods': foods })
def topic_list(request): topics = make_paginator(request, FoodTopic.objects.all()) if request.is_ajax(): return render(request, 'topic/list_ajax.tpl', {'topics': topics}) return render(request, 'topic/list.tpl', { 'topics': topics })
def food_category(request, category): foods = make_paginator(request, Food.objects.filter(category__name=category)) return render(request, 'food/list.tpl', { 'foods': foods, 'categorys': categorys, 'section': category })
def user_ate(request, user_id): user = get_object_or_404(User.objects.select_related('profile'), pk=user_id) profile = user.profile foods = make_paginator(request, user.foods_ate.all()) return render(request, 'user/food_list.tpl', { 'foods': foods, 'user': user, 'profile': profile, })
def user_following(request, user_id): user = get_object_or_404(User.objects.select_related('profile'), pk=user_id) profile = user.profile followings = make_paginator(request, user.following.all()) return render(request, 'user/user_list.tpl', { 'users': followings, 'user': user, 'profile': profile, })
def topics_collection(request, user_id): user = get_object_or_404(User.objects.select_related('profile'), pk=user_id) profile = user.profile collections = make_paginator(request, user.topics_collected.all()) return render(request, 'user/topic_collection.tpl', { 'collections': collections, 'user': user, 'profile': profile, })
def user_shared(request, user_id): user = get_object_or_404(User.objects.select_related('profile'), pk=user_id) profile = user.profile foods_shared = make_paginator(request, user.foods_shared.all()) return render(request, 'user/share.tpl', { 'foods': foods_shared, 'user': user, 'profile': profile, })
def user_followers(request, user_id): user = get_object_or_404(User.objects.select_related('profile'), pk=user_id) profile = user.profile followers = make_paginator(request, user.followers.all()) return render(request, 'user/user_list.tpl', { 'users': followers, 'user': user, 'profile': profile, })
def query_post(request): q = request.POST.get('q', '').strip() form = ForumPostForm() if q: posts = make_paginator(request, ForumPost.objects.filter(title__icontains=q).all(), per_page=20) else: posts = [] return render(request, 'forum/index.tpl', { 'form': form, 'posts': posts })
def post_index(request): if request.method == 'POST': if request.user.is_authenticated(): form = ForumPostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.creator = request.user post.save() create_action(request.user, POST, post) return HttpResponseRedirect(reverse('forum:index')) else: return HttpResponseRedirect(reverse('user:login')) else: order = request.GET.get('order', 'default') if not order in ('time', 'all'): order = 'default' cache_key = REDIS_POSTS_KEY.format(order) query_set = ForumPost.objects.defer('content', 'created', 'updated') if order == 'time': posts = make_paginator(request, query_set.order_by('-created'), per_page=20, cache_key=cache_key, cache_time=POSTS_CACHE_TIME) elif order == 'all': # 待写排名方法,参考了HackerNews的排名算法 posts = make_paginator(request, query_set.extra( select = { 'score': '(total_likes + 1) / POW(datediff(NOW(), created) + 1, 1.3)' }, order_by = ['-score'] ), per_page=20, cache_key=cache_key, cache_time=POSTS_CACHE_TIME) else: posts = make_paginator(request, query_set.order_by('-id'), per_page=20, cache_key=cache_key, cache_time=POSTS_CACHE_TIME) form = ForumPostForm() return render(request, 'forum/index.tpl', { 'form': form, 'posts': posts }) return render(request, 'forum/index.tpl', { 'form': form })
def food_detail(request, food_id): user = request.user user_authenticated = user.is_authenticated() food = get_object_or_404(Food.objects.prefetch_related( 'comments').select_related('user__profile'), pk=food_id) comments = make_paginator(request, food.comments.order_by('-created').all()) if request.method == 'POST': comment_form = FoodCommentForm(request.POST) if user_authenticated: if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.user = user comment.food = food comment.save() messages.success(request, COMMENT_SUCCESS) return HttpResponseRedirect( reverse('food:detail', kwargs={'food_id': food_id})) else: messages.error(request, COMMENT_FAIL) else: messages.error(request, COMMENT_AFTER_LOGIN) else: comment_form = FoodCommentForm() food_tags_ids = food.tags.values_list('id') similar_foods = Food.objects.filter(tags__in=food_tags_ids).exclude( id=food.id) similar_foods = similar_foods.annotate( same_tags=Count('tags')).order_by('-same_tags')[:4] total_views = rds.incr(REDIS_FOOD_VIEWS_KEY.format(food.id)) rds.zincrby(REDIS_FOOD_RANKING_KEY, food.id, 1) context = { 'food': food, 'comments': comments, 'comment_form': comment_form, 'similar_foods': similar_foods, 'total_views': total_views, 'score': 0 } if user_authenticated: rating = Vote.objects.filter( user=user, object_id=food.id, content_type=ContentType.objects.get_for_model( model=Food)).only('score').first() context['score'] = rating.score if rating else 0 context['is_wta'] = user.foods_wta.filter(pk=food.id).exists() context['is_ate'] = user.foods_ate.filter(pk=food.id).exists() return render(request, 'food/detail.tpl', context)
def post_detail(request, post_id): post = get_object_or_404(ForumPost.objects.prefetch_related('comments__user__profile'), pk=post_id) comments = make_paginator(request, post.comments.all()) if request.method == 'POST': if request.user.is_authenticated(): form = ForumPostCommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.user = request.user comment.post = post comment.save() return HttpResponseRedirect(reverse('forum:detail', kwargs={'post_id': post.id})) else: return HttpResponseRedirect(reverse('user:login')) else: form = ForumPostCommentForm() return render(request, 'forum/detail.tpl', { 'post': post, 'comments': comments, 'form': form })
def food_tag(request, tag): foods = make_paginator(request, Food.objects.filter(tags__name=tag)) return render(request, 'food/list.tpl', { 'foods': foods, 'categorys': categorys })
def explore(request): foods = make_paginator(request, Food.objects.all()[:50]) return render(request, 'food/explore.tpl', {'foods': foods})