Пример #1
0
class Media(db.get_base_model()):
    type = peewee.ForeignKeyField(MediaType)
    preview = peewee.BlobField(null=True)
    remote_url = peewee.CharField(null=True)
    local_path = peewee.CharField(null=True)
    data = peewee.TextField(null=True)
    transfer_status = peewee.IntegerField(default=0)
    size = peewee.IntegerField(null=True)
    mimetype = peewee.CharField(null=True)
    filehash = peewee.CharField(null=True)
    filename = peewee.CharField(null=True)
    encoding = peewee.CharField(null=True)

    def toDict(self):
        media = {
            "type": self.type.name,
            "preview": self.preview,
            "remote_url": self.remote_url,
            "local_path": self.local_path,
            "data": self.data,
            "transfer_status": self.transfer_status,
            "size": self.size,
            "mimetype": self.mimetype,
            "filehash": self.filehash,
            "filename": self.filename,
            "encoding": self.encoding
        }
Пример #2
0
class Group(db.get_base_model()):
    jid = peewee.CharField(null=False)
    picture = peewee.BlobField(null=True)
    subject = peewee.CharField(null=True)
    subject_owner_id = peewee.ForeignKeyField(Contact, null=True)
    subject_time = peewee.DateTimeField(null=True)
    created = peewee.DateTimeField(default=datetime.datetime.now())
Пример #3
0
class State(db.get_base_model()):
    name = peewee.CharField(unique=True)

    @classmethod
    def init(cls):
        cls.get_or_create(name=STATE_RECEIVED)
        cls.get_or_create(name=STATE_RECEIVED_REMOTE)
        cls.get_or_create(name=STATE_RECEIVED_READ)
        cls.get_or_create(name=STATE_RECEIVED_READ_REMOTE)
        cls.get_or_create(name=STATE_SENT_QUEUED)
        cls.get_or_create(name=STATE_SENT)
        cls.get_or_create(name=STATE_SENT_DELIVERED)
        cls.get_or_create(name=STATE_SENT_READ)

    def __unicode__(self):
        return self.name

    @classmethod
    def get_received_read(cls):
        return cls.get(State.name == STATE_RECEIVED_READ)

    @classmethod
    def get_received_read_remote(cls):
        return cls.get(State.name == STATE_RECEIVED_READ_REMOTE)

    @classmethod
    def get_received(cls):
        return cls.get(State.name == STATE_RECEIVED)

    @classmethod
    def get_received_remote(cls):
        return cls.get(State.name == STATE_RECEIVED_REMOTE)

    @classmethod
    def get_sent_queued(cls):
        return cls.get(State.name == STATE_SENT_QUEUED)

    @classmethod
    def get_sent(cls):
        return cls.get(State.name == STATE_SENT)

    @classmethod
    def get_sent_delivered(cls):
        return cls.get(State.name == STATE_SENT_DELIVERED)

    @classmethod
    def get_sent_read(cls):
        return cls.get(State.name == STATE_SENT_READ)
Пример #4
0
class Contact(db.get_base_model()):
    number = peewee.CharField(unique=False, null=True)
    jid = peewee.CharField(null=False, unique=False)
    last_seen_on = peewee.DateTimeField(null=True)
    status = peewee.CharField(null=True)
    push_name = peewee.CharField(null=True)
    name = peewee.CharField(null=True)
    picture = peewee.BlobField(null=True)

    def toDict(self):
        return {
            "number": self.number,
            "jid": self.jid,
            "last_seen_on": self.last_seen_on,
            "status": self.status,
            "push_name": self.push_name,
            "name": self.name,
            "picture": self.picture
        }
Пример #5
0
class Message(db.get_base_model()):
    id_gen = peewee.CharField(null=False, unique=True)
    conversation = peewee.ForeignKeyField(Conversation)
    created = peewee.DateTimeField(default=datetime.datetime.now())
    t_sent = peewee.DateTimeField(default=datetime.datetime.now())
    content = peewee.TextField(null=True)
    media = peewee.ForeignKeyField(Media, null=True)

    def getMediaType(self):
        if self.media is not None:
            return self.media.type

    def getState(self):
        from messagestate import MessageState
        return MessageState.get_state(self)

    @classmethod
    def getByState(cls, states, conversation = None):
        from messagestate import MessageState

        query = (cls.select()
                .join(MessageState)
                .join(State))
        if conversation:
            query = query.where(State.id << [state.id for state in states], Message.conversation == conversation)
        else:
            query = query.where(State.id << [state.id for state in states])

        return query

    def toDict(self):
        return {
            "id": self.id_gen,
            "conversation": self.conversation.toDict(),
            "created": self.created,
            "content": self.content,
            "t_sent": self.t_sent,
            "type": MessageProtocolEntity.MESSAGE_TYPE_TEXT if self.media is None else MessageProtocolEntity.MESSAGE_TYPE_MEDIA,
            "media": self.media.toDict() if self.media is not None else None,
            "state": self.getState().name
        }
