1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/ihatemoney_ynh.git synced 2024-09-03 19:26:15 +02:00

Added member weights support to API

This commit is contained in:
Jocelyn Delande 2015-08-20 11:11:09 +02:00
parent 2b071a1a3b
commit 06f10d0508
2 changed files with 68 additions and 8 deletions

View file

@ -153,7 +153,7 @@ class Person(db.Model):
query_class = PersonQuery query_class = PersonQuery
_to_serialize = ("id", "name", "activated") _to_serialize = ("id", "name", "weight", "activated")
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
project_id = db.Column(db.String(64), db.ForeignKey("project.id")) project_id = db.Column(db.String(64), db.ForeignKey("project.id"))
@ -219,7 +219,7 @@ class Bill(db.Model):
archive = db.Column(db.Integer, db.ForeignKey("archive.id")) archive = db.Column(db.Integer, db.ForeignKey("archive.id"))
def pay_each(self): def pay_each(self):
"""Compute what each person has to pay""" """Compute what each share has to pay"""
if self.owers: if self.owers:
# FIXME: SQL might dot that more efficiently # FIXME: SQL might dot that more efficiently
return self.amount / sum(i.weight for i in self.owers) return self.amount / sum(i.weight for i in self.owers)

View file

@ -583,9 +583,10 @@ class APITestCase(TestCase):
'contact_email': contact 'contact_email': contact
}) })
def api_add_member(self, project, name): def api_add_member(self, project, name, weight=1):
self.app.post("/api/projects/%s/members" % project, self.app.post("/api/projects/%s/members" % project,
data={"name": name}, headers=self.get_auth(project)) data={"name": name, "weight": weight},
headers=self.get_auth(project))
def get_auth(self, username, password=None): def get_auth(self, username, password=None):
password = password or username password = password or username
@ -792,8 +793,8 @@ class APITestCase(TestCase):
"what": "fromage", "what": "fromage",
"payer_id": 1, "payer_id": 1,
"owers": [ "owers": [
{"activated": True, "id": 1, "name": "alexis"}, {"activated": True, "id": 1, "name": "alexis", "weight": 1},
{"activated": True, "id": 2, "name": "fred"}], {"activated": True, "id": 2, "name": "fred", "weight": 1}],
"amount": 25.0, "amount": 25.0,
"date": "2011-08-10", "date": "2011-08-10",
"id": 1} "id": 1}
@ -835,8 +836,8 @@ class APITestCase(TestCase):
"what": "beer", "what": "beer",
"payer_id": 2, "payer_id": 2,
"owers": [ "owers": [
{"activated": True, "id": 1, "name": "alexis"}, {"activated": True, "id": 1, "name": "alexis", "weight": 1},
{"activated": True, "id": 2, "name": "fred"}], {"activated": True, "id": 2, "name": "fred", "weight": 1}],
"amount": 25.0, "amount": 25.0,
"date": "2011-09-10", "date": "2011-09-10",
"id": 1} "id": 1}
@ -853,6 +854,65 @@ class APITestCase(TestCase):
headers=self.get_auth("raclette")) headers=self.get_auth("raclette"))
self.assertStatus(404, req) self.assertStatus(404, req)
def test_weighted_bills(self):
# create a project
self.api_create("raclette")
# add members
self.api_add_member("raclette", "alexis")
self.api_add_member("raclette", "freddy familly", 4)
self.api_add_member("raclette", "arnaud")
# add a bill
req = self.app.post("/api/projects/raclette/bills", data={
'date': '2011-08-10',
'what': "fromage",
'payer': "1",
'payed_for': ["1", "2"],
'amount': '25',
}, headers=self.get_auth("raclette"))
# get this bill details
req = self.app.get("/api/projects/raclette/bills/1",
headers=self.get_auth("raclette"))
# compare with the added info
self.assertStatus(200, req)
expected = {
"what": "fromage",
"payer_id": 1,
"owers": [
{"activated": True, "id": 1, "name": "alexis", "weight": 1},
{"activated": True, "id": 2, "name": "freddy familly", "weight": 4}],
"amount": 25.0,
"date": "2011-08-10",
"id": 1}
self.assertDictEqual(expected, json.loads(req.data))
# getting it should return a 404
req = self.app.get("/api/projects/raclette",
headers=self.get_auth("raclette"))
expected = {
"active_members": [
{"activated": True, "id": 1, "name": "alexis", "weight": 1.0},
{"activated": True, "id": 2, "name": "freddy familly", "weight": 4.0},
{"activated": True, "id": 3, "name": "arnaud", "weight": 1.0}
],
"balance": {"1": 20.0, "2": -20.0, "3": 0},
"contact_email": "raclette@notmyidea.org",
"id": "raclette",
"members": [
{"activated": True, "id": 1, "name": "alexis", "weight": 1.0},
{"activated": True, "id": 2, "name": "freddy familly", "weight": 4.0},
{"activated": True, "id": 3, "name": "arnaud", "weight": 1.0}
],
"name": "raclette",
"password": "raclette"}
self.assertStatus(200, req)
self.assertEqual(expected, json.loads(req.data))
class ServerTestCase(APITestCase): class ServerTestCase(APITestCase):
def setUp(self): def setUp(self):