-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemas.py
More file actions
43 lines (35 loc) · 1.51 KB
/
schemas.py
File metadata and controls
43 lines (35 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from marshmallow import Schema, fields, validate, pre_load
from datetime import datetime
class UserSchema(Schema):
id = fields.Int(dump_only=True)
username = fields.Str(required=True)
password = fields.Str(required=True, load_only=True)
class PlainExpenseSchema(Schema):
id = fields.Int(dump_only=True)
name = fields.Str(required=True)
amount = fields.Float(required=True, validate=validate.Range(min=0.01))
date = fields.Date(format="%d-%m-%Y", default=datetime.now().date())
class PlainCategorySchema(Schema):
id = fields.Int(dump_only=True)
name = fields.Str(required=True, validate=validate.Length(min=1))
class ExpenseSchema(PlainExpenseSchema):
category_id = fields.Int(load_default=None)
user_id = fields.Int(dump_only=True)
category = fields.Nested(PlainCategorySchema(), dump_only=True)
@pre_load
def process_input(self, data, **kwargs):
if "date" not in data:
data["date"] = datetime.now().date().strftime("%d-%m-%Y")
return data
class CategorySchema(PlainCategorySchema):
expenses = fields.List(fields.Nested(PlainExpenseSchema()), dump_only=True)
class ExpenseUpdateSchema(Schema):
name = fields.Str()
amount = fields.Float(validate=validate.Range(min=0.01))
date = fields.Date(format="%d-%m-%Y")
category_id = fields.Int()
class ExpenseSummarySchema(Schema):
total_amount = fields.Float()
count = fields.Int()
average = fields.Float()
categories = fields.Dict(keys=fields.Str(), values=fields.Float())