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 user map of institutions (WIP) #773

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
31 changes: 31 additions & 0 deletions django/core/management/commands/delete_ror_affiliations.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just for testing, right? I'd leave this untracked since it won't get used in production

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.core.management.base import BaseCommand
from core.models import MemberProfile
import logging

logger = logging.getLogger(__name__)

class Command(BaseCommand):
help = """Remove coordinates from each affiliation in all member profiles"""

def handle(self, *args, **options):
all_member_profiles = MemberProfile.objects.all()

for profile in all_member_profiles:
updated = False
affiliations = profile.affiliations


for affiliation in affiliations:

if "coordinates" in affiliation:
del affiliation["coordinates"]
#del affiliation["links"]
#del affiliation["types"]
updated = True

if updated:
profile.affiliations = affiliations
profile.save()



72 changes: 72 additions & 0 deletions django/core/management/commands/update_ror_affiliations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from django.core.management.base import BaseCommand
from core.models import MemberProfile
import requests
import time
import logging
from pathlib import Path
import concurrent.futures
import json
import http.server
import socketserver
import argparse


from core.models import MemberProfile

logger = logging.getLogger(__name__)


class Command(BaseCommand):
help = """something"""

def __init__(self):
super().__init__()
self.session = requests.Session()

def lookup_ror_id(self, ror_id):
api_url = f"https://api.ror.org/organizations/{ror_id}"
try:
response = self.session.get(api_url, timeout=10)
response.raise_for_status()
data = response.json()
print(".", end="", flush=True)
return data["name"], data["addresses"][0]["lat"], data["addresses"][0]["lng"], data["links"][0], data["types"][0]
except requests.RequestException as e:
print("E", end="", flush=True)
return None, None, None, None, None



def handle(self, *args, **options):
# TODO: look up every memberprofile.affiliations that has a ror_id and add the lat, lon to each json record
#add sessions to make faster
#get links and types as well
#make metrics.py function to get list of institution data
all_member_profiles = MemberProfile.objects.all()

for profile in all_member_profiles:

updated = False
affiliations = profile.affiliations


for affiliation in affiliations:
if "ror_id" not in affiliation:
continue

if "coordinates" not in affiliation:
name, lat, lon, links, types = self.lookup_ror_id(affiliation["ror_id"])
if lat is not None and lon is not None:
affiliation["name"] = name
affiliation["coordinates"] = {"lat": lat, "lon": lon}
affiliation["link"] = links
affiliation["type"] = types
updated = True

if updated:
profile.affiliations = affiliations
profile.save()

self.session.close()


Loading