init
This commit is contained in:
28
web/commons/models/mixin/calculator.py
Normal file
28
web/commons/models/mixin/calculator.py
Normal file
@@ -0,0 +1,28 @@
|
||||
class CalculatorMixin:
|
||||
date = None
|
||||
|
||||
@classmethod
|
||||
def query_by_month(cls, query, year, month):
|
||||
last_month_year, last_month = Helper.get_last_month(year, month)
|
||||
query = query.filter(cls.date >= datetime.date(last_month_year, last_month, 26))
|
||||
query = query.filter(cls.date <= datetime.date(year, month, 25))
|
||||
return query
|
||||
|
||||
def upsert(self, *args, **kwargs):
|
||||
result = self.get_by_key(*args, **kwargs)
|
||||
if not result:
|
||||
session = db.session
|
||||
session.add(self)
|
||||
session.commit()
|
||||
else:
|
||||
session = db.session
|
||||
self.id = result.id
|
||||
session.add(result)
|
||||
session.commit()
|
||||
|
||||
@classmethod
|
||||
def get_last_date_from(cls, date):
|
||||
query = cls.query.filter(cls.date <= date)
|
||||
query = query.with_entities(func.max(cls.date))
|
||||
result = query.one_or_none()
|
||||
return result[0]
|
Reference in New Issue
Block a user