Skip to content

Commit

Permalink
add template tag. yet untested
Browse files Browse the repository at this point in the history
  • Loading branch information
knipknap committed Nov 24, 2018
1 parent bf44991 commit 75a5501
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
13 changes: 7 additions & 6 deletions django_find/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,26 +244,27 @@ def by_fullnames(cls, fullnames):
return primary_cls.objects.values_list(*selectors)

@classmethod
def dom_from_query(cls, query):
def dom_from_query(cls, query, aliases=None):
if not aliases:
aliases = cls.get_aliases()
fields = {}
aliases = cls.get_aliases()
for alias in aliases:
fields[alias] = cls.__name__+'.'+alias
query_parser = QueryParser(fields, aliases)
return query_parser.parse(query)

@classmethod
def q_from_query(cls, query):
def q_from_query(cls, query, aliases=None):
"""
Returns a Q-Object for the given query.
"""
dom = cls.dom_from_query(query)
dom = cls.dom_from_query(query, aliases)
serializer = DjangoSerializer(cls)
return dom.serialize(serializer)

@classmethod
def by_query(cls, query):
return cls.objects.filter(cls.q_from_query(query))
def by_query(cls, query, aliases=None):
return cls.objects.filter(cls.q_from_query(query, aliases))

@classmethod
def sql_from_query(cls, query, mode='SELECT', fullnames=None, extra_model=None):
Expand Down
8 changes: 8 additions & 0 deletions django_find/templates/django_find/form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% load i18n %}
<form action="" method="GET">
{% for key, value in getvars.iteritems %}
<input type="hidden" name="{{key}}" value="{{value}}"></input>
{% endfor %}
<input type="text" class="search-text" name="search" value="{{getvars.q}}"></input>
<input type="submit" class="search-button" value="{% trans 'Find' %}"></input>
</form>
37 changes: 37 additions & 0 deletions django_find/templatetags/searchtags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from __future__ import absolute_import, print_function
from django import template
from django.template.loader import render_to_string
from ..search import raw_search

class SearchNode(template.Node):
def __init__(self, model_var, fields):
self.model_var = template.Variable(model_var)
self.fields = fields

def render(self, context):
request = context['request']
getvars = request.GET.copy()

if 'q' in getvars:
# Search, and store the resulting queryset in the current
# context.
query = getvars['q']
model_name = self.model_var.var
model = self.model_var.resolve(context)
queryset = model.by_query(query, self.fields)
context['result_set'] = queryset

return render_to_string('django_find/form.html',
{'getvars': getvars})

def do_search(parser, token):
contents = token.split_contents()
if len(contents) < 2:
raise template.TemplateSyntaxError(
"%r tag requires at least 1 argument, " +
"in the form of {%% %r model [alias1 alias2 ...] %%}" % contents[0])

return SearchNode(contents[1], contents[2:])

register = template.Library()
register.tag('search', do_search)

0 comments on commit 75a5501

Please sign in to comment.