Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Maják
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TO
Maják
Commits
d2207489
Commit
d2207489
authored
Mar 16, 2023
by
Tomáš Valenta
Browse files
Options
Downloads
Patches
Plain Diff
pagination for search results
parent
2667f758
No related branches found
No related tags found
2 merge requests
!741
Release
,
!740
Add homepage article searching
Pipeline
#12322
passed
Apr 12, 2023
Stage: build
Changes
2
Pipelines
5
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
main/models.py
+47
-20
47 additions, 20 deletions
main/models.py
main/templates/main/main_article_search.html
+96
-52
96 additions, 52 deletions
main/templates/main/main_article_search.html
with
143 additions
and
72 deletions
main/models.py
+
47
−
20
View file @
d2207489
...
@@ -436,9 +436,10 @@ class MainArticlesPage(
...
@@ -436,9 +436,10 @@ class MainArticlesPage(
return
article_data_list
return
article_data_list
def
get_context
(
self
,
request
,
*
args
,
**
kwargs
):
def
get_context
(
self
,
request
,
get_articles
:
bool
=
True
,
*
args
,
**
kwargs
):
ctx
=
super
().
get_context
(
request
,
args
,
kwargs
)
ctx
=
super
().
get_context
(
request
,
args
,
kwargs
)
if
get_articles
:
article_timeline_list
=
self
.
get_article_data_list
(
1
)
article_timeline_list
=
self
.
get_article_data_list
(
1
)
ctx
[
"
article_timeline_list
"
]
=
article_timeline_list
ctx
[
"
article_timeline_list
"
]
=
article_timeline_list
ctx
[
"
show_next_timeline_articles
"
]
=
MainArticlePage
.
objects
.
filter
(
ctx
[
"
show_next_timeline_articles
"
]
=
MainArticlePage
.
objects
.
filter
(
...
@@ -488,6 +489,28 @@ class MainArticlesPage(
...
@@ -488,6 +489,28 @@ class MainArticlesPage(
}
}
return
JsonResponse
(
data
=
data
,
safe
=
False
)
return
JsonResponse
(
data
=
data
,
safe
=
False
)
def
get_all_articles_search_response
(
self
,
request
):
article_paginator
=
Paginator
(
MainArticlePage
.
objects
.
order_by
(
"
-date
"
)
.
live
()
.
search
(
request
.
GET
[
"
q
"
]),
10
,
)
article_page
=
article_paginator
.
get_page
(
request
.
GET
.
get
(
"
page
"
,
1
))
context
=
{
"
article_data_list
"
:
article_page
.
object_list
}
html_content
=
render
(
request
,
"
main/includes/person_article_preview.html
"
,
context
).
content
data
=
{
"
html
"
:
html_content
.
decode
(
"
utf-8
"
),
"
has_next
"
:
article_page
.
has_next
(),
}
return
JsonResponse
(
data
=
data
,
safe
=
False
)
@cached_property
@cached_property
def
search_url
(
self
):
def
search_url
(
self
):
return
self
.
url
+
self
.
reverse_subpage
(
"
search
"
)
return
self
.
url
+
self
.
reverse_subpage
(
"
search
"
)
...
@@ -497,22 +520,23 @@ class MainArticlesPage(
...
@@ -497,22 +520,23 @@ class MainArticlesPage(
if
request
.
method
==
"
GET
"
and
"
q
"
in
request
.
GET
:
if
request
.
method
==
"
GET
"
and
"
q
"
in
request
.
GET
:
query
=
request
.
GET
[
"
q
"
]
query
=
request
.
GET
[
"
q
"
]
# Show 30 top results for the time being
article_results
=
(
results
=
(
MainArticlePage
MainArticlePage
.
objects
.
objects
.
order_by
(
"
-date
"
)
.
live
()
.
live
()
.
search
(
query
)
.
search
(
query
)
[:
30
]
[:
11
]
)
)
return
render
(
return
render
(
request
,
request
,
"
main/main_article_search.html
"
,
"
main/main_article_search.html
"
,
{
{
**
self
.
get_context
(
request
),
**
self
.
get_context
(
request
,
get_articles
=
False
),
"
query
"
:
query
,
"
query
"
:
query
,
"
results
"
:
results
,
"
article_results
"
:
article_results
[:
10
],
"
show_more_articles
"
:
len
(
article_results
)
>
10
,
"
sub_heading
"
:
f
"
Vyhledávání „
{
query
}
“
"
"
sub_heading
"
:
f
"
Vyhledávání „
{
query
}
“
"
}
}
)
)
...
@@ -524,7 +548,10 @@ class MainArticlesPage(
...
@@ -524,7 +548,10 @@ class MainArticlesPage(
if
"
months
"
in
request
.
GET
:
if
"
months
"
in
request
.
GET
:
return
self
.
get_timeline_articles_response
(
request
)
return
self
.
get_timeline_articles_response
(
request
)
if
"
page
"
in
request
.
GET
:
if
"
page
"
in
request
.
GET
:
if
"
q
"
not
in
request
.
GET
:
return
self
.
get_articles_response
(
request
)
return
self
.
get_articles_response
(
request
)
else
:
return
self
.
get_all_articles_search_response
(
request
)
return
super
().
serve
(
request
,
*
args
,
**
kwargs
)
return
super
().
serve
(
request
,
*
args
,
**
kwargs
)
...
...
This diff is collapsed.
Click to expand it.
main/templates/main/main_article_search.html
+
96
−
52
View file @
d2207489
...
@@ -48,8 +48,30 @@
...
@@ -48,8 +48,30 @@
</div>
</div>
</div>
</div>
<section
class=
"mb-3 xl:mb-14"
>
<section
class=
"mb-3 xl:mb-14"
>
{% if results %}
{% if article_results %}
{% include 'main/includes/person_article_preview.html' with article_data_list=results %}
<div
id=
"searchArticleResultWrapper"
>
{% include 'main/includes/person_article_preview.html' with article_data_list=article_results %}
</div>
{% if show_more_articles %}
<div
class=
"grid-container"
>
<div
class=
"grid-content-with-right-side"
>
<a
onclick=
"showMoreArticles(event, this)"
href=
"#"
data-url=
"{{ page_url }}"
data-page=
"2"
data-query=
"{{ query }}"
class=
"btn btn--black btn--to-yellow-500 btn--hoveractive uppercase"
>
<span
class=
"btn__body-wrap"
>
<span
class=
"btn__body text-lg lg:text-base"
>
Zobrazit další
</span>
</span>
</a>
</div>
</div>
{% endif %}
{% else %}
{% else %}
<div
class=
"grid-container article-section mb-8"
>
<div
class=
"grid-container article-section mb-8"
>
<div
class=
"grid-full"
>
<div
class=
"grid-full"
>
...
@@ -61,4 +83,26 @@
...
@@ -61,4 +83,26 @@
{% include "main/includes/newsletter_section.html" %}
{% include "main/includes/newsletter_section.html" %}
</div>
</div>
</main>
</main>
<script
type=
"text/javascript"
>
async
function
showMoreArticles
(
event
,
btn
)
{
event
.
preventDefault
()
let
searchArticleResultWrapper
=
document
.
getElementById
(
'
searchArticleResultWrapper
'
)
let
url
=
(
btn
.
getAttribute
(
'
data-url
'
)
+
`?page=
${
btn
.
getAttribute
(
'
data-page
'
)}
`
+
`&q=
${
btn
.
getAttribute
(
'
data-query
'
)}
`
)
const
response
=
await
fetch
(
url
,
{
method
:
"
GET
"
,
headers
:
{
"
X-Requested-With
"
:
"
XMLHttpRequest
"
},
})
const
data
=
await
response
.
json
()
searchArticleResultWrapper
.
innerHTML
+=
data
.
html
;
if
(
!
data
.
has_next
)
{
btn
.
style
.
display
=
'
none
'
;
}
btn
.
setAttribute
(
'
data-page
'
,
parseInt
(
btn
.
getAttribute
(
'
data-page
'
))
+
1
)
}
</script>
{% endblock content %}
{% endblock content %}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment