From 6c4eb97c01fcb31ea0e3066fb44a726e948065d6 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 25 Jun 2018 09:01:13 +0200 Subject: [PATCH] Create a basic month view --- app/__init__.py | 7 ++- app/calendar_system/__init__.py | 15 ++++++ app/calendar_system/gregorian.py | 48 +++++++++++++++++++ app/templates/index.html | 2 + app/templates/month-view.html | 82 ++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 app/calendar_system/__init__.py create mode 100644 app/calendar_system/gregorian.py create mode 100644 app/templates/month-view.html diff --git a/app/__init__.py b/app/__init__.py index 2af7eef..51c440b 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from datetime import datetime import os from flask import Flask, current_app, redirect, render_template, url_for @@ -69,10 +70,14 @@ app = CalendarSocialApp(__name__) @app.route('/') def hello(): + from app.calendar_system.gregorian import GregorianCalendar + if not current_user.is_authenticated: return render_template('welcome.html') - return render_template('index.html') + calendar = GregorianCalendar(datetime.utcnow().timestamp()) + + return render_template('index.html', calendar=calendar) @app.route('/register', methods=['POST', 'GET']) diff --git a/app/calendar_system/__init__.py b/app/calendar_system/__init__.py new file mode 100644 index 0000000..a5dda87 --- /dev/null +++ b/app/calendar_system/__init__.py @@ -0,0 +1,15 @@ +class CalendarSystem: + __has_months__ = False + __has_weeks__ = False + + @property + def day_names(self): + raise NotImplementedError() + + @property + def month(self): + raise NotImplementedError() + + @property + def days(self): + raise NotImplementedError() diff --git a/app/calendar_system/gregorian.py b/app/calendar_system/gregorian.py new file mode 100644 index 0000000..3dc5b2d --- /dev/null +++ b/app/calendar_system/gregorian.py @@ -0,0 +1,48 @@ +from datetime import datetime, timedelta +from flask_babel import lazy_gettext as _ + +from . import CalendarSystem + + +class GregorianCalendar(CalendarSystem): + __name__ = _('Gregorian') + + __has_months__ = True + + START_DAY = 0 + END_DAY = 7 + + day_names = ( + _('Monday'), + _('Tuesday'), + _('Wednesday'), + _('Thursday'), + _('Friday'), + _('Saturday'), + _('Sunday'), + ) + + def __init__(self, timestamp): + self.timestamp = datetime.utcfromtimestamp(timestamp) + + @property + def month(self): + return self.timestamp.strftime("%B, %Y") + + @property + def days(self): + day_list = [] + + start_day = self.timestamp.replace(day=1) + + while start_day.weekday() > self.START_DAY: + start_day -= timedelta(days=1) + + day_list.append(start_day) + current_day = start_day + + while current_day.weekday() < self.END_DAY and current_day.month <= self.timestamp.month: + current_day += timedelta(days=1) + day_list.append(current_day) + + return day_list diff --git a/app/templates/index.html b/app/templates/index.html index f446a60..bc4246d 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -2,4 +2,6 @@ {% block content %} {{ _('Welcome to Calendar.social, %(username)s!', username=current_user.username) }} + +{% include 'month-view.html' %} {% endblock content %} diff --git a/app/templates/month-view.html b/app/templates/month-view.html new file mode 100644 index 0000000..792d06c --- /dev/null +++ b/app/templates/month-view.html @@ -0,0 +1,82 @@ + + + + + + + +{% for day in calendar.day_names %} + +{% endfor %} + + + +{% set markers = namespace() -%} +{% set markers.first = true -%} +{% for day in calendar.days -%} + {% if loop.index0 % (calendar.day_names) | length == 0 -%} + {% if not markers.first %} + + {%- else %} + {%- set markers.first = false %} + {%- endif %} + + {%- endif %} + +{% endfor %} + + +
{{ calendar.month }}
+ {{ day }} +
+ {{ day.day }} + {% if day.date() == calendar.timestamp.date() %} +
+ This is a task +
+ {% endif %} +