From d22fe825fbf34842f9fa93c2f533677a34816984 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Thu, 10 Jan 2019 23:30:05 +0300 Subject: improve post preview system, fix incorrect translation title on breadcrumbs --- weblog/templates/weblog/index.html | 4 ++-- weblog/views.py | 43 ++++++++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 11 deletions(-) (limited to 'weblog') diff --git a/weblog/templates/weblog/index.html b/weblog/templates/weblog/index.html index 0f8d79a..7d8d723 100755 --- a/weblog/templates/weblog/index.html +++ b/weblog/templates/weblog/index.html @@ -40,7 +40,7 @@

{% blocktrans with publish_date=post.publish_date %}Published on {{ publish_date }}{% endblocktrans %}{% if post.author %}{% blocktrans with author=post.author context 'Written by (Author)' %}, by {% endblocktrans %}{% endif %}


{% if post.preview_image %}{% endif %} - {{ post.preview_text|safe }} +

{{ post.preview_text|safe }}


{% trans 'Read more...' %} @@ -55,7 +55,7 @@

{% blocktrans with publish_date=post.publish_date %}Published on {{ publish_date }}{% endblocktrans %}{% if post.author %}{% blocktrans with author=post.author context 'Written by (Author)' %}, by {% endblocktrans %}{% endif %}


{% if post.preview_image %}{% endif %} - {{ post.preview_text|safe }} +

{{ post.preview_text|safe }}


{% trans 'Read more...' %} diff --git a/weblog/views.py b/weblog/views.py index d614cd6..64c4ec3 100755 --- a/weblog/views.py +++ b/weblog/views.py @@ -118,10 +118,17 @@ def Index(request, **kwargs): post['title'] = post_raw.title post['content'] = post_raw.content post['preview_image'] = post_raw.preview_image + # If the author of the post provided custom preview text use it if len(post_raw.preview_text) > 5: post['preview_text'] = post_raw.preview_text else: - post['preview_text'] = post_raw.content.split('

', 1)[0]+'

' + try: + # Otherwise, find first paragraph and use it as preview text + # If none found, the preview is empty + post['preview_text'] = \ + post_raw.content.split('

', 2)[1].split('

', 1)[0] + except IndexError: + post['preview_text'] = '' else: post_trans = None orig_lang = post_raw.original_language @@ -192,16 +199,23 @@ def PostView(request, category_slug, post_slug, language=None): context_dict['category'] = category # If we have the multilingual setting on, get the translation for the category if IS_MULTILINGUAL: - category_translations = CategoryTranslation.objects.filter(category=category) + category_translations =\ + CategoryTranslation.objects.filter(category=category) if category_translations.count() > 0: for cat_trans in category_translations: if current_language[0:2] == cat_trans.language[0:2]: context_dict['category'] = cat_trans # Put the necessary data about the category for the breadcrumbs if category_slug == 'misc': - context_dict['breadcrumbs'] = [{'url': reverse('weblog:CategoryIndex', kwargs={'category_slug': category_slug}), 'name': pgettext_lazy('Posts without category', 'Uncategorized')},] + context_dict['breadcrumbs'] = [{ + 'url': reverse('weblog:CategoryIndex', kwargs={ + 'category_slug': category_slug}), + 'name': pgettext_lazy('Posts without category', 'Uncategorized')},] else: - context_dict['breadcrumbs'] = [{'url': reverse('weblog:CategoryIndex', kwargs={'category_slug': category_slug}), 'name': context_dict['category']},] + context_dict['breadcrumbs'] = [{ + 'url': reverse('weblog:CategoryIndex', kwargs={ + 'category_slug': category_slug}), + 'name': context_dict['category']},] # Put the necessary information about the author, based on the # current project's settings @@ -214,7 +228,8 @@ def PostView(request, category_slug, post_slug, language=None): if ENABLE_COMMENTS: context_dict['comments'] = PostComment.objects.filter(post=post) - # If this is a POST request, it (probably) means that the user is attempting to post a comment + # If this is a POST request, it (probably) means that the user + # is attempting to post a comment if request.method == 'POST': form = PostCommentForm(request.POST) context_dict['comment_submission'] = True @@ -224,10 +239,16 @@ def PostView(request, category_slug, post_slug, language=None): # Make sure that either anonymous comments are allowed or # that the user is authenticated if request.user.is_authenticated: - new_comment = PostComment(author=request.user, post=post, content=comment_content, publish_date=datetime.datetime.now()) + new_comment = PostComment( + author=request.user, + post=post, + content=comment_content, + publish_date=datetime.datetime.now()) new_comment.save() elif ALLOW_ANON_COMMENTS: - new_comment = PostComment(post=post, content=comment_content, publish_date=datetime.datetime.now()) + new_comment = PostComment(post=post, + content=comment_content, + publish_date=datetime.datetime.now()) new_comment.save() else: context_dict['comment_submission_error'] = _('You need to sign in to submit a comment') @@ -278,9 +299,13 @@ def PostView(request, category_slug, post_slug, language=None): # If we are reading a translation, and not the original text, # use those translations for the breadcrumbs, otherwise, use the original title if 'post_translation' in context_dict: - context_dict['breadcrumbs'].append({'url': post.get_absolute_url(), 'name': post_translation.title}) + context_dict['breadcrumbs'].append({ + 'url': post.get_absolute_url(), + 'name': context_dict['post_translation'].title}) else: - context_dict['breadcrumbs'].append({'url': post.get_absolute_url(), 'name': post.title}) + context_dict['breadcrumbs'].append({ + 'url': post.get_absolute_url(), + 'name': post.title}) return render(request, 'weblog/post.html', context_dict) # A dirty hack to change the language on the fly. -- cgit v1.2.3