示例#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
    }
示例#2
0
def view(country_id=None):
    app = flask.current_app
    country = Country.get_or_404(country_id)
    return {
        "mk": sugar.MarkupGenerator(
            app.jinja_env.get_template("widgets/widgets_view.html")
        ),
        "country": country,
        "country_schema": CountrySchema(country)
    }
示例#3
0
def json(country_iso=None):
    app = flask.current_app
    country = Country.get_country_by_iso(country_iso)
    if country:
        response = {
                'id':  country.id,
                'data': country
        }
        return flask.jsonify(response)
    else:
        return flask.abort(404)
示例#4
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,
    }