-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new api for fetching project cleanup data
- Loading branch information
Showing
3 changed files
with
141 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import json, logging, gzip | ||
from yaml import load, Loader | ||
from flask_appbuilder import ModelRestApi | ||
from flask import request, send_file | ||
from flask_appbuilder.api import expose, rison | ||
from flask_appbuilder.models.sqla.interface import SQLAInterface | ||
from flask_appbuilder.security.decorators import protect | ||
from . import db | ||
from io import BytesIO | ||
from .models import ProjectCleanup | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
class ProjectCleanupApi(ModelRestApi): | ||
resource_name = "project_cleanup" | ||
datamodel = SQLAInterface(ProjectCleanup) | ||
|
||
@expose('/get_project_cleanup_data/<project_cleanup_id>', methods=['POST']) | ||
@protect() | ||
def get_project_cleanup_data(self, project_cleanup_id): | ||
try: | ||
result = \ | ||
db.session.\ | ||
query( | ||
ProjectCleanup.user_email, | ||
ProjectCleanup.user_name, | ||
ProjectCleanup.projects, | ||
ProjectCleanup.status, | ||
ProjectCleanup.deletion_date).\ | ||
filter(ProjectCleanup.project_cleanup_id==project_cleanup_id).\ | ||
one_or_none() | ||
if result is None: | ||
json_data = { | ||
'user_email': '', | ||
'user_name': '', | ||
'projects': '', | ||
'status': '', | ||
'deletion_date': ''} | ||
else: | ||
(user_email, user_name, projects, status, deletion_date) = \ | ||
result | ||
json_data = { | ||
'user_email': user_email, | ||
'user_name': user_name, | ||
'projects': projects, | ||
'status': status, | ||
'deletion_date': str(deletion_date)} | ||
# dump to json text | ||
json_data_dump = \ | ||
json.dumps(json_data) | ||
output = BytesIO(json_data_dump.encode()) | ||
output.seek(0) | ||
attachment_filename = \ | ||
f"project_cleanup_{project_cleanup_id}.json" | ||
return send_file(output, download_name=attachment_filename, as_attachment=True) | ||
except Exception as e: | ||
log.error(e) |
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,81 @@ | ||
import json | ||
import os | ||
from datetime import datetime, timedelta | ||
from app.models import ProjectCleanup | ||
from flask_appbuilder.const import ( | ||
API_SECURITY_PASSWORD_KEY, | ||
API_SECURITY_PROVIDER_KEY, | ||
API_SECURITY_REFRESH_KEY, | ||
API_SECURITY_USERNAME_KEY) | ||
|
||
def test_get_project_cleanup_data(db, test_client, tmp_path): | ||
res = \ | ||
test_client.post( | ||
"/api/v1/security/login", | ||
json={ | ||
API_SECURITY_USERNAME_KEY: "admin", | ||
API_SECURITY_PASSWORD_KEY: "password", | ||
API_SECURITY_PROVIDER_KEY: "db"}) | ||
assert res.status_code == 200 | ||
token = \ | ||
json.loads(res.data.decode("utf-8")).\ | ||
get("access_token") | ||
res = \ | ||
test_client.post( | ||
'/api/v1/project_cleanup/get_project_cleanup_data/1', | ||
headers={"Authorization": f"Bearer {token}"}) | ||
json_file = \ | ||
os.path.join(tmp_path, 'project_cleanup_1.json') | ||
with open(json_file, 'wb') as fp: | ||
fp.write(res.data) | ||
with open(json_file, 'r') as fp: | ||
json_data = json.load(fp) | ||
assert 'user_email' in json_data | ||
assert 'user_name' in json_data | ||
assert 'projects' in json_data | ||
assert 'status' in json_data | ||
assert 'deletion_date' in json_data | ||
assert json_data.get('user_email') == '' | ||
assert json_data.get('user_name') == '' | ||
assert json_data.get('projects') == '' | ||
assert json_data.get('status') == '' | ||
assert json_data.get('deletion_date') == '' | ||
project_cleanup1 = \ | ||
ProjectCleanup( | ||
user_email='[email protected]', | ||
user_name='e g', | ||
projects='["ProjectA", "ProjectB"]', | ||
deletion_date=datetime.now()+timedelta(days=15)) | ||
project_cleanup2 = \ | ||
ProjectCleanup( | ||
user_email='[email protected]', | ||
user_name='f h', | ||
projects='["ProjectC", "ProjectD"]', | ||
deletion_date=datetime.now()+timedelta(days=15)) | ||
try: | ||
db.session.add(project_cleanup1) | ||
db.session.add(project_cleanup2) | ||
db.session.flush() | ||
db.session.commit() | ||
except: | ||
db.session.rollback() | ||
raise | ||
res = \ | ||
test_client.post( | ||
'/api/v1/project_cleanup/get_project_cleanup_data/1', | ||
headers={"Authorization": f"Bearer {token}"}) | ||
json_file = \ | ||
os.path.join(tmp_path, 'project_cleanup_1.json') | ||
with open(json_file, 'wb') as fp: | ||
fp.write(res.data) | ||
with open(json_file, 'r') as fp: | ||
json_data = json.load(fp) | ||
assert 'user_email' in json_data | ||
assert 'user_name' in json_data | ||
assert 'projects' in json_data | ||
assert 'status' in json_data | ||
assert 'deletion_date' in json_data | ||
assert json_data.get('user_email') == '[email protected]' | ||
assert json_data.get('user_name') == 'e g' | ||
assert json_data.get('projects') == '["ProjectA", "ProjectB"]' | ||
assert json_data.get('status') == 'NOT_STARTED' |