Skip to content

Commit

Permalink
tag list handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazarii committed Nov 5, 2023
1 parent d97f30e commit 2e99a40
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
14 changes: 13 additions & 1 deletion web/api/v1/blog/filters.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
from functools import reduce
from operator import and_

from django.db.models import Q
from django_filters import rest_framework as filters

from main.filters import ListCharFilter


class ArticleFilter(filters.FilterSet):
search = filters.CharFilter(method='search_filter')
tags = ListCharFilter(method='tags_filter')

def search_filter(self, queryset, name, value):
def search_filter(self, queryset, name: str, value: str):
return queryset.filter(Q(title__icontains=value) | Q(content__icontains=value))

def tags_filter(self, queryset, name: str, value: list[str]):
tag_queries = [Q(tags__slug=tag) for tag in value]
# Use the reduce function to combine the Q objects with the 'and' operator
combined_query = reduce(and_, tag_queries)
return queryset.filter(combined_query)
26 changes: 16 additions & 10 deletions web/blog/static/blog/js/list.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$(function () {
apiRequest()
articleListRequest()

})
let requestedNewPage = false
Expand All @@ -11,17 +11,24 @@ $(window).scroll(function () {
if (nextUrl && nextUrl != 'None'){
requestedNewPage = true
console.log(nextUrl)
apiRequest()
articleListRequest()
}
}
});


function apiRequest() {
function articleListRequest() {
let pagination = $('#pagination')
const urlParams = new URLSearchParams(window.location.search);
const endpoint = pagination.attr('data-href-next')

const url = new URL(endpoint, window.location.origin);

urlParams.forEach((value, key) => url.searchParams.set(key, value))

$.ajax({
type: "GET",
url: pagination.attr('data-href-next'),
url: url,
success: function (data) {
if (data.results) {
appendUrls(pagination,data.next, data.previous)
Expand Down Expand Up @@ -78,11 +85,10 @@ const articleTemplate = (article) => {


function newArticlesRender(data, pagination) {
for (let article of data.results) {
const template = articleTemplate(article)
pagination.append(template);
$('.tag-list').click(tagClickHandler)

}
const template = data.results.map((article) => articleTemplate(article)).join('');
pagination.empty();
pagination.append(template);
$('.tag-list').unbind();
$('.tag-list').click(tagClickHandler);
return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
<h4><i class="fa fa-tags"></i> Popular Tags:</h4>
<div class="row">
<div class="col-lg-6">
<ul class="list-unstyled" id="tagList">
{% for tag in popular_tags %}
<li><a href=""><span class="badge badge-info">{{ tag.name }}</span></a></li>
{% endfor %}
</ul>
<ul class="list-unstyled" id="tagList"></ul>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion web/blog/templates/blog/post_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{% block container %}
<div class="row">
<div id="pagination" data-href-next="{% url 'api:v1:blog:post-list' %}" class="col-lg-8"></div>
<div id="pagination" data-href-next="/api/v1/blog/articles/?page_size=5" class="col-lg-8"></div>
{% include 'blog/includes/right_sidebar.html' %}
</div>
{% endblock container %}
Expand Down

0 comments on commit 2e99a40

Please sign in to comment.