def find_post(request, user_pk): '''Finds all posts of a user''' u = get_object_or_404(User, pk=user_pk) posts = Post.objects\ .filter(author=u)\ .order_by('-pubdate')\ .all() # Paginator paginator = Paginator(posts, settings.POSTS_PER_PAGE) page = request.GET.get('page') try: shown_posts = paginator.page(page) page = int(page) except PageNotAnInteger: shown_posts = paginator.page(1) page = 1 except EmptyPage: shown_posts = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template( 'forum/find_post.html', { 'posts': shown_posts, 'usr': u, 'pages': paginator_range(page, paginator.num_pages), 'nb': page })
def find_topic(request, user_pk): """Finds all topics of a user.""" displayed_user = get_object_or_404(User, pk=user_pk) topics = \ Topic.objects\ .filter(author=displayed_user)\ .exclude(Q(forum__group__isnull=False) & ~Q(forum__group__in=request.user.groups.all()))\ .prefetch_related("author")\ .order_by("-pubdate").all() # Paginator paginator = Paginator(topics, settings.TOPICS_PER_PAGE) page = request.GET.get("page") try: shown_topics = paginator.page(page) page = int(page) except PageNotAnInteger: shown_topics = paginator.page(1) page = 1 except EmptyPage: shown_topics = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template("forum/find/topic.html", { "topics": shown_topics, "usr": displayed_user, "pages": paginator_range(page, paginator.num_pages), "nb": page, })
def find_topic(request, user_pk): """Finds all topics of a user.""" u = get_object_or_404(User, pk=user_pk) topics = \ Topic.objects\ .filter(author=u)\ .exclude(Q(forum__group__isnull=False) & ~Q(forum__group__in=u.groups.all()))\ .prefetch_related("author")\ .order_by("-pubdate").all() # Paginator paginator = Paginator(topics, settings.TOPICS_PER_PAGE) page = request.GET.get("page") try: shown_topics = paginator.page(page) page = int(page) except PageNotAnInteger: shown_topics = paginator.page(1) page = 1 except EmptyPage: shown_topics = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template("forum/find/topic.html", { "topics": shown_topics, "usr": u, "pages": paginator_range(page, paginator.num_pages), "nb": page, })
def followed_topics(request): """ Displays the followed topics for the current user, with `settings.ZDS_APP['forum']['followed_topics_per_page']` topics per page. """ topics_followed = TopicAnswerSubscription.objects.get_objects_followed_by( request.user) # Paginator paginator = Paginator( topics_followed, settings.ZDS_APP['forum']['followed_topics_per_page']) page = request.GET.get("page") try: shown_topics = paginator.page(page) page = int(page) except PageNotAnInteger: shown_topics = paginator.page(1) page = 1 except EmptyPage: shown_topics = paginator.page(paginator.num_pages) page = paginator.num_pages topic_read = TopicRead.objects.list_read_topic_pk(request.user, shown_topics) return render( request, "forum/topic/followed.html", { "followed_topics": shown_topics, "topic_read": topic_read, "pages": paginator_range(page, paginator.num_pages), "nb": page })
def topic(request, topic_pk, topic_slug): """Display a thread and its posts using a pager.""" # TODO: Clean that up g_topic = get_object_or_404(PrivateTopic, pk=topic_pk) if not g_topic.author == request.user \ and request.user not in list(g_topic.participants.all()): raise PermissionDenied # Check link if not topic_slug == slugify(g_topic.title): return redirect(g_topic.get_absolute_url()) if request.user.is_authenticated(): if never_privateread(g_topic): mark_read(g_topic) posts = PrivatePost.objects.filter(privatetopic__pk=g_topic.pk)\ .order_by('position_in_topic')\ .all() last_post_pk = g_topic.last_message.pk # Handle pagination paginator = Paginator(posts, settings.POSTS_PER_PAGE) try: page_nbr = int(request.GET['page']) except KeyError: page_nbr = 1 try: posts = paginator.page(page_nbr) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: raise Http404 res = [] if page_nbr != 1: # Show the last post of the previous page last_page = paginator.page(page_nbr - 1).object_list last_post = (last_page)[len(last_page) - 1] res.append(last_post) for post in posts: res.append(post) # Build form to add an answer for the current topid. form = PrivatePostForm(g_topic, request.user) return render_template('mp/topic/index.html', { 'topic': g_topic, 'posts': res, 'pages': paginator_range(page_nbr, paginator.num_pages), 'nb': page_nbr, 'last_post_pk': last_post_pk, 'form': form })
def form_valid(self, form): self.queryset = form.search() context = self.get_context_data(**{ self.form_name: form, 'query': form.cleaned_data.get(self.search_field), 'object_list': self.queryset, 'models': self.request.GET.getlist('models', ''), }) # Retrieve page number if "page" in self.request.GET and self.request.GET["page"].isdigit(): page_number = int(self.request.GET["page"]) elif "page" not in self.request.GET: page_number = 1 else: raise Http404 # Create pagination paginator = self.get_paginator(self.queryset, self.paginate_by, 0, False) if paginator.num_pages == 0: num_pages = 1 else: num_pages = paginator.num_pages context['nb'] = page_number context['pages'] = paginator_range(page_number, num_pages) return self.render_to_response(context)
def find_topic_by_tag(request, tag_pk, tag_slug): """Finds all topics byg tag.""" tag = Tag.objects.filter(pk=tag_pk, slug=tag_slug).first() if tag is None: return redirect(reverse("zds.forum.views.index")) u = request.user if "filter" in request.GET: filter = request.GET["filter"] if request.GET["filter"] == "solve": topics = Topic.objects.filter( tags__in=[tag], is_sticky=False, is_solved=True).order_by("-last_message__pubdate").prefetch_related( "author", "last_message", "tags")\ .exclude(Q(forum__group__isnull=False) & ~Q(forum__group__in=u.groups.all()))\ .all() else: topics = Topic.objects.filter( tags__in=[tag], is_sticky=False, is_solved=False).order_by("-last_message__pubdate").prefetch_related( "author", "last_message", "tags")\ .exclude(Q(forum__group__isnull=False) & ~Q(forum__group__in=u.groups.all()))\ .all() else: filter = None topics = Topic.objects.filter(tags__in=[tag], is_sticky=False) .order_by( "-last_message__pubdate")\ .exclude(Q(forum__group__isnull=False) & ~Q(forum__group__in=u.groups.all()))\ .prefetch_related("author", "last_message", "tags").all() # Paginator paginator = Paginator(topics, settings.TOPICS_PER_PAGE) page = request.GET.get("page") try: shown_topics = paginator.page(page) page = int(page) except PageNotAnInteger: shown_topics = paginator.page(1) page = 1 except EmptyPage: shown_topics = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template("forum/find/topic_by_tag.html", { "topics": shown_topics, "tag": tag, "pages": paginator_range(page, paginator.num_pages), "nb": page, "filter": filter, })
def find_topic_by_tag(request, tag_pk, tag_slug): """Finds all topics byg tag.""" tag = Tag.objects.filter(pk=tag_pk, slug=tag_slug).first() if tag is None: return redirect(reverse("zds.forum.views.index")) u = request.user if "filter" in request.GET: filter = request.GET["filter"] if request.GET["filter"] == "solve": topics = Topic.objects.filter( tags__in=[tag], is_solved=True).order_by("-last_message__pubdate").prefetch_related( "author", "last_message", "tags")\ .exclude(Q(forum__group__isnull=False) & ~Q(forum__group__in=u.groups.all()))\ .all() else: topics = Topic.objects.filter( tags__in=[tag], is_solved=False).order_by("-last_message__pubdate")\ .prefetch_related( "author", "last_message", "tags")\ .exclude(Q(forum__group__isnull=False) & ~Q(forum__group__in=u.groups.all()))\ .all() else: filter = None topics = Topic.objects.filter(tags__in=[tag]).order_by("-last_message__pubdate")\ .exclude(Q(forum__group__isnull=False) & ~Q(forum__group__in=u.groups.all()))\ .prefetch_related("author", "last_message", "tags").all() # Paginator paginator = Paginator(topics, settings.ZDS_APP['forum']['topics_per_page']) page = request.GET.get("page") try: shown_topics = paginator.page(page) page = int(page) except PageNotAnInteger: shown_topics = paginator.page(1) page = 1 except EmptyPage: shown_topics = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template( "forum/find/topic_by_tag.html", { "topics": shown_topics, "tag": tag, "pages": paginator_range(page, paginator.num_pages), "nb": page, "filter": filter, })
def details(request, cat_slug, forum_slug): """Display the given forum and all its topics.""" forum = get_object_or_404(Forum, slug=forum_slug) if not forum.can_read(request.user): raise PermissionDenied sticky_topics = Topic.objects.filter(forum__pk=forum.pk, is_sticky=True).order_by( "-last_message__pubdate").prefetch_related("author", "last_message", "tags").all() if "filter" in request.GET: filter = request.GET["filter"] if request.GET["filter"] == "solve": topics = Topic.objects.filter( forum__pk=forum.pk, is_sticky=False, is_solved=True).order_by("-last_message__pubdate").prefetch_related( "author", "last_message", "tags").all() else: topics = Topic.objects.filter( forum__pk=forum.pk, is_sticky=False, is_solved=False).order_by("-last_message__pubdate").prefetch_related( "author", "last_message", "tags").all() else: filter = None topics = Topic.objects.filter(forum__pk=forum.pk, is_sticky=False) .order_by( "-last_message__pubdate").prefetch_related("author", "last_message", "tags").all() # Paginator paginator = Paginator(topics, settings.TOPICS_PER_PAGE) page = request.GET.get("page") try: shown_topics = paginator.page(page) page = int(page) except PageNotAnInteger: shown_topics = paginator.page(1) page = 1 except EmptyPage: shown_topics = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template("forum/category/forum.html", { "forum": forum, "sticky_topics": sticky_topics, "topics": shown_topics, "pages": paginator_range(page, paginator.num_pages), "nb": page, "filter": filter, })
def index(request): """Display the all private topics.""" # delete actions if request.method == 'POST': if 'delete' in request.POST: liste = request.POST.getlist('items') topics = PrivateTopic.objects.filter(pk__in=liste)\ .filter( Q(participants__in=[request.user]) | Q(author=request.user)) for topic in topics: if topic.participants.all().count() == 0: topic.delete() elif request.user == topic.author: topic.author = topic.participants.all()[0] topic.participants.remove(topic.participants.all()[0]) topic.save() else: topic.participants.remove(request.user) topic.save() privatetopics = PrivateTopic.objects\ .filter(Q(participants__in=[request.user]) | Q(author=request.user))\ .select_related("author", "participants")\ .distinct().order_by('-last_message__pubdate').all() # Paginator paginator = Paginator(privatetopics, settings.ZDS_APP['forum']['topics_per_page']) page = request.GET.get('page') try: shown_privatetopics = paginator.page(page) page = int(page) except PageNotAnInteger: shown_privatetopics = paginator.page(1) page = 1 except EmptyPage: shown_privatetopics = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template( 'mp/index.html', { 'privatetopics': shown_privatetopics, 'pages': paginator_range(page, paginator.num_pages), 'nb': page })
def index(request): """Displays the list of registered users.""" if request.is_ajax(): q = request.GET.get('q', '') if request.user.is_authenticated(): members = User.objects.filter(username__icontains=q).exclude( pk=request.user.pk)[:20] else: members = User.objects.filter(username__icontains=q)[:20] results = [] for member in members: member_json = {} member_json['id'] = member.pk member_json['label'] = member.username member_json['value'] = member.username results.append(member_json) data = json.dumps(results) mimetype = "application/json" return HttpResponse(data, mimetype) else: members = User.objects.order_by("-date_joined") # Paginator paginator = Paginator(members, settings.ZDS_APP['member']['members_per_page']) page = request.GET.get("page") try: shown_members = paginator.page(page) page = int(page) except PageNotAnInteger: shown_members = paginator.page(1) page = 1 except EmptyPage: shown_members = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template( "member/index.html", { "members": shown_members, "count": members.count(), "pages": paginator_range(page, paginator.num_pages), "nb": page, })
def index(request): """Display the all private topics.""" # delete actions if request.method == "POST": if "delete" in request.POST: liste = request.POST.getlist("items") topics = PrivateTopic.objects.filter(pk__in=liste).all() for topic in topics: if topic.participants.all().count() == 0: topic.delete() elif request.user == topic.author: topic.author = topic.participants.all()[0] topic.participants.remove(topic.participants.all()[0]) topic.save() else: topic.participants.remove(request.user) topic.save() privatetopics = ( PrivateTopic.objects.filter(Q(participants__in=[request.user]) | Q(author=request.user)) .select_related("author", "participants") .distinct() .order_by("-last_message__pubdate") .all() ) # Paginator paginator = Paginator(privatetopics, settings.TOPICS_PER_PAGE) page = request.GET.get("page") try: shown_privatetopics = paginator.page(page) page = int(page) except PageNotAnInteger: shown_privatetopics = paginator.page(1) page = 1 except EmptyPage: shown_privatetopics = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template( "mp/index.html", {"privatetopics": shown_privatetopics, "pages": paginator_range(page, paginator.num_pages), "nb": page}, )
def index(request): """Displays the list of registered users.""" if request.is_ajax(): q = request.GET.get('q', '') if request.user.is_authenticated() : members = User.objects.filter(username__icontains=q).exclude(pk=request.user.pk)[:20] else: members = User.objects.filter(username__icontains=q)[:20] results = [] for member in members: member_json = {} member_json['id'] = member.pk member_json['label'] = member.username member_json['value'] = member.username results.append(member_json) data = json.dumps(results) mimetype = "application/json" return HttpResponse(data, mimetype) else: members = User.objects.order_by("-date_joined") # Paginator paginator = Paginator(members, settings.MEMBERS_PER_PAGE) page = request.GET.get("page") try: shown_members = paginator.page(page) page = int(page) except PageNotAnInteger: shown_members = paginator.page(1) page = 1 except EmptyPage: shown_members = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template("member/index.html", { "members": shown_members, "count": members.count(), "pages": paginator_range(page, paginator.num_pages), "nb": page, })
def details(request, cat_slug, forum_slug): """Display the given forum and all its topics.""" forum = get_object_or_404(Forum, slug=forum_slug) if not forum.can_read(request.user): raise PermissionDenied if "filter" in request.GET: filter = request.GET["filter"] if filter == "solve": sticky_topics = get_topics(forum_pk=forum.pk, is_sticky=True, is_solved=True) topics = get_topics(forum_pk=forum.pk, is_sticky=False, is_solved=True) else: sticky_topics = get_topics(forum_pk=forum.pk, is_sticky=True, is_solved=False) topics = get_topics(forum_pk=forum.pk, is_sticky=False, is_solved=False) else: filter = None sticky_topics = get_topics(forum_pk=forum.pk, is_sticky=True) topics = get_topics(forum_pk=forum.pk, is_sticky=False) # Paginator paginator = Paginator(topics, settings.TOPICS_PER_PAGE) page = request.GET.get("page") try: shown_topics = paginator.page(page) page = int(page) except PageNotAnInteger: shown_topics = paginator.page(1) page = 1 except EmptyPage: shown_topics = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template("forum/category/forum.html", { "forum": forum, "sticky_topics": sticky_topics, "topics": shown_topics, "pages": paginator_range(page, paginator.num_pages), "nb": page, "filter": filter, })
def followed_topics(request): followed_topics = request.user.get_profile().get_followed_topics() # Paginator paginator = Paginator(followed_topics, settings.FOLLOWED_TOPICS_PER_PAGE) page = request.GET.get("page") try: shown_topics = paginator.page(page) page = int(page) except PageNotAnInteger: shown_topics = paginator.page(1) page = 1 except EmptyPage: shown_topics = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template("forum/topic/followed.html", {"followed_topics": shown_topics, "pages": paginator_range(page, paginator.num_pages), "nb": page})
def create_response(self): (paginator, page) = self.build_page() page_nbr = int(self.request.GET.get('page', 1)) context = { 'query': self.query, 'form': self.form, 'page': page, 'pages': paginator_range(page_nbr, paginator.num_pages), 'nb': page_nbr, 'paginator': paginator, 'suggestion': None, 'model_name': MODEL_NAMES } if self.results and hasattr(self.results, 'query') and self.results.query.backend.include_spelling: context['suggestion'] = self.form.get_suggestion() context.update(self.extra_context()) return render_template(self.template, context)
def details(request, cat_slug, forum_slug): ''' Display the given forum and all its topics ''' forum = get_object_or_404(Forum, slug=forum_slug) if not forum.can_read(request.user): raise Http404 sticky_topics = Topic.objects\ .filter(forum__pk=forum.pk, is_sticky=True)\ .order_by('-last_message__pubdate')\ .all() topics = Topic.objects\ .filter(forum__pk=forum.pk, is_sticky=False)\ .order_by('-last_message__pubdate')\ .all() # Paginator paginator = Paginator(topics, settings.TOPICS_PER_PAGE) page = request.GET.get('page') try: shown_topics = paginator.page(page) page = int(page) except PageNotAnInteger: shown_topics = paginator.page(1) page = 1 except EmptyPage: shown_topics = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template( 'forum/details.html', { 'forum': forum, 'sticky_topics': sticky_topics, 'topics': shown_topics, 'pages': paginator_range(page, paginator.num_pages), 'nb': page })
def index(request): ''' Display the all private topics ''' #delete actions if request.method == 'POST': if 'delete' in request.POST: liste = request.POST.getlist('items') topics=PrivateTopic.objects.filter(pk__in=liste).all() for topic in topics: if request.user == topic.author: topic.author=topic.participants.all()[0] topic.participants.remove(topic.participants.all()[0]) else : topic.participants.remove(request.user) topic.save() privatetopics = PrivateTopic.objects\ .filter(Q(participants__in=[request.user])|Q(author=request.user))\ .distinct().order_by('-last_message__pubdate').all() # Paginator paginator = Paginator(privatetopics, settings.TOPICS_PER_PAGE) page = request.GET.get('page') try: shown_privatetopics = paginator.page(page) page=int(page) except PageNotAnInteger: shown_privatetopics = paginator.page(1) page = 1 except EmptyPage: shown_privatetopics = paginator.page(paginator.num_pages) page=paginator.num_pages return render_template('mp/index.html', { 'privatetopics': shown_privatetopics, 'pages': paginator_range(page, paginator.num_pages), 'nb': page })
def find_post(request, user_pk): """Finds all posts of a user.""" u = get_object_or_404(User, pk=user_pk) if request.user.has_perm("forum.change_post"): posts = \ Post.objects.filter(author=u)\ .exclude(Q(topic__forum__group__isnull=False) & ~Q(topic__forum__group__in=u.groups.all()))\ .prefetch_related("author")\ .order_by("-pubdate").all() else: posts = \ Post.objects.filter(author=u)\ .filter(is_visible=True)\ .exclude(Q(topic__forum__group__isnull=False) & ~Q(topic__forum__group__in=u.groups.all()))\ .prefetch_related("author").order_by("-pubdate").all() # Paginator paginator = Paginator(posts, settings.POSTS_PER_PAGE) page = request.GET.get("page") try: shown_posts = paginator.page(page) page = int(page) except PageNotAnInteger: shown_posts = paginator.page(1) page = 1 except EmptyPage: shown_posts = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template("forum/find/post.html", { "posts": shown_posts, "usr": u, "pages": paginator_range(page, paginator.num_pages), "nb": page, })
def find_post(request, user_pk): """Finds all posts of a user.""" displayed_user = get_object_or_404(User, pk=user_pk) user = request.user if user.has_perm("forum.change_post"): posts = \ Post.objects.filter(author=displayed_user)\ .exclude(Q(topic__forum__group__isnull=False) & ~Q(topic__forum__group__in=user.groups.all()))\ .prefetch_related("author")\ .order_by("-pubdate").all() else: posts = \ Post.objects.filter(author=displayed_user)\ .filter(is_visible=True)\ .exclude(Q(topic__forum__group__isnull=False) & ~Q(topic__forum__group__in=user.groups.all()))\ .prefetch_related("author").order_by("-pubdate").all() # Paginator paginator = Paginator(posts, settings.ZDS_APP['forum']['posts_per_page']) page = request.GET.get("page") try: shown_posts = paginator.page(page) page = int(page) except PageNotAnInteger: shown_posts = paginator.page(1) page = 1 except EmptyPage: shown_posts = paginator.page(paginator.num_pages) page = paginator.num_pages return render_template( "forum/find/post.html", { "posts": shown_posts, "usr": displayed_user, "pages": paginator_range(page, paginator.num_pages), "nb": page, })
def create_response(self): (paginator, page) = self.build_page() page_nbr = int(self.request.GET.get('page', 1)) context = { 'query': self.query, 'form': self.form, 'page': page, 'pages': paginator_range(page_nbr, paginator.num_pages), 'nb': page_nbr, 'paginator': paginator, 'suggestion': None, 'model_name': MODEL_NAMES } if self.results and hasattr( self.results, 'query') and self.results.query.backend.include_spelling: context['suggestion'] = self.form.get_suggestion() context.update(self.extra_context()) return render(self.request, self.template, context)
def topic(request, topic_pk, topic_slug): """Display a thread and its posts using a pager.""" topic = get_object_or_404(Topic, pk=topic_pk) if not topic.forum.can_read(request.user): raise PermissionDenied # Check link if not topic_slug == slugify(topic.title): return redirect(topic.get_absolute_url()) # If the user is authenticated and has never read topic, we mark it as # read. if request.user.is_authenticated(): if never_read(topic): mark_read(topic) # Retrieves all posts of the topic and use paginator with them. posts = \ Post.objects.filter(topic__pk=topic.pk) \ .select_related() \ .order_by("position" ).all() last_post_pk = topic.last_message.pk # Handle pagination paginator = Paginator(posts, settings.POSTS_PER_PAGE) # The category list is needed to move threads categories = Category.objects.all() if "page" in request.GET: try: page_nbr = int(request.GET["page"]) except: # problem in variable format raise Http404 else: page_nbr = 1 try: posts = paginator.page(page_nbr) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: raise Http404 res = [] if page_nbr != 1: # Show the last post of the previous page last_page = paginator.page(page_nbr - 1).object_list last_post = last_page[len(last_page) - 1] res.append(last_post) for post in posts: res.append(post) # Build form to send a post for the current topic. form = PostForm(topic, request.user) form.helper.form_action = reverse("zds.forum.views.answer") + "?sujet=" \ + str(topic.pk) form_move = MoveTopicForm(topic=topic) return render_template("forum/topic/index.html", { "topic": topic, "posts": res, "categories": categories, "pages": paginator_range(page_nbr, paginator.num_pages), "nb": page_nbr, "last_post_pk": last_post_pk, "form": form, "form_move": form_move, })
def view_online(request, article_pk, article_slug): '''Show the given article if exists and is visible''' article = get_object_or_404(Article, pk=article_pk) try: sha = request.GET['version'] except KeyError: sha = article.sha_draft if article_slug != slugify(article.title): return redirect(article.get_absolute_url()) #find the good manifest file article_version = article.load_json() txt = open( os.path.join(article.get_path(), article_version['text'] + '.html'), "r") article_version['txt'] = txt.read() txt.close() article_version['pk'] = article.pk article_version['slug'] = article.slug article_version['is_locked'] = article.is_locked if request.user.is_authenticated(): article_version['antispam'] = article.antispam() #find reactions if request.user.is_authenticated(): if never_read(article): mark_read(article) reactions = Reaction.objects\ .filter(article__pk=article.pk)\ .order_by('position')\ .all() if article.last_reaction: last_reaction_pk = article.last_reaction.pk else: last_reaction_pk = 0 # Handle pagination paginator = Paginator(reactions, settings.POSTS_PER_PAGE) try: page_nbr = int(request.GET['page']) except KeyError: page_nbr = 1 try: reactions = paginator.page(page_nbr) except PageNotAnInteger: reactions = paginator.page(1) except EmptyPage: raise Http404 res = [] if page_nbr != 1: # Show the last reaction of the previous page last_page = paginator.page(page_nbr - 1).object_list last_reaction = (last_page)[len(last_page) - 1] res.append(last_reaction) for reaction in reactions: res.append(reaction) return render_template( 'article/view_online.html', { 'article': article_version, 'authors': article.authors, 'tags': article.subcategory, 'version': sha, 'prev': get_prev_article(article), 'next': get_next_article(article), 'reactions': res, 'pages': paginator_range(page_nbr, paginator.num_pages), 'nb': page_nbr, 'last_reaction_pk': last_reaction_pk })
def topic(request, topic_pk, topic_slug): ''' Display a thread and its posts using a pager ''' # TODO: Clean that up g_topic = get_object_or_404(Topic, pk=topic_pk) if not g_topic.forum.can_read(request.user): raise Http404 # Check link if not topic_slug == slugify(g_topic.title): return redirect(g_topic.get_absolute_url()) if request.user.is_authenticated(): if never_read(g_topic): mark_read(g_topic) posts = Post.objects\ .filter(topic__pk=g_topic.pk)\ .order_by('position')\ .all() last_post_pk = g_topic.last_message.pk # Handle pagination paginator = Paginator(posts, settings.POSTS_PER_PAGE) # The category list is needed to move threads categories = Category.objects.all() try: page_nbr = int(request.GET['page']) except KeyError: page_nbr = 1 try: posts = paginator.page(page_nbr) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: raise Http404 res = [] if page_nbr != 1: # Show the last post of the previous page last_page = paginator.page(page_nbr - 1).object_list last_post = (last_page)[len(last_page) - 1] res.append(last_post) for post in posts: res.append(post) return render_template( 'forum/topic.html', { 'topic': g_topic, 'posts': res, 'categories': categories, 'pages': paginator_range(page_nbr, paginator.num_pages), 'nb': page_nbr, 'last_post_pk': last_post_pk })
def view_online(request, article_pk, article_slug): """Show the given article if exists and is visible.""" article = get_object_or_404(Article, pk=article_pk) # The slug of the article must to be right. if article_slug != slugify(article.title): return redirect(article.get_absolute_url_online()) # Load the article. article_version = article.load_json() txt = open( os.path.join( article.get_path(), article_version['text'] + '.html'), "r") article_version['txt'] = txt.read() txt.close() article_version['pk'] = article.pk article_version['slug'] = article.slug article_version['image'] = article.image article_version['is_locked'] = article.is_locked article_version['get_absolute_url'] = article.get_absolute_url() article_version['get_absolute_url_online'] = article.get_absolute_url_online() # If the user is authenticated if request.user.is_authenticated(): # We check if he can post an article or not with # antispam filter. article_version['antispam'] = article.antispam() # If the user is never read, we mark this article read. if never_read(article): mark_read(article) # Find all reactions of the article. reactions = Reaction.objects\ .filter(article__pk=article.pk)\ .order_by('position')\ .all() # Retrieve pk of the last reaction. If there aren't reactions # for the article, we initialize this last reaction at 0. last_reaction_pk = 0 if article.last_reaction: last_reaction_pk = article.last_reaction.pk # Handle pagination. paginator = Paginator(reactions, settings.POSTS_PER_PAGE) try: page_nbr = int(request.GET['page']) except KeyError: page_nbr = 1 try: reactions = paginator.page(page_nbr) except PageNotAnInteger: reactions = paginator.page(1) except EmptyPage: raise Http404 res = [] if page_nbr != 1: # Show the last reaction of the previous page last_page = paginator.page(page_nbr - 1).object_list last_reaction = (last_page)[len(last_page) - 1] res.append(last_reaction) for reaction in reactions: res.append(reaction) # Build form to send a reaction for the current article. form = ReactionForm(article, request.user) return render_template('article/view.html', { 'article': article_version, 'authors': article.authors, 'tags': article.subcategory, 'prev': get_prev_article(article), 'next': get_next_article(article), 'reactions': res, 'pages': paginator_range(page_nbr, paginator.num_pages), 'nb': page_nbr, 'last_reaction_pk': last_reaction_pk, 'form': form })
def topic(request, topic_pk, topic_slug): """Display a thread and its posts using a pager.""" topic = get_object_or_404(Topic, pk=topic_pk) if not topic.forum.can_read(request.user): raise PermissionDenied # Check link if not topic_slug == slugify(topic.title): return redirect(topic.get_absolute_url()) # If the user is authenticated and has never read topic, we mark it as # read. if request.user.is_authenticated(): if never_read(topic): mark_read(topic) # Retrieves all posts of the topic and use paginator with them. posts = \ Post.objects.filter(topic__pk=topic.pk) \ .select_related() \ .order_by("position" ).all() last_post_pk = topic.last_message.pk # Handle pagination paginator = Paginator(posts, settings.POSTS_PER_PAGE) # The category list is needed to move threads categories = Category.objects.all() try: page_nbr = int(request.GET["page"]) except KeyError: page_nbr = 1 try: posts = paginator.page(page_nbr) except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: raise Http404 res = [] if page_nbr != 1: # Show the last post of the previous page last_page = paginator.page(page_nbr - 1).object_list last_post = last_page[len(last_page) - 1] res.append(last_post) for post in posts: res.append(post) # Build form to send a post for the current topic. form = PostForm(topic, request.user) form.helper.form_action = reverse("zds.forum.views.answer") + "?sujet=" \ + str(topic.pk) form_move = MoveTopicForm(topic=topic) return render_template("forum/topic/index.html", { "topic": topic, "posts": res, "categories": categories, "pages": paginator_range(page_nbr, paginator.num_pages), "nb": page_nbr, "last_post_pk": last_post_pk, "form": form, "form_move": form_move, })
def view_online(request, article_pk, article_slug): """Show the given article if exists and is visible.""" article = get_object_or_404(Article, pk=article_pk) # Load the article. article_version = article.load_json_for_public() txt = open( os.path.join(article.get_path(), article_version['text'] + '.html'), "r") article_version['txt'] = txt.read() txt.close() article_version = article.load_dic(article_version) # If the user is authenticated if request.user.is_authenticated(): # We check if he can post an article or not with # antispam filter. article_version['antispam'] = article.antispam() # If the user is never read, we mark this article read. if never_read(article): mark_read(article) # Find all reactions of the article. reactions = Reaction.objects\ .filter(article__pk=article.pk)\ .order_by('position')\ .all() # Retrieve pk of the last reaction. If there aren't reactions # for the article, we initialize this last reaction at 0. last_reaction_pk = 0 if article.last_reaction: last_reaction_pk = article.last_reaction.pk # Handle pagination. paginator = Paginator(reactions, settings.POSTS_PER_PAGE) try: page_nbr = int(request.GET['page']) except KeyError: page_nbr = 1 try: reactions = paginator.page(page_nbr) except PageNotAnInteger: reactions = paginator.page(1) except EmptyPage: raise Http404 res = [] if page_nbr != 1: # Show the last reaction of the previous page last_page = paginator.page(page_nbr - 1).object_list last_reaction = (last_page)[len(last_page) - 1] res.append(last_reaction) for reaction in reactions: res.append(reaction) # Build form to send a reaction for the current article. form = ReactionForm(article, request.user) return render_template( 'article/view.html', { 'article': article_version, 'authors': article.authors, 'tags': article.subcategory, 'prev': get_prev_article(article), 'next': get_next_article(article), 'reactions': res, 'pages': paginator_range(page_nbr, paginator.num_pages), 'nb': page_nbr, 'last_reaction_pk': last_reaction_pk, 'form': form })
def view_online(request, article_pk, article_slug): '''Show the given article if exists and is visible''' article = get_object_or_404(Article, pk=article_pk) try: sha = request.GET['version'] except KeyError: sha = article.sha_draft if article_slug != slugify(article.title): return redirect(article.get_absolute_url()) #find the good manifest file article_version = article.load_json() txt = open(os.path.join(article.get_path(), article_version['text']+'.html'), "r") article_version['txt'] = txt.read() txt.close() article_version['pk'] = article.pk article_version['slug'] = article.slug article_version['is_locked'] = article.is_locked if request.user.is_authenticated(): article_version['antispam'] = article.antispam() #find reactions if request.user.is_authenticated(): if never_read(article): mark_read(article) reactions = Reaction.objects\ .filter(article__pk=article.pk)\ .order_by('position')\ .all() if article.last_reaction: last_reaction_pk = article.last_reaction.pk else: last_reaction_pk = 0 # Handle pagination paginator = Paginator(reactions, settings.POSTS_PER_PAGE) try: page_nbr = int(request.GET['page']) except KeyError: page_nbr = 1 try: reactions = paginator.page(page_nbr) except PageNotAnInteger: reactions = paginator.page(1) except EmptyPage: raise Http404 res = [] if page_nbr != 1: # Show the last reaction of the previous page last_page = paginator.page(page_nbr - 1).object_list last_reaction = (last_page)[len(last_page) - 1] res.append(last_reaction) for reaction in reactions: res.append(reaction) return render_template('article/view_online.html', { 'article': article_version, 'authors': article.authors, 'tags': article.subcategory, 'version': sha, 'prev': get_prev_article(article), 'next': get_next_article(article), 'reactions': res, 'pages': paginator_range(page_nbr, paginator.num_pages), 'nb': page_nbr, 'last_reaction_pk': last_reaction_pk })