def setUp(self): """ init :return: """ school = PracticeSchool.objects.create(name='北京师范大学珠海分校', ) account = Account.objects.create( username='******', sex=int(AccountSexEnum.MALE), password=build_password_signature('password', gen_salt()), nickname='test', role=int(AccountRoleEnum.ADMIN), phone='13000000000', ) self.student = PracticeStudentUser.objects.create( account=account, school=school, code='1700000000', realname='121', ) self.course = PracticeCourse.objects.create( school=school, author=account, name='软件测试', description='教授软件开发测试流程及方法', start_time=time.time(), end_time=time.time() + 3600)
def put(self, request, aid=''): """ 修改用户信息 :param request: :param aid: :return: """ params = ParamsParser(request.JSON) if self.fetch_me: logic = AccountLogic(self.auth, self.auth.get_account()) else: # administrators(lambda x: True)(self) logic = AccountLogic(self.auth, aid) account = logic.account if params.has('nickname'): nickname = params.str('nickname', desc='昵称', max_length=MAX_ACCOUNT_NICKNAME_LENGTH) if Account.objects.filter(nickname=nickname).exclude( id=aid).exists(): raise AccountInfoExcept.nickname_is_exists() account.nickname = nickname with params.diff(account): account.sex = params.int('sex', desc='性别') account.motto = params.str('motto', desc='一句话签名', max_length=MAX_MOTTO_LENGTH) account.phone = params.str('phone', desc='联系电话') # account.save() if params.has('new_password'): new_password = params.str('new_password', desc='新密码', min_length=MIN_PASSWORD_LENGTH, max_length=MAX_PASSWORD_LENGTH) old_password = params.str('old_password', desc='旧密码', min_length=MIN_PASSWORD_LENGTH, max_length=MAX_PASSWORD_LENGTH) if not signatures.compare_password(old_password, account.password): raise AccountInfoExcept.old_password_error() account.password = signatures.build_password_signature( new_password, signatures.gen_salt()) # account.save() # 头像保存 if params.has('avator'): avator = params.str('avator', desc='头像数据') account.avator = logic.save_account_avator(account.id, avator) # 卡权限 if params.has('role'): account.role = params.int('role', desc='权限') account.save() return SuccessResult(id=account.id)
def create_studentuser(self, params: ParamsParser): """ 创建学生账户 :param params: :return: """ code = params.str('code', desc='学号') phone = params.str('phone', desc='手机') # 学号存在或者,账号已绑定该学校则直接放回 studentuser = PracticeStudentUser.objects.filter(school=self.school).filter( Q(code=code) | Q(account__phone=phone) ) if studentuser.exists(): return studentuser[0] account = Account.objects.filter(phone=phone) if account.exists(): account = account[0] else: try: with transaction.atomic(): account = Account.objects.create( username='******'.format(phone, self.school.name), sex=int(AccountSexEnum.UNKNOW), password=signatures.build_password_signature(phone, signatures.gen_salt()), nickname=params.str('realname', desc='真实名称'), role=int(AccountRoleEnum.USER), phone=phone, ) except Exception as ex: raise PracticeStudentUserInfoExcept.create_studentuser_fail() try: with transaction.atomic(): studentuser = PracticeStudentUser.objects.create( account=account, school=self.school, code=code, realname=params.str('realname', desc='真实名称'), ) except Exception as ex: raise PracticeStudentUserInfoExcept.create_studentuser_fail() return studentuser
def post(self, request): """ 上传图片识别存在 :param request: :return: """ params = ParamsParser(request.GET) gid = params.int('gid', desc='分组id', require=False, default=0) file = request.FILES.get('file') # 全局或者独立分组 logic = FaceUGroupsLogic(self.auth, gid if gid else '') d_logic = FaceUDistinguishLogic(self.auth, gid) # 人脸识别 client = FireHydrantFacecRecognitionClient() uuids, image = client.recognition(file.read(), d_logic.to_face_uuid()) # 结果图存储 _uid = gen_salt() if gid: storage = FireHydrantLocalStorage('facec_distinguish') vpath = storage.save_file(f"{gid}/{_uid}.jpeg", image, open_type='wb') else: storage = FireHydrantLocalStorage('facec_distinguish_static') vpath = storage.save_file(f"{_uid}.jpeg", image, open_type='wb') entity = FaceURecordEntity() entity.update({ 'storage': [vpath], 'record': d_logic.from_face_uuid(uuids) }) record = FaceUDistinguishRecord.objects.create( author=self.auth.get_account(), result=entity.dumps(), ) if gid: record.group_id = gid record.save() return SuccessResult(id=record.id)
def post(self, request): """ 注册账户 :param request: :return: """ params = ParamsParser(request.JSON) # TODO: 后续添加邮箱验证 username = params.str('username', desc='用户名', max_length=MAX_USERNAME_LENGTH) if Account.objects.filter(username=username).exists(): raise AccountInfoExcept.username_is_exists() with transaction.atomic(): account = Account.objects.create( username=username, sex=params.int('sex', desc='性别', default=0, require=False), nickname=username, password=signatures.build_password_signature(params.str( 'password', desc='密码', min_length=MIN_PASSWORD_LENGTH, max_length=MAX_PASSWORD_LENGTH), signatures.gen_salt()), ) return SuccessResult(id=account.id)