Exemple #1
0
    def post(self, username):
        user   = auth(session, required=True)
        
        if not 'avatar' in request.files:
            return {}, 400
        
        upload = request.files['avatar']

        revision = Revision()
        revision.add(upload)
        
        existing = Revision.query.filter(
                        Revision.hash == revision.hash
                   ).first()

        if existing: # We already have this file
            user.avatar = existing
            if not existing in user.revisions:
                user.revisions.append(existing)
            db.session.add(existing)
        else:
            user.avatar = revision
            user.revisions.append(revision)
            db.session.add(revision)

        db.session.add(user)
        db.session.commit()
        
        return redirect("/")
Exemple #2
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) 
Exemple #3
0
    def post(self, username):
        user = auth(session, required=True)
        
        if not 'revision' in request.files:
            return {}, 400
        
        upload = request.files['revision']

        revision = Revision()
        revision.add(upload)
        
        if user.public:
            revision.public = True

        existing = Revision.query.filter(
                        Revision.hash == revision.hash
                   ).first()

        if existing: # We already have this file
            if not existing in user.revisions:
                user.revisions.append(existing)
            db.session.add(existing)
        else:
            user.revisions.append(revision)
            db.session.add(revision)

        db.session.add(user)
        db.session.commit()
       
        if revision.public:
            threads = []
            for router in app.routes.values():
                threads.append(gevent.spawn(router.__setitem__, revision, revision))
            gevent.joinall(threads)

        return redirect("/")
Exemple #4
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
Exemple #5
0
def get(url, user_agent, user=None):
    """
    Return a Revision of a url.
    Check for the presence of a URL
     At its original location on the web.
     In the DHT.
     In the database.
    """
    revision = Revision()

    if user and not user.can("retrieve_resource"):
        revision.status   = 403
        revision.mimetype = "text"
        revision.content  = "This user account isn't allowed to retrieve resources."
        return revision

    # Ignore for now if the addr is on our LAN, VLAN or localhost.
    url    = urlparse.urlparse(url)
    domain = url.netloc
    path   = url.path or '/'

    try:    host = socket.gethostbyname(domain)
    except: host = ''

    if host:
        log(host)

    # Deep assumptions about the future of ipv4 here.
    if any([host.startswith(subnet) for subnet in local_subnets]):
        revision.status   = 403
        revision.mimetype = "text"
        revision.content  = "We're not currently proxying to local subnets."
        return revision

    # Check the web
    response = None
    try:
        log("Fetching %s from the original domain." % url.geturl())
        response = requests.get(url.geturl(), headers={'User-Agent': user_agent}, 
            timeout=app.config['HTTP_TIMEOUT'])
    except Exception, e:
        log("Error retrieving %s: %s" % (url.geturl(), e.message))