-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from matagus/update-template-tag-to-use-modern…
…-django-stuff Refactored template tag to be a simple_tag
- Loading branch information
Showing
4 changed files
with
13 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,29 @@ | ||
""" | ||
Several useful template tags! | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from django import template | ||
from django.db.models import Model | ||
from django.db.models import QuerySet | ||
|
||
from generic_links import utils | ||
|
||
|
||
register = template.Library() | ||
|
||
|
||
class RelatedLinksNode(template.Node): | ||
def __init__(self, context_var, obj, is_external): | ||
self.context_var = context_var | ||
self.obj_var = template.Variable(obj) | ||
self.is_external = is_external | ||
@register.simple_tag | ||
def get_links_for(obj: Model, is_external: bool | None = None) -> QuerySet: | ||
""" | ||
Usage: | ||
def render(self, context): | ||
obj = self.obj_var.resolve(context) | ||
context[self.context_var] = utils.get_links_for(obj).select_related("user").filter(is_external=self.is_external) | ||
return "" | ||
{% get_links_for <obj> as <some_var> %} | ||
or | ||
@register.tag | ||
def get_links_for(parser, token): | ||
""" | ||
Usage: {% get_links_for <obj> as <some_var> %} | ||
{% get_links_for <obj> <boolean> as <some_var> %} | ||
""" | ||
|
||
bits = token.split_contents() | ||
|
||
if len(bits) != 4: | ||
message = "'%s' tag requires three arguments" % bits[0] | ||
raise template.TemplateSyntaxError(message) | ||
|
||
return RelatedLinksNode(bits[3], bits[1], True) | ||
return utils.get_links_for(obj, is_external=is_external) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
Generated by Cookiecutter Django Package | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import os | ||
|