コード例 #1
0
def create_product():
    form = ProductForm()

    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))
        reader = geoip2.database.Reader(
            'GeoLite2-City_20190416/GeoLite2-City.mmdb')
        try:
            match = reader.city(request.remote_addr)
        except AddressNotFoundError:
            match = None
        product = Product(name, price, category, filename,
                          match and match.location.time_zone or 'Localhost')
        db.session.add(product)
        db.session.commit()
        flash(_('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)
コード例 #2
0
ファイル: views.py プロジェクト: weihaixiaoseu/flaskCookBook
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 = image.filename
            conn = S3Connection(app.config['AWS_ACCESS_KEY'],
                                app.config['AWS_SECRET_KEY'])
            bucket = conn.create_bucket(app.config['AWS_BUCKET'])
            key = bucket.new_key(filename)
            key.set_contents_from_file(image)
            key.make_public()
            key.set_metadata('Content-Type',
                             'image/' + filename.split('.')[-1].lower())
        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)
コード例 #3
0
def create_product():
    form = ProductForm(request.form, csrf_enabled=False)

    if form.validate_on_submit():
        name = form.name.data
        price = form.price.data
        # Mongo product
        # key = request.form.get('key')
        # product = Product(name=name,
        #                   key=key,
        #                   price=Decimal(price))
        # product.save()
        # SQL product
        category = Category.query.get_or_404(form.category.data)
        image = 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))
        product = Product(name, price, category, filename)
        db.session.add(product)
        db.session.commit()
        flash('The product {} has been created'.format(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)
コード例 #4
0
ファイル: views.py プロジェクト: weihaixiaoseu/flaskCookBook
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)
コード例 #5
0
def create_product():
    form = ProductForm()

    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)
            session = boto3.Session(
                aws_access_key_id=app.config['AWS_ACCESS_KEY'],
                aws_secret_access_key=app.config['AWS_SECRET_KEY'])
            s3 = session.resource('s3')
            bucket = s3.Bucket(app.config['AWS_BUCKET'])
            if bucket not in list(s3.buckets.all()):
                bucket = s3.create_bucket(
                    Bucket=app.config['AWS_BUCKET'],
                    CreateBucketConfiguration={
                        'LocationConstraint': 'ap-south-1'
                    },
                )
            bucket.upload_fileobj(image,
                                  filename,
                                  ExtraArgs={'ACL': 'public-read'})
        reader = geoip2.database.Reader(
            'GeoLite2-City_20190416/GeoLite2-City.mmdb')
        try:
            match = reader.city(request.remote_addr)
        except AddressNotFoundError:
            match = None

        product = Product(name, price, category, filename,
                          match and match.location.time_zone or 'Localhost')
        db.session.add(product)
        db.session.commit()
        flash(_('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)
コード例 #6
0
ファイル: views.py プロジェクト: welserjr/Flask_Upload_File
def create_product():
    form = ProductForm(request.form, csrf_enabled=False)
    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['image']
        filename = ''
        if image and allowed_file(image.filename):
            filename = secure_filename(image.filename)
            image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        product = Product(name, price, category, filename)
        db.session.add(product)
        db.session.commit()
        flash('The product %s has been created' % 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)
コード例 #7
0
def create_product():
    form = ProductForm()

    if form.validate_on_submit():
        name = form.name.data
        price = form.price.data
        category = Category.query.get_or_404(form.category.data)
        image = form.image.data
        if allowed_file(image.filename):
            filename = secure_filename(image.filename)
            image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        product = Product(name, price, category, filename)
        db.session.add(product)
        db.session.commit()
        flash('The product %s has been created' % 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)
コード例 #8
0
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 = image.filename
            conn = S3Connection(
                app.config['AWS_ACCESS_KEY'], app.config['AWS_SECRET_KEY']
            )
            bucket = conn.create_bucket(app.config['AWS_BUCKET'])
            key = bucket.new_key(filename)
            key.set_contents_from_file(image)
            key.make_public()
            key.set_metadata(
                'Content-Type', 'image/' + filename.split('.')[-1].lower()
            )
        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)
コード例 #9
0
def create_product():
    form = ProductForm(request.form, csrf_enabled=False)

    if form.validate_on_submit():
        name = request.form.get('name')
        price = request.form.get('price')
        category = Category.query.get_or_404(request.form.get('category'))
        image = request.files['image']
        filename = ''

        if image and allow_files(image.filename):
            filename = secure_filename(image.filename)
            image.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

        product = Product(name, price, category, filename)
        db.session.add(product)
        db.session.commit()
        flash('The product %s has been created' % name, 'success')
        return redirect(url_for('catalog.product', id=product.id))

    if form.errors:
        flash("Some messages you type in need to be fixed.", 'danger')

    return render_template('product-create.html', form=form)
コード例 #10
0
def create_product():
    form = ProductForm(request.form)

    categories = [(c.id, c.name) for c in Category.query.all()]
    form.category.choices = categories

    if request.method == 'POST':
        name = request.form.get('name')
        price = request.form.get('price')
        category = Category.query.get_or_404(request.form.get('category'))
        product = Product(name, price, category)
        db.session.add(product)
        db.session.commit()
        flash('The product %s has been created' % name, 'success')
        return redirect(url_for('catalog.product', id=product.id))
    return render_template('product_create.html', form=form)