Пример #1
0
    def store_and_retrieve(self):
        revision = Revision.query.first()

        # Permit this to compute in headless environments.
        if not revision:
            print "Couldn't find a revision to test with."
            choice = raw_input("Create revision [y/n]: ")
            if choice.lower() != "y":
                raise SystemExit

            revision         = Revision()
            revision.content = "Hello, world."
            revision.size    = len(revision.content)
            revision.hash    = hashlib.sha1(revision.content).hexdigest()
            revision.get_mimetype()
    
            db.session.add(revision)
            db.session.commit()
            
            print "New revision committed."

        # Make some transactions and then calculate_trust
        for i in range(10):
            self.honest_peers.values()[i][revision] = revision
        
        # This can take a long time.
        for i in range(10):
            self.honest_peers.values()[i].tbucket.calculate_trust()
        
        # Adjust trust ratings manually and then calculate_trust
        p = random.choice([p for p in self.peers[0]])
        p.trust = random.randint(1,100)
        self.honest_peers.values()[0].tbucket.calculate_trust()
        
        self.assertEqual(self.honest_peers.values()[0][revision], revision) 
Пример #2
0
    def put(self, hash):
        """
        Create an edit of a revision.
        """
        user   = auth(session, required=True)
        parser = restful.reqparse.RequestParser()
        parser.add_argument("document",type=str, required=True)
        args   = parser.parse_args()  

        parent = Revision.query.filter(and_(Revision.hash == hash, Revision.user == user)).first()
        if parent != None:
            revision         = Revision()
            revision.user    = user
            revision.parent  = parent
            revision.content = args.document
            revision.size    = len(revision.content)
            revision.hash    = hashlib.sha1(args.document).hexdigest()
            revision.get_mimetype()
            db.session.add(revision)
            db.session.add(parent)
            db.session.commit()
            return revision.jsonify(), 201
        return {}, 404