Example #1
0
class Entity(AppModel):
    def __init__(self):
        log.info("Entity::__init__")

        self.dbconn = DboMySQL()
        self.table = "entities"
        self._fields = self._getFields()

        self.storage_path = os.path.abspath(config.get("path", "storage"))

        self.Transaction = Transaction()

    def __actionToInt(self, action):
        log.info("Entity::__actionToInt")
        log.debug(action)

        result = None
        if action == "upload":
            result = UPLOAD
        elif action == "download":
            result = DOWNLOAD
        elif action == "delete":
            result = DELETE

        return result

    def __getSkipedEntities(self, params):
        rows = self.Transaction.getSkiped(params)
        skiped_entities = self.Transaction.prepareToClient(rows)
        for entity in skiped_entities:
            if entity["action"] == "upload":
                entity["action"] = "download"

        return skiped_entities

    def syncEntities(self, user_id, client_id, params):
        log.info("Entity::syncEntities")
        log.debug(params)

        # get unfinished transactions
        data = {
            "user_id": user_id,
            "client_id": client_id,
            "finished": self.Transaction.getLastSyncedDate(user_id, client_id),
        }
        log.debug(data)
        entities = self.__getSkipedEntities(data)

        # !!!!!! ADD REMOVE EXPIRED TRANSACTION
        self.Transaction.removeUncompleted(user_id, client_id)

        # ????
        # rows = self.Transaction.getUnCompleted(user_id, client_id)
        # entities = self.Transaction.prepareToClient(rows)
        # entities = entities + skiped_entities

        log.debug("Previous skiped transactions:")
        log.debug(entities)

        if params.has_key("entities"):
            data = {"user_id": user_id, "client_id": client_id}
            tickets = self.__sync(data, params.get("entities"))

        if len(tickets) > 0:
            log.debug("Tickets: %s" % tickets)
            rows = self.Transaction.findByUserTickets(user_id, client_id, tickets)
            rows = self.Transaction.prepareToClient(rows)
            entities = entities + rows

        log.debug("Entities: %s" % entities)
        entities = {"command": "syncEntities", "entities": entities}

        return entities

    def __sync(self, params, entities):
        log.info("Entity::__sync")
        log.debug(entities)

        tickets = []
        try:
            for entity in entities:
                type, path = entity.get("path").split(":")

                if type == "files":
                    switch = self.__files(params.get("user_id"), params.get("client_id"), entity)
                else:
                    switch = self.__undefined(type, entity)

                # switch = {
                #    'files': self.__files(params.get('user_id'), params.get('client_id'), entity)
                # }.get(type, self.__undefined(type, entity))

                if not switch is None:
                    tickets.append(switch)

        except Exception, e:
            log.error("Entities synchronization error %s" % repr(e))

        return tickets