From 3a1cdf8f6a5ae3855d724454470ff7e7bcc8813d Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 2 Jul 2018 10:48:10 +0200 Subject: [PATCH] [Refactor] Move app-level views into the CalendarSocialApp class --- calsocial/__init__.py | 130 ++++++++++++++++++++++++------------------ 1 file changed, 75 insertions(+), 55 deletions(-) diff --git a/calsocial/__init__.py b/calsocial/__init__.py index 7697316..4222a9e 100644 --- a/calsocial/__init__.py +++ b/calsocial/__init__.py @@ -15,6 +15,7 @@ # along with this program. If not, see . from datetime import datetime +from functools import wraps import os from flask import Flask, current_app, redirect, render_template, url_for @@ -44,6 +45,15 @@ def template_vars(): } +def route(*args, **kwargs): + def decorator(func): + setattr(func, 'routing', (args, kwargs)) + + return func + + return decorator + + class CalendarSocialApp(Flask): def __init__(self, name, config=None): from .models import db, User, Role @@ -64,60 +74,70 @@ class CalendarSocialApp(Flask): self.context_processor(template_vars) + for attr_name in self.__dir__(): + attr = getattr(self, attr_name) + + if not callable(attr): + continue + + args, kwargs = getattr(attr, 'routing', (None, None)) + + if args is None: + continue + + self.route(*args, **kwargs)(attr) + + @route('/') + def hello(self): + from .calendar_system.gregorian import GregorianCalendar + + if not current_user.is_authenticated: + return render_template('welcome.html') + + calendar = GregorianCalendar(datetime.utcnow().timestamp()) + + return render_template('index.html', calendar=calendar) + + @route('/register', methods=['POST', 'GET']) + def register(self): + if not current_app.config['REGISTRATION_ENABLED']: + return render_template('registration-disabled.html') + + from .forms import RegistrationForm + from .models import db, User + + form = RegistrationForm() + + if form.validate_on_submit(): + # TODO: This might become False later, if we want registrations to be confirmed via E-mail + user = User(active=True) + + form.populate_obj(user) + db.session.add(user) + db.session.commit() + + return redirect(url_for('hello')) + + return render_template('registration.html', form=form) + + @route('/new-event', methods=['GET', 'POST']) + @login_required + def new_event(self): + from .forms import EventForm + from .models import db, Event + + form = EventForm() + + if form.validate_on_submit(): + event = Event(user=current_user) + form.populate_obj(event) + + db.session.add(event) + db.session.commit() + + return redirect(url_for('hello')) + + return render_template('event-edit.html', form=form) + app = CalendarSocialApp(__name__) - - -@app.route('/') -def hello(): - from .calendar_system.gregorian import GregorianCalendar - - if not current_user.is_authenticated: - return render_template('welcome.html') - - calendar = GregorianCalendar(datetime.utcnow().timestamp()) - - return render_template('index.html', calendar=calendar) - - -@app.route('/register', methods=['POST', 'GET']) -def register(): - if not current_app.config['REGISTRATION_ENABLED']: - return render_template('registration-disabled.html') - - from .forms import RegistrationForm - from .models import db, User - - form = RegistrationForm() - - if form.validate_on_submit(): - # TODO: This might become False later, if we want registrations to be confirmed via E-mail - user = User(active=True) - - form.populate_obj(user) - db.session.add(user) - db.session.commit() - - return redirect(url_for('hello')) - - return render_template('registration.html', form=form) - - -@app.route('/new-event', methods=['GET', 'POST']) -@login_required -def new_event(): - from .forms import EventForm - from .models import db, Event - - form = EventForm() - - if form.validate_on_submit(): - event = Event(user=current_user) - form.populate_obj(event) - - db.session.add(event) - db.session.commit() - - return redirect(url_for('hello')) - - return render_template('event-edit.html', form=form)