# Coverage for my_app/catalog/views : 90%

## 106 statements 95 run 11 missing 0 excluded

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | 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(): if request.view_args and 'lang' in request.view_args: g.current_lang = request.view_args['lang'] request.view_args.pop('lang') @app.context_processor def inject_url_for(): return { '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(): return g.get('current_lang', 'en') 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): ctx = f(*args, **kwargs) if request.is_xhr or not template: return jsonify(ctx) else: return render_template(template, **ctx) 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(): products = Product.query.all() app.logger.info( 'Home page with total of %d products' % len(products) ) return {'count': len(products)} @catalog.route('/<lang>/product/<id>') def product(id): product = Product.query.filter_by(id=id).first() if not product: app.logger.warning('Requested product not found.') abort(404) return render_template('product.html', product=product) @catalog.route('/<lang>/products') @catalog.route('/<lang>/products/<int:page>') def products(page=1): products = Product.query.paginate(page, 10) return render_template('products.html', products=products) @catalog.route('/<lang>/product-create', methods=['GET', 'POST']) def create_product(): form = ProductForm(request.form) if form.validate_on_submit(): name = form.name.data price = form.price.data category = Category.query.get_or_404( form.category.data ) image = request.files and request.files['image'] filename = '' if image and allowed_file(image.filename): filename = secure_filename(image.filename) image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) match = geoip.geolite2.lookup(request.remote_addr) product = Product( name, price, category, filename, match and match.timezone or 'Localhost' ) db.session.add(product) db.session.commit() flash( unicode(_('The product %(name)s has been created', name=name)), 'success' ) return redirect(url_for('catalog.product', id=product.id)) if form.errors: flash(form.errors, 'danger') return render_template('product-create.html', form=form) @catalog.route('/<lang>/product-search') @catalog.route('/<lang>/product-search/<int:page>') def product_search(page=1): name = request.args.get('name') price = request.args.get('price') company = request.args.get('company') category = request.args.get('category') products = Product.query if name: products = products.filter(Product.name.like('%' + name + '%')) if price: products = products.filter(Product.price == price) if company: products = products.filter(Product.company.like('%' + company + '%')) if category: products = products.select_from(join(Product, Category)).filter( Category.name.like('%' + category + '%') ) return render_template( 'products.html', products=products.paginate(page, 10) ) @catalog.route('/<lang>/category-create', methods=['GET', 'POST']) def create_category(): form = CategoryForm(request.form) if form.validate_on_submit(): name = form.name.data category = Category(name) db.session.add(category) db.session.commit() flash( unicode(_('The category %(name)s has been created', name=name)), 'success' ) return redirect( url_for('catalog.category', id=category.id) ) if form.errors: flash(form.errors, 'danger') return render_template('category-create.html', form=form) @catalog.route('/<lang>/category/<id>') def category(id): category = Category.query.get_or_404(id) return render_template('category.html', category=category) @catalog.route('/<lang>/categories') def categories(): categories = Category.query.all() return render_template('categories.html', categories=categories)