2011-07-23 18:45:40 +02:00
|
|
|
from functools import wraps
|
2011-09-11 22:11:36 +02:00
|
|
|
import inspect
|
|
|
|
|
2011-07-23 18:45:40 +02:00
|
|
|
from flask import redirect, url_for, session, request
|
2011-08-21 22:35:01 +02:00
|
|
|
from werkzeug.routing import HTTPException, RoutingException
|
2011-07-23 18:45:40 +02:00
|
|
|
|
|
|
|
from models import Bill, Project
|
|
|
|
from forms import BillForm
|
|
|
|
|
2011-08-10 19:47:06 +02:00
|
|
|
def get_billform_for(project, set_default=True):
|
|
|
|
"""Return an instance of BillForm configured for a particular project.
|
|
|
|
|
|
|
|
:set_default: if set to True, on GET methods (usually when we want to
|
|
|
|
display the default form, it will call set_default on it.
|
|
|
|
|
|
|
|
"""
|
2011-07-23 18:45:40 +02:00
|
|
|
form = BillForm()
|
2011-08-10 12:59:30 +02:00
|
|
|
form.payed_for.choices = form.payer.choices = [(str(m.id), m.name) for m in project.active_members]
|
2011-08-10 19:47:06 +02:00
|
|
|
form.payed_for.default = [str(m.id) for m in project.active_members]
|
|
|
|
|
|
|
|
if set_default and request.method == "GET":
|
|
|
|
form.set_default()
|
2011-07-23 18:45:40 +02:00
|
|
|
return form
|
|
|
|
|
2011-08-21 22:35:01 +02:00
|
|
|
class Redirect303(HTTPException, RoutingException):
|
|
|
|
"""Raise if the map requests a redirect. This is for example the case if
|
|
|
|
`strict_slashes` are activated and an url that requires a trailing slash.
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-08-21 22:35:01 +02:00
|
|
|
The attribute `new_url` contains the absolute destination url.
|
2011-07-23 18:45:40 +02:00
|
|
|
"""
|
2011-08-21 22:35:01 +02:00
|
|
|
code = 303
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-08-21 22:35:01 +02:00
|
|
|
def __init__(self, new_url):
|
|
|
|
RoutingException.__init__(self, new_url)
|
|
|
|
self.new_url = new_url
|
|
|
|
|
|
|
|
def get_response(self, environ):
|
|
|
|
return redirect(self.new_url, 303)
|
2011-09-11 22:11:36 +02:00
|
|
|
|
|
|
|
def for_all_methods(decorator):
|
|
|
|
"""Apply a decorator to all the methods of a class"""
|
|
|
|
def decorate(cls):
|
|
|
|
for name, method in inspect.getmembers(cls, inspect.ismethod):
|
|
|
|
setattr(cls, name, decorator(method))
|
|
|
|
return cls
|
|
|
|
return decorate
|
|
|
|
|