mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-01 18:52:23 +02:00
Populate the demo project with defaults. (#616)
This commit is contained in:
parent
82393a110a
commit
0fd2958865
3 changed files with 52 additions and 12 deletions
|
@ -15,6 +15,7 @@ from sqlalchemy import orm
|
|||
from sqlalchemy.sql import func
|
||||
from sqlalchemy_continuum import make_versioned, version_class
|
||||
from sqlalchemy_continuum.plugins import FlaskPlugin
|
||||
from werkzeug.security import generate_password_hash
|
||||
|
||||
from ihatemoney.patch_sqlalchemy_continuum import PatchedBuilder
|
||||
from ihatemoney.versioning import (
|
||||
|
@ -330,6 +331,49 @@ class Project(db.Model):
|
|||
def __repr__(self):
|
||||
return f"<Project {self.name}>"
|
||||
|
||||
@staticmethod
|
||||
def create_demo_project():
|
||||
project = Project(
|
||||
id="demo",
|
||||
name="demonstration",
|
||||
password=generate_password_hash("demo"),
|
||||
contact_email="demo@notmyidea.org",
|
||||
default_currency="EUR",
|
||||
)
|
||||
db.session.add(project)
|
||||
db.session.commit()
|
||||
|
||||
members = {}
|
||||
for name in ("Amina", "Georg", "Alice"):
|
||||
person = Person()
|
||||
person.name = name
|
||||
person.project = project
|
||||
person.weight = 1
|
||||
db.session.add(person)
|
||||
|
||||
members[name] = person
|
||||
|
||||
db.session.commit()
|
||||
|
||||
operations = (
|
||||
("Georg", 200, ("Amina", "Georg", "Alice"), "Food shopping"),
|
||||
("Alice", 20, ("Amina", "Alice"), "Beer !"),
|
||||
("Amina", 50, ("Amina", "Alice", "Georg"), "AMAP"),
|
||||
)
|
||||
for (payer, amount, owers, subject) in operations:
|
||||
bill = Bill()
|
||||
bill.payer_id = members[payer].id
|
||||
bill.what = subject
|
||||
bill.owers = [members[name] for name in owers]
|
||||
bill.amount = amount
|
||||
bill.original_currency = "EUR"
|
||||
bill.converted_amount = amount
|
||||
|
||||
db.session.add(bill)
|
||||
|
||||
db.session.commit()
|
||||
return project
|
||||
|
||||
|
||||
class Person(db.Model):
|
||||
class PersonQuery(BaseQuery):
|
||||
|
|
|
@ -518,7 +518,11 @@ class BudgetTestCase(IhatemoneyTestCase):
|
|||
# test that a demo project is created if none is defined
|
||||
self.assertEqual([], models.Project.query.all())
|
||||
self.client.get("/demo")
|
||||
self.assertTrue(models.Project.query.get("demo") is not None)
|
||||
demo = models.Project.query.get("demo")
|
||||
self.assertTrue(demo is not None)
|
||||
|
||||
self.assertEqual(["Amina", "Georg", "Alice"], [m.name for m in demo.members])
|
||||
self.assertEqual(demo.get_bills().count(), 3)
|
||||
|
||||
def test_deactivated_demo(self):
|
||||
self.app.config["ACTIVATE_DEMO_PROJECT"] = False
|
||||
|
|
|
@ -559,11 +559,11 @@ def exit():
|
|||
@main.route("/demo")
|
||||
def demo():
|
||||
"""
|
||||
Authenticate the user for the demonstration project and redirect him to
|
||||
Authenticate the user for the demonstration project and redirects to
|
||||
the bills list for this project.
|
||||
|
||||
Create a demo project if it doesn't exists yet (or has been deleted)
|
||||
If the demo project is deactivated, one is redirected to the create project form
|
||||
If the demo project is deactivated, redirects to the create project form.
|
||||
"""
|
||||
is_demo_project_activated = current_app.config["ACTIVATE_DEMO_PROJECT"]
|
||||
project = Project.query.get("demo")
|
||||
|
@ -571,15 +571,7 @@ def demo():
|
|||
if not project and not is_demo_project_activated:
|
||||
raise Redirect303(url_for(".create_project", project_id="demo"))
|
||||
if not project and is_demo_project_activated:
|
||||
project = Project(
|
||||
id="demo",
|
||||
name="demonstration",
|
||||
password=generate_password_hash("demo"),
|
||||
contact_email="demo@notmyidea.org",
|
||||
default_currency="EUR",
|
||||
)
|
||||
db.session.add(project)
|
||||
db.session.commit()
|
||||
project = Project.create_demo_project()
|
||||
session[project.id] = True
|
||||
return redirect(url_for(".list_bills", project_id=project.id))
|
||||
|
||||
|
|
Loading…
Reference in a new issue