mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-05 20:51:49 +02:00
Show some respect to pep8 and fellow readers.
This commit is contained in:
parent
b5777e0584
commit
eade8519cb
8 changed files with 133 additions and 83 deletions
|
@ -76,7 +76,8 @@ properly.
|
||||||
| Setting name | Default | What does it do? |
|
| Setting name | Default | What does it do? |
|
||||||
+============================+===========================+========================================================================================+
|
+============================+===========================+========================================================================================+
|
||||||
| SQLALCHEMY_DATABASE_URI | ``sqlite:///budget.db`` | Specifies the type of backend to use and its location. More information |
|
| SQLALCHEMY_DATABASE_URI | ``sqlite:///budget.db`` | Specifies the type of backend to use and its location. More information |
|
||||||
| | | on the format used can be found on `the SQLAlchemy documentation`. |
|
| | | on the format used can be found on `the SQLAlchemy documentation |
|
||||||
|
| | | <http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls>`_. |
|
||||||
+----------------------------+---------------------------+----------------------------------------------------------------------------------------+
|
+----------------------------+---------------------------+----------------------------------------------------------------------------------------+
|
||||||
| SECRET_KEY | ``tralala`` | The secret key used to encrypt the cookies. **This needs to be changed**. |
|
| SECRET_KEY | ``tralala`` | The secret key used to encrypt the cookies. **This needs to be changed**. |
|
||||||
+----------------------------+---------------------------+----------------------------------------------------------------------------------------+
|
+----------------------------+---------------------------+----------------------------------------------------------------------------------------+
|
||||||
|
@ -90,8 +91,6 @@ properly.
|
||||||
| | | and copy its output into the value of *ADMIN_PASSWORD*. |
|
| | | and copy its output into the value of *ADMIN_PASSWORD*. |
|
||||||
+----------------------------+---------------------------+----------------------------------------------------------------------------------------+
|
+----------------------------+---------------------------+----------------------------------------------------------------------------------------+
|
||||||
|
|
||||||
.. _`the SQLAlechemy documentation`: http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls
|
|
||||||
|
|
||||||
In a production environment
|
In a production environment
|
||||||
---------------------------
|
---------------------------
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,31 @@
|
||||||
|
# You can find more information about what these settings mean in the
|
||||||
|
# documentation, available online at
|
||||||
|
# http://ihatemoney.readthedocs.io/en/latest/installation.html#configuration
|
||||||
|
|
||||||
|
# Turn this on if you want to have more output on what's happening under the
|
||||||
|
# hood.
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
|
|
||||||
|
# The database URI, reprensenting the type of database and how to connect to it.
|
||||||
|
# Enter an absolute path here.
|
||||||
SQLALCHEMY_DATABASE_URI = 'sqlite://'
|
SQLALCHEMY_DATABASE_URI = 'sqlite://'
|
||||||
SQLACHEMY_ECHO = DEBUG
|
SQLACHEMY_ECHO = DEBUG
|
||||||
|
|
||||||
# Will likely become the default value in flask-sqlalchemy >=3 ; could be removed
|
# Will likely become the default value in flask-sqlalchemy >=3 ; could be removed
|
||||||
# then:
|
# then:
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||||
|
|
||||||
|
# You need to change this secret key, otherwise bad things might happen to your
|
||||||
|
# users.
|
||||||
SECRET_KEY = "tralala"
|
SECRET_KEY = "tralala"
|
||||||
|
|
||||||
|
# A python tuple describing the name and email adress of the sender of the mails.
|
||||||
MAIL_DEFAULT_SENDER = ("Budget manager", "budget@notmyidea.org")
|
MAIL_DEFAULT_SENDER = ("Budget manager", "budget@notmyidea.org")
|
||||||
|
|
||||||
|
# If set to True, a demonstration project will be activated.
|
||||||
ACTIVATE_DEMO_PROJECT = True
|
ACTIVATE_DEMO_PROJECT = True
|
||||||
|
|
||||||
|
# If not empty, the specified password must be entered to create new projects.
|
||||||
|
# DO NOT enter the password in cleartext. Generate a password hash with
|
||||||
|
# "ihatemoney generate_password_hash" instead.
|
||||||
ADMIN_PASSWORD = ""
|
ADMIN_PASSWORD = ""
|
||||||
|
|
|
@ -13,6 +13,7 @@ from jinja2 import Markup
|
||||||
from ihatemoney.models import Project, Person
|
from ihatemoney.models import Project, Person
|
||||||
from ihatemoney.utils import slugify
|
from ihatemoney.utils import slugify
|
||||||
|
|
||||||
|
|
||||||
def get_billform_for(project, set_default=True, **kwargs):
|
def get_billform_for(project, set_default=True, **kwargs):
|
||||||
"""Return an instance of BillForm configured for a particular project.
|
"""Return an instance of BillForm configured for a particular project.
|
||||||
|
|
||||||
|
@ -21,8 +22,9 @@ def get_billform_for(project, set_default=True, **kwargs):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
form = BillForm(**kwargs)
|
form = BillForm(**kwargs)
|
||||||
form.payed_for.choices = form.payer.choices = [(m.id, m.name)
|
active_members = [(m.id, m.name) for m in project.active_members]
|
||||||
for m in project.active_members]
|
|
||||||
|
form.payed_for.choices = form.payer.choices = active_members
|
||||||
form.payed_for.default = [m.id for m in project.active_members]
|
form.payed_for.default = [m.id for m in project.active_members]
|
||||||
|
|
||||||
if set_default and request.method == "GET":
|
if set_default and request.method == "GET":
|
||||||
|
@ -31,7 +33,9 @@ def get_billform_for(project, set_default=True, **kwargs):
|
||||||
|
|
||||||
|
|
||||||
class CommaDecimalField(DecimalField):
|
class CommaDecimalField(DecimalField):
|
||||||
|
|
||||||
"""A class to deal with comma in Decimal Field"""
|
"""A class to deal with comma in Decimal Field"""
|
||||||
|
|
||||||
def process_formdata(self, value):
|
def process_formdata(self, value):
|
||||||
if value:
|
if value:
|
||||||
value[0] = str(value[0]).replace(',', '.')
|
value[0] = str(value[0]).replace(',', '.')
|
||||||
|
@ -49,8 +53,8 @@ class EditProjectForm(FlaskForm):
|
||||||
Returns the created instance
|
Returns the created instance
|
||||||
"""
|
"""
|
||||||
project = Project(name=self.name.data, id=self.id.data,
|
project = Project(name=self.name.data, id=self.id.data,
|
||||||
password=self.password.data,
|
password=self.password.data,
|
||||||
contact_email=self.contact_email.data)
|
contact_email=self.contact_email.data)
|
||||||
return project
|
return project
|
||||||
|
|
||||||
def update(self, project):
|
def update(self, project):
|
||||||
|
@ -70,12 +74,13 @@ class ProjectForm(EditProjectForm):
|
||||||
def validate_id(form, field):
|
def validate_id(form, field):
|
||||||
form.id.data = slugify(field.data)
|
form.id.data = slugify(field.data)
|
||||||
if (form.id.data == "dashboard") or Project.query.get(form.id.data):
|
if (form.id.data == "dashboard") or Project.query.get(form.id.data):
|
||||||
raise ValidationError(Markup(_("The project identifier is used "
|
message = _("The project identifier is used to log in and for the "
|
||||||
"to log in and for the URL of the project. "
|
"URL of the project. "
|
||||||
"We tried to generate an identifier for you but a project "
|
"We tried to generate an identifier for you but a "
|
||||||
"with this identifier already exists. "
|
"project with this identifier already exists. "
|
||||||
"Please create a new identifier "
|
"Please create a new identifier that you will be able "
|
||||||
"that you will be able to remember.")))
|
"to remember")
|
||||||
|
raise ValidationError(Markup(message))
|
||||||
|
|
||||||
|
|
||||||
class AuthenticationForm(FlaskForm):
|
class AuthenticationForm(FlaskForm):
|
||||||
|
@ -104,7 +109,7 @@ class BillForm(FlaskForm):
|
||||||
payer = SelectField(_("Payer"), validators=[Required()], coerce=int)
|
payer = SelectField(_("Payer"), validators=[Required()], coerce=int)
|
||||||
amount = CommaDecimalField(_("Amount paid"), validators=[Required()])
|
amount = CommaDecimalField(_("Amount paid"), validators=[Required()])
|
||||||
payed_for = SelectMultipleField(_("For whom?"),
|
payed_for = SelectMultipleField(_("For whom?"),
|
||||||
validators=[Required()], coerce=int)
|
validators=[Required()], coerce=int)
|
||||||
submit = SubmitField(_("Submit"))
|
submit = SubmitField(_("Submit"))
|
||||||
submit2 = SubmitField(_("Submit and add a new one"))
|
submit2 = SubmitField(_("Submit and add a new one"))
|
||||||
|
|
||||||
|
@ -114,7 +119,7 @@ class BillForm(FlaskForm):
|
||||||
bill.what = self.what.data
|
bill.what = self.what.data
|
||||||
bill.date = self.date.data
|
bill.date = self.date.data
|
||||||
bill.owers = [Person.query.get(ower, project)
|
bill.owers = [Person.query.get(ower, project)
|
||||||
for ower in self.payed_for.data]
|
for ower in self.payed_for.data]
|
||||||
|
|
||||||
return bill
|
return bill
|
||||||
|
|
||||||
|
@ -175,17 +180,17 @@ class InviteForm(FlaskForm):
|
||||||
for email in [email.strip() for email in form.emails.data.split(",")]:
|
for email in [email.strip() for email in form.emails.data.split(",")]:
|
||||||
if not validator.regex.match(email):
|
if not validator.regex.match(email):
|
||||||
raise ValidationError(_("The email %(email)s is not valid",
|
raise ValidationError(_("The email %(email)s is not valid",
|
||||||
email=email))
|
email=email))
|
||||||
|
|
||||||
|
|
||||||
class ExportForm(FlaskForm):
|
class ExportForm(FlaskForm):
|
||||||
export_type = SelectField(_("What do you want to download ?"),
|
export_type = SelectField(
|
||||||
validators=[Required()],
|
_("What do you want to download ?"),
|
||||||
coerce=str,
|
validators=[Required()],
|
||||||
choices=[("bills", _("bills")), ("transactions", _("transactions"))]
|
coerce=str,
|
||||||
)
|
choices=[("bills", _("bills")), ("transactions", _("transactions"))])
|
||||||
export_format = SelectField(_("Export file format"),
|
export_format = SelectField(
|
||||||
validators=[Required()],
|
_("Export file format"),
|
||||||
coerce=str,
|
validators=[Required()],
|
||||||
choices=[("csv", "csv"), ("json", "json")]
|
coerce=str,
|
||||||
)
|
choices=[("csv", "csv"), ("json", "json")])
|
||||||
|
|
|
@ -10,7 +10,8 @@ from ihatemoney.models import db
|
||||||
|
|
||||||
|
|
||||||
class GeneratePasswordHash(Command):
|
class GeneratePasswordHash(Command):
|
||||||
"Get password from user and hash it without printing it in clear text"
|
|
||||||
|
"""Get password from user and hash it without printing it in clear text."""
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
password = getpass(prompt='Password: ')
|
password = getpass(prompt='Password: ')
|
||||||
|
|
|
@ -9,13 +9,10 @@ from sqlalchemy import orm
|
||||||
db = SQLAlchemy()
|
db = SQLAlchemy()
|
||||||
|
|
||||||
|
|
||||||
# define models
|
|
||||||
|
|
||||||
|
|
||||||
class Project(db.Model):
|
class Project(db.Model):
|
||||||
|
|
||||||
_to_serialize = ("id", "name", "password", "contact_email",
|
_to_serialize = ("id", "name", "password", "contact_email",
|
||||||
"members", "active_members", "balance")
|
"members", "active_members", "balance")
|
||||||
|
|
||||||
id = db.Column(db.String(64), primary_key=True)
|
id = db.Column(db.String(64), primary_key=True)
|
||||||
|
|
||||||
|
@ -32,12 +29,13 @@ class Project(db.Model):
|
||||||
def balance(self):
|
def balance(self):
|
||||||
|
|
||||||
balances, should_pay, should_receive = (defaultdict(int)
|
balances, should_pay, should_receive = (defaultdict(int)
|
||||||
for time in (1, 2, 3))
|
for time in (1, 2, 3))
|
||||||
|
|
||||||
# for each person
|
# for each person
|
||||||
for person in self.members:
|
for person in self.members:
|
||||||
# get the list of bills he has to pay
|
# get the list of bills he has to pay
|
||||||
bills = Bill.query.options(orm.subqueryload(Bill.owers)).filter(Bill.owers.contains(person))
|
bills = Bill.query.options(orm.subqueryload(Bill.owers)).filter(
|
||||||
|
Bill.owers.contains(person))
|
||||||
for bill in bills.all():
|
for bill in bills.all():
|
||||||
if person != bill.payer:
|
if person != bill.payer:
|
||||||
share = bill.pay_each() * person.weight
|
share = bill.pay_each() * person.weight
|
||||||
|
@ -56,6 +54,7 @@ class Project(db.Model):
|
||||||
|
|
||||||
def get_transactions_to_settle_bill(self, pretty_output=False):
|
def get_transactions_to_settle_bill(self, pretty_output=False):
|
||||||
"""Return a list of transactions that could be made to settle the bill"""
|
"""Return a list of transactions that could be made to settle the bill"""
|
||||||
|
|
||||||
def prettify(transactions, pretty_output):
|
def prettify(transactions, pretty_output):
|
||||||
""" Return pretty transactions
|
""" Return pretty transactions
|
||||||
"""
|
"""
|
||||||
|
@ -63,36 +62,52 @@ class Project(db.Model):
|
||||||
return transactions
|
return transactions
|
||||||
pretty_transactions = []
|
pretty_transactions = []
|
||||||
for transaction in transactions:
|
for transaction in transactions:
|
||||||
pretty_transactions.append({'ower': transaction['ower'].name,
|
pretty_transactions.append({
|
||||||
'receiver': transaction['receiver'].name,
|
'ower': transaction['ower'].name,
|
||||||
'amount': round(transaction['amount'], 2)})
|
'receiver': transaction['receiver'].name,
|
||||||
|
'amount': round(transaction['amount'], 2)
|
||||||
|
})
|
||||||
return pretty_transactions
|
return pretty_transactions
|
||||||
|
|
||||||
#cache value for better performance
|
# cache value for better performance
|
||||||
balance = self.balance
|
balance = self.balance
|
||||||
credits, debts, transactions = [],[],[]
|
credits, debts, transactions = [], [], []
|
||||||
# Create lists of credits and debts
|
# Create lists of credits and debts
|
||||||
for person in self.members:
|
for person in self.members:
|
||||||
if round(balance[person.id], 2) > 0:
|
if round(balance[person.id], 2) > 0:
|
||||||
credits.append({"person": person, "balance": balance[person.id]})
|
credits.append({"person": person, "balance": balance[person.id]})
|
||||||
elif round(balance[person.id], 2) < 0:
|
elif round(balance[person.id], 2) < 0:
|
||||||
debts.append({"person": person, "balance": -balance[person.id]})
|
debts.append({"person": person, "balance": -balance[person.id]})
|
||||||
|
|
||||||
# Try and find exact matches
|
# Try and find exact matches
|
||||||
for credit in credits:
|
for credit in credits:
|
||||||
match = self.exactmatch(round(credit["balance"], 2), debts)
|
match = self.exactmatch(round(credit["balance"], 2), debts)
|
||||||
if match:
|
if match:
|
||||||
for m in match:
|
for m in match:
|
||||||
transactions.append({"ower": m["person"], "receiver": credit["person"], "amount": m["balance"]})
|
transactions.append({
|
||||||
|
"ower": m["person"],
|
||||||
|
"receiver": credit["person"],
|
||||||
|
"amount": m["balance"]
|
||||||
|
})
|
||||||
debts.remove(m)
|
debts.remove(m)
|
||||||
credits.remove(credit)
|
credits.remove(credit)
|
||||||
# Split any remaining debts & credits
|
# Split any remaining debts & credits
|
||||||
while credits and debts:
|
while credits and debts:
|
||||||
|
|
||||||
if credits[0]["balance"] > debts[0]["balance"]:
|
if credits[0]["balance"] > debts[0]["balance"]:
|
||||||
transactions.append({"ower": debts[0]["person"], "receiver": credits[0]["person"], "amount": debts[0]["balance"]})
|
transactions.append({
|
||||||
|
"ower": debts[0]["person"],
|
||||||
|
"receiver": credits[0]["person"],
|
||||||
|
"amount": debts[0]["balance"]
|
||||||
|
})
|
||||||
credits[0]["balance"] = credits[0]["balance"] - debts[0]["balance"]
|
credits[0]["balance"] = credits[0]["balance"] - debts[0]["balance"]
|
||||||
del debts[0]
|
del debts[0]
|
||||||
else:
|
else:
|
||||||
transactions.append({"ower": debts[0]["person"], "receiver": credits[0]["person"], "amount": credits[0]["balance"]})
|
transactions.append({
|
||||||
|
"ower": debts[0]["person"],
|
||||||
|
"receiver": credits[0]["person"],
|
||||||
|
"amount": credits[0]["balance"]
|
||||||
|
})
|
||||||
debts[0]["balance"] = debts[0]["balance"] - credits[0]["balance"]
|
debts[0]["balance"] = debts[0]["balance"] - credits[0]["balance"]
|
||||||
del credits[0]
|
del credits[0]
|
||||||
|
|
||||||
|
@ -107,7 +122,7 @@ class Project(db.Model):
|
||||||
elif debts[0]["balance"] == credit:
|
elif debts[0]["balance"] == credit:
|
||||||
return [debts[0]]
|
return [debts[0]]
|
||||||
else:
|
else:
|
||||||
match = self.exactmatch(credit-debts[0]["balance"], debts[1:])
|
match = self.exactmatch(credit - debts[0]["balance"], debts[1:])
|
||||||
if match:
|
if match:
|
||||||
match.append(debts[0])
|
match.append(debts[0])
|
||||||
else:
|
else:
|
||||||
|
@ -136,12 +151,15 @@ class Project(db.Model):
|
||||||
owers = [ower.name for ower in bill.owers]
|
owers = [ower.name for ower in bill.owers]
|
||||||
else:
|
else:
|
||||||
owers = ', '.join([ower.name for ower in bill.owers])
|
owers = ', '.join([ower.name for ower in bill.owers])
|
||||||
pretty_bills.append({"what": bill.what,
|
|
||||||
"amount": round(bill.amount, 2),
|
pretty_bills.append({
|
||||||
"date": str(bill.date),
|
"what": bill.what,
|
||||||
"payer_name": Person.query.get(bill.payer_id).name,
|
"amount": round(bill.amount, 2),
|
||||||
"payer_weight": Person.query.get(bill.payer_id).weight,
|
"date": str(bill.date),
|
||||||
"owers": owers})
|
"payer_name": Person.query.get(bill.payer_id).name,
|
||||||
|
"payer_weight": Person.query.get(bill.payer_id).weight,
|
||||||
|
"owers": owers
|
||||||
|
})
|
||||||
return pretty_bills
|
return pretty_bills
|
||||||
|
|
||||||
def remove_member(self, member_id):
|
def remove_member(self, member_id):
|
||||||
|
@ -176,6 +194,7 @@ class Project(db.Model):
|
||||||
class Person(db.Model):
|
class Person(db.Model):
|
||||||
|
|
||||||
class PersonQuery(BaseQuery):
|
class PersonQuery(BaseQuery):
|
||||||
|
|
||||||
def get_by_name(self, name, project):
|
def get_by_name(self, name, project):
|
||||||
return Person.query.filter(Person.name == name)\
|
return Person.query.filter(Person.name == name)\
|
||||||
.filter(Project.id == project.id).one()
|
.filter(Project.id == project.id).one()
|
||||||
|
@ -212,7 +231,8 @@ class Person(db.Model):
|
||||||
return "<Person %s for project %s>" % (self.name, self.project.name)
|
return "<Person %s for project %s>" % (self.name, self.project.name)
|
||||||
|
|
||||||
# We need to manually define a join table for m2m relations
|
# We need to manually define a join table for m2m relations
|
||||||
billowers = db.Table('billowers',
|
billowers = db.Table(
|
||||||
|
'billowers',
|
||||||
db.Column('bill_id', db.Integer, db.ForeignKey('bill.id')),
|
db.Column('bill_id', db.Integer, db.ForeignKey('bill.id')),
|
||||||
db.Column('person_id', db.Integer, db.ForeignKey('person.id')),
|
db.Column('person_id', db.Integer, db.ForeignKey('person.id')),
|
||||||
)
|
)
|
||||||
|
@ -224,11 +244,11 @@ class Bill(db.Model):
|
||||||
|
|
||||||
def get(self, project, id):
|
def get(self, project, id):
|
||||||
try:
|
try:
|
||||||
return self.join(Person, Project)\
|
return (self.join(Person, Project)
|
||||||
.filter(Bill.payer_id == Person.id)\
|
.filter(Bill.payer_id == Person.id)
|
||||||
.filter(Person.project_id == Project.id)\
|
.filter(Person.project_id == Project.id)
|
||||||
.filter(Project.id == project.id)\
|
.filter(Project.id == project.id)
|
||||||
.filter(Bill.id == id).one()
|
.filter(Bill.id == id).one())
|
||||||
except orm.exc.NoResultFound:
|
except orm.exc.NoResultFound:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -262,8 +282,10 @@ class Bill(db.Model):
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Bill of %s from %s for %s>" % (self.amount,
|
return "<Bill of %s from %s for %s>" % (
|
||||||
self.payer, ", ".join([o.name for o in self.owers]))
|
self.amount,
|
||||||
|
self.payer, ", ".join([o.name for o in self.owers])
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Archive(db.Model):
|
class Archive(db.Model):
|
||||||
|
|
|
@ -84,7 +84,6 @@ class DefaultConfigurationTestCase(BaseTestCase):
|
||||||
("Budget manager", "budget@notmyidea.org"))
|
("Budget manager", "budget@notmyidea.org"))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class BudgetTestCase(IhatemoneyTestCase):
|
class BudgetTestCase(IhatemoneyTestCase):
|
||||||
|
|
||||||
def test_notifications(self):
|
def test_notifications(self):
|
||||||
|
|
|
@ -26,7 +26,9 @@ def slugify(value):
|
||||||
value = six.text_type(re.sub('[^\w\s-]', '', value).strip().lower())
|
value = six.text_type(re.sub('[^\w\s-]', '', value).strip().lower())
|
||||||
return re.sub('[-\s]+', '-', value)
|
return re.sub('[-\s]+', '-', value)
|
||||||
|
|
||||||
|
|
||||||
class Redirect303(HTTPException, RoutingException):
|
class Redirect303(HTTPException, RoutingException):
|
||||||
|
|
||||||
"""Raise if the map requests a redirect. This is for example the case if
|
"""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.
|
`strict_slashes` are activated and an url that requires a trailing slash.
|
||||||
|
|
||||||
|
@ -43,6 +45,7 @@ class Redirect303(HTTPException, RoutingException):
|
||||||
|
|
||||||
|
|
||||||
class PrefixedWSGI(object):
|
class PrefixedWSGI(object):
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Wrap the application in this middleware and configure the
|
Wrap the application in this middleware and configure the
|
||||||
front-end server to add these headers, to let you quietly bind
|
front-end server to add these headers, to let you quietly bind
|
||||||
|
@ -55,6 +58,7 @@ class PrefixedWSGI(object):
|
||||||
|
|
||||||
:param app: the WSGI application
|
:param app: the WSGI application
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def __init__(self, app):
|
def __init__(self, app):
|
||||||
self.app = app
|
self.app = app
|
||||||
self.wsgi_app = app.wsgi_app
|
self.wsgi_app = app.wsgi_app
|
||||||
|
@ -85,12 +89,14 @@ def minimal_round(*args, **kw):
|
||||||
ires = int(res)
|
ires = int(res)
|
||||||
return (res if res != ires else ires)
|
return (res if res != ires else ires)
|
||||||
|
|
||||||
|
|
||||||
def list_of_dicts2json(dict_to_convert):
|
def list_of_dicts2json(dict_to_convert):
|
||||||
"""Take a list of dictionnaries and turns it into
|
"""Take a list of dictionnaries and turns it into
|
||||||
a json in-memory file
|
a json in-memory file
|
||||||
"""
|
"""
|
||||||
return BytesIO(dumps(dict_to_convert).encode('utf-8'))
|
return BytesIO(dumps(dict_to_convert).encode('utf-8'))
|
||||||
|
|
||||||
|
|
||||||
def list_of_dicts2csv(dict_to_convert):
|
def list_of_dicts2csv(dict_to_convert):
|
||||||
"""Take a list of dictionnaries and turns it into
|
"""Take a list of dictionnaries and turns it into
|
||||||
a csv in-memory file, assume all dict have the same keys
|
a csv in-memory file, assume all dict have the same keys
|
||||||
|
@ -110,9 +116,10 @@ def list_of_dicts2csv(dict_to_convert):
|
||||||
csv_data = []
|
csv_data = []
|
||||||
csv_data.append([key.encode('utf-8') for key in dict_to_convert[0].keys()])
|
csv_data.append([key.encode('utf-8') for key in dict_to_convert[0].keys()])
|
||||||
for dic in dict_to_convert:
|
for dic in dict_to_convert:
|
||||||
csv_data.append([dic[h].encode('utf8')
|
csv_data.append(
|
||||||
if isinstance(dic[h], unicode) else str(dic[h]).encode('utf8')
|
[dic[h].encode('utf8')
|
||||||
for h in dict_to_convert[0].keys()])
|
if isinstance(dic[h], unicode) else str(dic[h]).encode('utf8')
|
||||||
|
for h in dict_to_convert[0].keys()])
|
||||||
except (KeyError, IndexError):
|
except (KeyError, IndexError):
|
||||||
csv_data = []
|
csv_data = []
|
||||||
writer = csv.writer(csv_file)
|
writer = csv.writer(csv_file)
|
||||||
|
@ -123,4 +130,4 @@ def list_of_dicts2csv(dict_to_convert):
|
||||||
return csv_file
|
return csv_file
|
||||||
|
|
||||||
# base64 encoding that works with both py2 and py3 and yield no warning
|
# base64 encoding that works with both py2 and py3 and yield no warning
|
||||||
base64_encode = base64.encodestring if six.PY2 else base64.encodebytes
|
base64_encode = base64.encodestring if six.PY2 else base64.encodebytes
|
||||||
|
|
|
@ -73,14 +73,14 @@ def pull_project(endpoint, values):
|
||||||
project = Project.query.get(project_id)
|
project = Project.query.get(project_id)
|
||||||
if not project:
|
if not project:
|
||||||
raise Redirect303(url_for(".create_project",
|
raise Redirect303(url_for(".create_project",
|
||||||
project_id=project_id))
|
project_id=project_id))
|
||||||
if project.id in session and session[project.id] == project.password:
|
if project.id in session and session[project.id] == project.password:
|
||||||
# add project into kwargs and call the original function
|
# add project into kwargs and call the original function
|
||||||
g.project = project
|
g.project = project
|
||||||
else:
|
else:
|
||||||
# redirect to authentication page
|
# redirect to authentication page
|
||||||
raise Redirect303(
|
raise Redirect303(
|
||||||
url_for(".authenticate", project_id=project_id))
|
url_for(".authenticate", project_id=project_id))
|
||||||
|
|
||||||
|
|
||||||
@main.route("/admin", methods=["GET", "POST"])
|
@main.route("/admin", methods=["GET", "POST"])
|
||||||
|
@ -108,7 +108,7 @@ def authenticate(project_id=None):
|
||||||
form.id.data = request.args['project_id']
|
form.id.data = request.args['project_id']
|
||||||
project_id = form.id.data
|
project_id = form.id.data
|
||||||
if project_id is None:
|
if project_id is None:
|
||||||
#User doesn't provide project identifier, return to authenticate form
|
# User doesn't provide project identifier, return to authenticate form
|
||||||
msg = _("You need to enter a project identifier")
|
msg = _("You need to enter a project identifier")
|
||||||
form.errors["id"] = [msg]
|
form.errors["id"] = [msg]
|
||||||
return render_template("authenticate.html", form=form)
|
return render_template("authenticate.html", form=form)
|
||||||
|
@ -148,7 +148,7 @@ def authenticate(project_id=None):
|
||||||
return redirect(url_for(".list_bills"))
|
return redirect(url_for(".list_bills"))
|
||||||
|
|
||||||
return render_template("authenticate.html", form=form,
|
return render_template("authenticate.html", form=form,
|
||||||
create_project=create_project)
|
create_project=create_project)
|
||||||
|
|
||||||
|
|
||||||
@main.route("/")
|
@main.route("/")
|
||||||
|
@ -194,14 +194,14 @@ def create_project():
|
||||||
g.project = project
|
g.project = project
|
||||||
|
|
||||||
message_title = _("You have just created '%(project)s' "
|
message_title = _("You have just created '%(project)s' "
|
||||||
"to share your expenses", project=g.project.name)
|
"to share your expenses", project=g.project.name)
|
||||||
|
|
||||||
message_body = render_template("reminder_mail.%s" %
|
message_body = render_template("reminder_mail.%s" %
|
||||||
get_locale().language)
|
get_locale().language)
|
||||||
|
|
||||||
msg = Message(message_title,
|
msg = Message(message_title,
|
||||||
body=message_body,
|
body=message_body,
|
||||||
recipients=[project.contact_email])
|
recipients=[project.contact_email])
|
||||||
try:
|
try:
|
||||||
current_app.mail.send(msg)
|
current_app.mail.send(msg)
|
||||||
except SMTPRecipientsRefused:
|
except SMTPRecipientsRefused:
|
||||||
|
@ -212,7 +212,7 @@ def create_project():
|
||||||
|
|
||||||
# redirect the user to the next step (invite)
|
# redirect the user to the next step (invite)
|
||||||
flash(_("%(msg_compl)sThe project identifier is %(project)s",
|
flash(_("%(msg_compl)sThe project identifier is %(project)s",
|
||||||
msg_compl=msg_compl, project=project.id))
|
msg_compl=msg_compl, project=project.id))
|
||||||
return redirect(url_for(".invite", project_id=project.id))
|
return redirect(url_for(".invite", project_id=project.id))
|
||||||
|
|
||||||
return render_template("create_project.html", form=form)
|
return render_template("create_project.html", form=form)
|
||||||
|
@ -268,7 +268,7 @@ def edit_project():
|
||||||
attachment_filename="%s-%s.%s" %
|
attachment_filename="%s-%s.%s" %
|
||||||
(g.project.id, export_type, export_format),
|
(g.project.id, export_type, export_format),
|
||||||
as_attachment=True
|
as_attachment=True
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
edit_form.name.data = g.project.name
|
edit_form.name.data = g.project.name
|
||||||
edit_form.password.data = g.project.password
|
edit_form.password.data = g.project.password
|
||||||
|
@ -309,7 +309,7 @@ def demo():
|
||||||
project_id='demo'))
|
project_id='demo'))
|
||||||
if not project and is_demo_project_activated:
|
if not project and is_demo_project_activated:
|
||||||
project = Project(id="demo", name=u"demonstration", password="demo",
|
project = Project(id="demo", name=u"demonstration", password="demo",
|
||||||
contact_email="demo@notmyidea.org")
|
contact_email="demo@notmyidea.org")
|
||||||
db.session.add(project)
|
db.session.add(project)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
session[project.id] = project.password
|
session[project.id] = project.password
|
||||||
|
@ -327,14 +327,14 @@ def invite():
|
||||||
# send the email
|
# send the email
|
||||||
|
|
||||||
message_body = render_template("invitation_mail.%s" %
|
message_body = render_template("invitation_mail.%s" %
|
||||||
get_locale().language)
|
get_locale().language)
|
||||||
|
|
||||||
message_title = _("You have been invited to share your "
|
message_title = _("You have been invited to share your "
|
||||||
"expenses for %(project)s", project=g.project.name)
|
"expenses for %(project)s", project=g.project.name)
|
||||||
msg = Message(message_title,
|
msg = Message(message_title,
|
||||||
body=message_body,
|
body=message_body,
|
||||||
recipients=[email.strip()
|
recipients=[email.strip()
|
||||||
for email in form.emails.data.split(",")])
|
for email in form.emails.data.split(",")])
|
||||||
current_app.mail.send(msg)
|
current_app.mail.send(msg)
|
||||||
flash(_("Your invitations have been sent"))
|
flash(_("Your invitations have been sent"))
|
||||||
return redirect(url_for(".list_bills"))
|
return redirect(url_for(".list_bills"))
|
||||||
|
@ -352,11 +352,11 @@ def list_bills():
|
||||||
bills = g.project.get_bills().options(orm.subqueryload(Bill.owers))
|
bills = g.project.get_bills().options(orm.subqueryload(Bill.owers))
|
||||||
|
|
||||||
return render_template("list_bills.html",
|
return render_template("list_bills.html",
|
||||||
bills=bills, member_form=MemberForm(g.project),
|
bills=bills, member_form=MemberForm(g.project),
|
||||||
bill_form=bill_form,
|
bill_form=bill_form,
|
||||||
add_bill=request.values.get('add_bill', False),
|
add_bill=request.values.get('add_bill', False),
|
||||||
current_view="list_bills",
|
current_view="list_bills",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@main.route("/<project_id>/members/add", methods=["GET", "POST"])
|
@main.route("/<project_id>/members/add", methods=["GET", "POST"])
|
||||||
|
@ -376,7 +376,7 @@ def add_member():
|
||||||
@main.route("/<project_id>/members/<member_id>/reactivate", methods=["POST"])
|
@main.route("/<project_id>/members/<member_id>/reactivate", methods=["POST"])
|
||||||
def reactivate(member_id):
|
def reactivate(member_id):
|
||||||
person = Person.query.filter(Person.id == member_id)\
|
person = Person.query.filter(Person.id == member_id)\
|
||||||
.filter(Project.id == g.project.id).all()
|
.filter(Project.id == g.project.id).all()
|
||||||
if person:
|
if person:
|
||||||
person[0].activated = True
|
person[0].activated = True
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
Loading…
Reference in a new issue