|
42 | 42 |
|
43 | 43 | from idc_collections.models import Program, Collection, Attribute, Attribute_Ranges, \ |
44 | 44 | Attribute_Display_Values, DataSource, DataSourceJoin, DataVersion, DataSetType, \ |
45 | | - Attribute_Set_Type, Attribute_Display_Category, ImagingDataCommonsVersion, Attribute_Tooltips |
| 45 | + Attribute_Set_Type, Attribute_Display_Category, ImagingDataCommonsVersion, Attribute_Tooltips, Citation |
46 | 46 | from google_helpers.bigquery.bq_support import BigQuerySupport |
47 | 47 |
|
48 | 48 | from django.contrib.auth.models import User |
@@ -319,6 +319,35 @@ def add_source_joins(froms, from_col, tos=None, to_col=None): |
319 | 319 | DataSourceJoin.objects.bulk_create(src_joins) |
320 | 320 |
|
321 | 321 |
|
| 322 | +def load_citations(filename): |
| 323 | + try: |
| 324 | + cites_file = open(filename,"r") |
| 325 | + current_cites = [x.doi for x in Citation.objects.all()] |
| 326 | + new_cites = [] |
| 327 | + updated_cites = {} |
| 328 | + for line in csv_reader(cites_file): |
| 329 | + if "doi, citation" in line: |
| 330 | + print("[STATUS] Saw header line during citation load - skipping!") |
| 331 | + continue |
| 332 | + if line[0] in current_cites: |
| 333 | + updated_cites[line[0]] = line[1] |
| 334 | + else: |
| 335 | + new_cites.append(Citation(doi=line[0], cite=line[1])) |
| 336 | + if len(new_cites): |
| 337 | + Citation.objects.bulk_create(new_cites) |
| 338 | + print("[STATUS] The following {} DOI citations were added: {}".format(len(new_cites), " ".join([x.doi for x in new_cites]))) |
| 339 | + if len(updated_cites): |
| 340 | + to_update = Citation.objects.filter(doi__in=updated_cites.keys()) |
| 341 | + for upd in to_update: |
| 342 | + upd.cite = updated_cites[upd.doi] |
| 343 | + Citation.objects.bulk_update(to_update, ["cite"]) |
| 344 | + print("[STATUS] {} DOI citations were updated.".format(len(updated_cites))) |
| 345 | + except Exception as e: |
| 346 | + ERRORS_SEEN.append("Error seen while loading citations, check the logs!") |
| 347 | + logger.error("[ERROR] While trying to load citations: ") |
| 348 | + logger.exception(e) |
| 349 | + |
| 350 | + |
322 | 351 | def load_collections(filename, data_version="8.0"): |
323 | 352 | try: |
324 | 353 | collection_file = open(filename, "r") |
@@ -781,7 +810,8 @@ def parse_args(): |
781 | 810 |
|
782 | 811 | parser = ArgumentParser() |
783 | 812 | parser.add_argument('-j', '--config-file', type=str, default='', help='JSON file of version data to update') |
784 | | - parser.add_argument('-c', '--collex-file', type=str, default='', help='CSV data of collections to update/create') |
| 813 | + parser.add_argument('-c', '--collex-file', type=str, default='', help='CSV data of citations to update/create') |
| 814 | + parser.add_argument('-i', '--cites-file', type=str, default='', help='CSV data of collections to update/create') |
785 | 815 | parser.add_argument('-d', '--display-vals', type=str, default='', help='CSV data of display values to add/update') |
786 | 816 | parser.add_argument('-p', '--programs-file', type=str, default='', help='CSV data of programs to add/update') |
787 | 817 | parser.add_argument('-a', '--attributes-file', type=str, default='', help='CSV data of attributes to add/update') |
@@ -821,6 +851,8 @@ def main(): |
821 | 851 | len(args.programs_file) and load_programs(args.programs_file) |
822 | 852 | # Add/update collections - any new programs must be added first |
823 | 853 | len(args.collex_file) and load_collections(args.collex_file) |
| 854 | + # Add/update any citations |
| 855 | + len(args.cites_file) and load_citations(args.cites_file) |
824 | 856 | # Add/update display values for attributes |
825 | 857 | if len(args.display_vals): |
826 | 858 | dvals = load_display_vals(args.display_vals) |
|
0 commit comments