Skip to content

Commit

Permalink
Merge pull request #51 from AceFire6/jethro/add/date_filtering
Browse files Browse the repository at this point in the history
Add date filtering
  • Loading branch information
AceFire6 authored Sep 18, 2018
2 parents 7cc5e12 + 4743215 commit 7d8e476
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 30 deletions.
9 changes: 8 additions & 1 deletion ordering/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from . import app
from .constants import WIKIPEDIA
from .utils import _get_bool


def url_form(episode_name):
Expand Down Expand Up @@ -29,9 +30,15 @@ def inject_oldest_first_url():

@app.context_processor
def inject_newest_first():
return {'newest_first': request.url.endswith('/newest_first/')}
newest_first = request.args.get('newest_first', default=False, type=_get_bool)
return {'newest_first': newest_first}


@app.context_processor
def inject_show_dict():
return {'series_map': app.config['SHOW_DICT_WITH_NAMES']}


@app.context_processor
def inject_show_list():
return {'show_list': app.config['SHOW_DICT']}
2 changes: 1 addition & 1 deletion ordering/static/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ a.btn {
display:inline;
}

thead {
#episode-list thead {
color: #FFF;
background-color: #333333;
}
Expand Down
15 changes: 14 additions & 1 deletion ordering/static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,20 @@
} else {
enableColours();
}
})
});

$('.date-picker').daterangepicker({
autoUpdateInput: false,
showDropdowns: true,
minDate: '2012-10-10',
singleDatePicker: true,
locale: {
format: 'YYYY-MM-DD',
cancelLabel: 'Clear'
}
}).on('apply.daterangepicker', function(ev, picker) {
$(this).val(picker.startDate.format('YYYY-MM-DD'));
});
};

