Skip to content

Commit

Permalink
Merge pull request #674 from upconsulting/develop
Browse files Browse the repository at this point in the history
Story/iexp 255 (#673)
  • Loading branch information
jdamerow authored Dec 30, 2020
2 parents 18f056d + 90ae5ea commit e7e9a61
Show file tree
Hide file tree
Showing 9 changed files with 1,166 additions and 516 deletions.
250 changes: 250 additions & 0 deletions isiscb/countryCode.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
Name,code2,code3
Afghanistan,AF,AFG
Albania,AL,ALB
Algeria,DZ,DZA
American Samoa,AS,ASM
Andorra,AD,AND
Angola,AO,AGO
Anguilla,AI,AIA
Antarctica,AQ,ATA
Antigua and Barbuda,AG,ATG
Argentina,AR,ARG
Armenia,AM,ARM
Aruba,AW,ABW
Australia,AU,AUS
Austria,AT,AUT
Azerbaijan,AZ,AZE
Bahamas (the),BS,BHS
Bahrain,BH,BHR
Bangladesh,BD,BGD
Barbados,BB,BRB
Belarus,BY,BLR
Belgium,BE,BEL
Belize,BZ,BLZ
Benin,BJ,BEN
Bermuda,BM,BMU
Bhutan,BT,BTN
Bolivia (Plurinational State of),BO,BOL
"Bonaire,Sint Eustatius and Saba",BQ,BES
Bosnia and Herzegovina,BA,BIH
Botswana,BW,BWA
Bouvet Island,BV,BVT
Brazil,BR,BRA
British Indian Ocean Territory (the),IO,IOT
Brunei Darussalam,BN,BRN
Bulgaria,BG,BGR
Burkina Faso,BF,BFA
Burundi,BI,BDI
Cabo Verde,CV,CPV
Cambodia,KH,KHM
Cameroon,CM,CMR
Canada,CA,CAN
Cayman Islands (the),KY,CYM
Central African Republic (the),CF,CAF
Chad,TD,TCD
Chile,CL,CHL
China,CN,CHN
Christmas Island,CX,CXR
Cocos (Keeling) Islands (the),CC,CCK
Colombia,CO,COL
Comoros (the),KM,COM
Congo (the Democratic Republic of the),CD,COD
Congo (the),CG,COG
Cook Islands (the),CK,COK
Costa Rica,CR,CRI
Croatia,HR,HRV
Cuba,CU,CUB
Curaçao,CW,CUW
Cyprus,CY,CYP
Czechia,CZ,CZE
Côte d'Ivoire,CI,CIV
Denmark,DK,DNK
Djibouti,DJ,DJI
Dominica,DM,DMA
Dominican Republic (the),DO,DOM
Ecuador,EC,ECU
Egypt,EG,EGY
El Salvador,SV,SLV
Equatorial Guinea,GQ,GNQ
Eritrea,ER,ERI
Estonia,EE,EST
Eswatini,SZ,SWZ
Ethiopia,ET,ETH
Falkland Islands (the) [Malvinas],FK,FLK
Faroe Islands (the),FO,FRO
Fiji,FJ,FJI
Finland,FI,FIN
France,FR,FRA
French Guiana,GF,GUF
French Polynesia,PF,PYF
French Southern Territories (the),TF,ATF
Gabon,GA,GAB
Gambia (the),GM,GMB
Georgia,GE,GEO
Germany,DE,DEU
Ghana,GH,GHA
Gibraltar,GI,GIB
Greece,GR,GRC
Greenland,GL,GRL
Grenada,GD,GRD
Guadeloupe,GP,GLP
Guam,GU,GUM
Guatemala,GT,GTM
Guernsey,GG,GGY
Guinea,GN,GIN
Guinea-Bissau,GW,GNB
Guyana,GY,GUY
Haiti,HT,HTI
Heard Island and McDonald Islands,HM,HMD
Holy See (the),VA,VAT
Honduras,HN,HND
Hong Kong,HK,HKG
Hungary,HU,HUN
Iceland,IS,ISL
India,IN,IND
Indonesia,ID,IDN
Iran (Islamic Republic of),IR,IRN
Iraq,IQ,IRQ
Ireland,IE,IRL
Isle of Man,IM,IMN
Israel,IL,ISR
Italy,IT,ITA
Jamaica,JM,JAM
Japan,JP,JPN
Jersey,JE,JEY
Jordan,JO,JOR
Kazakhstan,KZ,KAZ
Kenya,KE,KEN
Kiribati,KI,KIR
Korea (the Democratic People's Republic of),KP,PRK
Korea (the Republic of),KR,KOR
Kuwait,KW,KWT
Kyrgyzstan,KG,KGZ
Lao People's Democratic Republic (the),LA,LAO
Latvia,LV,LVA
Lebanon,LB,LBN
Lesotho,LS,LSO
Liberia,LR,LBR
Libya,LY,LBY
Liechtenstein,LI,LIE
Lithuania,LT,LTU
Luxembourg,LU,LUX
Macao,MO,MAC
Madagascar,MG,MDG
Malawi,MW,MWI
Malaysia,MY,MYS
Maldives,MV,MDV
Mali,ML,MLI
Malta,MT,MLT
Marshall Islands (the),MH,MHL
Martinique,MQ,MTQ
Mauritania,MR,MRT
Mauritius,MU,MUS
Mayotte,YT,MYT
Mexico,MX,MEX
Micronesia (Federated States of),FM,FSM
Moldova (the Republic of),MD,MDA
Monaco,MC,MCO
Mongolia,MN,MNG
Montenegro,ME,MNE
Montserrat,MS,MSR
Morocco,MA,MAR
Mozambique,MZ,MOZ
Myanmar,MM,MMR
Namibia,NA,NAM
Nauru,NR,NRU
Nepal,NP,NPL
Netherlands (the),NL,NLD
New Caledonia,NC,NCL
New Zealand,NZ,NZL
Nicaragua,NI,NIC
Niger (the),NE,NER
Nigeria,NG,NGA
Niue,NU,NIU
Norfolk Island,NF,NFK
Northern Mariana Islands (the),MP,MNP
Norway,NO,NOR
Oman,OM,OMN
Pakistan,PK,PAK
Palau,PW,PLW
"Palestine,State of",PS,PSE
Panama,PA,PAN
Papua New Guinea,PG,PNG
Paraguay,PY,PRY
Peru,PE,PER
Philippines (the),PH,PHL
Pitcairn,PN,PCN
Poland,PL,POL
Portugal,PT,PRT
Puerto Rico,PR,PRI
Qatar,QA,QAT
Republic of North Macedonia,MK,MKD
Romania,RO,ROU
Russian Federation (the),RU,RUS
Rwanda,RW,RWA
Réunion,RE,REU
Saint Barthélemy,BL,BLM
"Saint Helena,Ascension and Tristan da Cunha",SH,SHN
Saint Kitts and Nevis,KN,KNA
Saint Lucia,LC,LCA
Saint Martin (French part),MF,MAF
Saint Pierre and Miquelon,PM,SPM
Saint Vincent and the Grenadines,VC,VCT
Samoa,WS,WSM
San Marino,SM,SMR
Sao Tome and Principe,ST,STP
Saudi Arabia,SA,SAU
Senegal,SN,SEN
Serbia,RS,SRB
Seychelles,SC,SYC
Sierra Leone,SL,SLE
Singapore,SG,SGP
Sint Maarten (Dutch part),SX,SXM
Slovakia,SK,SVK
Slovenia,SI,SVN
Solomon Islands,SB,SLB
Somalia,SO,SOM
South Africa,ZA,ZAF
South Georgia and the South Sandwich Islands,GS,SGS
South Sudan,SS,SSD
Spain,ES,ESP
Sri Lanka,LK,LKA
Sudan (the),SD,SDN
Suriname,SR,SUR
Svalbard and Jan Mayen,SJ,SJM
Sweden,SE,SWE
Switzerland,CH,CHE
Syrian Arab Republic,SY,SYR
Taiwan (Province of China),TW,TWN
Tajikistan,TJ,TJK
"Tanzania,United Republic of",TZ,TZA
Thailand,TH,THA
Timor-Leste,TL,TLS
Togo,TG,TGO
Tokelau,TK,TKL
Tonga,TO,TON
Trinidad and Tobago,TT,TTO
Tunisia,TN,TUN
Turkey,TR,TUR
Turkmenistan,TM,TKM
Turks and Caicos Islands (the),TC,TCA
Tuvalu,TV,TUV
Uganda,UG,UGA
Ukraine,UA,UKR
United Arab Emirates (the),AE,ARE
United Kingdom of Great Britain and Northern Ireland (the),GB,GBR
United States Minor Outlying Islands (the),UM,UMI
United States of America (the),US,USA
Uruguay,UY,URY
Uzbekistan,UZ,UZB
Vanuatu,VU,VUT
Venezuela (Bolivarian Republic of),VE,VEN
Viet Nam,VN,VNM
Virgin Islands (British),VG,VGB
Virgin Islands (U.S.),VI,VIR
Wallis and Futuna,WF,WLF
Western Sahara,EH,ESH
Yemen,YE,YEM
Zambia,ZM,ZMB
Zimbabwe,ZW,ZWE
Åland Islands,AX,ALA,
1 change: 0 additions & 1 deletion isiscb/isisdata/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ def set_facets(self, selected_facets, sqs, type_string, facet_operators):
if value and field.startswith(type_string):
field = field[len(type_string):]


# if facets should be connected with and just narrow query
# otherwise save value for combined query later
if operators.get(field, 'and') == 'or':
Expand Down
86 changes: 83 additions & 3 deletions isiscb/isisdata/isiscbviews/authority_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.shortcuts import get_object_or_404, render, redirect
from django.core.cache import caches
from django.db.models import Prefetch
from django.contrib.staticfiles import finders

from django.conf import settings

Expand All @@ -20,6 +21,20 @@
import datetime
import pytz
import base64
import csv
import os

# The following code initializes two maps:
# - one that maps two letter country codes (e.g. DE, US) to three letter country codes (e.g. DEU, USA)
# - and one that maps three letter country codes to country names
# this is needed for the map on authority pages (IEXP-255)
country_code_map = {}
name_map = {}
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../countryCode.csv'), newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
country_code_map[row['code2']] = row['code3']
name_map[row['code2']] = row['Name']

def authority(request, authority_id):
"""
Expand Down Expand Up @@ -137,12 +152,12 @@ def authority(request, authority_id):
# boxes
sqs =SearchQuerySet().models(Citation).facet('all_contributor_ids', size=100). \
facet('subject_ids', size=100).facet('institution_ids', size=100). \
facet('geographic_ids', size=100).facet('time_period_ids', size=100).\
facet('geographic_ids', size=1000).facet('time_period_ids', size=100).\
facet('category_ids', size=100).facet('other_person_ids', size=100).\
facet('publisher_ids', size=100).facet('periodical_ids', size=100).\
facet('concepts_by_subject_ids', size=100).facet('people_by_subject_ids', size=100).\
facet('institutions_by_subject_ids', size=100).facet('dataset_typed_names', size=100).\
facet('events_timeperiods_ids', size=100)
facet('events_timeperiods_ids', size=100).facet('geocodes', size=1000)
word_cloud_results = sqs.all().exclude(public="false").filter_or(author_ids=authority_id).filter_or(contributor_ids=authority_id) \
.filter_or(editor_ids=authority_id).filter_or(subject_ids=authority_id).filter_or(institution_ids=authority_id) \
.filter_or(category_ids=authority_id).filter_or(advisor_ids=authority_id).filter_or(translator_ids=authority_id) \
Expand Down Expand Up @@ -242,7 +257,6 @@ def authority(request, authority_id):
search_current = None
search_count = None


context = {
'authority_id': authority_id,
'authority': authority,
Expand Down Expand Up @@ -304,8 +318,74 @@ def authority(request, authority_id):
'url_linked_data_name': settings.URL_LINKED_DATA_NAME,
'related_dataset_facet': related_dataset_facet,
}

return render(request, 'isisdata/authority.html', context)

def get_place_map_data(request, authority_id):
sqs =SearchQuerySet().models(Citation).facet('geographic_ids', size=1000).facet('geocodes', size=1000)
word_cloud_results = sqs.all().exclude(public="false").filter_or(author_ids=authority_id).filter_or(contributor_ids=authority_id) \
.filter_or(editor_ids=authority_id).filter_or(subject_ids=authority_id).filter_or(institution_ids=authority_id) \
.filter_or(category_ids=authority_id).filter_or(advisor_ids=authority_id).filter_or(translator_ids=authority_id) \
.filter_or(publisher_ids=authority_id).filter_or(school_ids=authority_id).filter_or(meeting_ids=authority_id) \
.filter_or(periodical_ids=authority_id).filter_or(book_series_ids=authority_id).filter_or(time_period_ids=authority_id) \
.filter_or(geographic_ids=authority_id).filter_or(about_person_ids=authority_id).filter_or(other_person_ids=authority_id)
related_geographics_facet = word_cloud_results.facet_counts()['fields']['geographic_ids'] if 'fields' in word_cloud_results.facet_counts() else []

geocodes = word_cloud_results.facet_counts()['fields']['geocodes'] if 'fields' in word_cloud_results.facet_counts() else []
citation_count = _get_citation_count_per_country(geocodes)
country_map_data, country_name_map, is_mapped_map = _get_authority_places_map_data(related_geographics_facet)

labels = ['<b>{}</b><br>Citations: {}<br>Hits: {}<extra></extra>'.format(country_name_map.get(code, ''), citation_count.get(code,''), country_map_data.get(code, '')) for code in country_name_map.keys()]
# we need a map in the front end that maps three to two letter codes (USA to US for example)
# as the map needs three letter codes, but we have two letter codes indexed
two_letter_codes = { k : list(country_code_map.keys())[list(country_code_map.values()).index(k)] for k in list(country_map_data.keys())}
return JsonResponse({
'citation_count_countries': list(citation_count.keys()),
'citation_count': list(citation_count.values()),
'countries': list(country_map_data.keys()),
'map_data': list(country_map_data.values()),
'labels': labels,
'name_map': list(country_name_map.values()),
'two_letter_codes': two_letter_codes,
'is_mapped_map': is_mapped_map
})

def _get_citation_count_per_country(facets):
country_map = {}
for facet in facets:
if facet[0] in country_code_map:
country_map[country_code_map[facet[0]]] = facet[1]
return country_map

def _get_authority_places_map_data(facets):
country_map = {}
country_name_map = {}

ids = [f[0] for f in facets]
is_mapped_map = {}
facets_dict = dict(facets)
authority_ids = Authority.objects.filter(pk__in=ids, attributes__type_controlled__name=settings.COUNTRY_CODE_ATTRIBUTE).values_list('id').distinct()
for id in authority_ids:
country_attrs = Attribute.objects.filter(source_instance_id=id[0], type_controlled__name=settings.COUNTRY_CODE_ATTRIBUTE)
is_mapped_map[id[0]] = True if country_attrs else False
for attr in country_attrs:
attr_value = attr.value.display
attr_value_list = attr_value.split(",")
for code_two_letters in attr_value_list:
if not code_two_letters in country_code_map:
continue

code_two_letters = code_two_letters.strip()
code_three_letters = country_code_map[code_two_letters]
country_name_map[code_three_letters] = name_map[code_two_letters]
if code_three_letters in country_map:
country_map[code_three_letters] = country_map[code_three_letters] + facets_dict[id[0]]
else:
country_map[code_three_letters] = facets_dict[id[0]]

return country_map, country_name_map, is_mapped_map


def authority_author_timeline(request, authority_id):
now = datetime.datetime.now()

Expand Down
61 changes: 61 additions & 0 deletions isiscb/isisdata/static/isisdata/js/plotly-latest.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit e7e9a61

Please sign in to comment.