Пример #1
0
def edit(note_url):
    note_uid = unquote_plus(note_url)
    if request.method == 'GET':
        note = get_note(note_uid)
        return render_template('edit.html',
                               note=note,
                               highlight_css=c.highlight_css)
    else:
        new_text = request.form.get('note_text')
        old_note = get_note(note_uid)
        try:
            new_note = Note(new_text)
        except (ValueError, AttributeError) as e:
            flash(str(e))
            old_note.text = new_text
            return render_template('edit.html',
                                   note=old_note,
                                   highlight_css=c.highlight_css)
        if new_note.uid != old_note.uid and note_exists(new_note.uid):
            flash('Modified note has same date and title as another note. '
                  'File was not created.')
            return render_template('edit.html',
                                   note=new_note,
                                   highlight_css=c.highlight_css)
        else:
            old_note.remove_file()
            new_note.create_file()
            return redirect(url_for('render', note_url=new_note.url))
Пример #2
0
def save(note_url):
    note_uid = unquote_plus(note_url)
    new_text = request.form.get('note_text')
    old_note = get_note(note_uid)
    try:
        new_note = Note(new_text)
    except (ValueError, AttributeError) as e:
        print(str(e))
        return jsonify({
            'message': f'Not saved. {str(e)}',
            'success': False,
            'data': ''
        })
    if new_note.uid != old_note.uid and note_exists(new_note.uid):
        message = 'Not saved. Modified note has same date and title as ' \
                  'another note. File was not created.'
        return jsonify({'message': message, 'success': False, 'data': ''})
    else:
        old_note.remove_file()
        new_note.create_file()
        return jsonify({
            'message': 'File saved.',
            'success': True,
            'old_url': old_note.url,
            'new_url': new_note.url,
            'data': render_markdown(new_text)
        })
Пример #3
0
def new():
    if request.method == 'GET':
        curr_date = datetime.now().strftime('%Y-%m-%d')
        default_text = """---
title: New note
date: %s
---""" % curr_date
        return render_template('new.html', default_text=default_text)
    else:
        new_text = request.form.get('note_text')
        try:
            note = Note(new_text)
        except (ValueError, AttributeError) as e:
            flash(str(e))
            return render_template('new.html', default_text=new_text)
        if note_exists(note.uid):
            flash('New note has same date and title as another note.')
            return render_template('new.html', default_text=new_text)

        note.create_file()
        return redirect(url_for('render', note_uid=note.uid))