diff --git a/larigira/config.py b/larigira/config.py index 8c8031a..de00d11 100644 --- a/larigira/config.py +++ b/larigira/config.py @@ -29,6 +29,7 @@ def get_conf(prefix='LARIGIRA_'): conf['LOG_CONFIG'] = False conf['TMPDIR'] = os.getenv('TMPDIR', '/tmp/') conf['FILE_PATH_SUGGESTION'] = () # tuple of paths + conf['UI_CALENDAR_FREQUENCY_THRESHOLD'] = 4*60*60 # 4 hours conf.update(from_envvars(prefix=prefix)) return conf diff --git a/larigira/dbadmin/__init__.py b/larigira/dbadmin/__init__.py index 54fb763..fd1ba7a 100644 --- a/larigira/dbadmin/__init__.py +++ b/larigira/dbadmin/__init__.py @@ -4,14 +4,19 @@ This module contains a flask blueprint for db administration stuff Templates are self-contained in this directory ''' from __future__ import print_function +from datetime import datetime, timedelta, time +from collections import defaultdict +import itertools from flask import current_app, Blueprint, render_template, jsonify, abort, \ request, redirect, url_for from larigira.entrypoints_utils import get_avail_entrypoints from larigira.audiogen import get_audiogenerator -from larigira.timegen import get_timegenerator +from larigira.timegen_every import FrequencyAlarm +from larigira.timegen import get_timegenerator, timegenerate from larigira import forms +from larigira.config import get_conf from .suggestions import get_suggestions db = Blueprint('db', __name__, url_prefix='/db', template_folder='templates') @@ -34,6 +39,38 @@ def events_list(): return render_template('list.html', events=events) +@db.route('/calendar') +def events_calendar(): + model = current_app.larigira.controller.monitor.model + today = datetime.now().date() + maxdays = 30 + days = defaultdict(lambda: defaultdict(list)) + freq_threshold = get_conf()['UI_CALENDAR_FREQUENCY_THRESHOLD'] + for alarm in model.get_all_alarms(): + print('al', alarm.get('nick', alarm)) + if freq_threshold and alarm['kind'] == 'frequency' and \ + FrequencyAlarm(alarm).interval < freq_threshold: + continue + actions = tuple(model.get_actions_by_alarm(alarm)) + if not actions: + continue + t = datetime.fromtimestamp(int(today.strftime('%s'))) + for t in timegenerate(alarm, now=t, howmany=maxdays): + print(' t', t) + if t is None or \ + t > datetime.combine(today+timedelta(days=maxdays), time()): + break + days[t.date()][t].append((alarm, actions)) + + print(days.keys()) + weeks = defaultdict(list) + for d in sorted(days.keys()): + weeks[d.isocalendar()[:2]].append(d) + print(weeks) + + return render_template('calendar.html', days=days, weeks=weeks) + + @db.route('/add/time') def addtime(): kinds = get_avail_entrypoints('larigira.timeform_create') diff --git a/larigira/dbadmin/templates/calendar.html b/larigira/dbadmin/templates/calendar.html new file mode 100644 index 0000000..6981279 --- /dev/null +++ b/larigira/dbadmin/templates/calendar.html @@ -0,0 +1,59 @@ +{% extends "dbadmin_base.html" %} +{%block scripts%} +{{super()}} + +{%endblock%} +{% block styles%} +{{super()}} + +{% endblock %} + + +{% block title %}Larigira - Calendar{%endblock%} + +{% block content %} +
+ {% for week in weeks %} +
+ {% for day in weeks[week] %} +

{{day}}

+ {% for t in days[day] %} +
+ +
    + {% for alarm, actions in days[day][t] %} +
  • {{alarm.nick}}
  • + {% endfor %} +
+
+ {%endfor %} +
+ {% endfor %} +
+
+ {%endfor %} +
+{% endblock content %} +{# vim: set ts=2 sw=2 noet: #} diff --git a/larigira/dbadmin/templates/dbadmin_base.html b/larigira/dbadmin/templates/dbadmin_base.html index 6a0a11f..7a9ccf4 100644 --- a/larigira/dbadmin/templates/dbadmin_base.html +++ b/larigira/dbadmin/templates/dbadmin_base.html @@ -40,6 +40,7 @@ $(function() {
  • List
  • Add time
  • Add audio
  • +
  • Calendar
  • {# collapse #}