diff options
| author | Bobby <[email protected]> | 2022-12-31 11:59:38 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2022-12-31 11:59:38 -0500 |
| commit | 72e35f67163b67bdf5ccfa709954be21a509b411 (patch) | |
| tree | e63e8da060962fa0a73e2b5587a1ec10cdd7835f | |
| parent | f7f323657ac2b52ce5a6b3d49ca6a0cdb3320938 (diff) | |
| download | thatcomputerscientist-72e35f67163b67bdf5ccfa709954be21a509b411.tar.xz thatcomputerscientist-72e35f67163b67bdf5ccfa709954be21a509b411.zip | |
Articles Section
| -rw-r--r-- | blog/context_processors.py | 31 | ||||
| -rw-r--r-- | blog/urls.py | 1 | ||||
| -rw-r--r-- | blog/views.py | 21 | ||||
| -rw-r--r-- | templates/blog/articles.html | 49 | ||||
| -rw-r--r-- | templates/blog/home.html | 8 | ||||
| -rw-r--r-- | templates/blog/partials/sidebar.html | 2 | ||||
| -rw-r--r-- | templates/blog_admin/edit_post.html | 1 | ||||
| -rw-r--r-- | templates/blog_admin/new_post.html | 1 | ||||
| -rw-r--r-- | templates/blog_admin/partials/posts_topbar.html | 16 |
9 files changed, 97 insertions, 33 deletions
diff --git a/blog/context_processors.py b/blog/context_processors.py index 00d4323d..0faeeb5f 100644 --- a/blog/context_processors.py +++ b/blog/context_processors.py @@ -1,24 +1,29 @@ from .models import Post, Category, Comment import os from django.conf import settings +from bs4 import BeautifulSoup -def recent_posts(): - recent_posts = Post.objects.filter(is_public=True).order_by('-date')[:5] - for post in recent_posts: - from bs4 import BeautifulSoup - soup = BeautifulSoup(post.body, 'html.parser') +def add_excerpt(post): + soup = BeautifulSoup(post.body, 'html.parser') - # Create excerpt, count min 1000 characters and max upto next paragraph - excerpt = '' - for paragraph in soup.find_all('p'): - excerpt += str(paragraph) + # Create excerpt, count min 1000 characters and max upto next paragraph + excerpt = '' + for paragraph in soup.find_all('p'): + excerpt += str(paragraph) - if len(excerpt) >= 1000: - break - post.excerpt = excerpt + if len(excerpt) >= 1000: + break + return excerpt +def add_num_comments(post): + num_comments = Comment.objects.filter(post=post).count() + return num_comments - post.num_comments = Comment.objects.filter(post=post).count() +def recent_posts(): + recent_posts = Post.objects.filter(is_public=True).order_by('-date')[:5] + for post in recent_posts: + post.excerpt = add_excerpt(post) + post.num_comments = add_num_comments(post) return recent_posts def categories(request): diff --git a/blog/urls.py b/blog/urls.py index 526093cc..4c9a000e 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -8,6 +8,7 @@ urlpatterns = [ path('account', views.account, name='account'), path('register', views.register, name='register'), path('search', views.search, name='search'), + path('articles', views.articles, name='articles'), path('articles/<str:slug>', views.post, name='post'), path('articles/<str:slug>/comment', views.comment, name='comment'), path('articles/<str:slug>/edit_comment', views.edit_comment, name='edit_comment'), diff --git a/blog/views.py b/blog/views.py index 96faa177..50f3b3f6 100644 --- a/blog/views.py +++ b/blog/views.py @@ -6,7 +6,7 @@ import hashlib from random import choice from string import ascii_letters, digits from .models import Category, Post, Comment -from .context_processors import recent_posts, avatar_list +from .context_processors import recent_posts, avatar_list, add_excerpt, add_num_comments from announcements.models import Announcement from users.forms import RegisterForm from users.tokens import CaptchaTokenGenerator @@ -204,4 +204,21 @@ def search(request): # order by date posts = posts.order_by('-date') return render(request, 'blog/search.html', {'title': 'Search', 'posts': posts, 'categories': categories, 'tags': tags, 'cate': category, 'query': query}) - + +from django.core.paginator import Paginator +def articles(request): + page = request.GET.get('page') if request.GET.get('page') else 1 + try: + page = int(page) + except: + page = 1 + + posts = Post.objects.filter(is_public=True).order_by('-date') + posts = Paginator(posts, 10) + posts = posts.page(page) + # add excerpt to each post + for post in posts: + post.excerpt = add_excerpt(post) + post.num_comments = add_num_comments(post) + num_pages = posts.paginator.num_pages + return render(request, 'blog/articles.html', {'title': 'Articles', 'posts': posts, 'num_pages': num_pages, 'page': page}) diff --git a/templates/blog/articles.html b/templates/blog/articles.html new file mode 100644 index 00000000..ab07891c --- /dev/null +++ b/templates/blog/articles.html @@ -0,0 +1,49 @@ +{% extends 'blog/partials/base.html' %} {% block content %} + +<h2 style="margin-top:0px;"> All Posts</h2> +{% if posts %} +<div id="recent-posts" class="mtctitem"> + {% for post in posts %} + <div class="post" style="clear: both;"> + <h1> + <a href="{% url 'blog:post' post.slug %}">{{ post.title }}</a> + </h1> + <div style="text-align: justify; font-size: 13px; margin-bottom: 0px;"> + <span> + <img src="{% url 'ignis:post_image' '320' post.id %}.gif" alt="Cover image for {{ post.title }}" style="float: left; margin-right: 11px; width: 320px; height: auto;"> + </span> + {{ post.excerpt | safe }} + </div> + <div class="post-actions" style="clear: both;"> + <a href="{% url 'blog:post' post.slug %}">Continue Reading</a> | <a href="{% url 'blog:post' post.slug %}#comments">{{ post.num_comments }} Comments</a> + </div> + </div> + {% endfor %} +</div> +{% endif %} +<div> + <table id="pagination"> + <tr> + {% if page == 1 %} + <td><a class="disabled">«</a></td> + <td style="margin-right: 15px;"><a class="disabled">‹</a></td> + {% else %} + <td><a href="{% url 'blog:articles' %}?page=1">«</a></td> + <td style="margin-right: 15px;"><a href="{% url 'blog:articles' %}?page={{ page|add:'-1' }}">‹</a></td> + {% endif %} + {% load times %} + {% for i in num_pages|times %} + <td><a {% if i == page %}class="active"{% endif %} href="{% url 'blog:articles' %}?page={{ i }}">{{ i }}</a></td> + {% endfor %} + {% if page == num_pages %} + <td style="margin-left: 15px;" class="disabled"><a class="disabled">›</a></td> + <td><a class="disabled">»</a></td> + {% else %} + <td style="margin-left: 15px;"><a href="{% url 'blog:articles' %}?page={{ page|add:'1' }}">›</a></td> + <td><a href="{% url 'blog:articles' %}?page={{ num_pages }}">»</a></td> + {% endif %} + </tr> + </table> +</div> + +{% endblock %}
\ No newline at end of file diff --git a/templates/blog/home.html b/templates/blog/home.html index bf31b189..7c9a8bf7 100644 --- a/templates/blog/home.html +++ b/templates/blog/home.html @@ -4,11 +4,7 @@ <h2>Welcome</h2> <p> Welcome to the home of That Computer Scientist. This is my personal - website where I share all of my thoughts, ideas, and experiences. To know - more about me, please visit my <a href="#">About</a> page. If you - would like to connect with me, please visit my - <a href="#">Contact</a> page. Please use the sidebar to navigate - the site. + website where I share all of my thoughts, ideas, and experiences. </p> <p> This website is a work in progress. I am currently working on adding more @@ -27,7 +23,7 @@ {% if announcements is not None %} <div id="announcements" class="mtctitem"> <h2><img src = "{% static 'images/gifs/update.gif' %}" style="height: 14px; width: auto;"></h2> - <marquee behavior="scroll" direction="up" height="250" width="720" scrollamount="2" scrolldelay="10" onmouseover="this.stop()" onmouseout="this.start()"> + <marquee behavior="scroll" direction="up" height="250" width="720" scrollamount="2" scrolldelay="20" onmouseover="this.stop()" onmouseout="this.start()"> <ul style="width: 720px;"> {% for announcement in announcements %} <li> diff --git a/templates/blog/partials/sidebar.html b/templates/blog/partials/sidebar.html index 0bcdf445..0126d54d 100644 --- a/templates/blog/partials/sidebar.html +++ b/templates/blog/partials/sidebar.html @@ -98,7 +98,7 @@ <img src="{% static 'images/site/icons/pencil.gif' %}" alt="Blog" border="0"> </span> <span> - <a href="#"> + <a href="{% url 'blog:articles' %}"> Articles </a> </span> diff --git a/templates/blog_admin/edit_post.html b/templates/blog_admin/edit_post.html index 9cff737d..8c6b24cc 100644 --- a/templates/blog_admin/edit_post.html +++ b/templates/blog_admin/edit_post.html @@ -2,7 +2,6 @@ <link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet" /> <div class="main"> <section> - {% include 'blog_admin/partials/posts_topbar.html' %} <div class="article-cover"> <img src="{% url 'ignis:post_image' '730' post.id %}.gif" alt="Cover Image" style="width: 730px; margin: 0 auto; display: block;"> </div> diff --git a/templates/blog_admin/new_post.html b/templates/blog_admin/new_post.html index 28dcca89..4cad7c77 100644 --- a/templates/blog_admin/new_post.html +++ b/templates/blog_admin/new_post.html @@ -2,7 +2,6 @@ <link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet" /> <div class="main"> <section> - {% include 'blog_admin/partials/posts_topbar.html' %} <form action="{% url 'blog-admin:new-post' %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <div class="form-group"> diff --git a/templates/blog_admin/partials/posts_topbar.html b/templates/blog_admin/partials/posts_topbar.html index 326854a2..506c13dd 100644 --- a/templates/blog_admin/partials/posts_topbar.html +++ b/templates/blog_admin/partials/posts_topbar.html @@ -1,14 +1,12 @@ -<div class="float-right"> - <a href="{% url 'blog-admin:new-post' %}" >Create New Post</a> - {% comment %} Search Users Box {% endcomment %} - <form style="display: inline-block; margin-left: 10px;" action="{% url 'blog-admin:posts-search' %}" method="get"> - <input style="display: inline-block" type="text" name="q" placeholder="Search Posts" autocomplete="off"/> - <input style="display: inline-block" type="submit" value="Search" /> +<div> + <form style="display: inline-block;" action="{% url 'blog-admin:posts-search' %}" method="get"> + <input style="display: inline;" type="text" name="q" placeholder="Search Posts" autocomplete="off"/> + <input style="display: inline;" type="submit" value="Search" class="button" /> </form> + <span style="margin-left:10px;"><a href="{% url 'blog-admin:new-post' %}" class="button button-special" style="text-decoration: none;">Create New Post</a></span> </div> -<h1 style="font-size: 2em;">{{ title }}</h1> -<hr> -<br> +<br><br><br> +<h2>{{ title }}</h2> {% for message in messages %} <p class="{{message.tags}}" style="text-align:center;">{{ message }}</p> {% endfor %}
\ No newline at end of file |
