Exemplo n.º 1
0
    def test_url(self):
        with app.test_request_context():
            p = Paste.new(text='test')
            url = utils.create_paste_url(p)
            assert url == 'http://localhost/p/1/'

            url = utils.create_paste_url(p, relative=True)
            assert url == '/p/1/'
Exemplo n.º 2
0
    def test_url(self):
        with app.test_request_context():
            p = Paste.new(text='test')
            url = utils.create_paste_url(p)
            assert url == 'http://localhost/p/1/'

            url = utils.create_paste_url(p, relative=True)
            assert url == '/p/1/'
Exemplo n.º 3
0
def get():
    p_id = request.args.get('id', None, type=int)
    p_hash = request.args.get('hash', None)
    password = request.args.get('password')

    if p_hash:
        paste = Paste.by_hash(p_hash)
    elif p_id:
        paste = Paste.by_id(p_id)
    else:
        return jsonify(error='no id or hash supplied'), 400

    if paste is None:
        return jsonify(error='paste not found'), 404
    elif not p_hash and paste['unlisted']:
        return jsonify(error='paste is unlisted'), 400

    if paste['password']:
        if not password:
            return jsonify(error='paste is password protected'), 401
        elif not Paste.password_match(paste['hash'], password):
            return jsonify(error='incorrect password'), 401

    return jsonify(create_paste_dict(paste),
                   shorturl=paste['shortlink'],
                   url=create_paste_url(paste))
Exemplo n.º 4
0
def add():
    form = request.form
    errors = []

    if form.get('unlisted', type=int) in (0, 1):
        unlisted = bool(form.get('unlisted', type=int))
    else:
        unlisted = False

    paste = {
        'text': form.get('contents'),
        'title': form.get('title'),
        'password': form.get('password'),
        'unlisted': unlisted,
        'language': form.get('language', 'text')
    }

    if paste['text'] is None:
        errors.append('No contents specified')
    if paste['unlisted'] not in (True, False):
        errors.append("Invalid value: (unlisted: '{0}')".format(
            paste['unlisted']))

    if errors:
        return jsonify(success=False, url=None, password=None, error=errors)

    p = Paste.new(**paste)
    if p is None:
        return jsonify(success=False, url=None, password=None, error=errors)

    return jsonify(success=True,
                   url=create_paste_url(p),
                   password=paste['password'])
Exemplo n.º 5
0
    def new(self,
            text,
            title=None,
            language='text',
            password=None,
            unlisted=False):
        """
        Insert a new paste into the database.
        Returns the paste as a dict if successful.

        """
        if title is None:
            title = 'Untitled'

        (highlighted, language) = self._highlight(text, language)

        if unlisted not in (True, False):
            unlisted = False

        if password is not None:
            password = self._hash_password(password)

        _hash = md5(urandom(64)).hexdigest()
        created = datetime.utcnow()
        created = created.replace(tzinfo=pytz.utc)

        cur = self._cursor()
        try:
            cur.execute(
                """
                INSERT INTO pastes
                (hash, created, title, text, highlighted, language,
                password, unlisted) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
                RETURNING *
                """, (_hash, created, title, text, highlighted, language,
                      password, unlisted))
            paste = cur.fetchone()

            shortlink = None
            if not environ.get('PYPASTE_TESTING'):
                shortlink = self.get_shortlink(create_paste_url(paste))
            if shortlink is not None:
                cur.execute(
                    """
                    UPDATE pastes SET shortlink = %s
                    WHERE hash = %s
                    """, (shortlink, paste['hash']))

            self.conn.commit()
            cur.close()
            return paste

        except psycopg2.Error as e:
            print e
            self.conn.rollback()
            cur.close()
            return None
