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
def delete(request):
    """customer delete """
    id = request.matchdict['id']
    dbsession = DBSession()
    customer = dbsession.query(Customer).filter_by(id=id).first()
    address = dbsession.query(Address).filter_by(user_id=id).first()
    emails = dbsession.query(Email).filter_by(user_id=id).first()
    phones = dbsession.query(Phone).filter_by(user_id=id).first()
    
    if customer is None:
        request.session.flash("error;Customer not found!")
        return HTTPFound(location=request.route_url("customer_list"))        
    
    try:
        transaction.begin()
        dbsession.delete(customer)
        dbsession.delete(address)
        dbsession.delete(emails)
        dbsession.delete(phones)
        transaction.commit()
        request.session.flash("warning;The customer is deleted!")
    except IntegrityError:
        # delete error
        transaction.abort()
        request.session.flash("error;The customer could not be deleted!")
    
    return HTTPFound(location=request.route_url("customer_list"))
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
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
def list(request):
    """categories list """
    search = request.params.get("search", "")
        
    sort= "name"
    if request.GET.get("sort") and request.GET.get("sort") == "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(Category).\
        filter(Category.name.like(search + "%")).\
        order_by(sort + " " + direction)

    # paginate
    page_url = paginate.PageURL_WebOb(request)
    categories = 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("category/listPartial.html",
                                  {"categories": categories},
                                  request=request)
    else:
        # Render the full list page
        return render_to_response("category/list.html",
                                  {"categories": categories},
                                  request=request)
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
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"))
Exemple #8
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"))
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"))
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"))
def edit(request):
    """customer edit """
    id = request.matchdict['id']
    dbsession = DBSession()
    customer = dbsession.query(Customer).filter_by(id=id).one()
    print customer
    address = dbsession.query(Address).filter_by(user_id=id).one()
    
    
    if customer is None or address is None:
        request.session.flash("error;Customer not found!")
        return HTTPFound(location=request.route_url("customer_list"))        
    

    generalForm = Form(request, schema=CustomerForm, obj=customer)
    locationForm = Form(request, schema=LocationForm, obj=address)
       
    if "general_submitted" in request.POST and generalForm.validate():
        generalForm.bind(customer)
        dbsession.add(customer)
        request.session.flash("success; Successful call to update customer")
    if "location_submitted" in request.POST:
        locationForm.bind(address)
        dbsession.add(address)
        request.session.flash("success; Successful call to update location")
        


    action_url = request.route_url("customer_edit", id=id)
    return dict(generalForm=FormRenderer(generalForm),
                locationForm=FormRenderer(locationForm), 
                action_url=action_url)
Exemple #12
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)
Exemple #13
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"))
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"))
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)
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)
Exemple #17
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)
def list(request):
    """customers list """
    search = request.params.get("search", "")
        
    sort= "first_name"
    if request.GET.get("sort") and request.GET.get("sort") in \
            ["contact_first_name", "contact_last_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(Customer).\
        filter(Customer.first_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=30, 
                     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)
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)
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)
def new(request):
    """new customer """
    #categories = get_categories()
    #countries = get_countries()
    
    generalForm = Form(request, schema=CustomerForm)
    locationForm = Form(request, schema=LocationForm)    
    if "form_submitted" in request.POST and generalForm.validate():
        dbsession = DBSession()
        customer = generalForm.bind(Customer())
        dbsession.add(customer)
        request.session.flash("warning;New Customer is saved!")
        return HTTPFound(location = request.route_url("customer_list"))
    
    if "location_submitted" in request.POST and locationForm.validate():
        dbsession = DBSession()
        location = locationForm.bind(Address())
        dbsession.add(location)
        request.session.flash("warning;Customer Address is saved!")
        
    return dict(generalForm=FormRenderer(generalForm), 
                action_url=request.route_url("customer_new"))