Beispiel #1
0
    def password(self):
        """修改密码
        """
        # 字段信息验证
        password_form = form.Form(
                form.Password('password', self.vnotnullpassword),
                form.Password('new_password', self.vnotnullpassword,
                    self.vpassword),
                validators = [
                    self.vcmppassword,
                    ]
                )
        f = password_form()
        if not f.validates():
            self.resp = {'errno': errno_form, 'msg': f.get_note()}
            return self.json()

        params = self.req_params()
        password, new_password, repeat_password = params.password, params.new_password, params.repeat_password
        token = web.ctx.session.token
        userid = auth.decode_token(token)['userid']

        user_li = self.db.query_all("select id from user where id='%s' and password='******'" % (userid, auth.get_hashed_password(password,
                    user_passwd_salt)))
        if 0 == len(user_li):
            self.resp = {'errno': errno_db, 'msg': '密码出错'}
            return self.json()

        self.db.query("""update user set password='******' where id='%s'""" %
                (auth.get_hashed_password(new_password, user_passwd_salt), userid))
        self.db.commit()

        self.resp = {'errno': errno_ok, 'msg': '修改完成'}
        return self.json();
Beispiel #2
0
    def index(self):
        userid = auth.decode_token(web.ctx.session.token)['userid']
        quota_li = self.db.query_all("select * from quota where `user_id`='%s'" % (userid))
        self.db.commit()
        if 0 == len(quota_li):
            self.resp = {'errno': errno_db, 'msg': '暂无数据'}
            return self.json()

        self.resp = {'errno': errno_ok, 'total':
                fileutil.friend_size(quota_li[0]['total']), 'used':
                fileutil.friend_size(quota_li[0]['used'])}
        return self.json()
Beispiel #3
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()
Beispiel #4
0
    def __init__(self):
        super(DiskAction, self).__init__()

        """setting server side dir"""
        homedir = os.getcwd()
        token = web.ctx.session.token
        userid = auth.decode_token(token)['userid']
        quota_li = self.db.query_all("select fs_path from quota where user_id='%s'" % (userid))
        if 0 == len(quota_li):
            self.redirect('error')
        self.userdir = quota_li[0]['fs_path']
        self.filedir = '%s/static/upload/%s' % (homedir, self.userdir)
Beispiel #5
0
def is_admin():
    if not is_user():
        return False

    token = web.ctx.session.token
    userid = auth.decode_token(token)['userid']
    db = MySQLDB_(settings['MYSQL_HOST'], settings['MYSQL_USER'],
            settings['MYSQL_PASSWD'], settings['MYSQL_DB'])
    user_li = db.query_all("select * from user where `id`='%s'" % userid)
    db.close()

    if 0 == len(user_li):
        return False

    if int(user_li[0]['level']) != user_admin:
        return False

    return True
Beispiel #6
0
    def remove(self):
        """删除文件(非目录)
        """
        params = self.req_params()
        path = params.path
        filepath = fileutil.join_path(self.filedir, params.path)
        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')

        used = int(quota_li[0]['used'])

        if os.path.isdir(filepath):
            try:
                f_li = fileutil.list_all_files(filepath)
                # http://www.cnblogs.com/xiaowuyi/archive/2012/05/04/2482113.html
                # 删除非空目录
                shutil.rmtree(filepath)
                #os.rmdir(filepath)
                for f in f_li:
                    filepath = fileutil.join_path(self.userdir, f[len(self.filedir):])
                    used -= self._get_fs_size(filepath, userid)
                    self.db.query("delete from file_system where path='%s' and user_id='%s'" %
                            (filepath, userid))

                # 更新存储容量
                self.db.query("""update quota set used='%d' where user_id='%s'""" %
                        (used, userid))
                self.db.commit()
            except OSError, e:
                print 'e.message', e.message
                self.resp = {'errno': errno_notfound, 'msg': '目录不存在'}
                self.db.rollback()
                return self.json()
            self.resp = {'errno': errno_ok, 'msg': '目录删除完成'}
            return self.json()
Beispiel #7
0
    def account(self):
        """设置(邮箱、姓名)
        """
        token = web.ctx.session.token
        userid = auth.decode_token(token)['userid']
        if self.get():
            # 账号信息
            user_li = self.db.query_all("select `id`, `name`, `email` from user where `id`='%s'" % (userid))
            self.db.commit()
            if 0 == len(user_li):
                self.resp = {'errno': errno_db, 'msg': '用户不存在'}
                return self.json()
            self.resp = {'errno': errno_ok}
            self.resp.update(user_li[0])
            return self.json()

        params = self.req_params()
        # 字段信息验证
        account_form = form.Form(
                form.Textbox('name', self.vnotnullname, self.vname),
                form.Textbox('email', self.vnotnullemail, self.vemail),
                )
        f = account_form()
        if not f.validates():
            self.resp = {'errno': errno_form, 'msg': f.get_note()}
            return self.json()

        email, name = params.email, params.name

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

        self.db.query("update user set `email`='%s', `name`='%s' where `id`='%s'" % (email, name, userid))
        self.db.commit()

        self.resp = {'errno': errno_ok, 'msg': '修改完成'}
        return self.json()