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

Add code to purge collections and reinitialize from production #502

Merged
merged 1 commit into from
Nov 1, 2023
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
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