def item_add_route(category_id): """ Route to add new item """ target_category = get_category(category_id) if target_category is None: abort(404) # adding some protection csrf = generate_csrf_token() if request.method == 'POST': if csrf != request.form['csrf_token']: abort(403) else: add_item(category_id) flash('Item added') return redirect( url_for('category.category_route', category_id=category_id)) if request.method == 'GET': return render_template('item_edit.html', page={'title': 'Add category'}, user=user_info(), content={ 'is_edit': False, 'csrf_token': csrf, 'category': target_category })
def topic_create(request, slug): ''' 发表新帖子 ''' node = get_object_or_404(Node, slug=slug) form = CreateForm() if request.method == "POST": form = CreateForm(request.POST) if form.is_valid(): user = request.user topic = Topic(title=form.cleaned_data['title'], content=form.cleaned_data['content'], created=timezone.now(), node=node, author=user, reply_count=0, last_touched=timezone.now()) topic.save() return redirect(reverse('forum:index')) user = request.user # 发帖查重 if request.user.is_authenticated(): counter, notifications_count = user_info(request.user) active_page = 'topic' node_slug = node.slug update_reputation(user.id, -5) return render(request, 'topic/create.html', locals())
def favorite(request, uid): ''' 获得用户收藏 ''' if uid.isdigit(): user = get_object_or_404(User, pk=uid) else: user = get_object_or_404(User, username=uid) try: current_page = int(request.GET.get('p', 1)) except ValueError: current_page = 1 counter = user_info(user)[0] if request.user.is_authenticated(): _, notifications_count = user_info(request.user) favorites, page = Favorite.objects.get_fav_by_user(user.id, current_page) return render(request, 'topic/user_favorites.html', locals())
def categories_route(): """ List of all categories """ return render_template('categories.html', page={'title': 'Categories'}, user=user_info(), content={'categories': get_categories()})
def user_replies(request, uid): ''' 根据用户获得回复 ''' if uid.isdigit(): user = get_object_or_404(User, pk=uid) else: user = get_object_or_404(User, username=uid) try: current_page = int(request.GET.get('p', 1)) except ValueError: current_page = 1 counter = user_info(user)[0] if request.user.is_authenticated(): _, notifications_count = user_info(request.user) replies, reply_page = Reply.objects.get_replies_by_user(user.id, current_page=current_page) active_page = 'topic' return render(request, 'topic/user_replies.html', locals())
def members(request): ''' 获得所有的成员信息 ''' if request.user.is_authenticated(): counter, notifications_count = user_info(request.user) members = User.objects.all().order_by('-id')[:49] active_members = User.objects.all().order_by('-last_login')[:49] active_page = 'members' return render(request, 'topic/members.html', locals())
def profile(request, uid): ''' 获得用户描述信息 ''' if uid.isdigit(): user = get_object_or_404(User, pk=uid) else: user = get_object_or_404(User, username=uid) try: current_page = int(request.GET.get('p', 1)) except ValueError: current_page = 1 counter = user_info(user)[0] if request.user.is_authenticated(): _, notifications_count = user_info(request.user) topics, topic_page = Topic.objects.get_topics_by_user(user.id, current_page=current_page) replies, reply_page = Reply.objects.get_replies_by_user(user.id, current_page=current_page) active_page = '_blank' return render(request, 'topic/profile.html', locals())
def index_route(): """ Home sweet home, this is page where our journey begins """ return render_template('index.html', page={ 'title': 'Homepage', 'has_sidebar': True }, user=user_info(), content={'categories': get_categories()})
def category_delete_route(category_id): """ Deleting category from DB """ target_category = get_category(category_id) # checking access rights if target_category.owner != user_info()['id']: flash('Only owner can delete category') return redirect(url_for('category.categories_route')) if target_category is None: abort(404) # adding some protection csrf = generate_csrf_token() if request.method == 'POST': if csrf != request.form['csrf_token']: abort(403) else: delete_category(category_id) flash('Category deleted') # sending user to list of categories after all he has done return redirect(url_for('category.categories_route')) # as polite people we will ask some configmation first, # also we need it for CSRF check if request.method == 'GET': return render_template('confirm.html', page={'title': 'Delete category'}, user=user_info(), content={ 'csrf_token': csrf, 'message': 'Do you really want delete category ' + target_category.name + '?' })
def item_delete_route(item_id): """ Route to delete item """ target_item = get_item(item_id) # checking access rights if target_item.owner != user_info()['id']: flash('Only owner can delete item') return redirect(url_for('item.item_route', item_id=item_id)) if target_item is None: abort(404) # some protection csrf = generate_csrf_token() if request.method == 'POST': if csrf != request.form['csrf_token']: abort(403) else: delete_item(item_id) flash('Item deleted') # sending user to categories page for he has done return redirect(url_for('category.categories_route')) if request.method == 'GET': return render_template('confirm.html', page={'title': 'Delete item'}, user=user_info(), content={ 'csrf_token': csrf, 'message': 'Do you really want delete item ' + target_item.name + '?' })
def item_edit_route(item_id): """ Route to edit item """ target_item = get_item(item_id) # checking access rights if target_item.owner != user_info()['id']: flash('Only owner can edit item') return redirect(url_for('item.item_route', item_id=item_id)) if target_item is None: abort(404) # some protection csrf = generate_csrf_token() if request.method == 'POST': if csrf != request.form['csrf_token']: abort(403) else: update_item(item_id) flash('Item updated') # sending user to item page after edit is done return redirect(url_for('item.item_route', item_id=item_id)) if request.method == 'GET': return render_template('item_edit.html', page={'title': 'Edit item'}, user=user_info(), content={ 'is_edit': True, 'csrf_token': csrf, 'item': target_item })
def index(request): ''' 显示首页 ''' user = request.user if request.user.is_authenticated(): counter, notifications_count = user_info(request.user) status_counter = { 'users': User.objects.all().count(), 'nodes': Node.objects.all().count(), 'topics': Topic.objects.all().count(), 'replies': Reply.objects.all().count(), } try: current_page = int(request.GET.get('p', '1')) except ValueError: current_page = 1 if request.user.is_authenticated(): counter, notifications_count = user_info(request.user) hot_nodes = Node.objects.all() topics, topic_page = Topic.objects.get_all_topic(current_page=current_page) planes = Plane.objects.all().prefetch_related('node_set') active_page = 'topic' return render(request, 'topic/topics.html', locals())
def category_edit_route(category_id): """ Updating category info """ target_category = get_category(category_id) # checking access rights if target_category.owner != user_info()['id']: flash('Only owner can edit category') return redirect(url_for('category.categories_route')) if target_category is None: abort(404) csrf = generate_csrf_token() if request.method == 'POST': if csrf != request.form['csrf_token']: abort(403) else: update_category(category_id) flash('Category updated') return redirect(url_for('category.categories_route')) if request.method == 'GET': return render_template( 'category_edit.html', page={'title': 'Add category'}, user=user_info(), content={ 'is_edit': True, # changing template appearance from add to edit 'csrf_token': csrf, 'category': target_category })
def node_topics(request, slug): ''' 根据节点获得帖子 ''' node = get_object_or_404(Node, slug=slug) user = request.user if request.user.is_authenticated(): counter, notifications_count = user_info(request.user) try: current_page = int(request.GET.get('p', 1)) except ValueError: current_page = 1 topics, topic_page = Topic.objects.get_topics_by_node(slug=slug, current_page=current_page) active_page = 'topic' return render(request, 'topic/node_topics.html', locals())
def profile_route(): """ Originaly I planned to make it big and coolm with API key to update and delete stuff, with nice API reference and so on. Then I understood that this is overkill, so this page is very simple and just shows user's picture. """ user = user_info() if not user_is_authorized(): return redirect(url_for('auth.login_route')) return render_template('profile.html', page={'title': user['name'] + ' profile'}, user=user, content={'categories': get_categories()})
def topic_edit(request, t_id): topic = get_object_or_404(Topic, pk=t_id) user = request.user if request.method == "POST": if user.id == topic.author_id: form = CreateForm(request.POST) if form.is_valid(): user = request.user topic.title = form.cleaned_data['title'] topic.content = form.cleaned_data['content'] topic.save() return redirect(reverse('forum:index')) else: errors = {'invalid_permission': [u'没有权限修改该回复']} counter, notifications_count = user_info(request.user) update_reputation(user.id, -2) return render(request, 'topic/edit.html', locals())
def item_route(item_id): """ Route that outputs item info """ target_item = get_item(item_id) if target_item is None: abort(404) return render_template('item.html', page={ 'title': 'Item ' + target_item.name, 'has_sidebar': True }, user=user_info(), content={ 'categories': get_categories(), 'item': target_item })
def reply_edit(request, id): ''' 用户回复修改 ''' reply = get_object_or_404(Reply, pk=id) user = request.user if request.method == "POST": if user.id == reply.author_id: form = ReplyForm(request.POST) if form.is_valid(): if user.id == reply.author_id: Reply.objects.filter(pk=reply.id).update(updated=timezone.now(), content=form.cleaned_data['content']) return redirect(reverse('forum:reply', args=[reply.topic.id])) else: errors = {'invalid_permission': [u'没有权限修改该回复']} counter, notifications_count = user_info(request.user) update_reputation(user.id, -2) active_page = 'topic' return render(request, 'topic/reply_edit.html', locals())
def category_route(category_id): """ Outputing category info """ target_category = get_category(category_id) # ooops category not found if target_category is None: abort(404) return render_template('category.html', page={ 'title': 'Category ' + target_category.name, 'has_sidebar': True }, user=user_info(), content={ 'categories': get_categories(), 'category': target_category })
def category_add_route(): """ Add new category to data base """ # adding some protection csrf = generate_csrf_token() if request.method == 'POST': if csrf != request.form['csrf_token']: abort(403) else: add_category() flash('Category added') return redirect(url_for('category.categories_route')) if request.method == 'GET': return render_template('category_edit.html', page={'title': 'Add category'}, user=user_info(), content={ 'is_edit': False, 'csrf_token': csrf })
def reply_create(request, t_id): ''' 创建回复 ''' if request.method == "POST": if not request.user.is_authenticated(): return redirect(reverse('forum:login')) try: topic = Topic.objects.select_related('author').get(pk=t_id) except Topic.DoesNotExist: return Http404 form = ReplyForm(request.POST) if form.is_valid(): user = request.user now = timezone.now() notifications = [] if user.id != topic.author_id: notification = Notification(content=form.cleaned_data['content'], status=0, involved_type=1, involved_user=topic.author, involved_topic=topic, trigger_user=user, occurrence_time=now) notifications.append(notification) metions = find_mentions(form.cleaned_data['content']) if user.username in metions: metions.remove(user.username) if topic.author.username in metions: metions.remove(topic.author.username) if metions: metion_names = User.objects.filter(username__in=metions) for username in metion_names: notification = Notification(content=form.cleaned_data['content'], involved_topic=topic, involved_user=user, involved_type=0, trigger_user=user, occurrence_time=now) notifications.append(notification) if notifications: # 批量插入 Notification.objects.bulk_create(notifications) reply = Reply(content=form.cleaned_data['content'], created=timezone.now(), author=user, topic=topic, last_touched=now) reply.save() Topic.objects.filter(pk=topic.id).update(last_replied_by=user,last_replied_time=now, last_touched=now,reply_count=topic.reply_count+1) topic.reply_count=topic.reply_count+1 return redirect(reverse('forum:reply_create', args=[topic.id]) + '#reply' + str(topic.reply_count)) user = request.user topic = Topic.objects.get(pk=t_id) if request.user.is_authenticated(): counter, notifications_count = user_info(request.user) topic_favorited = Favorite.objects.filter(involved_topic=topic, owner_user=user).exists() reply_last_page = (topic.reply_count // 20 + (topic.reply_count % 20 and 1)) or 1 try: current_page = int(request.GET.get('p', reply_last_page)) except ValueError: current_page = reply_last_page replies, reply_page = Reply.objects.get_all_replies(t_id, current_page, 20) return render(request, 'topic/view.html', locals())