def signup(obj): """ Register an account :param obj: Json, {username:'', password:'', email:''} :return: Object, account/None """ try: info = DBHelper.set_info(obj['email']) ext_id = hashlib.md5(obj['username'] + '@' + app.config['APP_NAME']).hexdigest() obj = { 'username': obj['username'], 'password': obj['password'], 'user_info_id': info, 'status': 1, 'ext_id': ext_id } account = DBHelper.set_user(obj) if account is not None: return account else: return None except Exception as e: print(str(e)) return None
def post(self): """ register a new account for user :return: """ # if not request.json: # raise InvalidUsage('Payload is not JSON', status_code=400) obj = request.get_json() # obj = {'username':'******', 'password':'******', 'email':'3'} if 'username' not in obj: raise CustomError('Parameter <username> missing', status_code=400) if 'password' not in obj: raise CustomError('Parameter <password> missing', status_code=400) if 'email' not in obj: raise CustomError('Parameter <email> missing', status_code=400) info = DBHelper.set_info(obj['email']) if DBHelper.check_username(obj['username']) is False: raise CustomError('register failed, username has existed!', status_code=409) ext_id = hashlib.md5(obj['username'] + '@' + app.config['APP_NAME']).hexdigest() obj = { 'username': obj['username'], 'password': obj['password'], 'user_info_id': info, 'status': 1, 'ext_id': ext_id } account = DBHelper.set_user(obj) # app_log.info(('new user {0} registered').format(obj['username']), extra={'sender': 'DataSource'}) return { 'message': 'register successfully', 'ext_id': account.ext_id, 'status_code': 201 }, 201