# -*- coding: utf-8 -*-
import datetime
from ircb.lib.constants.signals import (STORE_MESSAGELOG_CREATE,
                                        STORE_MESSAGELOG_CREATED,
                                        STORE_MESSAGELOG_GET,
                                        STORE_MESSAGELOG_GOT,
                                        STORE_ACTIVITYLOG_CREATE,
                                        STORE_ACTIVITYLOG_CREATED,
                                        STORE_ACTIVITYLOG_GET,
                                        STORE_ACTIVITYLOG_GOT)
from ircb.models import get_session, MessageLog, ActivityLog
from ircb.stores.base import BaseStore

session = get_session()


class MessageLogStore(BaseStore):
    GET_SIGNAL = STORE_MESSAGELOG_GET
    GOT_SIGNAL = STORE_MESSAGELOG_GOT
    CREATE_SIGNAL = STORE_MESSAGELOG_CREATE
    CREATED_SIGNAL = STORE_MESSAGELOG_CREATED

    @classmethod
    def create(cls, hostname, roomname, message, event, timestamp,
               mask, user_id, from_nickname, from_user_id=None):
        log = MessageLog(
            hostname=hostname, roomname=roomname, message=message,
            event=event, timestamp=datetime.datetime.fromtimestamp(timestamp),
            mask=mask, user_id=user_id, from_nickname=from_nickname,
            from_user_id=from_user_id)
        session.add(log)
        session.commit()
        return log

    @classmethod
    def get(cls, filter=None, order_by=None, limit=30, sort='timestamp'):
        qs = session.query(MessageLog)
        if filter:
            for key, value in filter.items():
                qs = qs.filter(getattr(MessageLog, key) == value)

        if order_by:
            for item in order_by:
                if item.startswith('-'):
                    qs = qs.order_by(getattr(MessageLog, item[1:]).desc())
                else:
                    qs = qs.order_by(getattr(MessageLog, item))

        qs = qs.limit(limit)
        return sorted(qs.all(), key=lambda item: getattr(item, 'timestamp'))


class ActivityLogStore(BaseStore):
    CREATE_SIGNAL = STORE_ACTIVITYLOG_CREATE
    CREATED_SIGNAL = STORE_ACTIVITYLOG_CREATED
    GET_SIGNAL = STORE_ACTIVITYLOG_GET
    GOT_SIGNAL = STORE_ACTIVITYLOG_GOT

    @classmethod
    def get(cls):
        pass

    @classmethod
    def create(cls, hostname, roomname, message, event, timestamp,
               mask, user_id):
        log = ActivityLog(
            hostname=hostname, roomname=roomname, message=message,
            event=event, timestamp=datetime.datetime.fromtimestamp(timestamp),
            mask=mask, user_id=user_id)
        session.add(log)
        session.commit()
        return log

@@ -1502,16 +1502,19 @@
 ssageLog
+.id
 )%0A      
@@ -1946,56 +1946,122 @@
 rn s
-orted(qs.all(), key=lambda item: getattr(item, '
+ession.query(MessageLog).%5C%0A            filter(MessageLog.id.in_(qs.subquery())).%5C%0A            order_by(MessageLog.
 time
@@ -2065,18 +2065,22 @@
 imestamp
-'
 )
+.all(
 )%0A%0A%0Aclas

Optimize query to fetch latest N MessageLogs. #71