2018-08-09 19:46:54 +00:00
|
|
|
from datetime import datetime, timezone
|
2023-03-28 15:09:47 +00:00
|
|
|
|
2020-10-16 14:25:13 +00:00
|
|
|
from flask_sqlalchemy import event
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
|
|
|
|
STR_SIZE = 64
|
|
|
|
STR_BIG_SIZE = 128
|
|
|
|
STR_SM_SIZE = 32
|
2018-06-10 16:47:49 +00:00
|
|
|
STR_XSM_SIZE = 16
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Thing(db.Model):
|
2019-02-03 16:12:53 +00:00
|
|
|
"""The base class of all Devicehub resources.
|
|
|
|
|
|
|
|
This is a loose copy of
|
|
|
|
`schema.org's Thing class <https://schema.org/Thing>`_
|
|
|
|
using only needed fields.
|
|
|
|
"""
|
2023-03-28 15:09:47 +00:00
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
__abstract__ = True
|
2023-03-28 15:09:47 +00:00
|
|
|
updated = db.Column(
|
|
|
|
db.TIMESTAMP(timezone=True),
|
|
|
|
nullable=False,
|
|
|
|
index=True,
|
|
|
|
server_default=db.text('CURRENT_TIMESTAMP'),
|
|
|
|
)
|
|
|
|
updated.comment = """The last time Devicehub recorded a change for
|
2019-06-19 11:35:26 +00:00
|
|
|
this thing.
|
2018-06-12 14:50:05 +00:00
|
|
|
"""
|
2023-03-28 15:09:47 +00:00
|
|
|
created = db.Column(
|
|
|
|
db.TIMESTAMP(timezone=True),
|
|
|
|
nullable=False,
|
|
|
|
index=True,
|
|
|
|
server_default=db.text('CURRENT_TIMESTAMP'),
|
|
|
|
)
|
2019-06-19 11:35:26 +00:00
|
|
|
created.comment = """When Devicehub created this."""
|
2018-07-14 14:41:22 +00:00
|
|
|
|
|
|
|
def __init__(self, **kwargs) -> None:
|
2019-05-10 16:00:38 +00:00
|
|
|
# We need to set 'created' before sqlalchemy inits the class
|
|
|
|
# to be able to use sorted containers
|
|
|
|
self.created = kwargs.get('created', datetime.now(timezone.utc))
|
2018-07-14 14:41:22 +00:00
|
|
|
super().__init__(**kwargs)
|
2020-10-16 14:25:13 +00:00
|
|
|
|
2023-03-28 15:09:47 +00:00
|
|
|
def delete(self):
|
|
|
|
db.session.delete(self)
|
|
|
|
|
2020-10-16 14:30:53 +00:00
|
|
|
|
2020-10-27 20:29:38 +00:00
|
|
|
def update_object_timestamp(mapper, connection, thing_obj):
|
2023-03-28 15:09:47 +00:00
|
|
|
"""This function update the stamptime of field updated"""
|
2020-10-27 20:29:38 +00:00
|
|
|
thing_obj.updated = datetime.now(timezone.utc)
|
2020-10-16 14:25:13 +00:00
|
|
|
|
2023-03-28 15:09:47 +00:00
|
|
|
|
2020-10-27 20:29:38 +00:00
|
|
|
def listener_reset_field_updated_in_actual_time(thing_obj):
|
2023-03-28 15:09:47 +00:00
|
|
|
"""This function launch a event than listen like a signal when some object is saved"""
|
2020-10-27 20:29:38 +00:00
|
|
|
event.listen(thing_obj, 'before_update', update_object_timestamp, propagate=True)
|