Ejemplo n.º 1
0
def image_xiao():
    "小程序上传图片"
    resjson.action_code = 11

    if not check_login():
        return resjson.print_json(resjson.NOT_LOGIN)

    #获取post过来的文件名称,从name=file参数中获取
    imageFile = request.files['file']
    prefix = request.form['prefix']

    if not prefix:
        return resjson.print_json(11, _(u'缺少前缀'))
    #判断文件类型
    fileName = imageFile.filename
    if '.' not in fileName or fileName.rsplit(
            '.', 1)[1] not in ['png', 'jpg', 'JPG', 'PNG']:
        return resjson.print_json(12, _(u'只允许上传图片'))

    #文件上传
    try:
        fus = FileUploadService()
        image = fus.save_storage(imageFile, prefix)
    except Exception as e:
        log_info(e)
        return resjson.print_json(13, _(u'上传失败,请检查云存储配置'))

    return resjson.print_json(0, u'ok', {'image': image})
Ejemplo n.º 2
0
def save():
    """保存商品"""
    g.page_title = _(u'保存商品')

    form = ItemForm(CombinedMultiDict((request.files, request.form)))
    current_time = current_timestamp()

    if not form.validate_on_submit():
        return render_template('admin/item/detail.html.j2',
                               form=form,
                               item=form.data)

    goods_img = ''
    if form.goods_img.data:
        fus = FileUploadService()
        try:
            goods_img = fus.save_storage(form.goods_img.data, 'item')
        except Exception as e:
            form.goods_img.errors = (_(u'上传失败,请检查云存储配置'))
            return render_template('admin/item/detail.html.j2',
                                   form=form,
                                   item=form.data)

    goods_id = toint(form.goods_id.data)
    if goods_id:
        item = Goods.query.get_or_404(goods_id)
    else:
        item = model_create(Goods, {'detail': '', 'add_time': current_time})

    goods_img = goods_img if goods_img else item.goods_img
    data = {
        'cat_id': form.cat_id.data,
        'goods_name': form.goods_name.data,
        'goods_img': goods_img,
        'goods_desc': form.goods_desc.data,
        'goods_price': form.goods_price.data,
        'market_price': form.market_price.data,
        'is_sale': form.is_sale.data,
        'stock_quantity': form.stock_quantity.data,
        'is_hot': form.is_hot.data,
        'is_recommend': form.is_recommend.data,
        'update_time': current_time
    }
    model_update(item, data, commit=True)

    return redirect(url_for('admin.item.index'))
Ejemplo n.º 3
0
def info_base():
    """基本信息"""
    g.page_title = _(u'基本信息')

    form = InfoBaseForm(CombinedMultiDict((request.files, request.form)))
    ss = SysSetting.query.filter(SysSetting.key == 'config_info_base').first()

    data = {}
    try:
        data = json.loads(ss.value)
    except Exception as e:
        log_info("[view.admin.config] [info_base]:%s" % e.__str__())

    if request.method == 'GET':
        form.fill_form(data=data)
        return render_template('admin/config/info_base.html.j2', form=form)

    if not form.validate_on_submit():
        return render_template('admin/config/info_base.html.j2', form=form)

    # 校验图片上传
    app_logo = ''
    if form.app_logo.data:
        fus = FileUploadService()
        try:
            app_logo = fus.save_storage(form.app_logo.data, 'applogo')
        except Exception as e:
            form.app_logo.errors = (_(u'上传失败,请检查云存储配置'))
            return render_template('admin/config/info_base.html.j2', form=form)
    app_logo = app_logo if app_logo else data.get('app_logo', '')
    data = {
        'app_name': form.app_name.data,
        'app_logo': app_logo,
        'app_recommend': form.app_recommend.data
    }
    # 配置表中没该key,先添加该key
    if ss is None:
        ss = SysSetting()
        ss.key = 'config_info_base'
        db.session.add(ss)

    ss.value = json.dumps(data)
    db.session.commit()
    return redirect(url_for('admin.index.success', title=_(u'设置基本信息成功')))
Ejemplo n.º 4
0
def image():
    """上传图片"""
    resjson.action_code = 10

    if not check_login():
        return resjson.print_json(resjson.NOT_LOGIN)

    wtf_form = UploadImageForm()
    if not wtf_form.validate_on_submit():
        for key, value in wtf_form.errors.items():
            msg = value[0]
        return resjson.print_json(10, msg)

    try:
        fus = FileUploadService()
        image = fus.save_storage(wtf_form.image.data, wtf_form.prefix.data)
    except Exception as e:
        return resjson.print_json(10, _(u'上传失败,请检查云存储配置'))

    return resjson.print_json(0, u'ok', {'image': image})
