Skip to content

Commit dfc37e3

Browse files
Improve the application for better user experience.
1 parent ca5fb0e commit dfc37e3

10 files changed

+96
-13
lines changed

db.sqlite3

0 Bytes
Binary file not shown.

templates/base.html

+8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<!DOCTYPE html>
2+
<html lang="en">
23
{% load static %}
34
<html>
45
<head>
56
<meta charset="utf-8">
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
69
<title>{% block title %}Web Chat Boards{% endblock %}</title>
10+
11+
<meta name="description" content="A django Basic to Advanced Tutorial projects.">
12+
<meta name="Mohammed" content="django tutorial">
13+
714
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
15+
816
{% block stylesheet %}{% endblock %}
917
</head>
1018
<body>

templates/chat_board_topics.html

+22-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<a href="{% url 'new_board_topic' chat_board.pk %}" class="btn btn-primary">New topic</a>
1919
</div>
2020

21-
<table class="table mb-4">
21+
<table class="table table-striped mb-4">
2222
<thead class="thead-inverse">
2323
<tr>
2424
<th>Topic</th>
@@ -30,13 +30,29 @@
3030
</thead>
3131
<tbody>
3232
{% for topic in topics %}
33+
{% url 'topic_posts' chat_board.pk topic.pk as topic_url %}
3334
<tr>
34-
<td><a href="{% url 'topic_posts' chat_board.pk topic.pk %}">{{ topic.subject }}</td>
35-
<td>{{ topic.boardStarter.username }}</td>
36-
<td>{{ topic.replies }}</td>
37-
<td>{{ topic.views }}</td>
38-
<td>{{ topic.lastUpdate|naturaltime }}</td>
35+
<td>
36+
<p class="mb-0">
37+
<a href="{{ topic_url }}">{{ topic.subject }}</a>
38+
</p>
39+
<small class="text-muted">
40+
Pages:
41+
{% for i in topic.get_page_range %}
42+
<a href="{{ topic_url }}?page={{ i }}">{{ i }}</a>
43+
{% endfor %}
44+
{% if topic.has_many_pages %}
45+
... <a href="{{ topic_url }}?page={{ topic.get_page_count }}">Last Page</a>
46+
{% endif %}
47+
</small>
48+
</td>
49+
<td>{{ topic.boardStarter.username }}</td>
50+
<td>{{ topic.replies }}</td>
51+
<td>{{ topic.views }}</td>
52+
<td>{{ topic.lastUpdate|naturaltime }}</td>
3953
</tr>
54+
55+
4056
{% endfor %}
4157
</tbody>
4258
</table>

templates/includes/pagination.html

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
{% if is_paginated %}
22
<nav aria-label="Topics pagination" class="mb-4">
33
<ul class="pagination">
4+
{% if page_obj.number > 1 %}
5+
<li class="page-item">
6+
<a class="page-link" href="?page=1">First</a>
7+
</li>
8+
{% else %}
9+
<li class="page-item disabled">
10+
<span class="page-link">First</span>
11+
</li>
12+
{% endif %}
13+
414
{% if page_obj.has_previous %}
515
<li class="page-item">
616
<a class="page-link" href="?page={{ page_obj.previous_page_number }}">Previous</a>
@@ -19,7 +29,7 @@
1929
<span class="sr-only">(current)</span>
2030
</span>
2131
</li>
22-
{% else %}
32+
{% elif page_num > page_obj.number|add:'-3' and page_num < page_obj.number|add:'3' %}
2333
<li class="page-item">
2434
<a class="page-link" href="?page={{ page_num }}">{{ page_num }}</a>
2535
</li>
@@ -35,6 +45,16 @@
3545
<span class="page-link">Next</span>
3646
</li>
3747
{% endif %}
48+
49+
{% if page_obj.number != paginator.num_pages %}
50+
<li class="page-item">
51+
<a class="page-link" href="?page={{ paginator.num_pages }}">Last</a>
52+
</li>
53+
{% else %}
54+
<li class="page-item disabled">
55+
<span class="page-link">Last</span>
56+
</li>
57+
{% endif %}
3858
</ul>
3959
</nav>
4060
{% endif %}

