def update_user(self, user_id, validated, account_id): """ Update user information If new email address already exist raise httpconflict error There is no such conflict then user updated given parameters(email, password, first_name, last_name) Args: user_id: validated: account_id: Client can see and update own users. Returns: User: Updated user. """ user = self._get_user(user_id, account_id) if user.email != validated['email']: if self.get(model=User, email=validated['email']): raise HTTPConflict(description="This email address used by another user") user.email = validated['email'] user.password = validated['password'] user.first_name = validated['first_name'] user.last_name = validated['last_name'] return user
def create(self, params, meta, **kwargs): kwargs['consumer_id'] = kwargs['validated']['consumer_id'] project = self.db.get(Project, _id=kwargs['project_id']) if not project: raise (HTTPNotFound(description="There is no such project !")) if not project.is_user_limit_available(): raise HTTPPaymentRequired( description="Please check your project user limit") consumer_exist = self.db.exists( model=Consumer, _id=kwargs['consumer_id'], account_id=kwargs['token']['account_id']) if not consumer_exist: raise (HTTPNotFound(description="There is no such consumer !")) attached_consumer = self.db.get_attached_consumer( kwargs['consumer_id'], kwargs['project_id']) if attached_consumer: raise (HTTPConflict( description="These consumer already attached this project")) project.user_used = Project.user_used + 1 project_consumer = ProjectConsumer(consumer_id=kwargs['consumer_id'], project_id=kwargs['project_id']) self.db.session.add(project_consumer) return {"consumer_id": kwargs['consumer_id']}
def create_user(self, validated, jwt_token, role): """ Create a account user according in jwt_token's `account_id`. if email address already exist raise httpconflict error. if we don't have conflict, user created given parameters(email, password, role, account_id, first_name, last_name) and changes committed. Args: validated: (Dict) jwt_token: (Dict) role: (ERoles) Returns: dict: Repr of the resource """ hashed_password = data_hashing(validated['password']) if self.exists(model=User, email=validated['email']): raise HTTPConflict(description="This email address used by another user.") user = User(email=validated['email'], password=hashed_password, role=role, account_id=jwt_token['account_id'], first_name=validated['first_name'], last_name=validated['last_name']) self.session.add(user) return user
def update(self, params, meta, **kwargs): registration_id = kwargs['registration_id'] validated = kwargs['validated'] hashed_password = data_hashing(validated['password']) account = self.db.get(Account, is_active=False, _id=registration_id) if account is None: raise HTTPNotFound( description= "The account does not exist by given registration_id or already activated." ) if account.approve_code == validated['approve_code']: admin = User(email=validated['email'], password=hashed_password, role=ERoles.admin, first_name=validated['first_name'], last_name=validated['last_name'], account_id=account.id) account.is_active = True self.db.session.add(admin) token_payload = create_auth_token_payload( admin.id, role=admin.role, account_id=admin.account_id) token = encode_jwt_token(token_payload) add_user_token(admin.id, token) return { "token": token, "id": admin.id, "email": admin.email, "first_name": admin.first_name, "last_name": admin.last_name } else: raise HTTPConflict(description="Approve code does not match.")
def create(self, params, meta, **kwargs): """ default account type is "trial". you can see account types in zopsm.lib.setting file """ validated = kwargs.get('validated') account = self.db.exists(Account, email=validated['email']) if not account: approve_code = generate_token() testing = params.get('testing') saas_logger.debug(testing) project_limit = ACCOUNT_LIMIT['project_limit'] account = Account(approve_code=approve_code, organization_name=validated['organization_name'], email=validated['email'], project_limit=project_limit) self.db.session.add(account) self.db.session.flush() mail_response = send_account_approve_mail(approve_code, validated['email'], account.id, testing) email = Email(provider_mail_id=mail_response['id'], account_id=account.id, text=mail_response['text'], subject=mail_response['subject'], receiver=validated['email'], provider="MAILGUN", category="approve") self.db.session.add(email) else: raise HTTPConflict(description="These email address already used") return account
def before_post(self, req, resp, db_session, request): try: environment = db_session.query(Environment) \ .filter_by(id=request.environment_id).one() except NoResultFound: db_session.rollback() raise HTTPConflict( 'Conflict', 'Environment ID #%s not exist.' % (request.environment_id)) if environment.busy: db_session.rollback() raise HTTPConflict( 'Conflict', 'Environment ID #%s is busy.' % (request.environment_id)) # mark environment as busy environment.busy = True request.environment = environment
def process_response(self, req, resp, resource, req_succeeded): if req_succeeded: try: self.db.session.commit() except IntegrityError as e: self.db.session.rollback() saas_logger.error(e) raise HTTPConflict( description="There is an conflict please change input value" ) except Exception as e: self.db.session.rollback() saas_logger.error(e) raise HTTPInternalServerError( description= "Critical error occurred. We handle this error as soon as " "possible time") else: self.db.session.rollback() Session.remove()
def check_tenant_exists(self, **kwargs): if self.db.get_tenant(**kwargs): raise HTTPConflict( title='Tenant exists, 409', description='Tenant has already exist, nothing to do!')