home » zplus/freepost.git
Author zPlus <zplus@peers.community> 2018-07-23 11:55:27
Committer zPlus <zplus@peers.community> 2018-07-23 11:55:27
Commit d4dbdc6 (patch)
Tree a85b9a1
Parent(s)

Fix pagination. modified: freepost/__init__.py modified: freepost/database.py modified: freepost/static/stylus/reset.styl modified: freepost/templates/homepage.html new file: freepost/templates/posts.html modified: freepost/templates/search.html


commits diff: a4d094d..d4dbdc6
6 files changed, 162 insertions, 175 deletionsdownload


Diffstat
-rwxr-xr-x freepost/__init__.py 35
-rw-r--r-- freepost/database.py 19
-rwxr-xr-x freepost/static/stylus/reset.styl 6
-rwxr-xr-x freepost/templates/homepage.html 90
-rw-r--r-- freepost/templates/posts.html 100
-rwxr-xr-x freepost/templates/search.html 87

Diff options
View
Side
Whitespace
Context lines
Inter-hunk lines
+19/-16 M   freepost/__init__.py
index 060df8c..6b15c49
old size: 25K - new size: 25K
@@ -127,11 +127,7 @@ def homepage ():
127 127 else:
128 128 posts = []
129 129
130 - return template (
131 - 'homepage.html',
132 - page_number=page,
133 - posts=posts,
134 - sorting=sort)
130 + return template ('homepage.html', posts=posts, sort=sort)
135 131
136 132 # TODO implement this
137 133 @get ('/topic/<name>', name='topic')
@@ -161,9 +157,7 @@ def topic (name):
161 157 return template (
162 158 'homepage.html',
163 159 topic=name,
164 - page_number=page,
165 - posts=posts,
166 - sorting=sort)
160 + posts=posts)
167 161
168 162 @get ('/about', name='about')
169 163 def about ():
@@ -789,17 +783,26 @@ def search ():
789 783 # Get the search query
790 784 query = request.query.get ('q')
791 785
792 - # Results order
793 - order = request.query.get ('order')
794 - if order not in [ 'newest', 'points' ]:
795 - order = 'newest'
786 + # Get the offset
787 + page = int (request.query.get ('page') or 0)
796 788
797 - results = database.search (query, order=order)
789 + if page < 0:
790 + return "Page cannot be less than zero."
791 +
792 + # Page increment
793 + if 'next' in request.query:
794 + page += 1
795 + if 'previous' in request.query:
796 + page -= 1
797 +
798 + # Results order
799 + sort = request.query.get ('sort')
800 + if sort not in [ 'newest', 'points' ]:
801 + sort = 'newest'
798 802
799 - if not results:
800 - results = []
803 + posts = database.search (query, sort=sort, page=page) or []
801 804
802 - return template ('search.html', results=results, query=query, order=order)
805 + return template ('search.html', posts=posts)
803 806
804 807 @get ('/rss')
805 808 def rss_default ():

