Skip to content

Commit e94e774

Browse files
committed
Support fetching logs from MessageLog store. #71
- support basic filter, order_by, limit, sort query
1 parent 1ae93af commit e94e774

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

ircb/stores/logs.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ class MessageLogStore(BaseStore):
2020
CREATE_SIGNAL = STORE_MESSAGELOG_CREATE
2121
CREATED_SIGNAL = STORE_MESSAGELOG_CREATED
2222

23-
@classmethod
24-
def get(cls, query):
25-
pass
26-
2723
@classmethod
2824
def create(cls, hostname, roomname, message, event, timestamp,
2925
mask, user_id, from_nickname, from_user_id=None):
@@ -36,6 +32,23 @@ def create(cls, hostname, roomname, message, event, timestamp,
3632
session.commit()
3733
return log
3834

35+
@classmethod
36+
def get(cls, filter=None, order_by=None, limit=30, sort='timestamp'):
37+
qs = session.query(MessageLog)
38+
if filter:
39+
for key, value in filter.items():
40+
qs = qs.filter(getattr(MessageLog, key) == value)
41+
42+
if order_by:
43+
for item in order_by:
44+
if item.startswith('-'):
45+
qs = qs.order_by(getattr(MessageLog, item[1:]).desc())
46+
else:
47+
qs = qs.order_by(getattr(MessageLog, item))
48+
49+
qs = qs.limit(limit)
50+
return sorted(qs.all(), key=lambda item: getattr(item, 'timestamp'))
51+
3952

4053
class ActivityLogStore(BaseStore):
4154
CREATE_SIGNAL = STORE_ACTIVITYLOG_CREATE

0 commit comments

Comments
 (0)