コード例 #1
0
ファイル: views.py プロジェクト: pbecotte/devblog
def detail(slug):
    if current_user.is_authenticated():
        query = Entry.query
    else:
        query = Entry.public()
    entry = get_object_or_404(query, Entry.slug == slug)
    image_url = '{bucket}{path}/{filename}'.format(
        bucket=current_app.config['S3_LOCATION'],
        path=current_app.config['S3_UPLOAD_DIRECTORY'],
        filename=entry.image) if entry.image else ''
    return render_template('detail.html', entry=entry, image=image_url)
コード例 #2
0
ファイル: api.py プロジェクト: pbecotte/devblog
def api_entry(slug):
    if current_user.has_role('admin'):
        query = Entry.query
    else:
        query = Entry.public()
    entry_model = get_object_or_404(query, Entry.slug == slug)
    entry = EntryDetailSchema().dump(entry_model).data
    image_url = '{bucket}{path}/{filename}'.format(
        bucket=current_app.config['S3_LOCATION'],
        path=current_app.config['S3_UPLOAD_DIRECTORY'],
        filename=entry_model.image) if entry_model.image else ''
    return jsonify(data={'entry': entry, 'image': image_url})
コード例 #3
0
ファイル: api.py プロジェクト: pbecotte/devblog
def api_update_entry(slug):
    entry = get_object_or_404(Entry.query, Entry.slug == slug)
    form = UpdateForm()
    try:
        input_dict = EntryDetailSchema(strict=True).loads(form.entry.data).data
        for key, value in input_dict.items():
            setattr(entry, key, value)
        entry.image = form.image if form.image.data else entry.image
        db.session.commit()
        output = EntryDetailSchema().dump(entry).data
        return jsonify(data={'entry': output}, messages=['Saved!'])
    except ValidationError as err:
        resp = jsonify({"error": err.messages})
        resp.status_code = 401
        return resp
コード例 #4
0
ファイル: views.py プロジェクト: pbecotte/devblog
def edit(slug):
    entry = get_object_or_404(Entry.query, Entry.slug == slug)
    if request.method == 'POST':
        form = CreateForm()
        if form.title and form.content:
            entry.title = form.title.data
            entry.tagline = form.tagline.data
            entry.image = handle_image(form.image) if form.image.data else entry.image
            entry.content = form.content.data
            entry.published = form.published.data
            entry.save()

            flash('Entry saved successfully.', 'success')
            if entry.published:
                return redirect(url_for('entry.detail', slug=entry.slug))
            else:
                return redirect(url_for('entry.edit', slug=entry.slug))
        else:
            flash('Title and Content are required.', 'danger')

    form = CreateForm(obj=entry)
    image_form = ImageForm()
    return render_template('edit.html', entry=entry, form=form, image_form=image_form)