Skip to content

Commit

Permalink
Moved job filter computation inside inner function
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibhas committed Apr 25, 2016
1 parent ab29db4 commit d926450
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion hasjob/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ <h2>What these jobs pay per annum</h2>
{%- if not paginated -%}
{% from "macros.html" import filters_setup_script %}
{%- if request.is_xhr -%}
{{ filters_setup_script(job_location_filters, job_type_filters, job_category_filters) }}
{{ filters_setup_script(job_filters) }}
<script type="text/javascript">
// This is a temporary fix that is needed until browsers start recognizing the 'title'
// parameter for pushState and replaceState
Expand Down
2 changes: 1 addition & 1 deletion hasjob/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
}
</script>
{%- if not paginated -%}
{{ filters_setup_script(job_location_filters, job_type_filters, job_category_filters) }}
{{ filters_setup_script(job_filters) }}
{%- endif %}
{%- endblock %}

Expand Down
30 changes: 16 additions & 14 deletions hasjob/templates/macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,20 @@ <h1 itemprop="name">{{ headline or post.headline }}</h1>
{{ campaign_header_postscript() }}
{% endmacro %}

{% macro filters_setup_script(job_location_filters, job_type_filters, job_category_filters) %}
{%- if (job_location_filters is defined) and (job_type_filters is defined) and (job_category_filters is defined) %}
<script type="text/javascript">
window.Hasjob.Config.jobsArchive = {{request.args.get('archive') | tojson}},
window.Hasjob.Config.jobLocationFilters = {{job_location_filters | tojson}},
window.Hasjob.Config.jobTypeFilters = {{job_type_filters | tojson}},
window.Hasjob.Config.jobCategoryFilters = {{job_category_filters | tojson}},
window.Hasjob.Config.selectedLocations = {{request.args.getlist('l') | tojson}},
window.Hasjob.Config.selectedTypes = {{request.args.getlist('t') | tojson}},
window.Hasjob.Config.selectedCategories = {{request.args.getlist('c') | tojson}},
window.Hasjob.Config.selectedQuery = {{request.args.getlist('q') | tojson}},
window.Hasjob.Config.selectedEquity = {{request.args.get('e') | tojson}}
</script>
{%- endif %}
{% macro filters_setup_script(job_filters) %}
{% with all_jobs_filters=job_filters() %}
{%- if (all_jobs_filters.job_location_filters is defined) and (all_jobs_filters.job_type_filters is defined) and (all_jobs_filters.job_category_filters is defined) %}
<script type="text/javascript">
window.Hasjob.Config.jobsArchive = {{request.args.get('archive') | tojson}},
window.Hasjob.Config.jobLocationFilters = {{all_jobs_filters.job_location_filters | tojson}},
window.Hasjob.Config.jobTypeFilters = {{all_jobs_filters.job_type_filters | tojson}},
window.Hasjob.Config.jobCategoryFilters = {{all_jobs_filters.job_category_filters | tojson}},
window.Hasjob.Config.selectedLocations = {{request.args.getlist('l') | tojson}},
window.Hasjob.Config.selectedTypes = {{request.args.getlist('t') | tojson}},
window.Hasjob.Config.selectedCategories = {{request.args.getlist('c') | tojson}},
window.Hasjob.Config.selectedQuery = {{request.args.getlist('q') | tojson}},
window.Hasjob.Config.selectedEquity = {{request.args.get('e') | tojson}}
</script>
{%- endif %}
{%- endwith %}
{% endmacro %}
24 changes: 14 additions & 10 deletions hasjob/views/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,13 +770,17 @@ def inject_filter_options():
# Don't compute filter choices for paginated results
if index_is_paginated():
return dict()
basequery = getposts(showall=True, order=False, limit=False)
filters = g.get('event_data', {}).get('filters', {})
cache_key = 'jobfilters/' + (g.board.name + '/' if g.board else '') + hashlib.sha1(repr(filters)).hexdigest()
result = cache.get(cache_key)
if not result:
result = dict(job_location_filters=filter_locations(g.board, filters),
job_type_filters=filter_types(basequery, board=g.board, filters=filters),
job_category_filters=filter_categories(basequery, board=g.board, filters=filters))
cache.set(cache_key, result, timeout=3600)
return result

def get_job_filters():
basequery = getposts(showall=True, order=False, limit=False)
filters = g.get('event_data', {}).get('filters', {})
cache_key = 'jobfilters/' + (g.board.name + '/' if g.board else '') + hashlib.sha1(repr(filters)).hexdigest()
result = cache.get(cache_key)
if not result:
result = dict(job_location_filters=filter_locations(g.board, filters),
job_type_filters=filter_types(basequery, board=g.board, filters=filters),
job_category_filters=filter_categories(basequery, board=g.board, filters=filters))
cache.set(cache_key, result, timeout=3600)
return result

return dict(job_filters=get_job_filters)

0 comments on commit d926450

Please sign in to comment.