示例#1
0
def item_update(id, slug):
    item = Item.get_or_404(id, slug)

    if not item.editable_by(current_user):
        abort(403)

    form = ItemForm()
    if form.validate_on_submit():
        item.title = form.title.data
        item.description = form.description.data
        item.price = Price(fixed_value=form.price.data*100, currency='USD')
        item.active = form.active.data
        item.expiry = datetime.datetime.now() + datetime.timedelta(days=form.expires_in.data)
        private_viewer_emails = [_.strip() for _ in form.private_viewers.data.split(',')]
        item.private_viewer_keys = [user.key for user in UserProfile.query(UserProfile.email.IN(private_viewer_emails))]
        if form.image.has_file():
            app.logger.debug(form.image.data)
            app.logger.debug(dir(form.image.data))
            app.logger.debug(form.image.data.mimetype_params)
            blob = blobstore.parse_blob_info(to_fieldstorage(form.image.data))
            item.image = blob.key()
        item.put()

        flash(_T('Item updated'), 'success')

        return redirect(url_for('item', id=id, slug=slug))

    form = ItemForm(title=item.title, description=item.description,
                    price=item.price.fixed_value / 100, youtube=item.youtube,
                    private_viewers=','.join([user.username for user in ndb.get_multi(item.private_viewer_keys)]),
                    active=item.active)

    return render_template('item/update.html', form=form, id=id, slug=slug,
                           action=blobstore.create_upload_url(url_for('item_update', id=id, slug=slug)),
                           title=_T('Update Item') + ' %s' % item.title)
示例#2
0
def item_create():
    form = ItemForm()

    if form.validate_on_submit():
        item = Item()
        item.title = form.title.data
        item.seller_id = current_user.get_id()
        item.slug = slugify(form.title.data)
        item.description = form.description.data
        item.price = Price(fixed_value=form.price.data*100, currency='USD')
        if form.image.has_file():
            blob = blobstore.parse_blob_info(to_fieldstorage(form.image.data))
            item.image = blob.key()
        item.youtube = form.youtube.data
        item.active = form.active.data
        item.expiry = datetime.datetime.now() + datetime.timedelta(days=form.expires_in.data)
        private_viewer_emails = [_.strip() for _ in form.private_viewers.data.split(',')]
        item.private_viewer_keys = [user.key for user in UserProfile.query(UserProfile.email.IN(private_viewer_emails))]
        k = item.put()

        flash(_T('Your item has been created!'), 'success')
        return redirect(url_for('item', id=k.id(), slug=item.slug))

    return render_template('item/create.html', form=form,
                           action=blobstore.create_upload_url(url_for('item_create')),
                           title=_T('Create Item'))