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/>.
|
||||
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user