Skip to content

Commit 8aa6a32

Browse files
committed
Fix serializing & deserializing data. Fixes #75
- convert data to serializable dict when publishing it from stores - allow loading model objects from serialized data in storeclient
1 parent bebf7d7 commit 8aa6a32

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

ircb/models/lib.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# -*- coding: utf-8 -*-
2+
import datetime
3+
24
from sqlalchemy.ext.declarative import declarative_base
35
from sqlalchemy import create_engine
46
from sqlalchemy.orm import sessionmaker
@@ -9,12 +11,28 @@
911

1012
class _Base(object):
1113

12-
def to_dict(self):
14+
def to_dict(self, serializable=True):
1315
d = {}
1416
for col in self.__table__.columns:
1517
d[col.name] = getattr(self, col.name)
18+
if serializable:
19+
if getattr(self, 'created'):
20+
d['created'] = self.created.timestamp()
21+
if getattr(self, 'last_updated'):
22+
d['last_updated'] = self.last_updated.timestamp()
1623
return d
1724

25+
@classmethod
26+
def from_dict(cls, data, serialized=True):
27+
if serialized:
28+
if 'created' in data:
29+
data['created'] = datetime.datetime.fromtimestamp(
30+
data['created'])
31+
if 'last_updated' in data:
32+
data['last_updated'] = datetime.datetime.fromtimestamp(
33+
data['last_updated'])
34+
return cls(**data)
35+
1836
Base = declarative_base(cls=_Base)
1937

2038

ircb/models/logs.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,25 @@ class BaseLog(object):
2323
last_updated = sa.Column(sa.DateTime,
2424
default=datetime.datetime.utcnow)
2525

26-
def to_dict(self, serializable=False):
26+
def to_dict(self, serializable=True):
2727
d = super().to_dict()
28-
d['timestamp'] = self.timestamp.timestamp()
29-
d['created'] = self.created.timestamp()
30-
d['last_updated'] = self.last_updated.timestamp()
28+
if serializable:
29+
d['timestamp'] = self.timestamp.timestamp()
3130
return d
3231

32+
@classmethod
33+
def from_dict(cls, data, serialized=True):
34+
if serialized:
35+
data['timestamp'] = datetime.datetime.fromtimestamp(
36+
data['timestamp'])
37+
if 'created' in data:
38+
data['created'] = datetime.datetime.fromtimestamp(
39+
data['created'])
40+
if 'last_updated' in data:
41+
data['last_updated'] = datetime.datetime.fromtimestamp(
42+
data['last_updated'])
43+
return cls(**data)
44+
3345

3446
class MessageLog(BaseLog, Base):
3547
"""

ircb/models/network.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,4 @@ def to_dict(self, serializable=False):
8989
self.status if isinstance(self.status, str) else self.status.code)
9090
d['ssl_verify'] = ssl_verify
9191
d['status'] = status
92-
if serializable:
93-
d['created'] = self.created.timestamp()
94-
d['last_updated'] = self.last_updated.timestamp()
9592
return d

ircb/models/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class User(Base, UserMixin):
3535
created = Column(DateTime, default=datetime.datetime.utcnow)
3636
last_updated = Column(DateTime, default=datetime.datetime.utcnow)
3737

38-
def to_dict(self):
38+
def to_dict(self, serializable=False):
3939
d = super().to_dict()
4040
d.pop('password')
4141
d['is_active'] = self.is_active()

ircb/storeclient/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def initialize(cls):
4141
def get(cls, data, raw=False):
4242
result = yield from cls._get(data)
4343
if isinstance(result, dict):
44-
result = cls.model(**result) if raw is False else result
44+
result = cls.model.from_dict(result) if raw is False else result
4545
elif isinstance(result, tuple):
4646
if raw:
4747
result = result
4848
else:
49-
result = [cls.model(**item) for item in result]
49+
result = [cls.model.from_dict(item) for item in result]
5050
return result
5151

5252
@classmethod
@@ -67,7 +67,7 @@ def delete(cls, data):
6767
@classmethod
6868
def create_or_update(cls, data):
6969
result = yield from cls._create_or_update(data)
70-
return cls.model(**result)
70+
return cls.model.from_dict(result)
7171

7272
@classmethod
7373
def _get(cls, data):
@@ -166,7 +166,7 @@ def _on_message(cls, signal, data, taskid=None):
166166
raw_callbacks = cls.raw_callbacks['delete']
167167
if callbacks:
168168
for callback in callbacks:
169-
callback(cls.model(**data))
169+
callback(cls.model.from_dict(data))
170170
if raw_callbacks:
171171
for callback in raw_callbacks:
172172
callback(data)

0 commit comments

Comments
 (0)