Esempio n. 1
0
def edit_excursion(key_id, uid):
    excursion_type = ExcursionType.retrieve_by_id(key_id)
    if not excursion_type:
        return redirect(url_for('excursion.admin.index'))
    excursion = None
    for ex in excursion_type.excursions:
        if ex.uid == uid:
            excursion = ex
            break
    else:
        return redirect(url_for('excursion.admin.edit', key_id=key_id))
    if request.method == 'POST' and 'delete_btn' in request.form:
        for i, ex in enumerate(excursion_type.excursions):
            if ex.uid == uid:
                del excursion_type.excursions[i]
                excursion_type.put()
                break
        return redirect(url_for('excursion.admin.edit', key_id=key_id))
    form = ExcursionForm(obj=excursion)
    if request.method == 'POST' and form.validate_on_submit():
        for i, ex in enumerate(excursion_type.excursions):
            if ex.uid == uid:
                form.populate_obj(excursion_type.excursions[i])
                excursion_type.put()
                break
        return redirect(url_for('excursion.admin.edit', key_id=key_id))
    return render_template(
        'excursion/admin/excursion/edit.html',
        ex_form=form,
        excursion_type=excursion_type,
        excursion=excursion
    )
Esempio n. 2
0
def add_excursion(key_id):
    excursion_type = ExcursionType.retrieve_by_id(key_id)
    if not excursion_type:
        return redirect(url_for('excursion.admin.index'))
    if request.method == 'POST' and 'cancel_btn' in request.form:
        return redirect(url_for('excursion.admin.edit', key_id=excursion_type.key.id()))
    form = ExcursionForm()
    if request.method == 'POST' and form.validate_on_submit():
        excursion = Excursion()
        form.populate_obj(excursion)
        excursion.uid = str(uuid.uuid1()).replace('-', '')
        excursion_type.excursions.append(excursion)
        excursion_type.put()
        return redirect(url_for('excursion.admin.edit', key_id=excursion_type.key.id()))
    return render_template(
        'excursion/admin/excursion/add.html',
        ex_form=form,
        excursion_type=excursion_type
    )