from django.core.urlresolvers import reverse
from django.template.defaultfilters import urlencode
from django import template

from dynamiq.utils import get_advanced_search_formset_class

from ..forms import MEPSearchForm, MEPSearchAdvancedFormset

register = template.Library()


@register.simple_tag
def simple_search_shortcut(search_string):
    """
    Return a simple search URL from a search string, like "daniel OR country:CZ".
    """
    base_url = reverse("search")
    return "%s?q=%s" % (base_url, urlencode(search_string))


@register.inclusion_tag('blocks/search_form.html', takes_context=True)
def render_search_form(context):
    """
    Display the search form, if a `dynamiq` key is on the context, it will
    used, otherwise, it create an empty form
    """
    if 'dynamiq' in context:
        dynamiq = context['dynamiq']
    else:
        request = context['request']
        formset_class = get_advanced_search_formset_class(request.user, MEPSearchAdvancedFormset, MEPSearchForm)
        formset = formset_class(None)
        dynamiq = {
                "q": "",
                "label": "",
                "formset": formset,
            }
    return {
        'dynamiq': dynamiq
    }

from django.core.urlresolvers import reverse
from django.template.defaultfilters import urlencode
from django import template

from dynamiq.utils import get_advanced_search_formset_class

from ..forms import MEPSearchForm, MEPSearchAdvancedFormset

register = template.Library()


@register.simple_tag
def simple_search_shortcut(search_string, sort=None):
    """
    Return a simple search URL from a search string, like "daniel OR country:CZ".
    """
    base_url = reverse("search")
    query_string = "q=%s" % urlencode(search_string)
    if sort:
        query_string = "%s&sort=%s" % (query_string, sort)
    return "%s?%s" % (base_url, query_string)


@register.inclusion_tag('blocks/search_form.html', takes_context=True)
def render_search_form(context):
    """
    Display the search form, if a `dynamiq` key is on the context, it will
    used, otherwise, it create an empty form
    """
    if 'dynamiq' in context:
        dynamiq = context['dynamiq']
    else:
        request = context['request']
        formset_class = get_advanced_search_formset_class(request.user, MEPSearchAdvancedFormset, MEPSearchForm)
        formset = formset_class(None)
        dynamiq = {
                "q": "",
                "label": "",
                "formset": formset,
            }
    return {
        'dynamiq': dynamiq
    }

Make possible to define a sort in simple_search_shortcut tt
[enh] Make possible to define a sort in simple_search_shortcut tt

  from django.core.urlresolvers import reverse
  from django.template.defaultfilters import urlencode
  from django import template
  
  from dynamiq.utils import get_advanced_search_formset_class
  
  from ..forms import MEPSearchForm, MEPSearchAdvancedFormset
  
  register = template.Library()
  
  
  @register.simple_tag
- def simple_search_shortcut(search_string):
+ def simple_search_shortcut(search_string, sort=None):
      """
      Return a simple search URL from a search string, like "daniel OR country:CZ".
      """
      base_url = reverse("search")
+     query_string = "q=%s" % urlencode(search_string)
+     if sort:
+         query_string = "%s&sort=%s" % (query_string, sort)
-     return "%s?q=%s" % (base_url, urlencode(search_string))
+     return "%s?%s" % (base_url, query_string)
  
  
  @register.inclusion_tag('blocks/search_form.html', takes_context=True)
  def render_search_form(context):
      """
      Display the search form, if a `dynamiq` key is on the context, it will
      used, otherwise, it create an empty form
      """
      if 'dynamiq' in context:
          dynamiq = context['dynamiq']
      else:
          request = context['request']
          formset_class = get_advanced_search_formset_class(request.user, MEPSearchAdvancedFormset, MEPSearchForm)
          formset = formset_class(None)
          dynamiq = {
                  "q": "",
                  "label": "",
                  "formset": formset,
              }
      return {
          'dynamiq': dynamiq
      }
  
Make possible to define a sort in simple_search_shortcut tt
## Code Before:
from django.core.urlresolvers import reverse
from django.template.defaultfilters import urlencode
from django import template

from dynamiq.utils import get_advanced_search_formset_class

from ..forms import MEPSearchForm, MEPSearchAdvancedFormset

register = template.Library()


@register.simple_tag
def simple_search_shortcut(search_string):
    """
    Return a simple search URL from a search string, like "daniel OR country:CZ".
    """
    base_url = reverse("search")
    return "%s?q=%s" % (base_url, urlencode(search_string))


@register.inclusion_tag('blocks/search_form.html', takes_context=True)
def render_search_form(context):
    """
    Display the search form, if a `dynamiq` key is on the context, it will
    used, otherwise, it create an empty form
    """
    if 'dynamiq' in context:
        dynamiq = context['dynamiq']
    else:
        request = context['request']
        formset_class = get_advanced_search_formset_class(request.user, MEPSearchAdvancedFormset, MEPSearchForm)
        formset = formset_class(None)
        dynamiq = {
                "q": "",
                "label": "",
                "formset": formset,
            }
    return {
        'dynamiq': dynamiq
    }

## Instruction:
Make possible to define a sort in simple_search_shortcut tt
## Code After:
from django.core.urlresolvers import reverse
from django.template.defaultfilters import urlencode
from django import template

from dynamiq.utils import get_advanced_search_formset_class

from ..forms import MEPSearchForm, MEPSearchAdvancedFormset

register = template.Library()


@register.simple_tag
def simple_search_shortcut(search_string, sort=None):
    """
    Return a simple search URL from a search string, like "daniel OR country:CZ".
    """
    base_url = reverse("search")
    query_string = "q=%s" % urlencode(search_string)
    if sort:
        query_string = "%s&sort=%s" % (query_string, sort)
    return "%s?%s" % (base_url, query_string)


@register.inclusion_tag('blocks/search_form.html', takes_context=True)
def render_search_form(context):
    """
    Display the search form, if a `dynamiq` key is on the context, it will
    used, otherwise, it create an empty form
    """
    if 'dynamiq' in context:
        dynamiq = context['dynamiq']
    else:
        request = context['request']
        formset_class = get_advanced_search_formset_class(request.user, MEPSearchAdvancedFormset, MEPSearchForm)
        formset = formset_class(None)
        dynamiq = {
                "q": "",
                "label": "",
                "formset": formset,
            }
    return {
        'dynamiq': dynamiq
    }
