def add_post(request, forum, section, topic, preview): if not request.user.has_perm('forum.add_post'): raise Http404() topic = get_number_from_param_or_404(topic) f = get_object_or_404(Forum, address=forum) s, ss = None, None try: s = f.section_set.get(address=section) except ObjectDoesNotExist: ss = get_object_or_404(Sub_section, address=section) s = ss.section t = get_object_or_404(s.topic_set.all(), id=topic) if request.method == 'POST': errors = {'body': False} body = request.POST['input_body'] if len(body) > 1000: errors['body'] = True if not True in errors.values(): if not preview: try: p = create_and_get_post(creator=request.user, topic=t, body=body) except ValueError: # soon(30.08.12, 12:42) # FIXME # Обработать нормально эту ситуацию raise Http404() return redirect(p.get_absolute_url()) else: p = Post(creator=request.user, topic=t, body=body) return direct_to_template(request, 'forum/add_post.hdt', { 'post': p, 'topic': t, 'forum': f, 'section': s }) return direct_to_template(request, 'forum/add_post.hdt', { 'post_body': text_block, 'errors': errors }) else: raise Http404() #-------------------------------------------------------------------------------
def get_news_or_404(id): return get_object_or_404(News, id=get_number_from_param_or_404(id))
def get_news_or_404(id): return get_object_or_404(News, id=get_number_from_param_or_404(id))
def posts(request, forum, section, topic, page): topic = get_number_from_param_or_404(topic) page = get_number_from_param_or_404(page) f = get_object_or_404(Forum, address = forum) try: s = f.section_set.get(address = section) t = get_object_or_404( s.topic_set.filter(sub_section = None), id = topic ) p = t.post_set.all() first_post = p[0]; paginator = Paginator(p[1:], 10) try: p = paginator.page(page) except EmptyPage: p = paginator.page(paginator.num_pages) # if p.count() <= (page - 1) * 10 and p.count(): # raise Http404() return direct_to_template( request, 'forum/topic.hdt', { # 'posts' : p[:10] if page == 1 else \ # list(p[:1]) + \ # list(p[(page - 1) * 10:page * 10]), 'first_post' : first_post, 'posts' : p, 'topic' : t, 'section' : s, 'forum' : f # 'page' : page } ) except ObjectDoesNotExist: ss = get_object_or_404(Sub_section, address = section) t = get_object_or_404(ss.topic_set.all(), id = topic) p = t.post_set.all() first_post = p[0]; paginator = Paginator(p[1:], 10) try: p = paginator.page(page) except EmptyPage: p = paginator.page(paginator.num_pages) # if p.count() <= (page - 1) * 10 and p.count(): # raise Http404() return direct_to_template( request, 'forum/topic.hdt', { # 'posts' : p[:10] if page == 1 else \ # list(p[:1]) + \ # list(p[(page - 1) * 10:page * 10]), 'first_post' : first_post, 'posts' : p, 'topic' : t, 'sub_section' : ss, 'section' : ss.section, 'forum' : f # 'page' : page } )
def add_post(request, forum, section, topic, preview): if not request.user.has_perm('forum.add_post'): raise Http404() topic = get_number_from_param_or_404(topic) f = get_object_or_404(Forum, address = forum) s, ss = None, None try: s = f.section_set.get(address = section) except ObjectDoesNotExist: ss = get_object_or_404(Sub_section, address = section) s = ss.section t = get_object_or_404(s.topic_set.all(), id = topic) if request.method == 'POST': errors = {'body': False} body = request.POST['input_body'] if len(body) > 1000: errors['body'] = True if not True in errors.values(): if not preview: try: p = create_and_get_post( creator = request.user, topic = t, body = body ) except ValueError: # soon(30.08.12, 12:42) # FIXME # Обработать нормально эту ситуацию raise Http404() return redirect(p.get_absolute_url()) else: p = Post( creator = request.user, topic = t, body = body ) return direct_to_template( request, 'forum/add_post.hdt', { 'post' : p, 'topic' : t, 'forum' : f, 'section' : s } ) return direct_to_template( request, 'forum/add_post.hdt', { 'post_body' : text_block, 'errors' : errors } ) else: raise Http404() #-------------------------------------------------------------------------------
def posts(request, forum, section, topic, page): topic = get_number_from_param_or_404(topic) page = get_number_from_param_or_404(page) f = get_object_or_404(Forum, address=forum) try: s = f.section_set.get(address=section) t = get_object_or_404(s.topic_set.filter(sub_section=None), id=topic) p = t.post_set.all() first_post = p[0] paginator = Paginator(p[1:], 10) try: p = paginator.page(page) except EmptyPage: p = paginator.page(paginator.num_pages) # if p.count() <= (page - 1) * 10 and p.count(): # raise Http404() return direct_to_template( request, 'forum/topic.hdt', { # 'posts' : p[:10] if page == 1 else \ # list(p[:1]) + \ # list(p[(page - 1) * 10:page * 10]), 'first_post': first_post, 'posts': p, 'topic': t, 'section': s, 'forum': f # 'page' : page }) except ObjectDoesNotExist: ss = get_object_or_404(Sub_section, address=section) t = get_object_or_404(ss.topic_set.all(), id=topic) p = t.post_set.all() first_post = p[0] paginator = Paginator(p[1:], 10) try: p = paginator.page(page) except EmptyPage: p = paginator.page(paginator.num_pages) # if p.count() <= (page - 1) * 10 and p.count(): # raise Http404() return direct_to_template( request, 'forum/topic.hdt', { # 'posts' : p[:10] if page == 1 else \ # list(p[:1]) + \ # list(p[(page - 1) * 10:page * 10]), 'first_post': first_post, 'posts': p, 'topic': t, 'sub_section': ss, 'section': ss.section, 'forum': f # 'page' : page })