Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ianlongden committed Mar 21, 2023
1 parent 19d0b70 commit d8f9805
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 229 deletions.
61 changes: 52 additions & 9 deletions agr_literature_service/api/triggers/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
-- <year>,
-- <resource abbrev>
-- <volume>(<issue>):<page(s)>
short_citation TEXT;
short_citation TEXT default '';
author_short author.name%type default '';
ref_year reference.page_range%type;
res_abbr TEXT default '';
Expand All @@ -43,17 +43,19 @@
iso resource.iso_abbreviation%type;
medline resource.iso_abbreviation%type;
citation TEXT default '';
-- Long citation
-- citation = get_citation_from_args(authorNames, year, title, journal,
-- ref_db_obj.volume or '',
-- ref_db_obj.issue_name or '',
-- ref_db_obj.page_range or '')
citation TEXT default '';
-- volume, issue and page range same as short citation
title reference.title%type;
authors author.name%type default '';
authors author.name%type default ' ';
auth record;
BEGIN
raise notice 'update citations for %', ref_id;
-- Get first author for short citation
SELECT CONCAT(SUBSTRING(author.first_name, 1 ,1), ' ', author.last_name) FROM author into author_short
WHERE author.reference_id = ref_id AND
Expand All @@ -78,15 +80,20 @@
-- raise notice 'Record %', auth;
authors := authors || auth.name || '; ';
end loop;
authors := SUBSTRING(authors, 1, LENGTH(authors)-2);
raise notice 'Authors %', authors;
IF authors != ' ' THEN
authors := SUBSTRING(authors, 1, LENGTH(authors)-2);
ELSE
authors := '';
END IF;
-- raise notice 'Authors %', authors;
-- Get the resource abbr
SELECT res.iso_abbreviation, res.medline_abbreviation, res.title into iso, medline, journal
FROM reference ref, resource res
WHERE ref.resource_id = res.resource_id AND
ref.reference_id = ref_id;
IF iso is NULL THEN
IF iso is not NULL THEN
res_abbr := iso;
ELSIF medline is not NULL THEN
res_abbr := medline;
Expand All @@ -101,19 +108,36 @@
if title is NULL THEN
title := '';
END IF;
if volume is NULL THEN
volume := '';
END IF;
if issue_name is NULL THEN
issue_name := '';
END IF;
if page_range is NULL THEN
page_range := '';
END IF;
if ref_year is NULL THEN
ref_year := '';
END IF;
if journal is NULL THEN
journal := '';
END IF;
-- build the ref_details
-- <volume>(<issue>):<page(s)>
ref_details := volume || ' (' || issue_name || '): ' || page_range;
raise notice 'rd: %', ref_details;
raise notice 'tit: %', title;
citation := authors || ', (' || ref_year || ') ' || title;
citation := citation || ' ' || journal || ' ' || ref_details;
-- raise notice '%', citation;
raise notice '%', citation;
short_citation := author_short || ', ' || ref_year || ', ' || res_abbr || ', ' || ref_details;
-- raise notice '%', short_citation;
IF citation_id is NULL THEN
raise notice 'sh cit: %', short_citation;
raise notice 'cit: %', citation;
INSERT INTO citation (citation, short_citation) VALUES (citation, short_citation);
citation_id := (SELECT currval('citation_citation_id_seq'));
raise notice 'citation_id %', citation_id;
Expand Down Expand Up @@ -176,13 +200,32 @@
returns TRIGGER
as $$
BEGIN
raise notice 'trgfunc_reference_update_citation: %: % % %', NEW.reference_id, NEW.title, OLD.title, OLD.curie;
IF NEW.title != OLD.title OR
NEW.volume != OLD.volume OR
NEW.issue_name != OLD.issue_name OR
NEW.page_range != OLD.page_range OR
NEW.date_published != OLD.date_published THEN
raise notice 'Calling update_citations with %', NEW.title;
PERFORM update_citations(NEW.reference_id);
ELSE
raise notice 'Failed update critieria?';
END IF;
raise notice 'Exiting trgfunc_reference_update_citation';
return NEW;
END $$ language plpgsql;
""")

trgfunc_reference_create_citation = PGFunction(
schema="public",
signature="trgfunc_reference_create_citation()",
definition="""
returns TRIGGER
as $$
BEGIN
raise notice 'trgfunc_reference_create_citation: %: % % %', NEW.reference_id, NEW.title, OLD.title, OLD.curie;
PERFORM update_citations(NEW.reference_id);
raise notice 'Exiting trgfunc_reference_create_citation';
return NEW;
END $$ language plpgsql;
""")
Expand All @@ -204,7 +247,7 @@
is_constraint=False,
definition="""AFTER INSERT ON reference
FOR EACH ROW
EXECUTE FUNCTION trgfunc_reference_update_citation()""",
EXECUTE FUNCTION trgfunc_reference_create_citation()""",
)

trgfunc_resource_update_citation = PGFunction(
Expand Down
1 change: 1 addition & 0 deletions alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ script_location = alembic

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
file_template = %%(year)d%%(month).2d%%(day).2d_%%(rev)s_%%(slug)s

# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory.
Expand Down
2 changes: 2 additions & 0 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
trg_reference_update_citation,
trg_reference_create_citation,
trgfunc_reference_update_citation,
trgfunc_reference_create_citation,
trg_resource_update_citation,
trgfunc_resource_update_citation)

Expand All @@ -32,6 +33,7 @@
trg_reference_update_citation,
trg_reference_create_citation,
trgfunc_reference_update_citation,
trgfunc_reference_create_citation,
trg_resource_update_citation,
trgfunc_resource_update_citation])

Expand Down
Loading

0 comments on commit d8f9805

Please sign in to comment.