Skip to content

Commit

Permalink
added new api for fetching project cleanup data
Browse files Browse the repository at this point in the history
  • Loading branch information
avikdatta committed Feb 7, 2024
1 parent 35cda3e commit 48f0ed1
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .admin_home_api import AdminHomeApi
from .raw_seqrun_api import RawSeqrunApi
from .raw_analysis_api import RawAnalysisApi
from .project_cleanup_api import ProjectCleanupApi


"""
Expand All @@ -17,4 +18,5 @@
appbuilder.add_api(RawMetadataDataApi)
appbuilder.add_api(AdminHomeApi)
appbuilder.add_api(RawSeqrunApi)
appbuilder.add_api(RawAnalysisApi)
appbuilder.add_api(RawAnalysisApi)
appbuilder.add_api(ProjectCleanupApi)
57 changes: 57 additions & 0 deletions app/project_cleanup_api.py
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)
81 changes: 81 additions & 0 deletions tests/test_project_cleanup_api.py
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'

0 comments on commit 48f0ed1

Please sign in to comment.