Exemplo n.º 1
0
 def prepareToClient(self, rows):
     log.info('Transaction::prepareToClient')
     log.debug(rows)
     
     entities = []
     for row in rows:
         try:
             row['Entity.size'] = int(row['Entity.size'])
         except Exception, e:
             row['Entity.size'] = 0
         
         entity = {
             'path': row['Entity.path'], 
             'modified': Datetime.utcformat_datetime(row['Entity.filedate']),
             'size':row['Entity.size'], 
             'hash': row['Entity.hash'],
             'action': self.__actionToStr(row['Transaction.action']),
             'ticket': row['Transaction.ticket']
         }
         entities.append(entity)
Exemplo n.º 2
0
    def __files(self, user_id, client_id, param):
        log.info("Entity::__files")
        log.debug(param)
        log.debug({"user_id": user_id, "client_id": client_id})

        ticket = None
        # try:
        path = param.get("path")
        size = param.get("size")
        # modified = datetime.strptime(param.get('modified'), "%d-%m-%Y %H:%M:%S")
        modified = Datetime.str_to_utc(param.get("modified"))
        filedate = str(modified.strftime("%Y-%m-%d %H:%M:%S"))
        hash = param.get("hash")

        # check for valid incoming params

        removed = False
        if param.has_key("removed"):
            removed = param.get("removed") == True or param.get("removed") == "true" or param.get("removed") == "True"

        fileFolder = "/".join(re.compile(r".{1}", re.DOTALL).findall(hash))

        storedFolder = os.path.normpath("%s/%s/" % (self.storage_path, fileFolder))
        storedFile = "%s/%s" % (storedFolder, hash)

        type, folders_path = path.split(":")

        #######################################
        #        f = stored + folders_path
        #        log.debug('Type: %s' % type)
        #        log.debug('Path: %s' % f)
        #        if os.path.isfile(f):
        #            fdata = json.loads(open(f).read())
        #            if removed == True:
        #                if modified > fdata['modified']:
        #                    action = DELETE
        #            elif hash != fdata['hash'] or size != fdata['size']:
        #                if modified > fdata['modified']:
        #                    action = UPLOAD
        #                elif modified < fdata['modified']:
        #                    action = DOWNLOAD
        #
        #            return ticket
        #        else:
        #            action = UPLOAD
        #            data = {
        #                'path': path,
        #                'modified': filedate,
        #                'size': size,
        #                'hash': hash,
        #            }
        #
        #            created = File.create(f, json.dumps(data))
        #            if created is False:
        #                log.critical('Cannot create file by path: %s' % f)
        #                return ticket
        #######################################

        if removed == True:
            action = DELETE
        elif os.path.isfile(storedFile):
            return ticket
        #            if modified > fdata['modified']:
        #                action = UPLOAD
        #            elif modified < fdata['modified']:
        #                action = DOWNLOAD
        else:
            action = UPLOAD

        ################## old functionality
        #            f = stored + folders_path
        #            if removed == True:
        #                if os.path.isfile(f):
        #                    if modified <= os.path.getmtime(f):
        #                        return ticket
        #
        #                action = DELETE
        #            else:
        #                action = UPLOAD
        #                if os.path.isfile(f):
        #                    if hash != crc32(open(f).read()):
        #                        if modified > os.path.getmtime(f):
        #                            action = UPLOAD
        #                        elif modified < os.path.getmtime(f):
        #                            action = DOWNLOAD
        #                        else:
        #                            return ticket
        #                            #if os.path.exists(f):
        ##################

        log.debug("Transaction action: %s" % action)

        data = {
            "user_id": user_id,
            "path": path,
            "size": size,
            "filedate": filedate,
            "hash": hash,
            "stored": storedFolder,
        }
        entity_id = self.create(data)

        if entity_id != None:
            new_ticket = md5("%s" % (random.random())).hexdigest()

            data = {
                "user_id": user_id,
                "entity_id": entity_id,
                "client_id": client_id,
                "ticket": new_ticket,
                "action": action,
            }
            transaction_id = self.Transaction.create(data)

            if transaction_id != None:
                rowcount = self.update({"transaction_id": transaction_id}, {"id": entity_id})

                ticket = new_ticket
            else:
                log.error("Undefined inserted transaction id")
        else:
            log.error("Undefined inserted entity id")

        # except Exception, e:
        #    log.error('Incoming parameters is invalid %s' % repr(e))

        return ticket
Exemplo n.º 3
0
 def testDatetime_str_to_utc(self):
     
     #print '/'.join(re.compile(r".{1}", re.DOTALL).findall('some123456789client0987654321hash'))
     utc = Datetime.str_to_utc("2010-10-10T10:10:10")
     self.assertEqual(str(utc), '2010-10-10 10:10:10')