Ejemplo n.º 5
0
def save():
    """保存管理员"""
    primary_key = request.form.get('primary_key', '').strip()
    if primary_key and primary_key != '0':
        form = AdminUsersEditForm(CombinedMultiDict((request.files, request.form)))
    else:
        form = AdminUsersForm(CombinedMultiDict((request.files, request.form)))

    if not form.validate_on_submit():
        return render_template('admin/auth/admin_user_detail.html.j2', form=form)
    
    admin_uid = toint(form.admin_uid.data)
    au = AdminUsers()
    if admin_uid > 0:
        au = AdminUsers.query.filter(AdminUsers.admin_uid == admin_uid).first()
    else:
        db.session.add(au)
        au.add_time = int(time.time())
        au.salt = randomstr(random_len=32)
        password = sha256(form.password.data.encode('utf8')).hexdigest()
        sha256_password_salt = sha256((password+au.salt).encode('utf8')).hexdigest()
        au.password = sha256_password_salt

    avatar = ''
    if form.avatar.data:
        fus = FileUploadService()
        try:
            avatar = fus.save_storage(form.avatar.data, 'avatar')
        except Exception as e:
            log_error(u'[FileUploadService] Exception:%s' % e)
            form.avatar.errors = (_(u'上传失败,请检查云存储配置'),)
            return render_template('admin/auth/admin_user_detail.html.j2', form=form)

    au.username = form.username.data
    au.mobile = form.mobile.data
    au.nickname = form.username.data
    au.update_time = int(time.time())
    au.avatar = avatar if avatar else au.avatar
    db.session.commit()

    return redirect(url_for('admin.auth.index'))
Ejemplo n.º 6
0
def save():
    """保存广告"""
    g.page_title = _(u'保存广告')

    form = AdvForm(CombinedMultiDict((request.files, request.form)))
    current_time = current_timestamp()

    if not form.validate_on_submit():
        return render_template('admin/adv/detail.html.j2', form=form)

    img = ''
    if form.img.data:
        fus = FileUploadService()
        try:
            img = fus.save_storage(form.img.data, 'adv')
        except Exception as e:
            form.img.errors = (_(u'上传失败,请检查云存储配置'))
            return render_template('admin/adv/detail.html.j2', form=form)

    adv_id = toint(form.adv_id.data)
    if adv_id:
        adv = Adv.query.get_or_404(adv_id)
    else:
        adv = model_create(Adv, {'add_time': current_time})

    img = img if img else adv.img
    data = {
        'ac_id': form.ac_id.data,
        'img': img,
        'desc': form.desc.data,
        'ttype': form.ttype.data,
        'tid': form.tid.data,
        'url': form.url.data,
        'sorting': form.sorting.data,
        'is_show': form.is_show.data,
        'platform_type': form.platform_type.data
    }
    model_update(adv, data, commit=True)

    return redirect(url_for('admin.adv.index'))
Ejemplo n.º 7
0
def ueditor():
    """ueditor上传"""
    res = current_app.response_class(mimetype='application/json')
    # res = make_response(mimetype='application/json')
    action = request.args.get('action')

    if action == 'config':
        # 解析JSON格式的配置文件
        # 这里使用PHP版本自带的config.json文件
        config_filename = os.path.join(current_app.static_folder, 'default',
                                       'admin', 'plugins', 'ue', 'config.json')
        with open(config_filename) as fp:
            data = fp.read()
            CONFIG = json.loads(re.sub(r'\/\*.*\*\/', '', data))
            # log_info('##########:%s' % CONFIG)
            res.data = json.dumps(CONFIG)

        log_info('-----------------')
        return res

    # 获取上传文件
    upfile = request.files['upfile']

    try:
        fus = FileUploadService()
        image = fus.save_storage(upfile, 'ueditor')
    except Exception as e:
        return json.dumps({'state': 'FAIL'})

    data = {
        'state': 'SUCCESS',
        'source': image,
        'url': image,
        'title': image,
        'original': image
    }
    res.data = json.dumps(data)
    return res
Ejemplo n.º 8
0
def category_save():
    """保存分类"""
    g.page_title = _(u'保存分类')

    form = CategoryForm(CombinedMultiDict((request.files, request.form)))

    if not form.validate_on_submit():
        return render_template('admin/item/category_detail.html.j2', form=form)

    cat_img = ''
    if form.cat_img.data:
        fus = FileUploadService()
        try:
            cat_img = fus.save_storage(form.cat_img.data, 'category')
        except Exception as e:
            form.cat_img.errors = (_(u'上传失败,请检查云存储配置'))
            return render_template('admin/item/category_detail.html.j2',
                                   form=form)

    cat_id = toint(form.cat_id.data)
    if cat_id:
        category = GoodsCategories.query.get_or_404(cat_id)
    else:
        category = model_create(GoodsCategories,
                                {'add_time': current_timestamp()})

    cat_img = cat_img if cat_img else category.cat_img
    data = {
        'cat_name': form.cat_name.data,
        'cat_img': cat_img,
        'is_show': form.is_show.data,
        'is_recommend': form.is_recommend.data
    }
    model_update(category, data, commit=True)

    return redirect(url_for('admin.item.categories'))
Ejemplo n.º 9
0
def galleries_save():
    """保存商品相册"""
    g.page_title = _(u'保存商品')

    goods_id = toint(request.form.get('goods_id', '0'))
    images = request.files.getlist('image')
    current_time = current_timestamp()

    for image in images:
        try:
            fus = FileUploadService()
            img = fus.save_storage(image, 'item')
            data = {'goods_id': goods_id, 'img': img, 'add_time': current_time}
            model_create(GoodsGalleries, data)
        except Exception as e:
            err_msg = _(u'上传失败,请检查云存储配置')
            return redirect(
                url_for('admin.item.galleries',
                        goods_id=goods_id,
                        err_msg=err_msg))

    db.session.commit()

    return redirect(url_for('admin.item.galleries', goods_id=goods_id))