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

Highlight certain dates in the calendar datepicker #259

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1608805
gwsumm.js: allow highlighting of selected dates
mattpitkin Apr 25, 2019
b86dd26
bootstrap.py: allow calendar to take selected dates
mattpitkin Apr 25, 2019
494feef
gwsumm.js: only add selected dates if array length is greater than 0
mattpitkin Apr 25, 2019
ce7c087
core.py: attempt to parse selected dates from calendar ini file section
mattpitkin Apr 25, 2019
8704e18
Some changes to the highlighted dates option
mattpitkin Apr 26, 2019
b4ec533
core.py: fix typo
mattpitkin Apr 26, 2019
6e4d7d9
a variety of fixes
mattpitkin Apr 26, 2019
c147007
Add option for highlighting available dates
mattpitkin Apr 26, 2019
638975f
Minor fixes
mattpitkin Apr 26, 2019
a1951da
Add PHP directory lister
mattpitkin Apr 26, 2019
1a721be
Some fixes to setup.py
mattpitkin Apr 26, 2019
52e5f83
Get rid of PHP and just create file listing the available directories…
mattpitkin Apr 27, 2019
d3b4805
Do not highlight today if highlighting selected dates
mattpitkin Apr 27, 2019
319a196
gwsumm.js: minor fix
mattpitkin Apr 27, 2019
d93b394
bootstrap.py: shorted to elif as suggested by @alurban
mattpitkin Apr 27, 2019
16e0e22
core.py: swap try except for using default value from pop as suggeste…
mattpitkin Apr 27, 2019
866afde
Update the unit test of calendar
mattpitkin Apr 27, 2019
01dcdef
test_bootstrap.py: fix calendar unit tests
mattpitkin Apr 27, 2019
b510807
core.py: fix indent
mattpitkin Apr 28, 2019
8f88e87
gwsumm.js: strip whitespace from date list, and remove unncessary con…
mattpitkin Apr 29, 2019
3eb6ed4
Make sure all tabs can see the file that lists date directories
mattpitkin Apr 29, 2019
e7ce564
Fix calendar test
mattpitkin Apr 29, 2019
c97d4c1
gw_summary: make sure directories are sorted
mattpitkin Apr 30, 2019
282a058
gw_summary: minor fix of directory listing
mattpitkin May 1, 2019
5ac29a1
static.py: Remove blank line
mattpitkin May 1, 2019
d0cebb9
test_bootstrap.py: minor formatting change
mattpitkin May 1, 2019
83523c4
Set the file containing the list of highlighted dates in the config p…
mattpitkin May 1, 2019
19dd9f8
test_bootstrap.py: fix test_calendar
mattpitkin May 1, 2019
1dfeae7
core.py: fix typo
mattpitkin May 1, 2019
60261ee
Update bin/gw_summary
May 2, 2019
224cbef
gw_summary: check if config already contains a date-file
mattpitkin May 2, 2019
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
19 changes: 19 additions & 0 deletions bin/gw_summary
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,25 @@ os.chdir(opts.output_dir)
plotdir = os.path.join(path, 'plots')
mkdir(plotdir)

# add file containing a list of directories if required
if (mode.get_mode().is_calendar() and
config.has_option("calendar", "highlight-available")):
# directory to list
dirpath = os.path.split(path)[0]
datedirs = sorted([thisdir for thisdir in os.listdir(dirpath)
if os.path.isdir(os.path.join(dirpath, thisdir))])
dirfile = os.path.join(dirpath, 'list-dirs.txt')
alurban marked this conversation as resolved.
Show resolved Hide resolved
if not config.has_option('calendar', 'date-file'):
config.set('calendar', 'date-file')
with open(dirfile, 'w') as fp:
fp.write(','.join(datedirs))
else:
if not os.path.isfile(config.get('calendar', 'date-file')):
raise parser.error("The file '{}' supplied to 'date-file' in the "
"[calendar] section option does not "
"exist.".format(config.get('calendar',
'date-file')))

