示例#1
0
    def post(self):
        import time

        time.sleep(1.5)

        form = self.form()

        # 验证
        if form.validate(self.request.arguments):
            # 验证通过
            post = form.values()
            ar = model.User().find("email = ?", post["email"]).where("password = ?", util.md5(post["password"]))

            user = ar.get()
            if user != None:
                # 为用户登陆
                self.set_current_user(
                    {
                        "userId": user["id"],
                        "userName": user["name"],
                        "email": user["email"],
                        "roles": user.getRoleCodes(),
                    }
                )

                return self.write({"success": True, "url": "/admin"})

        return self.write({"success": False, "msg": "邮箱或密码错误"})
示例#2
0
    def edit(self,id,data):
        #检查账号,邮箱是否唯一
        count = self.find('name = ?' , data['name'])\
                    .where('id != ?' , id)\
                    .count()

        if 0 != count :
            return {
                'success' : False ,
                'msg' : '账号已经存在'
            }

        count = self.find('email = ?' , data['email'])\
                    .where('id != ?' , id)\
                    .count()

        if 0 != count :
            return {
                'success' : False ,
                'msg' : '邮箱已经存在'
            }

        vals = {
            'name' : data['name'] ,
            'email' : data['email'] ,
        }

        if util.validators.isEmpty(data['password']) == False:
            vals['password'] = util.md5(data['password'])

        self.find('id = ?',id).save(vals)

        RoleHasUser().find('user_id = ?',id).delete()

        # 关联角色
        for r in data['roles'] : 
            RoleHasUser().find().add({
                'user_id' : id ,
                'role_id' : r
            })

        return {
            'success' : True
        }
示例#3
0
    def post(self):
        if not self.request.files.has_key("fileData"):
            return self.write({"error": 1, "message": "没有文件被上传"})

        path = "/" + self.settings["upload_path"] + "/" + util.date.timeToStr("%Y-%m") + "/"
        if False == os.path.exists(self.settings["static_path"] + path):
            os.mkdir(self.settings["static_path"] + path)

        fileData = self.request.files["fileData"][0]
        nameInfo = fileData["filename"].split(".")
        suffix = nameInfo[len(nameInfo) - 1]

        fileName = util.md5(fileData["filename"] + str(util.time.time())) + "." + util.filters.trim(suffix)
        filePath = self.settings["static_path"] + path + fileName
        fileHandle = open(filePath, "wb")
        fileHandle.write(fileData["body"])
        fileHandle.close()

        url = "/static" + path + fileName
        model.Attachment().add(
            {
                "ip": self.request.remote_ip,
                "url": url,
                "user_id": self.user["userId"],
                "title": fileData["filename"],
                "type": fileData["content_type"],
                "size": os.path.getsize(filePath) / 1024,
            }
        )

        return self.write(
            {
                "error": 0,
                "suffix": suffix,
                "fileName": fileData["filename"],
                "type": fileData["content_type"],
                "url": url,
            }
        )
示例#4
0
    def add(self,data):
        #检查账号,邮箱是否唯一
        count = self.find('name = ?' , data['name'])\
                    .count()

        if 0 != count :
            return {
                'success' : False ,
                'msg' : '账号已经存在'
            }

        count = self.find('email = ?' , data['email'])\
                    .count()

        if 0 != count :
            return {
                'success' : False ,
                'msg' : '邮箱已经存在'
            }
        
        id = self.find().add({
            'password' : util.md5(data['password']) ,
            'name' : data['name'] ,
            'email' : data['email'] ,
            'time' : util.date.time()
        })

        # 关联角色
        for r in data['roles'] : 
            RoleHasUser().find().add({
                'user_id' : id ,
                'role_id' : r
            })

        return {
            'success' : True
        }