def userstatus(username, enable=True, callback=None): ''' 启用或停用openfire用户''' not_empty(username) able = 'enable' if enable else 'disable' val = dict(type=able, username=username) url = _make_url(val) return client.fetch(url, callback=callback)
def info_city_listname(self, listname): ''' city info by listname ''' not_empty(listname) try: return self._coll().info(listname, key='listname') except Exception as e: print e return None
def add(username, password, name, email=None, group=None, callback=None): ''' 添加一个用户到openfire ''' not_empty(username, password, name) val = dict(type='add', username=username, password=hashPassword(password), name=name, email=email, groups=group) url = _make_url(val) print url return client.fetch(url, callback=callback)
def update(username, password=None, name=None, email=None, group=None, callback=None): ''' 修改用户息 ''' not_empty(username) val = dict(type='update', username=username, password=hashPassword(password), name=name, email=email, groups=group) url = _make_url(val) print url return client.fetch(url, callback=callback)
def add(uid,pid,body,**kwargs): not_empty(uid,pid,body) try: val = dict(uid=uid,pid=pid,body=body) _id = Tb().insert(val,saft=True) val['_id'] = str(_id) return True,val except Exception as e: return False,e.message
def add(uid, pid, body, **kwargs): not_empty(uid, pid, body) try: val = dict(uid=uid, pid=pid, body=body) _id = Tb().insert(val, saft=True) val['_id'] = str(_id) return True, val except Exception as e: return False, e.message
def m_info(table,_id): not_empty(_id) try: cond = dict(_id=ObjectId(_id)) cond.update(StatusCond) return True,mongo_conv(Tb(table).find_one(cond,{'status':0})) except InvalidId as e: return False,"查询参数格式错误" except Exception as e: return False,e.message
def info_category_listname(listname): ''' category info by listname ''' not_empty(listname) try: val = Tb().find_one(dict(listname = listname)) val["_id"] = str(val['_id']) return val except Exception as e: return None
def info_category_listname(listname): ''' category info by listname ''' not_empty(listname) try: val = Tb().find_one(dict(listname=listname)) val["_id"] = str(val['_id']) return val except Exception as e: return None
def m_update(table, _id, **kwargs): try: not_empty(table, _id) cond = dict(_id=ObjectId(_id)) cond.update(StatusCond) Tb(table).update(cond, {'$set': kwargs}) return True, None except InvalidId as e: return False, "查询参数格式错误" except Exception as e: return False, e.message
def m_update(table,_id,**kwargs): try: not_empty(table,_id) cond = dict(_id = ObjectId(_id)) cond.update(StatusCond) Tb(table).update(cond,{'$set':kwargs}) return True,None except InvalidId as e: return False,"查询参数格式错误" except Exception as e: return False,e.message
def info_group_listname(listname): not_empty(listname) try: val = Tb()().find_one(dict(listname = listname)) val['_id'] = str(val['_id']) return val except Exception as e: return None return None
def add(name, groupid, listname, intro, **kwargs): """ add category """ not_empty(name, listname, groupid) val = dict(name=name, groupid=groupid, listname=listname, intro=intro) val.update(kwargs) try: _id = Tb().insert(val, saft=True) val["_id"] = str(_id) except Exception as e: return False, e.message return True, val
def reset_pwd(uid, old_password, new_password): try: not_empty(uid, old_password, new_password) _id = ObjectId(uid) kwargs = dict(_id=_id, password=hashPassword(old_password)) valid = m_exists(TName, **kwargs) if valid: Tb().update(kwargs, {'$set': {'password': hashPassword(new_password)}}) return True, 'OK' else: return False, 'INVALIDED' except ValueError: return False, 'NO_EMPTY'
def hashPassword(password): """Hash the user password. Args: password : the user input password. Returns: the password with md5 hashable """ not_empty(password) md5 = hashlib.md5() md5.update(password) return md5.hexdigest()
def add(name,parent=None,level=0): try: not_empty(name) r = m_exists(TName,name=name,parent=parent,level=level) if not r: val = dict(name=name,parent=parent,level=level,status=0) _id = Tb().insert(val,saft=True) val['_id'] = str(_id) return True,val else: return False,'EXIST' except Exception as e: return False,e.message
def add(name, parent=None, level=0): try: not_empty(name) r = m_exists(TName, name=name, parent=parent, level=level) if not r: val = dict(name=name, parent=parent, level=level) _id = Tb().insert(val, saft=True) val['_id'] = str(_id) return True, val else: return False, 'EXITS' except Exception as e: return False, e.message
def reset_pwd(self, uid, old_password, new_password): try: not_empty(uid, old_password, new_password) _id = ObjectId(uid) kwargs = dict(_id=_id, password=hashPassword(old_password)) valid = self.exists(**kwargs) if valid: self.update(uid, password= hashPassword(new_password)) return True, 'OK' else: return False, 'INVALIDED' except ValueError: return False, 'NO_EMPTY'
def reset_pwd(self, uid, old_password, new_password): try: not_empty(uid, old_password, new_password) _id = ObjectId(uid) kwargs = dict(_id=_id, password=hashPassword(old_password)) valid = self.exists(**kwargs) if valid: self.update(uid, password=hashPassword(new_password)) return True, 'OK' else: return False, 'INVALIDED' except ValueError: return False, 'NO_EMPTY'
def add(self, username, password, city=None, **kwargs): try: not_empty(username, password) r = self.exists(username=username) if not r: val = dict(username=username, password=hashPassword(password), city=city) _id = self.insert(val,**kwargs) val['_id'] = str(_id) return True, val else: return False, 'EXISTS' except ValueError: return False, 'NO_EMPTY'
def add(name, groupid, listname, intro, **kwargs): ''' add category ''' not_empty(name, listname, groupid) val = dict(name=name, groupid=groupid, listname=listname, intro=intro) val.update(kwargs) try: _id = Tb().insert(val, saft=True) val["_id"] = str(_id) except Exception as e: return False, e.message return True, val
def auth_login(self, site,otherid,name,**kwargs): try: not_empty(site,otherid,name) r = self.exists(site=site,otherid=otherid,name=name) if r: r = mongo_conv(r) return True,r else: val = dict(site=site,otherid=otherid,name=name) _id = self.insert(val) val['_id'] = str(_id) return True,val except Exception as e: return False,e.message
def list_by_post(pid, since=None, size=10): not_empty(pid) cond = dict(pid=pid) r, lst = m_page(TName, since, size, **cond) arr = [] for item in lst: try: r, v = m_info(T_ACCOUNT, item['uid']) if r: item['user'] = v['username'] arr.append(item) except Exception as e: pass return r, arr
def list_by_post(pid,since=None,size=10): not_empty(pid) cond = dict(pid=pid) r,lst = m_page(TName,since,size,**cond) arr = [] for item in lst: try: r,v = m_info(T_ACCOUNT,item['uid']) if r:item['user'] = v['username'] arr.append(item) except Exception as e: pass return r,arr
def add(username, password, city=None, **kwargs): try: not_empty(username, password) r = m_exists(TName, username=username) if not r: val = dict(username=username, password=hashPassword(password), city=city) val.update(kwargs) _id = Tb().insert(val, saft=True) val['_id'] = str(_id) return True, val else: return False, 'EXISTS' except ValueError: return False, 'NO_EMPTY'
def add(username,password,city=None,**kwargs): try: not_empty(username,password) r = m_exists(TName,username=username,password=password) if not r: val = dict(username=username,password=password,city=city) val.update(kwargs) _id = Tb().insert(val,saft=True) val['_id'] = str(_id) return True,val else: return False,'EXITS' except Exception as e: return False,e.message
def auth_login(site,otherid,name,**kwargs): try: not_empty(site,otherid,name) r = m_exists(TName,site=site,otherid=otherid,name=name) if r: r = mongo_conv(r) return True,r else: val = dict(site=site,otherid=otherid,name=name) _id = Tb().insert(val,saft=True) val['_id'] = str(_id) return True,val except Exception as e: return False,e.message
def active_account(self, key): try: not_empty(key) redis = get_context().get_redis() username = redis.get(key) if not username: return False, 'EXPIRED' existed = self.exists(username=username) if existed: self.update(username, key='username', status=ACTIVATED) return True, dict(username=username) else: return False, 'NO_EXIST' except ValueError: return False, 'NO_EMPTY'
def add(self, username, password, city=None, **kwargs): try: not_empty(username, password) r = self.exists(username=username) if not r: val = dict(username=username, password=hashPassword(password), city=city) _id = self.insert(val, **kwargs) val['_id'] = str(_id) return True, val else: return False, 'EXISTS' except ValueError: return False, 'NO_EMPTY'
def forgot_pwd(username): try: not_empty(username) existed = m_exists(TName, username=username) if existed: key = random_key() redis = get_context().get_redis() redis.set(key, username, 60 * 60) r = send_mail([username], '找回密码', get_email_content('email_forget_password.html', key=key, username=username)) return r, 'OK' if r else 'FAIL' else: return False, 'NO_EXIST' except ValueError: return False, 'NO_EMPTY'
def apply_active_account(username): try: not_empty(username) existed = m_exists(TName, username=username) if existed: key = random_key() redis = get_context().get_redis() redis.set(key, username, 60 * 60) r = send_mail([username], '账号激活', get_email_content('email_active_account.html', key=key, username=username)) return r, 'OK' if r else 'FAIL' else: return False, 'NO_EXIST' except ValueError: return False, 'NO_EMPTY'
def login(username,password,isadmin = None): try: not_empty(username,password) cond = dict(username=username,password=password) if isadmin: cond.update(isadmin = isadmin) r = m_exists(TName,**cond) if r: r = mongo_conv(r) return True, r else: return False,None except Exception as e: return False,e.message
def active_account(self,key): try: not_empty(key) redis = get_context().get_redis() username = redis.get(key) if not username: return False, 'EXPIRED' existed = self.exists(username=username) if existed: self.update(username,key='username',status=ACTIVATED) return True, dict(username=username) else: return False, 'NO_EXIST' except ValueError: return False, 'NO_EMPTY'
def active_account(key): try: not_empty(key) redis = get_context().get_redis() username = redis.get(key) if not username: return False, 'EXPIRED' existed = m_exists(TName, username=username) if existed: Tb().update(dict(username=username), {'$set': {'status': ACTIVATED}}) return True, dict(username=username) else: return False, 'NO_EXIST' except ValueError: return False, 'NO_EMPTY'
def add(title, pid, city, body, source=None, **kwargs): """ add report """ try: not_empty(title, body) val = dict(title=title, pid=pid, city=city, body=body, source=source) r = m_exists(TName, title=title, pid=pid) if not r: val.update(kwargs) _id = Tb().insert(val, saft=True) val["_id"] = str(_id) else: return False, "exists" except Exception as e: return False, e.message return True, val
def add(name, listname, **kwargs): ''' add group ''' not_empty(name, listname) r = m_exists(TName,name=name,listname=listname) if r: return False,'存在name 或 listname ' val = dict(name = name, listname = listname) val.update(kwargs) try: _id = Tb().insert(val, saft = True) val["_id"] = str(_id) except Exception as e: return False, e.message return True, val
def reset_forgotten_password(self, key, new_password): try: not_empty(key, new_password) redis = get_context().get_redis() username = redis.get(key) if not username: return False, 'EXPIRED' existed = self.exists(username=username) if existed: self.update(username,key='username', password=hashPassword(new_password)) return True, 'OK' else: return False, 'NO_EXIST' except ValueError: return False, 'NO_EMPTY'
def add(title, pid, city, body, source=None, **kwargs): ''' add report ''' try: not_empty(title, body) val = dict(title=title, pid=pid, city=city, body=body, source=source) r = m_exists(TName, title=title, pid=pid) if not r: val.update(kwargs) _id = Tb().insert(val, saft=True) val["_id"] = str(_id) else: return False, 'exists' except Exception as e: return False, e.message return True, val
def reset_forgotten_password(key, new_password): try: not_empty(key, new_password) redis = get_context().get_redis() username = redis.get(key) if not username: return False, 'EXPIRED' existed = m_exists(TName, username=username) if existed: Tb().update(dict(username=username), {'$set': {'password': hashPassword(new_password)}}) return True, 'OK' else: return False, 'NO_EXIST' except ValueError: return False, 'NO_EMPTY'
def add(category, name, **kwargs): try: not_empty(name) cond = dict(category = category, name=name) val = m_exists(TName,**cond) if not val: #如果不存在此标签 则添加 cond['usage'] = 1 cond['hot'] = 0 r,_id = m_add(TName,cond) cond['_id'] = _id else: cond.update(status={'$ne':-1}) Tb().update(cond,{'$inc':{'usage':1}}) return True, cond except Exception as e: return False,e.message
def apply_active_account(self, username, host): try: not_empty(username) existed = self.exists(username=username) if existed: key = random_key() redis = get_context().get_redis() redis.set(key, username, 60 * 60) r = send_mail([username], '账号激活', get_email_content('email_active_account.html', host=host, key=key, username=username)) return r, 'OK' if r else 'FAIL' else: return False, 'NO_EXIST' except ValueError: return False, 'NO_EMPTY'
def add(category, name, **kwargs): try: not_empty(name) cond = dict(category=category, name=name) val = m_exists(TName, **cond) if not val: #如果不存在此标签 则添加 cond['usage'] = 1 cond['hot'] = 0 r, _id = m_add(TName, cond) cond['_id'] = _id else: cond.update(status={'$ne': -1}) Tb().update(cond, {'$inc': {'usage': 1}}) return True, cond except Exception as e: return False, e.message
def forgot_pwd(self, username, host): try: not_empty(username) existed = self.exists(username=username) if existed: key = random_key() redis = get_context().get_redis() redis.set(key, username, 60 * 60) r = send_mail([username], '找回密码', get_email_content('email_forget_password.html', host=host, key=key, username=username)) return r, 'OK' if r else 'FAIL' else: return False, 'NO_EXIST' except ValueError: return False, 'NO_EMPTY'
def add(uid, body, city=None, **kwargs): try: not_empty(uid, body) r = m_exists(TName, uid=uid, body=body) if not city: r, v = m_info(account.TName, uid) if r: city = v.get('city', None) if not r: val = dict(uid=uid, body=body, city=city) val.update(kwargs) _id = Tb().insert(val, saft=True) val['_id'] = str(_id) return True, val else: return False, 'EXITS' except Exception as e: return False, e.message
def login(self, username, password, isadmin=None): try: not_empty(username, password) cond = dict(username=username, password=hashPassword(password)) #cond = dict(username=username, password=password) if isadmin: cond.update(isadmin=isadmin) r = self.exists(**cond) if r: r = mongo_conv(r) return True, r else: return False, 'NO_EXISTED' except ValueError: return False, 'NO_EMPTY' except Exception as e: return False, e.message
def reset_forgotten_password(self, key, new_password): try: not_empty(key, new_password) redis = get_context().get_redis() username = redis.get(key) if not username: return False, 'EXPIRED' existed = self.exists(username=username) if existed: self.update(username, key='username', password=hashPassword(new_password)) return True, 'OK' else: return False, 'NO_EXIST' except ValueError: return False, 'NO_EMPTY'
def m_del(table, _id, is_del=False): ''' 通用删除逻辑 table : 表名 _id : 主键 is_del: 是否为硬删除 ''' try: not_empty(table, _id) if is_del: Tb(table).remove(dict(_id=ObjectId(_id))) else: cond = dict(_id=ObjectId(_id)) cond.update(StatusCond) Tb(table).update(cond, {'$set': {'status': -1}}) except InvalidId as e: return False, "查询参数格式错误" except Exception as e: return False, e.message return True, None
def m_add(table, data): not_empty(table) _id = Tb(table).insert(data, saft=True) return True, str(_id)
def get(self): _id = self.get_argument("_id", None) not_empty(_id) info = AModel.info(_id) self.write(dict(status=True, data=info))