# -----------------------------------------------------------------------------
# Setup

Expand Down
23 changes: 20 additions & 3 deletions gwsumm/html/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class option for <p>


def calendar(date, tag='a', class_='navbar-brand dropdown-toggle',
id_='calendar', dateformat=None, mode=None):
id_='calendar', dateformat=None, mode=None,
highlighteddates=None, datefile=None):
"""Construct a bootstrap-datepicker calendar.

Parameters
Expand All @@ -85,6 +86,15 @@ def calendar(date, tag='a', class_='navbar-brand dropdown-toggle',
active date for the calendar
tag : `str`
type of enclosing HTML tag, default: ``<a>``
highlighteddates: `str`
a string containing a comma seperated list of dates in the format
``yyyymmdd`` or ``yyyy-mm-dd`` to highlight in the calendar.
Non-highlighted dates are disabled.
datefile: `str`
the name of a file containing a comma seperated list of dates in the
format ``yyyymmdd`` to highlight in the calendar. Non-highlighted dates
are disabled. The `highlighteddates` option takes precedence over this
one.

Returns
-------
Expand All @@ -109,9 +119,16 @@ def calendar(date, tag='a', class_='navbar-brand dropdown-toggle',
page = markup.page()
page.a('&laquo;', class_='navbar-brand step-back', title='Step back',
onclick='stepDate(-1)')
attributekwargs = {'data-date': data_date,
'data-date-format': 'dd-mm-yyyy',
'data-viewmode': '%ss' % mode.name}
if highlighteddates is not None:
attributekwargs['highlight-dates'] = highlighteddates.replace('-', '')
elif datefile is not None:
# set location of the dates file
attributekwargs['highlight-available-dates'] = datefile
page.a(id_=id_, class_=class_, title='Show/hide calendar',
**{'data-date': data_date, 'data-date-format': 'dd-mm-yyyy',
'data-viewmode': '%ss' % mode.name})
**attributekwargs)
page.add(datestring)
page.b('', class_='caret')
page.a.close()
Expand Down
27 changes: 18 additions & 9 deletions gwsumm/html/tests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# global variables
DATE = datetime.strptime('20140410', '%Y%m%d')
CALENDAR = """<a class="navbar-brand step-back" title="Step back" onclick="stepDate(-1)">&laquo;</a>
<a id="calendar" class="navbar-brand dropdown-toggle" title="Show/hide calendar" data-date="10-04-2014" data-date-format="dd-mm-yyyy" data-viewmode="{}">
<a id="calendar" class="navbar-brand dropdown-toggle" title="Show/hide calendar" data-date="10-04-2014" data-date-format="dd-mm-yyyy" data-viewmode="{}" {} {}>
{}
<b class="caret"></b>
</a>
Expand All @@ -51,16 +51,25 @@ def test_banner():
'<div class="banner">\n<h1 class=\"test\">Test</h1>\n'
'<p class=\"subtest\">Subtest</p>\n</div>')

@pytest.mark.parametrize('mode, datefmt', [
('day', 'April 10 2014'),
('week', 'Week of April 10 2014'),
('month', 'April 2014'),
('year', '2014'),
@pytest.mark.parametrize('mode, datefmt, highlighteddates, datefile', [
('day', 'April 10 2014', '2014-04-10', None),
('week', 'Week of April 10 2014', None, 'list-dirs.txt'),
('month', 'April 2014', '20140410,20140412', None),
('year', '2014', '20140410,20140412', 'list-dirs.txt'),
])
def test_calendar(mode, datefmt):
cal = bootstrap.calendar(DATE, mode=mode)
def test_calendar(mode, datefmt, highlighteddates, datefile):
cal = bootstrap.calendar(DATE, mode=mode,
highlighteddates=highlighteddates,
datefile=datefile)
hdcheck = ""
hacheck = ""
if highlighteddates is not None:
hdcheck = 'highlight-dates="{}"'.format(
highlighteddates.replace('-', ''))
elif datefile is not None:
hacheck = 'highlight-available-dates="list-dirs.txt"'
assert parse_html(str(cal)) == parse_html(CALENDAR.format(
'%ss' % mode, datefmt))
'%ss' % mode, hdcheck, hacheck, datefmt))


