Skip to content
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

🐛 Fix study sync f-string #799

Merged
merged 2 commits into from
Nov 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions creator/studies/management/commands/syncstudies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,60 @@
from creator.studies.models import Study


API = 'https://kf-api-dataservice.kidsfirstdrc.org'
API = "https://kf-api-dataservice.kidsfirstdrc.org"


class Command(BaseCommand):
help = 'Sync studies with the dataservice'
help = "Sync studies with the dataservice"

def add_arguments(self, parser):
parser.add_argument('--api',
default=API,
help='Address of the dataservice api',
type=str)
parser.add_argument(
"--api",
default=API,
help="Address of the dataservice api",
type=str,
)

def handle(self, *args, **options):
api = options.get('api')
env = 'prd'
if '-dev' in api:
env = 'dev'
elif '-qa' in api:
env = 'qa'
api = options.get("api")
env = "prd"
if "-dev" in api:
env = "dev"
elif "-qa" in api:
env = "qa"

bucket = f'kf-study-us-east-1-{env}-'
bucket = f"kf-study-us-east-1-{env}-"

resp = requests.get(f'{api}/studies?limit=100')
studies = resp.json()['results']
resp = requests.get(f"{api}/studies?limit=100")
studies = resp.json()["results"]

for study in studies:
fields = study
del fields['_links']
del fields["_links"]

if fields['name'] is None:
fields['name'] = ''
if fields["name"] is None:
fields["name"] = ""
new_study, created = Study.objects.update_or_create(
defaults=fields,
kf_id=fields['kf_id'])
s3_id = fields['kf_id'].lower().replace('_', '-')
defaults=fields, kf_id=fields["kf_id"]
)
s3_id = fields["kf_id"].lower().replace("_", "-")
new_study.bucket = f"{bucket}{s3_id}"

# If the study was found in the dataservice, it must not be deleted
new_study.deleted = False
new_study.save()
if created:
print('Created', study['kf_id'])
print("Created", study["kf_id"])
else:
print('Updated', study['kf_id'])
print("Updated", study["kf_id"])

ds_studies = {study['kf_id'] for study in studies}
ds_studies = {study["kf_id"] for study in studies}
creator_studies = {study.kf_id for study in Study.objects.all()}
deleted_studies = creator_studies - ds_studies

for study in deleted_studies:
Study.objects.filter(kf_id=study).update(deleted=True)
print(
f"{len(deleted_studies)} studies were marked as deleted: ",
"{deleted_studies}",
f"{deleted_studies}",
)