Esempio n. 1
0
def home():
    return {
            'countries': [Country.from_flat(country_row)
                          for country_row in database.get_all("country")],
            'regions': CountrySchema.regions,
            'date_types': CountrySchema.date_types
    }
Esempio n. 2
0
def edit(country_id=None):
    app = flask.current_app
    session = database.get_session()
    if country_id:
        country_row = database.get_or_404("country", country_id)
        country = Country.from_flat(country_row)
    else:
        country_row = database.new("country")
        country = None

    if flask.request.method == "POST":
        form_data = dict(CountrySchema.from_defaults().flatten())
        form_data.update(flask.request.form.to_dict())

        country_schema = CountrySchema.from_flat(form_data)

        if country_schema.validate():
            if country_row is None:
                country_row = database.new('country')
            country_row.update(country_schema.flatten())
            session.save(country_row)
            session.commit()
            flask.flash('Country saved.', 'success')
            view_url = flask.url_for("country.home")
            return flask.redirect(view_url)
        else:
            flask.flash(u"Errors in country information", "error")
    else:
        if country_row is None:
            country_schema = CountrySchema()
        else:
            country_schema = CountrySchema.from_flat(country_row)

    return {
        "mk": sugar.MarkupGenerator(
            app.jinja_env.get_template("widgets/widgets_edit.html")
        ),
        "country_schema": country_schema,
        "country": country,
    }