templates/reply_topic.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<button type="submit" class="btn btn-success">Post a reply</button>
3333
</form>
3434

35-
{% for post in topic.posts.all %}
35+
{% for post in topic.get_last_five_posts %}
3636
<div class="card mb-2">
3737
<div class="card-body p-3">
3838
<div class="row mb-3">

templates/topic_posts.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131

3232
{% for post in posts %}
33-
<div class="card mb-2 {% if forloop.first %} border-dark{% endif %}">
33+
<div id="{{ post.pk }}" class="card {% if forloop.last %}mb-4{% else %}mb-2{% endif %} {% if forloop.first %} border-dark{% endif %}">
3434
{% if forloop.first %}
3535
<div class="card-header text-white bg-dark py-2 px-3">{{ topic.subject}}</div>
3636
{% endif %}
755 Bytes
Binary file not shown.
344 Bytes
Binary file not shown.

webchat/models.py

+22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.utils.text import Truncator
55
from markdown import markdown
66
from django.utils.html import mark_safe
7+
import math
78

89
class ChatBoard(models.Model):
910
name = models.CharField(max_length = 35, unique = True)
@@ -30,6 +31,27 @@ class ChatTopic(models.Model):
3031
def __str__(self):
3132
return self.subject
3233

34+
def get_page_count(self):
35+
count = self.posts.count()
36+
pages = count / 15
37+
return math.ceil(pages)
38+
39+
def has_many_pages(self, count = None):
40+
if count is None:
41+
count = self.get_page_count()
42+
return count > 6
43+
44+
def get_page_range(self):
45+
count = self.get_page_count()
46+
if self.has_many_pages(count):
47+
return range(1,5)
48+
return range(1, count + 1)
49+
50+
def get_last_five_posts(self):
51+
return self.posts.order_by('createdAt')[:5]
52+
53+
54+
3355
class Post(models.Model):
3456
message = models.TextField(max_length = 5000)
3557
topic = models.ForeignKey(ChatTopic, related_name = 'posts',on_delete=models.CASCADE)

webchat/views.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from django.core.paginator import PageNotAnInteger
1919
from django.views.generic import ListView
2020
from django.urls import reverse_lazy
21+
from django.urls import reverse
2122

2223

2324
#function based view home- FBC
@@ -57,7 +58,7 @@ class TopicListView(ListView):
5758
model = ChatTopic
5859
context_object_name = 'topics'
5960
template_name = 'chat_board_topics.html'
60-
paginate_by = 20
61+
paginate_by = 10
6162

6263
def get_context_data(self, **kwargs):
6364
kwargs['chat_board'] = self.chat_board
@@ -108,8 +109,13 @@ class PostListView(ListView):
108109
paginate_by = 3
109110

110111
def get_context_data(self, **kwargs):
111-
self.topic.views += 1
112-
self.topic.save()
112+
113+
session_key = 'viewd_topic_{}'.format(self.topic.pk)
114+
if not self.request.session.get(session_key, False):
115+
self.topic.views += 1
116+
self.topic.save()
117+
self.request.session[session_key] = True
118+
113119
kwargs['topic'] = self.topic
114120
return super().get_context_data(**kwargs)
115121

@@ -128,7 +134,18 @@ def reply_topic(request, pk, topic_pk):
128134
post.topic = topic
129135
post.createdBy = request.user
130136
post.save()
131-
return redirect('topic_posts', pk=pk, topic_pk=topic_pk)
137+
138+
topic.lastUpdate = timezone.now()
139+
topic.save()
140+
141+
topic_url = reverse('topic_posts', kwargs={'pk':pk, 'topic_pk':topic_pk})
142+
topic_post_url = '{url}?page={page}#{id}'.format(
143+
url=topic_url,
144+
id=post.pk,
145+
page=topic.get_page_count()
146+
)
147+
148+
return redirect(topic_post_url)
132149
else:
133150
form = PostForm()
134151
return render(request,'reply_topic.html', {'topic':topic, 'form':form})

0 commit comments

Comments
 (0)