Exemplo n.º 1
0
    def register(self):
        """注册
        """
        # 字段信息验证
        register_form = form.Form(
                form.Textbox('name', self.vnotnullname, self.vname),
                form.Textbox('email', self.vnotnullemail, self.vemail),
                form.Password('password', self.vnotnullpassword, self.vpassword),
                validators = [
                    self.vcmppassword,
                    ]
                )
        f = register_form()
        if not f.validates():
            self.resp = {'errno': errno_form, 'msg': f.get_note()}
            return self.json()

        params = self.req_params()
        name, email, password = params.name, params.email, params.password

        if not user_unique(self, name, email):
            return self.json()

        self.db.query("""insert into user (`name`, `email`, `password`, `create_time`, `last_login_time`) values('%s', '%s', '%s', '%s', '%s')""" % (name, email,
                    auth.get_hashed_password(password, user_passwd_salt),
                    timeutil.get_current_date(), timeutil.get_current_date()))
        user_li = self.db.query_all("""select id from user where name='%s'""" % (name))
        if 0 == len(user_li):
            self.resp = {'errno': errno_db, 'msg': '数据库操作失败'}
            return self.json()
        fs_path = user_li[0]['id'] + '/'
        print 'fs_path', fs_path
        self.db.query("""insert into quota (total, create_time, fs_path,
                user_id) values('%s', '%s', '%s', '%s')""" %
                (default_total_quota, timeutil.get_current_date(), fs_path,
                user_li[0]['id']))

        self.db.commit()

        self.resp = {'errno': errno_ok, 'msg': '注册完成'}
        return self.json()
Exemplo n.º 2
0
    def upload(self):
        """上传文件,并保存
        http://webpy.org/cookbook/fileupload.zh-cn
        http://webpy.org/cookbook/storeupload.zh-cn
        http://outofmemory.cn/code-snippet/3286/webpy-upload-file
        """
        try:
            x = web.input(disk={})
        except ValueError:
            self.resp = {'errno': errno_file_too_large, 'msg': e.message}
            return self.json()

        params = self.req_params()
        path = params.path

        if 'disk' in x:
            filepath = x.disk.filename.replace('\\', '/')
            if not filepath:
                self.resp = {'errno': 1, 'msg': '请选择文件'}
                return self.json()
            filename = filepath.split('/')[-1]
            # 获取文件大小
            # https://groups.google.com/forum/#!topic/webpy/si190XseR30
            size = int(web.ctx.env['CONTENT_LENGTH'])
            token = web.ctx.session.token
            userid = auth.decode_token(token)['userid']

            quota_li = self.db.query_all("""select used, total, fs_path from quota where user_id='%s'"""
                    % (userid))
            if 0 == len(quota_li):
                self.redirect('error')

            if size + int(quota_li[0]['used']) > int(quota_li[0]['total']):
                self.resp = {'errno': errno_file_too_large, 'msg': '您的存在空间不足'}
                return self.json()

            f = open(fileutil.join_path(fileutil.join_path(self.filedir, path), filename), 'w')
            f.write(x.disk.file.read())
            f.close()
            userdir = quota_li[0]['fs_path']
            size = os.path.getsize(fileutil.join_path(fileutil.join_path(self.filedir,
                path), filename))
            used = int(quota_li[0]['used']) + size
            filepath = fileutil.join_path(fileutil.join_path(userdir, path), filename)
            # 更新存储容量
            self.db.query("""update quota set used='%d' where user_id='%s'""" %
                    (used, userid))
            self.db.query("insert into file_system (name, path, size, create_time, user_id) values('%s', '%s', '%s', '%s', '%s')" % (filename,
                        filepath, size, timeutil.get_current_date(), userid));
            self.db.commit()
            self.resp = {'errno': 0, 'msg': '上传成功'}

        return self.json()