Skip to content

Commit

Permalink
Merge branch 'master' into cs/SC-3473-import-cca-roles-from-hq
Browse files Browse the repository at this point in the history
  • Loading branch information
Charl1996 committed Aug 29, 2024
2 parents 767d629 + 8b0e2e8 commit 1ff1d96
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
21 changes: 14 additions & 7 deletions hq_superset/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
json_error_response,
json_success,
)
from .exceptions import TableMissing

from .models import DataSetChange
from .oauth2_server import authorization, require_oauth
from .tasks import process_dataset_change


class OAuth(BaseApi):
Expand Down Expand Up @@ -60,16 +60,23 @@ def post_dataset_change(self) -> FlaskResponse:

try:
request_json = json.loads(request.get_data(as_text=True))
change = DataSetChange(**request_json)
change.update_dataset()
return json_success('Dataset updated')
except json.JSONDecodeError:
return json_error_response(
'Invalid JSON syntax',
status=HTTPStatus.BAD_REQUEST.value,
)
except TableMissing:

try:
# ensure change request is parsable
DataSetChange(**request_json)
except:
return json_error_response(
'Data source not found',
status=HTTPStatus.HTTP_404_NOT_FOUND.value,
'Could not parse change request',
status=HTTPStatus.BAD_REQUEST.value,
)

process_dataset_change.delay(request_json)
return json_success(
'Dataset change accepted',
status=HTTPStatus.ACCEPTED.value,
)
13 changes: 0 additions & 13 deletions hq_superset/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import time
from dataclasses import dataclass
from typing import Any

Expand All @@ -7,7 +6,6 @@
OAuth2TokenMixin,
)
from cryptography.fernet import MultiFernet
from sqlalchemy import update
from superset import db

from .const import OAUTH2_DATABASE_NAME
Expand Down Expand Up @@ -78,17 +76,6 @@ def set_client_secret(self, plaintext):
def check_client_secret(self, plaintext):
return self.get_client_secret() == plaintext

def revoke_tokens(self):
revoked_at = int(time.time())
stmt = (
update(OAuth2Token)
.where(OAuth2Token.client_id == self.client_id)
.where(OAuth2Token.access_token_revoked_at == 0)
.values(access_token_revoked_at=revoked_at)
)
db.session.execute(stmt)
db.session.commit()


class OAuth2Token(db.Model, OAuth2TokenMixin):
__bind_key__ = OAUTH2_DATABASE_NAME
Expand Down
5 changes: 2 additions & 3 deletions hq_superset/oauth2_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@

def save_token(token: dict, request: FlaskOAuth2Request) -> None:
client = request.client
client.revoke_tokens()

token = OAuth2Token(
client_id=client.client_id,
token_type=token['token_type'],
access_token=token['access_token'],
scope=client.domain,
expires_in=0, # Token does not expire
expires_in=10, # 10 Seconds
)
db.session.add(token)
db.session.commit()
Expand All @@ -42,7 +41,7 @@ def save_token(token: dict, request: FlaskOAuth2Request) -> None:
def config_oauth2(app):
authlib_logger = logging.getLogger('authlib')
authlib_logger.addHandler(logging.StreamHandler(sys.stdout))
authlib_logger.setLevel(logging.DEBUG)
authlib_logger.setLevel(logging.INFO)

authorization.init_app(app)
authorization.register_grant(grants.ClientCredentialsGrant)
Expand Down
11 changes: 11 additions & 0 deletions hq_superset/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from superset.extensions import celery_app

from .exceptions import TableMissing
from .models import DataSetChange
from .services import AsyncImportHelper, refresh_hq_datasource


Expand All @@ -15,3 +17,12 @@ def refresh_hq_datasource_task(domain, datasource_id, display_name, export_path,
finally:
if os.path.exists(export_path):
os.remove(export_path)


@celery_app.task(name='process_dataset_change')
def process_dataset_change(request_json):
change = DataSetChange(**request_json)
try:
change.update_dataset()
except TableMissing:
pass
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='hq_superset',
version='0.3.3',
version='0.3.5',
description='CommCare HQ Superset Integration',
license='Apache2',
author='Dimagi Inc.',
Expand Down

0 comments on commit 1ff1d96

Please sign in to comment.