Refactor routing and the way of running the development server #13
@ -15,6 +15,7 @@
|
|||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from functools import wraps
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from flask import Flask, current_app, redirect, render_template, url_for
|
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):
|
class CalendarSocialApp(Flask):
|
||||||
def __init__(self, name, config=None):
|
def __init__(self, name, config=None):
|
||||||
from .models import db, User, Role
|
from .models import db, User, Role
|
||||||
@ -64,12 +74,21 @@ class CalendarSocialApp(Flask):
|
|||||||
|
|
||||||
self.context_processor(template_vars)
|
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('/')
|
if args is None:
|
||||||
def hello():
|
continue
|
||||||
|
|
||||||
|
self.route(*args, **kwargs)(attr)
|
||||||
|
|
||||||
|
@route('/')
|
||||||
|
def hello(self):
|
||||||
from .calendar_system.gregorian import GregorianCalendar
|
from .calendar_system.gregorian import GregorianCalendar
|
||||||
|
|
||||||
if not current_user.is_authenticated:
|
if not current_user.is_authenticated:
|
||||||
@ -79,9 +98,8 @@ def hello():
|
|||||||
|
|
||||||
return render_template('index.html', calendar=calendar)
|
return render_template('index.html', calendar=calendar)
|
||||||
|
|
||||||
|
@route('/register', methods=['POST', 'GET'])
|
||||||
@app.route('/register', methods=['POST', 'GET'])
|
def register(self):
|
||||||
def register():
|
|
||||||
if not current_app.config['REGISTRATION_ENABLED']:
|
if not current_app.config['REGISTRATION_ENABLED']:
|
||||||
return render_template('registration-disabled.html')
|
return render_template('registration-disabled.html')
|
||||||
|
|
||||||
@ -102,10 +120,9 @@ def register():
|
|||||||
|
|
||||||
return render_template('registration.html', form=form)
|
return render_template('registration.html', form=form)
|
||||||
|
|
||||||
|
@route('/new-event', methods=['GET', 'POST'])
|
||||||
@app.route('/new-event', methods=['GET', 'POST'])
|
|
||||||
@login_required
|
@login_required
|
||||||
def new_event():
|
def new_event(self):
|
||||||
from .forms import EventForm
|
from .forms import EventForm
|
||||||
from .models import db, Event
|
from .models import db, Event
|
||||||
|
|
||||||
@ -121,3 +138,6 @@ def new_event():
|
|||||||
return redirect(url_for('hello'))
|
return redirect(url_for('hello'))
|
||||||
|
|
||||||
return render_template('event-edit.html', form=form)
|
return render_template('event-edit.html', form=form)
|
||||||
|
|
||||||
|
|
||||||
|
app = CalendarSocialApp(__name__)
|
||||||
|
Loading…
Reference in New Issue
Block a user