Skip to content

Commit

Permalink
Merge pull request #502 from NASA-IMPACT/500-sync-with-prod-webapp-do…
Browse files Browse the repository at this point in the history
…esnt-work-as-expected

Add code to purge collections and reinitialize from production
  • Loading branch information
code-geek authored Nov 1, 2023
2 parents b06cb7b + a317dfc commit 827c07f
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion sde_collections/models/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,42 @@ def sinequa_configuration(self) -> str:
def github_issue_link(self) -> str:
return f"https://github.com/NASA-IMPACT/sde-project/issues/{self.github_issue_number}"

@classmethod
def _fetch_json_results(cls, url):
response = requests.get(url)

if response.status_code != 200:
print(f"Error: {response.status_code}")
return

return response.json()

@classmethod
def _create_from_json(cls, json_results):
for collection in json_results:
print("Creating collection: ", collection["name"])
collection.pop("curated_by")
cls.objects.create(**collection)

@classmethod
def purge_and_reset_collections(cls) -> None:
"""Delete all collections from local, and get the whole list of collections from prod."""
cls.objects.all().delete()

BASE_URL = "https://sde-indexing-helper.nasa-impact.net/api/collections-read/"

response_json = cls._fetch_json_results(BASE_URL)
cls._create_from_json(response_json["results"])

while response_json["next"]:
response_json = cls._fetch_json_results(response_json["next"])
cls._create_from_json(response_json["results"])

def sync_with_production_webapp(self) -> None:
"""Sync with the production webapp."""
"""Sync with the production webapp.
This is useful when syncing a particular collection.
To delete all existing collections from local, and get the whole list of collections from prod,
use the purge_and_reset_collections function."""

BASE_URL = "https://sde-indexing-helper.nasa-impact.net"
url = f"{BASE_URL}/api/collections-read/{self.id}/"
Expand Down

0 comments on commit 827c07f

Please sign in to comment.