$(document).ready(function() {
Expand Down
61 changes: 44 additions & 17 deletions ordering/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
{% assets 'css_all' %}
<link rel="stylesheet" href="{{ ASSET_URL }}">
{% endassets %}
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
</head>

<body>
Expand All @@ -38,26 +39,50 @@ <h1>Arrowverse Episode Order</h1>
<div class="row">

<div class="col-11">
<form id="hide-shows-form" method="get" action="{{ url_for('index') }}">
<input name="newest_first" value="{{ newest_first }}" title="Newest First" type="hidden">
<select id="show-filter-select"
title="Filter options"
name="hide_show"
multiple="multiple">
{% for show_id, show_data in show_list.items() %}
<option value="{{ show_id }}" {% if show_id in hidden_show_list %}selected{% endif %}>
{{ show_data.name }}
</option>
{% endfor %}
</select>
<form action="{{ url_for('index') }}" id="filter-form" method="get" class="row form-inline">
<div class="col-7">
<input name="newest_first" value="{{ newest_first }}" title="Newest First" type="hidden">
<select id="show-filter-select"
title="Filter options"
name="hide_show"
multiple="multiple"
class="form-control" >
{% for show_id, show_data in show_list.items() %}
<option value="{{ show_id }}" {% if show_id in hidden_show_list %}selected{% endif %}>
{{ show_data.name }}
</option>
{% endfor %}
</select>
</div>

<div class="col">
<div class="row">
<div class="col-6">
<input title="From Date"
class="form-control date-picker"
type="text"
name="from_date"
id="from-date"
{% if from_date %}value="{{ from_date }}"{% endif %}
placeholder="From Date">
</div>
<div class="col-6">
<input title="To Date"
class="form-control date-picker"
type="text"
name="to_date"
id="to-date"
{% if to_date %}value="{{ to_date }}"{% endif %}
placeholder="To Date">
</div>
</div>
</div>
</form>
</div>

<div class="col">
<button type="submit" id="filter-button" class="btn btn-outline-secondary" form="hide-shows-form">
Filter
</button>
</div>
<button type="submit" id="filter-button" class="btn btn-outline-secondary" form="filter-form">
Filter
</button>
</div>
</div>
</div>
Expand Down Expand Up @@ -186,6 +211,8 @@ <h5 class="modal-title">Contact</h5>
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
{% assets 'js_all' %}
<script src="{{ ASSET_URL }}"></script>
{% endassets %}
Expand Down
38 changes: 32 additions & 6 deletions ordering/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from concurrent.futures import as_completed, ThreadPoolExecutor
from datetime import datetime
from dateutil.parser import parse as parse_date_string
from operator import itemgetter
import re

Expand Down Expand Up @@ -58,7 +59,7 @@ def get_episode_list(series_soup, series):
date = row[-1]
reference = re.search(r'\[\d+\]$', row[-1])
date = date[:reference.start()] if reference else date
row[-1] = air_date = datetime.strptime(date, '%B %d, %Y')
row[-1] = air_date = datetime.strptime(date, '%B %d, %Y').date()
except ValueError:
continue

Expand Down Expand Up @@ -124,7 +125,7 @@ def _handle_john_con_noir_episode(episode_list):
break


def sort_episodes(show_list_set):
def sort_and_filter_episodes(show_list_set, from_date=None, to_date=None):
full_list = []
shows_in_list = []

Expand All @@ -147,13 +148,28 @@ def sort_episodes(show_list_set):
if CONSTANTINE in shows_in_list:
_handle_john_con_noir_episode(full_list)

filtered_list = _filter_on_air_date(full_list, from_date, to_date)

count = 0
for row in full_list:
for row in filtered_list:
count += 1
row['row_number'] = count
row['air_date'] = f'{row["air_date"]:%B %d, %Y}'

return full_list
return filtered_list


def _filter_on_air_date(episode_list, from_date, to_date):
if not from_date and not to_date:
return episode_list

if from_date:
episode_list = [episode for episode in episode_list if episode['air_date'] >= from_date]

if to_date:
episode_list = [episode for episode in episode_list if episode['air_date'] <= to_date]

return episode_list


@app.cache.memoize(timeout=TWELVE_HOURS)
Expand All @@ -168,7 +184,7 @@ def get_show_list_from_show_html(show_name, show_html):


@app.cache.memoize(timeout=TWELVE_HOURS)
def get_full_series_episode_list(excluded_series=None):
def get_full_series_episode_list(excluded_series=None, from_date=None, to_date=None):
excluded_series = [] if excluded_series is None else excluded_series
shows_to_get = [show for show in app.config['SHOWS'] if show['id'] not in excluded_series]
show_lists = []
Expand All @@ -186,11 +202,21 @@ def get_full_series_episode_list(excluded_series=None):
show_list = get_show_list_from_show_html(show_name, show_html)
show_lists.append(show_list)

return sort_episodes(show_lists)
return sort_and_filter_episodes(show_lists, from_date=from_date, to_date=to_date)


def _get_bool(arg):
if isinstance(arg, bool):
return arg

return arg == 'True'


def _get_date(arg):
if isinstance(arg, datetime):
return arg

if arg is None:
return None

return parse_date_string(arg, dayfirst=True).date()
14 changes: 10 additions & 4 deletions ordering/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import render_template, request

from . import app
from .utils import _get_bool, get_full_series_episode_list
from .utils import _get_bool, _get_date, get_full_series_episode_list


@app.route('/', methods=['GET'])
Expand All @@ -10,16 +10,22 @@ def index():

newest_first = request.args.get('newest_first', default=False, type=_get_bool)
hide_shows_list = request.args.getlist('hide_show')
from_date = request.args.get('from_date', default=None, type=_get_date)
to_date = request.args.get('to_date', default=None, type=_get_date)

episode_list = get_full_series_episode_list(excluded_series=hide_shows_list)
episode_list = get_full_series_episode_list(
excluded_series=hide_shows_list,
from_date=from_date,
to_date=to_date,
)

if newest_first:
episode_list = episode_list[::-1]

context['table_content'] = episode_list
context['show_list'] = app.config['SHOW_DICT']
context['hidden_show_list'] = hide_shows_list
context['newest_first'] = newest_first
context['from_date'] = from_date
context['to_date'] = to_date

return render_template('index.html', **context)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jsmin==2.2.2
lesscpy==0.13.0
MarkupSafe==1.0
ply==3.11
python-dateutil==2.7.3
redis==2.10.6
requests==2.19.1
six==1.11.0
Expand Down
1 change: 1 addition & 0 deletions requirements_to_freeze.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cssmin
dateutil
beautifulsoup4
Flask
Flask-Assets
Expand Down

0 comments on commit 7d8e476

Please sign in to comment.