Refactor routing and the way of running the development server #13

Merged
gergely merged 1 commits from refactor into master 2018-07-02 09:02:27 +00:00

View File

@ -15,6 +15,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
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,12 +74,21 @@ class CalendarSocialApp(Flask):
self.context_processor(template_vars)
for attr_name in self.__dir__():
attr = getattr(self, attr_name)
app = CalendarSocialApp(__name__)
if not callable(attr):
continue
args, kwargs = getattr(attr, 'routing', (None, None))
@app.route('/')
def hello():
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:
@ -79,9 +98,8 @@ def hello():
return render_template('index.html', calendar=calendar)
@app.route('/register', methods=['POST', 'GET'])
def register():
@route('/register', methods=['POST', 'GET'])
def register(self):
if not current_app.config['REGISTRATION_ENABLED']:
return render_template('registration-disabled.html')
@ -102,10 +120,9 @@ def register():
return render_template('registration.html', form=form)
@app.route('/new-event', methods=['GET', 'POST'])
@login_required
def new_event():
@route('/new-event', methods=['GET', 'POST'])
@login_required
def new_event(self):
from .forms import EventForm
from .models import db, Event
@ -121,3 +138,6 @@ def new_event():
return redirect(url_for('hello'))
return render_template('event-edit.html', form=form)
app = CalendarSocialApp(__name__)