Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date filter to search results #338

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions nyaa/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _generate_query_string(term, category, filter, user):


def search_elastic(term='', user=None, sort='id', order='desc',
category='0_0', quality_filter='0', page=1,
category='0_0', quality_filter='0', date_filter='', page=1,
rss=False, admin=False, logged_in_user=None,
per_page=75, max_search_results=1000):
# This function can easily be memcached now
Expand Down Expand Up @@ -136,6 +136,10 @@ def search_elastic(term='', user=None, sort='id', order='desc',
default_operator="AND",
query=term)

if date_filter:
date_min, date_max = date_filter.replace('-00', '-01').split(' - ')[:2]
s = s.filter('range', created_time={'gte': date_min, 'lte': date_max})

# User view (/user/username)
if user:
s = s.filter('term', uploader_id=user)
Expand Down Expand Up @@ -227,7 +231,7 @@ def wrapper(*args, **kwargs):


def search_db(term='', user=None, sort='id', order='desc', category='0_0',
quality_filter='0', page=1, rss=False, admin=False,
quality_filter='0', date_filter='', page=1, rss=False, admin=False,
logged_in_user=None, per_page=75):
sort_keys = {
'id': models.Torrent.id,
Expand Down Expand Up @@ -354,6 +358,12 @@ def search_db(term='', user=None, sort='id', order='desc', category='0_0',
qpc.filter((models.Torrent.main_category_id == main_cat_id) &
(models.Torrent.sub_category_id == sub_cat_id))

if date_filter:
date_min, date_max = date_filter.replace('-00', '-01').split(' - ')[:2]
date_min += ' 00:00:00'
date_max += ' 23:59:59'
qpc.filter((models.Torrent.created_time <= date_max) & (models.Torrent.created_time >= date_min))

if filter_tuple:
qpc.filter(models.Torrent.flags.op('&')(
int(filter_tuple[0])).is_(filter_tuple[1]))
Expand Down
16 changes: 16 additions & 0 deletions nyaa/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ $(document).ready(function() {
$(this).blur().children('i').toggleClass('fa-folder-open fa-folder');
$(this).next().stop().slideToggle(250);
});

// Date range picker
$(function() {
var el = $('input[name="d"');
el.daterangepicker({
"linkedCalendars": false,
"autoUpdateInput": true,
"startDate": el.attr('value') ? el.attr('value') : moment().subtract(6, 'days'),
"endDate": el.attr('value') ? el.attr('value').replace(/.* - /, '') : moment(),
"autoApply": true,
"locale": {
"format": "YYYY-MM-DD",
"separator": " - "
},
});
});
});

function _format_time_difference(seconds) {
Expand Down
5 changes: 5 additions & 0 deletions nyaa/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<script src="{{ static_cachebuster('js/bootstrap-select.js') }}"></script>
<script src="{{ static_cachebuster('js/main.js') }}"></script>

<!-- Include Date Range Picker -->
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use cloudflare's CDNJS with an integrity check, no need to contact even more domains

<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css" />

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
Expand Down
18 changes: 18 additions & 0 deletions nyaa/templates/search_results.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@
{% endif %}
{% endif %}

{% if 'q' in request.args %}
<div class="row">
<div class="col-md-3">
<form action="{{ url_for('main.home') }}" method="get" style="display:flex">
{% for arg, value in request.args.items() %}
{% if arg != 'd' %}
<input type="hidden" name="{{ arg }}" value="{{ value }}"/>
{% endif %}
{% endfor %}
<input class="form-control" type="text" name="d" value="{{ request.args['d'] }}">
<button class="btn btn-primary">
<i class="fa fa-calendar fa-fw"></i>
</button>
</form>
</div>
</div>
{% endif %}

{% if (use_elastic and torrent_query.hits.total > 0) or (torrent_query.items) %}
<div class="table-responsive">
<table class="table table-bordered table-hover table-striped torrent-list">
Expand Down
5 changes: 5 additions & 0 deletions nyaa/views/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def home(rss):

category = chain_get(req_args, 'c', 'cats')
quality_filter = chain_get(req_args, 'f', 'filter')
date_filter = chain_get(req_args, 'd', 'date')

user_name = chain_get(req_args, 'u', 'user')
page_number = chain_get(req_args, 'p', 'page', 'offset')
Expand All @@ -67,6 +68,9 @@ def home(rss):

results_per_page = app.config.get('RESULTS_PER_PAGE', DEFAULT_PER_PAGE)

if not re.match(r'^\d{4}-\d{2}-\d{2} - \d{4}-\d{2}-\d{2}$', str(date_filter)):
date_filter = ''

user_id = None
if user_name:
user = models.User.by_username(user_name)
Expand Down Expand Up @@ -100,6 +104,7 @@ def home(rss):
'order': sort_order or 'desc',
'category': category or '0_0',
'quality_filter': quality_filter or '0',
'date_filter': date_filter,
'page': page_number,
'rss': render_as_rss,
'per_page': results_per_page
Expand Down