Exemple #1
0
def save():
    """
    :kind: API

    save a new snippet.

    post parameters:
    :source: source code to save
    :language: source language
    :driver: driver
    :predecessor: snippet id
    
    returns a json object with these attributes:
    :_id: snippet id
    :blob_id: blob id
    :language: language
    :driver: driver
    :predecessor: predecessor
    :revision: time saved
    """

    blob_id = add_blob(request.form['source'])

    snippet = {
        'blob_id': blob_id,
        'language': request.form['language'],
        'driver': request.form['driver'],
        'predecessor': request.form['predecessor'],
        'revision': now()
    }

    snippet['_id'] = sha(canonical_json(snippet))

    # silently ignore duplicate keys (assuming that
    # same hash => same object :/)
    g.db.snippets.insert(snippet, safe=False)

    return simplejson.dumps(snippet)
Exemple #2
0
def bookmarklet(blob_id, language):
    """
    :kind: internal

    create a new snippet from the blob at `blob_id` in language `language`
    and open the editor there.
    used by the bookmarklet.
    """

    snippet = {
        'blob_id': blob_id,
        'language': language,
        'driver': 'some_driver',
        'predecessor': '',
        'revision': now()
    }

    snippet['_id'] = sha(canonical_json(snippet))

    # silently ignore duplicate keys (assuming that
    # same hash => same object :/)
    g.db.snippets.insert(snippet, safe=False)

    return redirect(url_for('fork', id=snippet['_id']))
Exemple #3
0
def get_timestamp(when=None):
    if when is None:
        when = now()
    when = int(1000*1000*when)
    return int_to_b64(when)