Skip to content

Commit ce65b33

Browse files
committed
Updated SnippetList new feature.
1 parent 34ea90f commit ce65b33

File tree

11 files changed

+104
-120
lines changed

11 files changed

+104
-120
lines changed

base/main.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from .pagination import PAGE_VAR, Pagination
2+
3+
4+
class ObjectList:
5+
pagination_class = Pagination
6+
7+
def __init__(self, request, model, queryset, list_per_page):
8+
self.model = model
9+
self.queryset = queryset
10+
self.list_per_page = list_per_page
11+
self.params = dict(request.GET.lists())
12+
if PAGE_VAR in self.params:
13+
del self.params[PAGE_VAR]
14+
self.result_objects = self.get_objects(request)
15+
16+
def paginate(self, request, queryset):
17+
pagination = self.pagination_class(
18+
request,
19+
self.model,
20+
queryset,
21+
self.list_per_page,
22+
)
23+
self.pagination = pagination
24+
return pagination.get_objects()
25+
26+
def get_objects(self, request):
27+
paginate_result = self.paginate(request, self.queryset)
28+
return paginate_result

cab/components/components.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django_components import Component, register
2+
from pydantic import BaseModel
3+
4+
from base.main import ObjectList
5+
6+
7+
@register("snippet_list")
8+
class SnippetListComponent(Component):
9+
10+
template_file = "templates/snippet_list.html"
11+
12+
class Kwargs(BaseModel):
13+
snippet_list: ObjectList
14+
model_config = {"arbitrary_types_allowed": True}
15+
16+
def get_template_data(self, args, kwargs, slots, context):
17+
return {
18+
"snippet_list": kwargs.snippet_list.result_objects,
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<section class="border-2 border-base-green-400 rounded-sm" aria-labelledby="snippet-list-title">
2+
<h2 id="snippet-list-title" class="sr-only">Snippet List</h2>
3+
{% for snippet in snippet_list %}
4+
<article class="relative flex flex-col px-8 pt-4 pb-1 border-b-2 border-base-green-400 last:border-b-0 lg:flex-row">
5+
<div class="flex flex-row justify-start gap-3 text-sm md:text-base lg:my-6 lg:w-1/5 lg:flex-col">
6+
<span>{{ snippet.rating_score }} rating</span>
7+
<span>{{ snippet.bookmark_count }} bookmark</span>
8+
</div>
9+
<div class="flex flex-col lg:w-4/5">
10+
<h3 class="py-2 my-0 text-lg md:text-2xl">
11+
<a href="{{ snippet.get_absolute_url }}" class="text-base-green-400 hover:text-base-green-800 underline underline-offset-8 decoration-3 decoration-base-orange-400 hover:decoration-10 duration-500">{{ snippet.title|truncatechars:"80" }}</a>
12+
</h3>
13+
<p class="mb-0 py-4 min-h-[4rem] text-xs md:text-sm">{{ snippet.description|truncatechars:"250" }}</p>
14+
<footer class="flex flex-col flex-wrap py-2 text-xs md:text-sm">
15+
<div class="my-1">
16+
<strong class="text-sm md:text-base mr-2">Tags : </strong>
17+
<ul class="inline-flex gap-2 flex-wrap list-none m-0 text-xs md:text-sm">{% for tag in snippet.tags.all %}<li class="my-2 inline"><a href="{% url 'cab_snippet_matches_tag' tag.slug %}" class="py-1.5 px-2 text-white border-2 bg-base-green-400 border-base-green-400 rounded-lg hover:bg-base-white hover:text-base-green-400 duration-500 transition-colors no-underline">{{ tag.name }}</a></li>{% endfor %}</ul>
18+
</div>
19+
<p class="text-start">
20+
<strong class="text-sm md:text-base">Author : </strong>
21+
<a href="{{ snippet.author.get_absolute_url }}" class="text-base-green-400 underline underline-offset-4 hover:text-base-green-800">{{ snippet.author.username }}</a>
22+
<span class="mx-2" aria-hidden="true"> | </span>
23+
<strong class="text-sm md:text-base">Updated : </strong>
24+
<time>{{ snippet.updated_date }}</time>
25+
</p>
26+
</footer>
27+
</div>
28+
</article>
29+
{% endfor %}
30+
</section>

cab/utils.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.utils.safestring import mark_safe
88
from markdown import markdown as markdown_func
99

10+
from base.main import ObjectList
1011
from base.pagination import Pagination
1112

1213

@@ -35,23 +36,29 @@ def object_list(
3536
hits
3637
number of objects, total
3738
"""
39+
from cab.models import Snippet
40+
3841
if extra_context is None:
3942
extra_context = {}
4043
queryset = queryset._clone()
4144
model = queryset.model
4245
opts = model._meta
4346
if paginate_by:
44-
pagination = Pagination(request, model, queryset, paginate_by)
45-
object_list = pagination.get_objects()
47+
if queryset.model == Snippet:
48+
object_list = ObjectList(request, queryset.model, queryset, 15)
49+
pagination = object_list.pagination
50+
else:
51+
pagination = Pagination(request, model, queryset, paginate_by)
52+
object_list = pagination.get_objects()
4653

4754
context = {
48-
"%s_list" % template_object_name: object_list,
55+
"object_list": object_list,
4956
"pagination": pagination,
5057
"hits": pagination.result_count,
5158
}
5259
else:
5360
context = {
54-
"%s_list" % template_object_name: object_list,
61+
"object_list": queryset,
5562
}
5663
if not allow_empty and len(queryset) == 0:
5764
raise Http404

djangosnippets/templates/base.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</ul>
3636
</nav>
3737
</div></header>
38-
<div id="main">
38+
<div id="main" class="container mb-auto mx-auto w-7/8">
3939
{% block secondary_nav %}
4040
<nav id="subnav">
4141
<ul>
@@ -59,14 +59,16 @@
5959

6060
<div id="base-container">
6161
<h1>{% block content_header %}{% endblock %}</h1>
62-
<div id="content">
62+
{% with current_url_name=request.resolver_match.url_name %}
63+
<div id="content" class="{% if current_url_name == 'home' %}w-1/2{% else %}w-full md:w-3/4{% endif %}">
6364
{% block content %}
6465
{% endblock %}
6566
</div>
66-
<div id="sidebar">
67+
<div id="sidebar" class="{% if current_url_name == 'home' %}w-1/2{% else %}hidden md:block md:w-1/4{% endif %}">
6768
{% block sidebar %}
6869
{% endblock %}
6970
</div>
71+
{% endwith %}
7072
{% block extra_content %}
7173
{% endblock %}
7274
</div>

djangosnippets/templates/cab/partials/most_bookmarked.html

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,11 @@
44

55
<h1>Most bookmarked snippets{% if months %} last {{ months }} months{% endif %}</h1>
66

7-
<div id="content">
7+
<div id="content" class="w-full md:w-3/4">
88

99

1010
{% if object_list %}
11-
<table class="snippet_list">
12-
<thead>
13-
<tr>
14-
<th>Title</th>
15-
<th>Author</th>
16-
<th>Tags</th>
17-
<th>Publication date</th>
18-
<th>Rating</th>
19-
</tr>
20-
</thead>
21-
<tbody>
22-
{% for snippet in object_list %}
23-
<tr>
24-
<td><a href="{{ snippet.get_absolute_url }}">{% if not snippet.title|strip %}Untitled{% else %}{{ snippet.title }}{% endif %}</a></td>
25-
<td><a href="{{ snippet.author.get_absolute_url }}">{{ snippet.author.username }}</a></td>
26-
<td>{% for tag in snippet.tags.all %}<a href="{% url 'cab_snippet_matches_tag' tag.slug %}">{{ tag.name }}</a> {% endfor %}</td>
27-
<td>{{ snippet.pub_date }}</td>
28-
<td><span class="rating-{% if snippet.rating_score >= 0 %}positive{% else %}negative{% endif %}">{% if snippet.rating_score >= 0 %}+{% endif %}{{ snippet.rating_score }}</span></td>
29-
</tr>
30-
{% endfor %}
31-
</tbody>
32-
</table>
11+
{% component 'snippet_list' snippet_list=object_list / %}
3312
{% component "pagination" pagination_obj=pagination / %}
3413
<p class="count">{{ hits }} snippet{{ hits|pluralize }} posted so far.</p>
3514
{% else %}
@@ -38,7 +17,7 @@ <h1>Most bookmarked snippets{% if months %} last {{ months }} months{% endif %}<
3817
</div>
3918

4019

41-
<div id="sidebar">
20+
<div id="sidebar" class="hidden md:block md:w-1/4">
4221
<p>You're looking at the most-bookmarked snippets on the site; {% if user.is_authenticated %}you can help useful snippets show up here by clicking the "bookmark this snippet" link on their pages and ading them to <a href="{% url 'cab_user_bookmarks' %}">your bookmarks</a>{% else %}if you'd like to help useful snippets show up here, <a href="{% url 'account_login' %}">sign up for an account</a> and you'll get your own bookmarks list{% endif %}.</p>
4322

4423
<nav class="filter">

djangosnippets/templates/cab/partials/tag_list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
{% load core_tags %}
33
<h1>All tags</h1>
4-
<div id="content">
4+
<div id="content" class="w-full md:w-3/4">
55
{% if object_list %}
66
<ul>
77
{% for tag in object_list %}

djangosnippets/templates/cab/partials/top_rated.html

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,17 @@
44

55

66
<h1>Top-rated snippets{% if months %} last {{ months }} months{% endif %}</h1>
7-
<div id="content">
7+
<div id="content" class="w-full md:w-3/4">
88
{% if object_list %}
9-
<table class="snippet_list">
10-
<thead>
11-
<tr>
12-
<th>Title</th>
13-
<th>Author</th>
14-
<th>Tags</th>
15-
<th>Publication date</th>
16-
<th>Rating</th>
17-
</tr>
18-
</thead>
19-
<tbody>
20-
{% for snippet in object_list %}
21-
<tr>
22-
<td><a href="{{ snippet.get_absolute_url }}">{% if not snippet.title|strip %}Untitled{% else %}{{ snippet.title }}{% endif %}</a></td>
23-
<td><a href="{{ snippet.author.get_absolute_url }}">{{ snippet.author.username }}</a></td>
24-
<td>{% for tag in snippet.tags.all %}<a href="{% url 'cab_snippet_matches_tag' tag.slug %}">{{ tag.name }}</a> {% endfor %}</td>
25-
<td>{{ snippet.pub_date }}</td>
26-
<td><span class="rating-{% if snippet.rating_score >= 0 %}positive{% else %}negative{% endif %}">{% if snippet.rating_score >= 0 %}+{% endif %}{{ snippet.rating_score }}</span></td>
27-
</tr>
28-
{% endfor %}
29-
</tbody>
30-
</table>
9+
{% component 'snippet_list' snippet_list=object_list / %}
3110
{% component "pagination" pagination_obj=pagination / %}
3211
<p class="count">{{ hits }} snippet{{ hits|pluralize }} posted so far.</p>
3312
{% else %}
3413
<p class="empty">No snippets posted yet.</p>
3514
{% endif %}
3615
</div>
3716

38-
<div id="sidebar">
17+
<div id="sidebar" class="hidden md:block md:w-1/4">
3918
<p>You're looking at the top-rated snippets currently on the site; {% if user.is_authenticated %}if you'd like to contribute, just click the ratings link in the sidebar of a particular snippet's page; you can rate any snippet "useful" or "not useful"{% else %}if you'd like to contribute, <a href="{% url 'account_login' %}">sign up for an account</a> and you'll be able to rate any snippet you see{% endif %}.</p>
4019

4120
<nav class="filter">

djangosnippets/templates/cab/snippet_list.html

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,8 @@
88

99
{% block content %}
1010
{% if object_list %}
11-
<table class="snippet_list">
12-
<thead>
13-
<tr>
14-
<th>Title</th>
15-
<th>Author</th>
16-
<th>Tags</th>
17-
<th>Publication date</th>
18-
<th>Rating</th>
19-
</tr>
20-
</thead>
21-
<tbody>
22-
{% for snippet in object_list %}
23-
<tr>
24-
<td><a href="{{ snippet.get_absolute_url }}">{% if not snippet.title|strip %}Untitled{% else %}{{ snippet.title }}{% endif %}</a></td>
25-
<td><a href="{{ snippet.author.get_absolute_url }}">{{ snippet.author.username }}</a></td>
26-
<td>{% for tag in snippet.tags.all %}<a href="{% url 'cab_snippet_matches_tag' tag.slug %}">{{ tag.name }}</a> {% endfor %}</td>
27-
<td>{{ snippet.pub_date }}</td>
28-
<td><span class="rating-{% if snippet.rating_score >= 0 %}positive{% else %}negative{% endif %}">{% if snippet.rating_score >= 0 %}+{% endif %}{{ snippet.rating_score }}</span></td>
29-
</tr>
30-
{% endfor %}
31-
</tbody>
32-
</table>
33-
{% component "pagination" pagination_obj=pagination / %}
11+
{% component 'snippet_list' snippet_list=object_list / %}
12+
{% component 'pagination' pagination_obj=pagination / %}
3413
<p class="count">{{ hits }} snippet{{ hits|pluralize }} posted so far.</p>
3514
{% else %}
3615
<p class="empty">No snippets posted yet.</p>

djangosnippets/templates/cab/user_bookmarks.html

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,7 @@
88

99
{% block content %}
1010
{% if object_list %}
11-
<table>
12-
<thead>
13-
<tr>
14-
<th>Title</th>
15-
<th>Language</th>
16-
<th>Added</th>
17-
<th></th>
18-
</tr>
19-
</thead>
20-
<tbody>
21-
{% for bookmark in object_list %}
22-
<tr>
23-
<td><a href="{{ bookmark.snippet.get_absolute_url }}">{{ bookmark.snippet.title }}</a></td>
24-
<td><a href="{% url 'cab_language_detail' slug=bookmark.snippet.language.slug %}">{{ bookmark.snippet.language.name }}</a></td>
25-
<td>{{ bookmark.date|timesince }}</td>
26-
<td><a href="{% url 'cab_bookmark_delete' snippet_id=bookmark.snippet.id %}">Delete this bookmark</a></td>
27-
</tr>
28-
{% endfor %}
29-
</tbody>
30-
</table>
31-
11+
{% component 'snippet_list' snippet_list=object_list / %}
3212
{% component "pagination" pagination_obj=pagination / %}
3313
{% else %}
3414
<p>You haven't bookmarked any snippets yet.</p>

0 commit comments

Comments
 (0)