def put(self, username): """ Creates user --- tags: - admin - users produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string - in: body name: body required: true description: JSON object for creating a new user schema: properties: password: type: string default: password role: type: string default: user - in: path name: username description: the unique user required: true type: string default: dmisuser responses: 201: description: User Created 400: description: Invalid request 401: description: Unauthorized, credentials are invalid 403: description: Forbidden, username already exists 500: description: Internal Server Error """ try: user_dto = UserDTO(request.get_json()) user_dto.username = username user_dto.validate() except DataError as e: current_app.logger.error(f'error validating request: {str(e)}') return str(e), 400 try: UserService.create_user(user_dto) except UserExistsError as e: return {"Error": str(e)}, 403 except Exception as e: error_msg = f'User Create - Unhandled error: {str(e)}' current_app.logger.critical(error_msg) return {"Error": error_msg}, 500
def as_dto(self, logged_in_username: str) -> UserDTO: """ Create DTO object from user in scope """ user_dto = UserDTO() user_dto.id = self.id user_dto.username = self.username user_dto.role = UserRole(self.role).name user_dto.mapping_level = MappingLevel(self.mapping_level).name user_dto.is_expert = self.is_expert or False user_dto.date_registered = str(self.date_registered) try: user_dto.projects_mapped = len(self.projects_mapped) # Handle users that haven't touched a project yet. except: user_dto.projects_mapped = 0 user_dto.tasks_mapped = self.tasks_mapped user_dto.tasks_validated = self.tasks_validated user_dto.tasks_invalidated = self.tasks_invalidated user_dto.twitter_id = self.twitter_id user_dto.linkedin_id = self.linkedin_id user_dto.facebook_id = self.facebook_id user_dto.validation_message = self.validation_message user_dto.total_time_spent = 0 user_dto.time_spent_mapping = 0 user_dto.time_spent_validating = 0 sql = """SELECT SUM(TO_TIMESTAMP(action_text, 'HH24:MI:SS')::TIME) FROM task_history WHERE (action='LOCKED_FOR_VALIDATION' or action='AUTO_UNLOCKED_FOR_VALIDATION') and user_id = :user_id;""" total_validation_time = db.engine.execute(text(sql), user_id=self.id) for row in total_validation_time: total_validation_time = row[0] if total_validation_time: total_validation_seconds = total_validation_time.total_seconds( ) user_dto.time_spent_validating = total_validation_seconds user_dto.total_time_spent += user_dto.time_spent_validating sql = """SELECT SUM(TO_TIMESTAMP(action_text, 'HH24:MI:SS')::TIME) FROM task_history WHERE (action='LOCKED_FOR_MAPPING' or action='AUTO_UNLOCKED_FOR_MAPPING') and user_id = :user_id;""" total_mapping_time = db.engine.execute(text(sql), user_id=self.id) for row in total_mapping_time: total_mapping_time = row[0] if total_mapping_time: total_mapping_seconds = total_mapping_time.total_seconds() user_dto.time_spent_mapping = total_mapping_seconds user_dto.total_time_spent += user_dto.time_spent_mapping if self.username == logged_in_username: # Only return email address when logged in user is looking at their own profile user_dto.email_address = self.email_address user_dto.is_email_verified = self.is_email_verified return user_dto
def as_dto(self, logged_in_username: str) -> UserDTO: """ Create DTO object from user in scope """ user_dto = UserDTO() user_dto.username = self.username user_dto.role = UserRole(self.role).name user_dto.mapping_level = MappingLevel(self.mapping_level).name user_dto.tasks_mapped = self.tasks_mapped user_dto.tasks_validated = self.tasks_validated user_dto.twitter_id = self.twitter_id user_dto.linkedin_id = self.linkedin_id user_dto.facebook_id = self.facebook_id if self.username == logged_in_username: # Only return email address when logged in user is looking at their own profile user_dto.email_address = self.email_address user_dto.is_email_verified = self.is_email_verified return user_dto
def as_dto(self) -> UserDTO: user_dto = UserDTO() user_dto.username = self.username user_dto.role = UserRole(self.role).name return user_dto