-
Notifications
You must be signed in to change notification settings - Fork 48
Feat: Create Accounts on UI #2176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joshuaunity
wants to merge
24
commits into
main
Choose a base branch
from
feat/creata-accounts-UI
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+676
−87
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
4bac1ff
Feat: implement account creation functionality with UI and API integr…
joshuaunity b278a99
Feat: improve account creation validation for unique names and non-em…
joshuaunity 0f3ca03
ref: improved account form UX with color picker additions
joshuaunity e99fa0e
chore: add changelog entry
joshuaunity 25504e2
Merge branch 'main' into feat/creata-accounts-UI
joshuaunity bb4b466
docs: improve changelog entry to mention both API and UI for account …
Copilot 198a092
chore: update accoutn role name
joshuaunity 44e3a96
ref: move advanced color picker to account edit form and add consulta…
joshuaunity 9bd8064
feat: enhance account UI with 'Add client account' button for consult…
joshuaunity 2707f49
feat: add account patch schema and enhance account roles management i…
joshuaunity 21cf7ae
feat: enhance security documentation on account roles and their manag…
joshuaunity 64f0be0
chore: modify changelog
joshuaunity 845514d
ref: strip down permission guard
joshuaunity 8c0f5bb
Merge branch 'feat/creata-accounts-UI' of github.com:FlexMeasures/fle…
joshuaunity 7cf7783
feat: clarify consultancy account functionality and user roles in sec…
joshuaunity e30ab32
Update flexmeasures/auth/policy.py
joshuaunity a5e0f85
Update flexmeasures/auth/policy.py
joshuaunity 879c811
fix: fixed failing test using wrong user
joshuaunity a3e83a0
chore: udpate docs
joshuaunity 39559c8
fix: removed double scroolbar in users and accounts tables
joshuaunity c30938b
feat: add button to create client accounts in account details
joshuaunity 69845ab
fix: remove redundant error handling for account creation
joshuaunity 3d518f4
Merge branch 'main' into feat/creata-accounts-UI
joshuaunity db9bac2
fix: reorder inheritance in AccountIdField for clarity
joshuaunity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,30 +8,38 @@ | |
| from sqlalchemy import or_, select, func | ||
| from flask_sqlalchemy.pagination import SelectPagination | ||
|
|
||
| from flexmeasures.auth.policy import user_has_admin_access | ||
| from flexmeasures.auth.policy import ( | ||
| user_has_admin_access, | ||
| CONSULTANT_ROLE, | ||
| FlexMeasuresPlatform, | ||
| ) | ||
| from flexmeasures.auth.decorators import permission_required_for_context | ||
| from flexmeasures.data.models.annotations import Annotation, get_or_create_annotation | ||
| from flexmeasures.data.models.audit_log import AuditLog | ||
| from flexmeasures.data.models.user import Account, User | ||
| from flexmeasures.data.models.user import Account, User, AccountRole | ||
| from flexmeasures.data.models.generic_assets import GenericAsset | ||
| from flexmeasures.data.services.accounts import get_accounts, get_audit_log_records | ||
| from flexmeasures.api.common.schemas.users import AccountIdField | ||
| from flexmeasures.data.schemas.account import AccountSchema | ||
| from flexmeasures.data.schemas.account import ( | ||
| AccountSchema, | ||
| AccountCreateSchema, | ||
| AccountPatchSchema, | ||
| ) | ||
| from flexmeasures.data.schemas.annotations import AnnotationSchema | ||
| from flexmeasures.utils.time_utils import server_now | ||
| from flexmeasures.api.common.schemas.users import AccountAPIQuerySchema | ||
|
|
||
| """ | ||
| API endpoints to manage accounts. | ||
|
|
||
| Both POST (to create) and DELETE are not accessible via the API, but as CLI functions. | ||
| Editing (PATCH) is also not yet implemented, but might be next, e.g. for the name or roles. | ||
| DELETE is not accessible via the API, but as a CLI function. | ||
| """ | ||
|
|
||
| # Instantiate schemas outside of endpoint logic to minimize response time | ||
| account_schema = AccountSchema() | ||
| accounts_schema = AccountSchema(many=True) | ||
| partial_account_schema = AccountSchema(partial=True) | ||
| partial_account_schema = AccountPatchSchema() | ||
| account_create_schema = AccountCreateSchema() | ||
| annotation_schema = AnnotationSchema() | ||
|
|
||
|
|
||
|
|
@@ -178,6 +186,61 @@ def index( | |
|
|
||
| return response, 200 | ||
|
|
||
| @route("", methods=["POST"]) | ||
| @use_args(account_create_schema, arg_name="account_data") | ||
| @permission_required_for_context( | ||
| "create-children", | ||
| ctx_loader=FlexMeasuresPlatform.init, | ||
| ) | ||
| @as_json | ||
| def post(self, account_data: dict): | ||
| """ | ||
| .. :quickref: Accounts; Create an account. | ||
| --- | ||
| post: | ||
| summary: Create a new account. | ||
| description: | | ||
| Create a new account with a required name. | ||
|
|
||
| - Admin users can create accounts. | ||
| - Consultant users can create accounts only if their account has the | ||
| `CONSULTANCY_ACCOUNT_ROLE` account role. | ||
| - For consultant users, the newly created account is automatically | ||
| linked to their own account as consultancy account. | ||
|
|
||
| security: | ||
| - ApiKeyAuth: [] | ||
| requestBody: | ||
| description: Account fields for creation. | ||
| required: true | ||
| content: | ||
| application/json: | ||
| schema: AccountCreateSchema | ||
| example: | ||
| name: New Customer Account | ||
| consultancy_account_id: 2 | ||
| responses: | ||
| 201: | ||
| description: PROCESSED | ||
| 401: | ||
| description: UNAUTHORIZED | ||
| 403: | ||
| description: INVALID_SENDER | ||
| 422: | ||
| description: UNPROCESSABLE_ENTITY | ||
| tags: | ||
| - Accounts | ||
| """ | ||
|
|
||
| if current_user.has_role(CONSULTANT_ROLE): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also check if the account has that "Consultancy " role. And this should only happen if no consultancy_account_id is sent in. |
||
| account_data["consultancy_account_id"] = current_user.account.id | ||
|
|
||
| account = Account(**account_data) | ||
| db.session.add(account) | ||
| db.session.commit() | ||
|
|
||
| return account_schema.dump(account), 201 | ||
|
|
||
| @route("/<id>", methods=["GET"]) | ||
| @use_kwargs({"account": AccountIdField(data_key="id")}, location="path") | ||
| @permission_required_for_context("read", ctx_arg_name="account") | ||
|
|
@@ -259,9 +322,10 @@ def patch(self, account_data: dict, id: int, account: Account): | |
| required: true | ||
| content: | ||
| application/json: | ||
| schema: AccountSchema | ||
| schema: AccountPatchSchema | ||
| example: | ||
| name: Test Account Updated | ||
| account_roles: [1, 3] | ||
| primary_color: '#1a3443' | ||
| secondary_color: '#f1a122' | ||
| logo_url: 'https://example.com/logo.png' | ||
|
|
@@ -326,7 +390,31 @@ def patch(self, account_data: dict, id: int, account: Account): | |
| "logo_url", | ||
| "consultancy_account_id", | ||
| "attributes", | ||
| "account_roles", | ||
| ] | ||
|
|
||
| if "account_roles" in account_data: | ||
| raw_roles = account_data["account_roles"] | ||
| if not isinstance(raw_roles, list) or any( | ||
| not isinstance(role_id, int) for role_id in raw_roles | ||
| ): | ||
| return {"errors": ["account_roles must be a list of integer IDs."]}, 422 | ||
|
|
||
| resolved_roles = [ | ||
| db.session.get(AccountRole, role_id) for role_id in raw_roles | ||
| ] | ||
| invalid_role_ids = [ | ||
| role_id | ||
| for role_id, db_role in zip(raw_roles, resolved_roles) | ||
| if db_role is None | ||
| ] | ||
| if invalid_role_ids: | ||
| return { | ||
| "errors": [f"Invalid account role ID(s): {invalid_role_ids}."] | ||
| }, 422 | ||
|
|
||
| account_data["account_roles"] = resolved_roles | ||
|
|
||
| modified_fields = { | ||
| field: getattr(account, field) | ||
| for field in fields_to_check | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.