diff --git a/bin/gw_summary b/bin/gw_summary index ae8b7241..bbef59ee 100644 --- a/bin/gw_summary +++ b/bin/gw_summary @@ -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') + 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 diff --git a/gwsumm/html/bootstrap.py b/gwsumm/html/bootstrap.py index 641ac680..73f2ec87 100644 --- a/gwsumm/html/bootstrap.py +++ b/gwsumm/html/bootstrap.py @@ -76,7 +76,8 @@ class option for

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 @@ -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: ```` + 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 ------- @@ -109,9 +119,16 @@ def calendar(date, tag='a', class_='navbar-brand dropdown-toggle', page = markup.page() page.a('«', 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() diff --git a/gwsumm/html/tests/test_bootstrap.py b/gwsumm/html/tests/test_bootstrap.py index 1c7e688b..438588b9 100644 --- a/gwsumm/html/tests/test_bootstrap.py +++ b/gwsumm/html/tests/test_bootstrap.py @@ -31,7 +31,7 @@ # global variables DATE = datetime.strptime('20140410', '%Y%m%d') CALENDAR = """« - + {} @@ -51,16 +51,25 @@ def test_banner(): '

') -@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(): diff --git a/gwsumm/tabs/core.py b/gwsumm/tabs/core.py index da5783ea..53b03290 100644 --- a/gwsumm/tabs/core.py +++ b/gwsumm/tabs/core.py @@ -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 ------------------------ @@ -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) @@ -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`. diff --git a/share/js/gwsumm.js b/share/js/gwsumm.js index 8f5c4adb..c87a5dff 100644 --- a/share/js/gwsumm.js +++ b/share/js/gwsumm.js @@ -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();} @@ -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