def test_calendar_no_mode():
Expand Down
23 changes: 22 additions & 1 deletion gwsumm/tabs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,21 @@ def from_ini(cls, cp, section)
except KeyError:
kwargs['duration'] = cp.getfloat(section, 'duration')

# check for calendar dates to highlight
if cp.has_section('calendar'):
if cp.has_option('calendar', 'highlighted-dates'):
try:
kwargs['highlighteddates']
except KeyError:
kwargs['highlighteddates'] = cp.get('calendar', 'highlighted-dates')
elif (cp.has_option('calendar', 'highlight-available') and
cp.has_option('calendar', 'date-file')):
if cp.getboolean('calendar', 'highlight-available'):
try:
kwargs['datefile']
except KeyError:
kwargs['datefile'] = cp.get('calendar', 'date-file')

return cls(name, *args, **kwargs)

# -- HTML operations ------------------------
Expand Down Expand Up @@ -788,6 +803,10 @@ def __init__(self, *args, **kwargs):
% (type(self).__name__, mode))
else:
span = (start, end)

self.highlighteddates = kwargs.pop('highlighteddates', None)
self.datefile = kwargs.pop('datefile', None)

self.span = span
super(IntervalTab, self).__init__(*args, **kwargs)

Expand All @@ -810,7 +829,9 @@ def html_calendar(self):
"format including %r for archive calendar"
% (self.path, requiredpath))
# format calendar
return html.calendar(date, mode=self.mode)
return html.calendar(date, mode=self.mode,
highlighteddates=self.highlighteddates,
datefile=self.datefile)

def html_navbar(self, brand=None, calendar=True, **kwargs):
"""Build the navigation bar for this `Tab`.
Expand Down
35 changes: 33 additions & 2 deletions share/js/gwsumm.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ function shortenDate() {

// When document is ready, run this stuff:
$(window).load(function() {
// get directories if required
var selected_dates = new Array();
if ( document.getElementById('calendar').hasAttribute('highlight-available-dates') ){
var geturl = document.getElementById('calendar').getAttribute('highlight-available-dates');
var request = new XMLHttpRequest();
request.onload = function() {
if ( this.status != 404 ){
selected_dates = this.responseText.replace(/^\s+|\s+$/g, '').split(',');
}
};
request.open("GET", geturl, false); // use synchronous request
request.send();
}

// shorten the date
if ($('#calendar').length){ shortenDate();}
Expand All @@ -190,8 +203,26 @@ $(window).load(function() {
$('#calendar').datepicker({
weekStart: 1,
endDate: moment().utc().format('DD/MM/YYYY'),
todayHighlight: true,
todayBtn: "linked"
todayHighlight: ( !document.getElementById('calendar').hasAttribute('highlight-dates') && !document.getElementById('calendar').hasAttribute('highlight-available-dates') ),
todayBtn: "linked",
beforeShowDay: function(date) {
var calendar_date = date.getUTCFullYear() + ('0'+(date.getMonth()+1)).slice(-2) + ('0'+date.getDate()).slice(-2);
if ( document.getElementById('calendar').hasAttribute('highlight-dates') || document.getElementById('calendar').hasAttribute('highlight-available-dates') ){
// highlight selected dates if given
if ( document.getElementById('calendar').hasAttribute('highlight-dates') ){
selected_dates = document.getElementById('calendar').getAttribute('highlight-dates').split(',');
}
if (selected_dates.length > 0 ){
if ( selected_dates.indexOf(calendar_date) == -1 ){
// disable dates that are not given
return {enabled: false, tooltip: 'Date not available'};
}
else{
return {classes: 'highlighted', enabled: true};
}
}
}
}
}).on('changeDate', move_to_date);

// load correct run type
Expand Down