Пример #6
0
class MediaType(db.get_base_model()):
    name = peewee.CharField()

    def __unicode__(self):
        return self.name

    @classmethod
    def init(cls):
        cls.get_or_create(name=TYPE_IMAGE)
        cls.get_or_create(name=TYPE_AUDIO)
        cls.get_or_create(name=TYPE_VIDEO)
        cls.get_or_create(name=TYPE_VCARD)
        cls.get_or_create(name=TYPE_LOCATION)

    @classmethod
    def get_image(cls):
        return cls.get_mediatype(TYPE_IMAGE)

    @classmethod
    def get_audio(cls):
        return cls.get_meditype(TYPE_AUDIO)

    @classmethod
    def get_video(cls):
        return cls.get_mediatype(TYPE_VIDEO)

    @classmethod
    def get_vcard(cls):
        return cls.get_mediatype(TYPE_VCARD)

    @classmethod
    def get_location(cls):
        return cls.get_mediatype(TYPE_LOCATION)

    @classmethod
    def get_mediatype(cls, name):
        return cls.get(cls.name == name)
Пример #7
0
class Conversation(db.get_base_model()):
    contact = peewee.ForeignKeyField(Contact, null=True)
    group = peewee.ForeignKeyField(Group, null=True)
    broadcast = peewee.ForeignKeyField(Broadcast, null=True)
    created = peewee.DateTimeField(default=datetime.datetime.now())

    def getType(self):
        if self.contact:
            return TYPE_CONTACT

        if self.group:
            return TYPE_GROUP

        if self.broadcast:
            return TYPE_BROADCAST

    def toDict(self):
        return {
            "contact": self.contact.toDict() if self.contact else None,
            "group": self.group.toDict() if self.group else None,
            "broadcast": self.broadcast.toDict() if self.broadcast else None,
            "type": self.getType(),
            "created": self.created
        }
Пример #8
0
class Status(db.get_base_model()):
    text = peewee.TextField()
    created = peewee.DateTimeField(default=datetime.datetime.now())
Пример #9
0
class MessageState(db.get_base_model()):
    message = peewee.ForeignKeyField(Message)
    state = peewee.ForeignKeyField(State)
    contact = peewee.ForeignKeyField(Contact, null=True)

    @classmethod
    def get_state(cls, message):
        try:
            return MessageState.get(message=message).state
        except peewee.DoesNotExist:
            return None

    @classmethod
    def update_received_state(cls, message, state):
        try:
            mstate = MessageState.get(message=message)
        except peewee.DoesNotExist:
            mstate = MessageState(message=message)
        mstate.state = state
        mstate.save()

    @classmethod
    def set_received(cls, message):
        cls.update_received_state(message, State.get_received())

    @classmethod
    def set_received_read_remote(cls, message):
        cls.update_received_state(message, State.get_received_read_remote())

    @classmethod
    def set_received_read(cls, message):
        cls.update_received_state(message, State.get_received_read())

    @classmethod
    def set_received_remote(cls, message):
        cls.update_received_state(message, State.get_received_remote())

    @classmethod
    def set_sent(cls, message):
        messageState = MessageState.get_or_create(
            message=message, state=State.get_sent_queued())[0]
        messageState.state = State.get_sent()
        messageState.save()

    @classmethod
    def set_sent_queued(cls, message):
        messageState = MessageState(message=message,
                                    state=State.get_sent_queued())
        messageState.save()

    @classmethod
    def set_sent_delivered(cls, message, contact=None):
        if contact:
            messageState = MessageState.get(message=message, contact=contact)
        else:
            messageState = MessageState.get(message=message)
        messageState.state = State.get_sent_delivered()
        messageState.save()

    @classmethod
    def set_sent_read(cls, message, contact=None):
        if contact:
            messageState = MessageState.get(message=message, contact=contact)
        else:
            messageState = MessageState.get(message=message)
        messageState.state = State.get_sent_read()
        messageState.save()
Пример #10
0
class Broadcast(db.get_base_model()):
    wId = peewee.CharField() #Can't remember so far what that was for, but it's in openwa so maybe important
    created = peewee.DateTimeField(default=datetime.datetime.now())
Пример #11
0
class GroupContact(db.get_base_model()):
    contact = peewee.ForeignKeyField(Contact)
    group = peewee.ForeignKeyField(Group)
Пример #12
0
class BroadcastContact(db.get_base_model()):
    contact = peewee.ForeignKeyField(Contact)
    broadcast = peewee.ForeignKeyField(Broadcast)