コード例 #1
0
def get_countries():
    """Gets all countires with id, name value pairs """
    dbsession = DBSession()
    countries_q = dbsession.query(Country).order_by(Country.name)
    countries = [(country.id, country.name) for country in countries_q.all()]

    return countries
コード例 #2
0
def get_categories():
    """Gets all categories with id name value pairs """
    dbsession = DBSession()
    categories_q = dbsession.query(Category).order_by(Category.name)
    categories = [(category.id, category.name)
                  for category in categories_q.all()]

    return categories
コード例 #3
0
def new(request):
    """new country """
    form = Form(request, schema=CountryForm)
    if "form_submitted" in request.POST and form.validate():
        dbsession = DBSession()
        country = form.bind(Country())
        # TODO: db error control?
        dbsession.add(country)
        request.session.flash("warning;New Country is saved!")
        return HTTPFound(location=request.route_url("country_list"))

    return dict(form=FormRenderer(form),
                action_url=request.route_url("country_new"))
コード例 #4
0
def new(request):
    """new customer """
    categories = get_categories()
    countries = get_countries()

    form = Form(request, schema=CustomerForm)
    if "form_submitted" in request.POST and form.validate():
        dbsession = DBSession()
        customer = form.bind(Customer())
        dbsession.add(customer)
        request.session.flash("warning;New Customer is saved!")
        return HTTPFound(location=request.route_url("customer_list"))

    return dict(form=FormRenderer(form),
                categories=categories,
                countries=countries,
                action_url=request.route_url("customer_new"))
コード例 #5
0
def edit(request):
    """country edit """
    id = request.matchdict['id']
    dbsession = DBSession()
    country = dbsession.query(Country).filter_by(id=id).one()
    if country is None:
        request.session.flash("error;Country not found!")
        return HTTPFound(location=request.route_url("country_list"))

    form = Form(request, schema=CountryForm, obj=country)
    if "form_submitted" in request.POST and form.validate():
        form.bind(country)
        dbsession.add(country)
        request.session.flash("warning;The Country is saved!")
        return HTTPFound(location=request.route_url("country_list"))

    action_url = request.route_url("country_edit", id=id)
    return dict(form=FormRenderer(form), action_url=action_url)
コード例 #6
0
def delete(request):
    """country delete """
    id = request.matchdict['id']
    dbsession = DBSession()
    country = dbsession.query(Country).filter_by(id=id).first()
    if country is None:
        request.session.flash("error;Country not found!")
        return HTTPFound(location=request.route_url("country_list"))

    try:
        transaction.begin()
        dbsession.delete(country)
        transaction.commit()
        request.session.flash("warning;The country is deleted!")
    except IntegrityError:
        # delete error
        transaction.abort()
        request.session.flash("error;The country could not be deleted!")

    return HTTPFound(location=request.route_url("country_list"))
コード例 #7
0
def list(request):
    """customers list """
    search = request.params.get("search", "")

    sort = "company_name"
    if request.GET.get("sort") and request.GET.get("sort") in \
            ["company_name", "contact_first_name", "contact_last_name", "category"]:
        sort = request.GET.get("sort")
    if sort == "category":
        sort = "category.name"

    direction = "asc"
    if request.GET.get("direction") and request.GET.get("direction") in [
            "asc", "desc"
    ]:
        direction = request.GET.get("direction")

    # db query
    dbsession = DBSession()
    query = dbsession.query(Customer).join(Category).\
        filter(Customer.company_name.like(search + "%")).\
                   order_by(sort + " " + direction)

    # paginate
    page_url = paginate.PageURL_WebOb(request)
    customers = Page(query,
                     page=int(request.params.get("page", 1)),
                     items_per_page=10,
                     url=page_url)

    if "partial" in request.params:
        # Render the partial list page
        return render_to_response("customer/listPartial.html",
                                  {"customers": customers},
                                  request=request)
    else:
        # Render the full list page
        return render_to_response("customer/list.html",
                                  {"customers": customers},
                                  request=request)
コード例 #8
0
def list(request):
    """countries list """
    search = request.params.get("search", "")

    sort = "code"
    if request.GET.get("sort") and request.GET.get("sort") in ["code", "name"]:
        sort = request.GET.get("sort")

    direction = "asc"
    if request.GET.get("direction") and request.GET.get("direction") in [
            "asc", "desc"
    ]:
        direction = request.GET.get("direction")

    # db query
    dbsession = DBSession()
    query = dbsession.query(Country).\
        filter(or_(Country.code.like(search + "%"),
                   Country.name.like(search + "%"))).\
                   order_by(sort + " " + direction)

    # paginate
    page_url = paginate.PageURL_WebOb(request)
    countries = Page(query,
                     page=int(request.params.get("page", 1)),
                     items_per_page=10,
                     url=page_url)

    if "partial" in request.params:
        # Render the partial list page
        return render_to_response("country/listPartial.html",
                                  {"countries": countries},
                                  request=request)
    else:
        # Render the full list page
        return render_to_response("country/list.html",
                                  {"countries": countries},
                                  request=request)
コード例 #9
0
def edit(request):
    """customer edit """
    id = request.matchdict['id']
    dbsession = DBSession()
    customer = dbsession.query(Customer).filter_by(id=id).one()
    if customer is None:
        request.session.flash("error;Customer not found!")
        return HTTPFound(location=request.route_url("customer_list"))

    categories = get_categories()
    countries = get_countries()

    form = Form(request, schema=CustomerForm, obj=customer)
    if "form_submitted" in request.POST and form.validate():
        form.bind(customer)
        dbsession.add(customer)
        request.session.flash("warning;The Customer is saved!")
        return HTTPFound(location=request.route_url("customer_list"))

    action_url = request.route_url("customer_edit", id=id)
    return dict(form=FormRenderer(form),
                categories=categories,
                countries=countries,
                action_url=action_url)