def validate_user_sources(sources: Union[int, str, List[Union[int, str]]]) -> List[int]: """Return a list of user source ids given a user id, a role name or a list thereof.""" sources = ( sources if isinstance(sources, list) else [sources] ) # Make sure sources is a list user_source_ids: List[int] = [] for source in sources: if isinstance(source, int): # Parse as user id try: user_source_ids.extend( db.session.query(DataSource.id) .filter(DataSource.user_id == source) .one_or_none() ) except TypeError: current_app.logger.warning("Could not retrieve data source %s" % source) pass else: # Parse as role name user_ids = [user.id for user in get_users(source)] user_source_ids.extend( [ params[0] for params in db.session.query(DataSource.id) .filter(DataSource.user_id.in_(user_ids)) .all() ] ) return list(set(user_source_ids)) # only unique ids
def with_options( form: Union[AssetForm, NewAssetForm]) -> Union[AssetForm, NewAssetForm]: if "asset_type_name" in form: form.asset_type_name.choices = [("none chosen", "--Select type--")] + [ (atype.name, atype.display_name) for atype in AssetType.query.all() ] if "owner_id" in form: form.owner_id.choices = [(-1, "--Select existing--")] + [ (o.id, o.username) for o in get_users(role_name="Prosumer") ] if "market_id" in form: form.market_id.choices = [(-1, "--Select existing--")] + [ (m.id, m.display_name) for m in get_markets() ] return form
def index(self, account: Account, include_inactive: bool = False): """API endpoint to list all users of an account. .. :quickref: User; Download user list This endpoint returns all accessible users. By default, only active users are returned. The `include_inactive` query parameter can be used to also fetch inactive users. Accessible users are users in the same account as the current user. Only admins can use this endpoint to fetch users from a different account (by using the `account_id` query parameter). **Example response** An example of one user being returned: .. sourcecode:: json [ { 'active': True, 'email': '*****@*****.**', 'account_id': 13, 'flexmeasures_roles': [1, 3], 'id': 1, 'timezone': 'Europe/Amsterdam', 'username': '******' } ] :reqheader Authorization: The authentication token :reqheader Content-Type: application/json :resheader Content-Type: application/json :status 200: PROCESSED :status 400: INVALID_REQUEST :status 401: UNAUTHORIZED :status 403: INVALID_SENDER :status 422: UNPROCESSABLE_ENTITY """ users = get_users(account_name=account.name, only_active=not include_inactive) return users_schema.dump(users), 200
def get(account: Account, include_inactive: bool = False): """List users of an account.""" users = get_users(account_name=account.name, only_active=not include_inactive) return users_schema.dump(users), 200
def get(args): """List all users.""" users = get_users(only_active=not args["include_inactive"]) return users_schema.dump(users), 200