Exemplo n.º 6
0
    def new(self, text, title=None, language='text', password=None, unlisted=False):
        """
        Insert a new paste into the database.
        Returns the paste as a dict if successful.

        """
        if title is None:
            title = 'Untitled'

        (highlighted, language) = self._highlight(text, language)

        if unlisted not in (True, False):
            unlisted = False

        if password is not None:
            password = self._hash_password(password)

        _hash = md5(urandom(64)).hexdigest()
        created = datetime.utcnow()
        created = created.replace(tzinfo=pytz.utc)

        cur = self._cursor()
        try:
            cur.execute(
                """
                INSERT INTO pastes
                (hash, created, title, text, highlighted, language,
                password, unlisted) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
                RETURNING *
                """,
                (_hash, created, title, text, highlighted, language,
                password, unlisted)
            )
            paste = cur.fetchone()

            shortlink = None
            if not environ.get('PYPASTE_TESTING'):
                shortlink = self.get_shortlink(create_paste_url(paste))
            if shortlink is not None:
                cur.execute(
                    """
                    UPDATE pastes SET shortlink = %s
                    WHERE hash = %s
                    """, (shortlink, paste['hash'])
                )

            self.conn.commit()
            cur.close()
            return paste

        except psycopg2.Error as e:
            print e
            self.conn.rollback()
            cur.close()
            return None
Exemplo n.º 7
0
def add():
    form = request.form
    errors = []

    if form.get('unlisted', type=int) in (0, 1):
        unlisted = bool(form.get('unlisted', type=int))
    else:
        unlisted = False

    paste = {
        'text': form.get('contents'),
        'title': form.get('title'),
        'password': form.get('password'),
        'unlisted': unlisted,
        'language': form.get('language', 'text')
    }

    if paste['text'] is None:
        errors.append('No contents specified')
    if paste['unlisted'] not in (True, False):
        errors.append(
            "Invalid value: (unlisted: '{0}')".format(paste['unlisted'])
        )

    if errors:
        return jsonify(
            success=False,
            url=None,
            password=None,
            error=errors
        )

    p = Paste.new(**paste)
    if p is None:
        return jsonify(
            success=False,
            url=None,
            password=None,
            error=errors
        )

    return jsonify(
        success=True,
        url=create_paste_url(p),
        password=paste['password']
    )
Exemplo n.º 8
0
def new():
    """
    Endpoint for creating a new paste.
    """
    form = request.form

    text = form.get('text')
    if text is None:
        return jsonify(error='required value missing: text'), 400

    unlisted = form.get('unlisted', 'f')
    if unlisted.lower() in ('1', 'true', 't', 'y'):
        unlisted = True
    else:
        unlisted = False

    paste = {
        'text': text,
        'title': form.get('title'),
        'language': form.get('lang', 'text'),
        'password': form.get('password'),
        'unlisted': unlisted,
    }

    p = Paste.new(**paste)
    if not Paste:
        return internal_server_error()

    response = {
        'url': create_paste_url(p),
        'shorturl': p['shortlink'],
        'paste': create_paste_dict(p),
        'password': paste['password'],
    }

    return jsonify(response)
Exemplo n.º 9
0
def new():
    """
    Endpoint for creating a new paste.
    """
    form = request.form

    text = form.get("text")
    if text is None:
        return jsonify(error="required value missing: text"), 400

    unlisted = form.get("unlisted", "f")
    if unlisted.lower() in ("1", "true", "t", "y"):
        unlisted = True
    else:
        unlisted = False

    paste = {
        "text": text,
        "title": form.get("title"),
        "language": form.get("lang", "text"),
        "password": form.get("password"),
        "unlisted": unlisted,
    }

    p = Paste.new(**paste)
    if not Paste:
        return internal_server_error()

    response = {
        "url": create_paste_url(p),
        "shorturl": p["shortlink"],
        "paste": create_paste_dict(p),
        "password": paste["password"],
    }

    return jsonify(response)
Exemplo n.º 10
0
def get():
    p_id = request.args.get("id", None, type=int)
    p_hash = request.args.get("hash", None)
    password = request.args.get("password")

    if p_hash:
        paste = Paste.by_hash(p_hash)
    elif p_id:
        paste = Paste.by_id(p_id)
    else:
        return jsonify(error="no id or hash supplied"), 400

    if paste is None:
        return jsonify(error="paste not found"), 404
    elif not p_hash and paste["unlisted"]:
        return jsonify(error="paste is unlisted"), 400

    if paste["password"]:
        if not password:
            return jsonify(error="paste is password protected"), 401
        elif not Paste.password_match(paste["hash"], password):
            return jsonify(error="incorrect password"), 401

    return jsonify(create_paste_dict(paste), shorturl=paste["shortlink"], url=create_paste_url(paste))