+10/-9 M   freepost/database.py
index f81d8ea..ed49217
old size: 18K - new size: 18K
@@ -589,22 +589,22 @@ def vote_comment (comment_id, user_id, vote):
589 589 )
590 590
591 591 # Search posts
592 - def search (query, page = 0, order = 'newest'):
592 + def search (query, sort='newest', page=0):
593 593 if not query:
594 - return None
594 + return []
595 595
596 596 # Remove multiple white spaces and replace with '|' (for query REGEXP)
597 597 query = re.sub (' +', '|', query.strip ())
598 598
599 599 if len (query) == 0:
600 - return None
600 + return []
601 601
602 602 cursor = db.cursor (MySQLdb.cursors.DictCursor)
603 603
604 - if order == 'newest':
605 - order = 'P.created DESC'
606 - if order == 'points':
607 - order = 'P.vote DESC'
604 + if sort == 'newest':
605 + sort = 'P.created DESC'
606 + if sort == 'points':
607 + sort = 'P.vote DESC'
608 608
609 609 cursor.execute (
610 610 """
@@ -612,12 +612,13 @@ def search (query, page = 0, order = 'newest'):
612 612 FROM post AS P
613 613 JOIN user AS U ON P.userId = U.id
614 614 WHERE P.title REGEXP %(query)s
615 - ORDER BY {order}
615 + ORDER BY {sort}
616 616 LIMIT %(limit)s
617 617 OFFSET %(offset)s
618 - """.format (order=order),
618 + """.format (sort=sort),
619 619 {
620 620 'query': query,
621 + 'sort': sort,
621 622 'limit': settings['defaults']['search_results_per_page'],
622 623 'offset': page * settings['defaults']['search_results_per_page']
623 624 }

+6/-0 M   freepost/static/stylus/reset.styl
index cafd060..a195e69
old size: 5K - new size: 5K
@@ -127,6 +127,12 @@ label
127 127 textarea.form-control
128 128 height 8rem
129 129
130 + .pagination
131 + > .page_number
132 + font-size .7rem
133 + font-weight bold
134 + margin 0 1rem
135 +
130 136 /* When users vote, this <iframe/> is used as target, such that
131 137 * the page is not reloaded
132 138 */

+5/-85 M   freepost/templates/homepage.html
index b150470..494610e
old size: 4K - new size: 226B
@@ -1,94 +1,14 @@
1 - {% from 'vote.html' import vote %}
2 -
3 1 {% extends 'layout.html' %}
4 2
5 3 {# Set variables for base layour #}
6 - {% set active_page = sorting %}
4 + {% set active_page = sort %}
7 5 {% set title = '' %}
8 6
9 7 {% block content %}
10 - <div class="posts">
11 -
12 - {# include 'banner.html' #}
13 -
14 - {% for post in posts %}
15 - {% set topics = split_topics (post.topics) %}
16 -
17 - <div class="post">
18 - {# Print the item position number #}
19 - <div class="position">
20 - {{ page_number * settings ('defaults', 'items_per_page') + loop.index }}.
21 - </div>
22 -
23 - <div class="info">
24 - <div class="title">
25 - <a href="{{ post.link if post.link and post.link|length > 0 else url ('post', hash_id=post.hashId) }}">
26 - {{ post.title }}
27 -
28 - {# Post content preview #}
29 - {% if post.text %}
30 - <img
31 - alt=""
32 - title="{{ post.text|md2txt }}"
33 - src="{{ url ('static', filename='images/text.svg') }}"
34 - class="text_preview" />
35 - {% endif %}
36 - </a>
37 -
38 - {# URL hostname #}
39 - {% if post.link %}
40 - <span class="netloc">
41 - {{ post.link|netloc }}
42 - </span>
43 - {% endif %}
44 - </div>
45 -
46 - <div class="topics">
47 - {% for topic in topics %}
48 - <a href="{{ url ('topic', name=topic) }}" class="topic">{{ topic }}</a>
49 - {% endfor %}
50 - </div>
51 -
52 - <div class="about">
53 - {{ vote ('post', post, user) }}
54 -
55 - <em class="username">
56 - <a href="{{ url ('post', hash_id=post.hashId) }}">
57 - <time title="{{ post.created|title }}" datetime="{{ post.created|datetime }}">
58 - {{ post.created|ago }}
59 - </time>
60 - </a>
61 - </em>
62 - by
63 - <a href="{{ url ('user_public', username=post.username) }}">
64 - {{ post.username }}
65 - </a>
66 -
67 - <a href="{{ url ('post', hash_id=post.hashId) }}#comments">
68 - {% if post.commentsCount > 0 %}
69 - {{ post.commentsCount }} comments
70 - {% else %}
71 - discuss
72 - {% endif %}
73 - </a>
74 - </div>
75 - </div>
76 - </div>
77 -
78 - {% endfor %}
79 -
80 - <div class="more">
81 - {% if page_number > 0 %}
82 - <a href="{{ request.urlparts.path if page_number == 1 else '?page=' ~ (page_number - 1) }}" class="button button_default1">
83 - Previous
84 - </a>
85 - {% endif %}
86 -
87 - <a href="?page={{ page_number + 1 }}{{ '&sort=' ~ request.query.sort }}" class="button button_default1">
88 - Next
89 - </a>
90 - </div>
91 - </div>
8 +
9 + {# include 'banner.html' #}
10 +
11 + {% include 'posts.html' %}
92 12
93 13
94 14 {% endblock %}

+100/-0 A   freepost/templates/posts.html
index 0000000..ffe58e8
old size: 0B - new size: 4K
new file mode: -rw-r--r--
@@ -0,0 +1,100 @@
1 + {% from 'vote.html' import vote %}
2 + {% set page_number = request.query.page|int or 0 %}
3 +
4 + <div class="posts">
5 + {% for post in posts %}
6 + {% set topics = split_topics (post.topics) %}
7 +
8 + <div class="post">
9 + {# Print the item position number #}
10 + <div class="position">
11 + {{ page_number * settings ('defaults', 'items_per_page') + loop.index }}.
12 + </div>
13 +
14 + <div class="info">
15 + <div class="title">
16 + <a href="{{ post.link if post.link and post.link|length > 0 else url ('post', hash_id=post.hashId) }}">
17 + {{ post.title }}
18 +
19 + {# Post content preview #}
20 + {% if post.text %}
21 + <img
22 + alt=""
23 + title="{{ post.text|md2txt }}"
24 + src="{{ url ('static', filename='images/text.svg') }}"
25 + class="text_preview" />
26 + {% endif %}
27 + </a>
28 +
29 + {# URL hostname #}
30 + {% if post.link %}
31 + <span class="netloc">
32 + {{ post.link|netloc }}
33 + </span>
34 + {% endif %}
35 + </div>
36 +
37 + <div class="topics">
38 + {% for topic in topics %}
39 + <a href="{{ url ('topic', name=topic) }}" class="topic">{{ topic }}</a>
40 + {% endfor %}
41 + </div>
42 +
43 + <div class="about">
44 + {{ vote ('post', post, user) }}
45 +
46 + <em class="username">
47 + <a href="{{ url ('post', hash_id=post.hashId) }}">
48 + <time title="{{ post.created|title }}" datetime="{{ post.created|datetime }}">
49 + {{ post.created|ago }}
50 + </time>
51 + </a>
52 + </em>
53 + by
54 + <a href="{{ url ('user_public', username=post.username) }}">
55 + {{ post.username }}
56 + </a>
57 +
58 + <a href="{{ url ('post', hash_id=post.hashId) }}#comments">
59 + {% if post.commentsCount > 0 %}
60 + {{ post.commentsCount }} comments
61 + {% else %}
62 + discuss
63 + {% endif %}
64 + </a>
65 + </div>
66 + </div>
67 + </div>
68 +
69 + {% endfor %}
70 +
71 + <form class="pagination">
72 + {% for key, value in request.query.items () %}
73 + <input type="hidden" name="{{ key }}" value="{{ value }}" />
74 + {% endfor %}
75 +
76 + {% if page_number > 0 %}
77 + <button type="submit"
78 + name="page"
79 + value="{{ page_number - 1 }}"
80 + class="button button_default1">
81 + Previous
82 + </button>
83 + {% endif %}
84 +
85 + {% if page_number > 0 %}
86 + <span class="page_number">
87 + PAGE: {{ page_number }}
88 + </span>
89 + {% endif %}
90 +
91 + {% if posts %}
92 + <button type="submit"
93 + name="page"
94 + value="{{ page_number + 1 }}"
95 + class="button button_default1">
96 + Next
97 + </button>
98 + {% endif %}
99 + </form>
100 + </div>

+22/-65 M   freepost/templates/search.html
index a849397..a53f7d3
old size: 2K - new size: 1011B
@@ -1,3 +1,5 @@
1 + {% from 'vote.html' import vote %}
2 +
1 3 {% extends 'layout.html' %}
2 4
3 5 {# Set variables for base layour #}
@@ -6,71 +8,26 @@
6 8
7 9 {% block content %}
8 10
9 - <div class="search">
10 - <form action="/search">
11 - <p>
12 - <input type="text" name="q" value="{{ query if query else '' }}" placeholder="Search..." required="required" autofocus />
13 - <input type="submit" value="Search" />
14 - </p>
15 - <p>
16 - Order by
17 - <label>
18 - <input type="radio" name="order" value="newest" {{ 'checked' if order == 'newest' }}>
19 - Most recent
20 - </label>
21 - <label>
22 - <input type="radio" name="order" value="points" {{ 'checked' if order == 'points' }}>
23 - Points
24 - </label>
25 - </p>
26 - </form>
27 - </div>
28 -
29 - <div class="posts">
30 -
31 - {% for post in results %}
32 -
33 - <div class="post">
34 - <div class="title">
35 - {% if post.link and post.link|length > 0 %}
36 - <a href="{{ post.link }}">
37 - {{ post.title }}
38 - </a>
39 - {% else %}
40 - <a href="{{ url ('post', hash_id=post.hashId) }}">
41 - {{ post.title }}
42 - </a>
43 - {% endif %}
44 - </div>
45 -
46 - <div class="info">
47 - <em>
48 - <a href="{{ url ('post', hash_id=post.hashId) }}">
49 - <time title="{{ post.created|title }}" datetime="{{ post.created|datetime }}">
50 - {{ post.created|ago }}
51 - </time>
52 - </a>
53 - </em>
54 - by
55 - <a href="{{ url ('user_public', username=post.username) }}">
56 - {{ post.username }}
57 - </a>
58 -
59 - <a href="{{ url ('post', hash_id=post.hashId) }}#comments">
60 - {{ post.commentsCount if post.commentsCount else '' }} comments
61 - </a>
62 - </div>
63 - </div>
64 -
65 - {% endfor %}
66 -
67 - {# Add once I'll have fulltext search
68 - <div class="more">
69 - <a href="?page={{ page + 1 }}" class="button button_default1">
70 - More
71 - </a>
11 + <div class="search">
12 + <form action="/search">
13 + <p>
14 + <input type="text" name="q" value="{{ request.query.q or '' }}" placeholder="Search..." required="required" />
15 + <input type="submit" value="Search" />
16 + </p>
17 + <p>
18 + Order by
19 + <label>
20 + <input type="radio" name="sort" value="newest" {{ 'checked' if not request.query.sort or request.query.sort == 'newest' }}>
21 + Most recent
22 + </label>
23 + <label>
24 + <input type="radio" name="sort" value="points" {{ 'checked' if request.query.sort == 'points' }}>
25 + Points
26 + </label>
27 + </p>
28 + </form>
72 29 </div>
73 - #}
74 - </div>
30 +
31 + {% include 'posts.html' %}
75 32
76 33 {% endblock %}