Coverage for my_app/catalog/views : 56%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
|
import os from functools import wraps from werkzeug import secure_filename from flask import request, Blueprint, render_template, jsonify, flash, \ redirect, url_for as flask_url_for, g, abort from my_app import db, app, ALLOWED_EXTENSIONS, babel from my_app.catalog.models import Product, Category, ProductForm, CategoryForm from sqlalchemy.orm.util import join from flask.ext.babel import lazy_gettext as _ import geoip
catalog = Blueprint('catalog', __name__)
@app.before_request def before():
@app.context_processor def inject_url_for(): 'url_for': lambda endpoint, **kwargs: flask_url_for( endpoint, lang=g.get('current_lang', 'en'), **kwargs ) }
url_for = inject_url_for()['url_for']
@babel.localeselector def get_locale():
def template_or_json(template=None): """"Return a dict from your view and this will either pass it to a template or render json. Use like:
@template_or_json('template.html') """ def decorated(f): @wraps(f) def decorated_fn(*args, **kwargs): return jsonify(ctx) else: return decorated_fn return decorated
def allowed_file(filename): return '.' in filename and \ filename.lower().rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.errorhandler(404) def page_not_found(e): app.logger.error(e) return render_template('404.html'), 404
@catalog.route('/') @catalog.route('/<lang>/') @catalog.route('/<lang>/home') @template_or_json('home.html') def home(): 'Home page with total of %d products' % len(products) )
@catalog.route('/<lang>/product/<id>') def product(id): app.logger.warning('Requested product not found.') abort(404)
@catalog.route('/<lang>/products') @catalog.route('/<lang>/products/<int:page>') def products(page=1):
@catalog.route('/<lang>/product-create', methods=['GET', 'POST']) def create_product():
form.category.data ) filename = secure_filename(image.filename) image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) name, price, category, filename, match and match.timezone or 'Localhost' ) unicode(_('The product %(name)s has been created', name=name)), 'success' )
@catalog.route('/<lang>/product-search') @catalog.route('/<lang>/product-search/<int:page>') def product_search(page=1): products = products.filter(Product.price == price) products = products.filter(Product.company.like('%' + company + '%')) products = products.select_from(join(Product, Category)).filter( Category.name.like('%' + category + '%') ) 'products.html', products=products.paginate(page, 10) )
@catalog.route('/<lang>/category-create', methods=['GET', 'POST']) def create_category():
unicode(_('The category %(name)s has been created', name=name)), 'success' ) url_for('catalog.category', id=category.id) )
@catalog.route('/<lang>/category/<id>') def category(id):
@catalog.route('/<lang>/categories') def categories(): |