# Path: botfriend/util.py
# def isstr(x):
#     """Compatibility method equivalent to isinstance(x, basestring)"""
#     if major == 2:
#         return isinstance(x, basestring)
#     return isinstance(x, bytes) or isinstance(x, str)


import datetime
import importlib
import logging
import json
import os
import sys
import yaml
from .util import isstr
from nose.tools import set_trace
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy import (
    create_engine,
    Binary,
    Boolean,
    Column,
    Integer,
    Unicode,
    DateTime,
    ForeignKey,
)
from sqlalchemy.exc import (
    IntegrityError
)
from sqlalchemy.orm import (
    backref,
    relationship,
)
from sqlalchemy.orm.exc import (
    NoResultFound,
    MultipleResultsFound,
)
from sqlalchemy.orm.session import Session
from sqlalchemy.ext.declarative import declarative_base
            from botfriend.bot import Bot
        from .bot import Bot
        return qu

    @property
    def undeliverable_posts(self):
        """Find posts that had errrors when we tried to publish them to one or
        more publications.
        """
        _db = Session.object_session(self)
        return _db.query(Post).join(Post.publications).filter(
            Post.bot==self).filter(Publication.error != None).order_by(
                Publication.most_recent_attempt.asc()
            )                
    
    @hybrid_property
    def json_state(self):
        """Try to interpret .state as a JSON object."""
        if not self.state:
            return self.state
        return json.loads(self.state)

    @json_state.setter
    def json_state(self, state):
        self.state = json.dumps(state)

    @hybrid_property
    def state(self):
        return self._state

    @state.setter
    def state(self, new_value):

# Path: botfriend/util.py
# def isstr(x):
#     """Compatibility method equivalent to isinstance(x, basestring)"""
#     if major == 2:
#         return isinstance(x, basestring)
#     return isinstance(x, bytes) or isinstance(x, str)


# Path: botfriend/model.py
import datetime
import importlib
import logging
import json
import os
import sys
import yaml
from .util import isstr
from nose.tools import set_trace
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy import (
    create_engine,
    Binary,
    Boolean,
    Column,
    Integer,
    Unicode,
    DateTime,
    ForeignKey,
)
from sqlalchemy.exc import (
    IntegrityError
)
from sqlalchemy.orm import (
    backref,
    relationship,
)
from sqlalchemy.orm.exc import (
    NoResultFound,
    MultipleResultsFound,
)
from sqlalchemy.orm.session import Session
from sqlalchemy.ext.declarative import declarative_base
            from botfriend.bot import Bot
        from .bot import Bot

        return qu

    @property
    def undeliverable_posts(self):
        """Find posts that had errrors when we tried to publish them to one or
        more publications.
        """
        _db = Session.object_session(self)
        return _db.query(Post).join(Post.publications).filter(
            Post.bot==self).filter(Publication.error != None).order_by(
                Publication.most_recent_attempt.asc()
            )                
    
    @hybrid_property
    def json_state(self):
        """Try to interpret .state as a JSON object."""
        if not self.state:
            return self.state
        return json.loads(self.state)

    @json_state.setter
    def json_state(self, state):
        self.state = json.dumps(state)

    @hybrid_property
    def state(self):
        return self._state

    @state.setter
    def state(self, new_value):

        if not isstr(new_value):