def fulltime_request(user, address=None): auth = yield get_db_object_by_attr(Auth, user_id=user.id, ignore=True) if auth and auth.identifier: raise gen.Return(already_exist_error('user %s is already a xiaodier' % user.nickname)) request_record = RequestRecord(user_id=user.id, detail=address) yield execute(('add', request_record)) yield execute(('commit', None)) raise gen.Return(None)
def first_award(user, key): type_, type_doc, point = key award = yield get_db_object_by_attr(Award, user_id=user.id, type=type_, ignore=True) if award is None: user.original_point += point point_record = PointRecord(user_id=user.id, type=type_doc, balance=user.bright_point + user.original_point, number='+' + str(point)) award = Award(user_id=user.id, type=type_) yield execute(('add_all', [user, award, point_record])) yield execute(('commit', None))
def wrapper(self, *args, **kwargs): username = self.get_argument("username", None) token = self.get_argument("token", None) if permission == Permission.COMMON: self._G.user = yield authorizer.authenticate_common( username=username, token=token) elif permission == Permission.MANAGER: self._G.user = yield authorizer.authenticate_manager( username=username, token=token) elif permission == Permission.ADMIN: self._G.user = authorizer.authenticate_admin(username=username, token=token) if identified and not self._G.user.identified: not_authorized_error('user %s is not identified' % self._G.user.nickname) if innocence: black_list = yield execute([('query', BlackList), ('filter', None), ('first', None)]) if self._G.user.id in json.loads(black_list.list): not_authorized_error('user %s is not innocence' % self._G.user.nickname) if xiaodier and self._G.user.role not in XIAODIER: not_authorized_error('user %s is not xiaodier' % self._G.user.nickname) # run generator until finish f = func(self, *args, **kwargs) while True: try: f.send((yield next(f, None))) except StopIteration: break raise gen.Return(None)
def _authenticate(self, object_=User, username=None, token=None): user = yield execute([('query', object_), ('filter', object_.username == username), ('first', None)]) if user is None: raise gen.Return(not_authorized_error('wrong username')) if not AuthorizerHelper.verify_token(user, token): raise gen.Return(not_authorized_error('wrong token')) raise gen.Return(user)
def get_delivery(school=None, sort=None, ascend=None, keyword=None, page_num=None): from sqlalchemy import and_, or_ order_way = getattr(Task, ORDER_FIELDS[sort]) if not ascend: order_way = order_way.desc() if keyword: keyword = ['%{}%'.format(k) for k in keyword.encode('utf-8').split()] task_fields = ['source', 'type', 'destination'] task_list = [ and_(getattr(Task, attr).like(k) for k in keyword) for attr in task_fields ] user_fields = ['nickname', 'campus', 'hostel'] user_list = [ and_(getattr(User, attr).like(k) for k in keyword) for attr in user_fields ] deliveries = yield execute( sqls=[('query', Task), ('join', ( User, Task.puser_id == User.id)), ('filter', Task.school == school), ('filter', or_(*(task_list + user_list))), ('order_by', order_way), ('offset', (page_num - 1) * MAX_DELIVERY_PER_PAGE), ( 'limit', MAX_DELIVERY_PER_PAGE), ('all', None)]) else: deliveries = yield execute( sqls=[('query', Task), ('filter', Task.school == school), ('order_by', order_way), ('offset', (page_num - 1) * MAX_DELIVERY_PER_PAGE), ( 'limit', MAX_DELIVERY_PER_PAGE), ('all', None)]) raise gen.Return([delivery.to_json() for delivery in deliveries])
def identify_user(user, role=None, school='cqu', sid=None, password=None, truename=None): if user.identified: raise gen.Return(already_exist_error('user %s is already identified' % user.nickname)) if (yield get_db_object_by_attr(Auth, school_id=sid, ignore=True)) is not None: raise gen.Return(already_exist_error('school_id %s is already identified' % sid)) result = yield CommonTaskFactory.get_task(TaskNames.IDENTIFY_USER.value).run( school=school, sid=sid, password=password, truename=truename ) if result is None: raise gen.Return(invalid_argument_error('certification is failed, please check the params')) auth = Auth( user_id=user.id, truename=truename, school_id=sid, school_role=role, identifier="" ) engagement = Engagement( user_id=user.id, engagement=json.dumps([0]) ) sign = Sign( user_id=user.id, day=-1, record=json.dumps([]) ) user.identified = True user.school = school yield execute(('add_all', [auth, engagement, sign, user])) yield execute(('commit', None)) raise gen.Return(None)
def register_user(password=None, nickname=None, code=None, headimg=None, sex=None, phone=None, unique_id=None): if (yield get_db_object_by_attr(User, phone=phone, ignore=True)) is not None: raise gen.Return(already_exist_error('phone %s alreay exist' % phone)) if (yield get_db_object_by_attr(User, nickname=nickname, ignore=True)) is not None: raise gen.Return(already_exist_error('nickname %s alreay exist' % nickname)) if headimg: headimg_path = yield save_image_to_oss(headimg, OSS_HEADIMG_PATH, str(datetime.now()), when_fail=DEFAULT_HEADIMG) else: headimg_path = DEFAULT_HEADIMG bright_point = 0 if code: user = yield get_db_object_by_attr(User, code=code) if user: user.bright_point += INVITE_AWARD_POINT user.code = None execute(('add', user)) point_record = PointRecord( username=user.username, type='邀好友注册', balance=user.bright_point + user.original_point, number='+%d' % INVITE_AWARD_POINT, time=datetime.now().__format__("%Y-%m-%d") ) yield execute(('add', point_record)) yield execute(('commit', None)) bright_point = INVITE_AWARD_POINT user = User( username=generate_unique_username(), password=password_manager.encrypt(password), nickname=nickname, phone=phone, role=Permission.COMMON, unique_id=unique_id, code=(yield generate_invitingcode()), headimg=headimg_path, sex=sex, identified=False, school="", campus="", hostel="", introduction="", original_point=DEFAULT_POINT, bright_point=bright_point, credibility=DEFAULT_CREDIBILITY ) yield execute(('add', user)) yield execute(('commit', None)) raise gen.Return(user)
def get_db_object_by_attr(object_, **kwargs): assert len(kwargs) >= 1, 'function get_db_object_by_attr need argument' if 'ignore' in kwargs: ignore = kwargs.pop('ignore') else: ignore = False if len(kwargs) > 1: filter_ = and_(*[ getattr(object_, key) == value for key, value in kwargs.iteritems() ]) else: key, value = kwargs.popitem() filter_ = getattr(object_, key) == value user = yield execute(sqls=[('query', object_), ('filter', filter_), ('first', None)]) if not user and not ignore: raise gen.Return(invalid_argument_error('wrong %s' % key)) raise gen.Return(user)
def _asave(self, delivery): yield execute(('add', delivery)) yield execute(('commit', None))
def do_forget_password(phone=None, password=None): user = yield get_db_object_by_attr(User, phone=phone) user = password_manager.forget_password(user, password) yield execute(('add', user)) yield execute(('commit', None))