-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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
[dashboard] Refactor API using SIP-35 #9315
Merged
dpgaspar
merged 11 commits into
apache:master
from
preset-io:refactor/api-dashboard-sip-35
Mar 20, 2020
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0ef03de
[dashboard] Refactor API using SIP-35
dpgaspar dc009a0
[dashboard] Fix, import
dpgaspar 1c65f6b
[dashboard] more tests
dpgaspar d791077
Merge remote-tracking branch 'upstream/master' into refactor/api-dash…
dpgaspar 3b69a1e
[dashboards] a misc of improvements
dpgaspar f4967dc
[charts] Fix, DAO and tests
dpgaspar 6b37866
Merge remote-tracking branch 'upstream/master' into refactor/api-dash…
dpgaspar ea2756f
[dashboards] small exceptions refactor
dpgaspar 7d34185
[dashboards] lint
dpgaspar a10a2d6
[dashboards] Improves comments on base classes
dpgaspar 0846c8e
[dashboards] lint
dpgaspar 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. |
This file contains 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
from typing import Dict, Optional | ||
|
||
from flask_appbuilder.models.sqla import Model | ||
from sqlalchemy.exc import SQLAlchemyError | ||
|
||
from superset.commands.exceptions import ( | ||
CreateFailedError, | ||
DeleteFailedError, | ||
UpdateFailedError, | ||
) | ||
from superset.extensions import db | ||
|
||
|
||
def generic_create(model_cls: Model, properties: Dict, commit=True) -> Optional[Model]: | ||
""" | ||
Generic for creating models | ||
""" | ||
model = model_cls() | ||
for key, value in properties.items(): | ||
setattr(model, key, value) | ||
try: | ||
db.session.add(model) | ||
if commit: | ||
db.session.commit() | ||
except SQLAlchemyError as e: # pragma: no cover | ||
db.session.rollback() | ||
raise CreateFailedError(exception=e) | ||
return model | ||
|
||
|
||
def generic_update(model: Model, properties: Dict, commit=True) -> Optional[Model]: | ||
""" | ||
Generic update a model | ||
""" | ||
for key, value in properties.items(): | ||
setattr(model, key, value) | ||
try: | ||
db.session.merge(model) | ||
if commit: | ||
db.session.commit() | ||
except SQLAlchemyError as e: # pragma: no cover | ||
db.session.rollback() | ||
raise UpdateFailedError(exception=e) | ||
return model | ||
|
||
|
||
def generic_delete(model: Model, commit=True): | ||
try: | ||
db.session.delete(model) | ||
if commit: | ||
db.session.commit() | ||
except SQLAlchemyError as e: # pragma: no cover | ||
db.session.rollback() | ||
raise DeleteFailedError(exception=e) | ||
return model |
This file contains 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't pass in the class here. Instead, the caller will have created whatever instance he wants and then should pass it in. You can then combine your create / update into a single
upsert
method. Also, you can call this something likedef upsert(model, commit=True)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do that and end up with less code. Yet I tend to think it's more readable this way (and easier to reason), DAO create methods call
generic_create
, update methods callgeneric_update
, also Exceptions follow the same pattern.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we shouldn't combine create and update. If I'm trying to update a pre-existing record and someone deletes it in the interim from another process, I want that to error. Upsert would hide the race condition.