From 09475b87821142c5cd36c6b90fb97deb2a058312 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 22 Jan 2019 21:41:35 -0800 Subject: allow passing description+extra to batch endpoints Pretty messy, but I needed some way to do this. In particular, requires json.dumps() in python code, for now. Blech. --- rust/src/editing.rs | 6 +- rust/src/endpoint_handlers.rs | 172 ++++++++++++++++++++++-------------------- rust/src/endpoints.rs | 14 +++- 3 files changed, 105 insertions(+), 87 deletions(-) (limited to 'rust/src') diff --git a/rust/src/editing.rs b/rust/src/editing.rs index e181e8a7..c17e5964 100644 --- a/rust/src/editing.rs +++ b/rust/src/editing.rs @@ -42,6 +42,8 @@ pub fn make_edit_context( editor_id: FatcatId, editgroup_id: Option, autoaccept: bool, + description: Option, + extra: Option, ) -> Result { // *either* autoaccept is false and editgroup_id is Some, *or* autoaccept is true and // editgroup_id is None @@ -54,8 +56,8 @@ pub fn make_edit_context( editor: None, changelog_index: None, submitted: None, - description: None, - extra: None, + description: description, + extra: extra, annotations: None, edits: None, }; diff --git a/rust/src/endpoint_handlers.rs b/rust/src/endpoint_handlers.rs index 4567e810..d9bd3403 100644 --- a/rust/src/endpoint_handlers.rs +++ b/rust/src/endpoint_handlers.rs @@ -26,9 +26,11 @@ macro_rules! entity_batch_handler { autoaccept: bool, editor_id: FatcatId, editgroup_id: Option, + description: Option, + extra: Option, ) -> Result> { - let edit_context = make_edit_context(conn, editor_id, editgroup_id, autoaccept)?; + let edit_context = make_edit_context(conn, editor_id, editgroup_id, autoaccept, description, extra)?; edit_context.check(&conn)?; let model_list: Vec<&models::$model> = entity_list.iter().map(|e| e).collect(); let edits = $model::db_create_batch(conn, &edit_context, model_list.as_slice())?; @@ -264,86 +266,94 @@ impl Server { expand_flags: ExpandFlags, hide_flags: HideFlags, ) -> Result { - let (ident, rev): (ReleaseIdentRow, ReleaseRevRow) = - match (doi, wikidata_qid, isbn13, pmid, pmcid, core_id, arxiv_id, jstor_id) { - (Some(doi), None, None, None, None, None, None, None) => { - check_doi(doi)?; - release_ident::table - .inner_join(release_rev::table) - .filter(release_rev::doi.eq(doi)) - .filter(release_ident::is_live.eq(true)) - .filter(release_ident::redirect_id.is_null()) - .first(conn)? - } - (None, Some(wikidata_qid), None, None, None, None, None, None) => { - check_wikidata_qid(wikidata_qid)?; - release_ident::table - .inner_join(release_rev::table) - .filter(release_rev::wikidata_qid.eq(wikidata_qid)) - .filter(release_ident::is_live.eq(true)) - .filter(release_ident::redirect_id.is_null()) - .first(conn)? - } - (None, None, Some(isbn13), None, None, None, None, None) => { - // TODO: check_isbn13(isbn13)?; - release_ident::table - .inner_join(release_rev::table) - .filter(release_rev::isbn13.eq(isbn13)) - .filter(release_ident::is_live.eq(true)) - .filter(release_ident::redirect_id.is_null()) - .first(conn)? - } - (None, None, None, Some(pmid), None, None, None, None) => { - check_pmid(pmid)?; - release_ident::table - .inner_join(release_rev::table) - .filter(release_rev::pmid.eq(pmid)) - .filter(release_ident::is_live.eq(true)) - .filter(release_ident::redirect_id.is_null()) - .first(conn)? - } - (None, None, None, None, Some(pmcid), None, None, None) => { - check_pmcid(pmcid)?; - release_ident::table - .inner_join(release_rev::table) - .filter(release_rev::pmcid.eq(pmcid)) - .filter(release_ident::is_live.eq(true)) - .filter(release_ident::redirect_id.is_null()) - .first(conn)? - } - (None, None, None, None, None, Some(core_id), None, None) => { - // TODO: check_core_id(core_id)?; - release_ident::table - .inner_join(release_rev::table) - .filter(release_rev::core_id.eq(core_id)) - .filter(release_ident::is_live.eq(true)) - .filter(release_ident::redirect_id.is_null()) - .first(conn)? - } - (None, None, None, None, None, None, Some(arxiv_id), None) => { - // TODO: check_arxiv_id(arxiv_id)?; - release_ident::table - .inner_join(release_rev::table) - .filter(release_rev::arxiv_id.eq(arxiv_id)) - .filter(release_ident::is_live.eq(true)) - .filter(release_ident::redirect_id.is_null()) - .first(conn)? - } - (None, None, None, None, None, None, None, Some(jstor_id)) => { - // TODO: check_jstor_id(jstor_id)?; - release_ident::table - .inner_join(release_rev::table) - .filter(release_rev::jstor_id.eq(jstor_id)) - .filter(release_ident::is_live.eq(true)) - .filter(release_ident::redirect_id.is_null()) - .first(conn)? - } - _ => { - return Err( - FatcatError::MissingOrMultipleExternalId("in lookup".to_string()).into(), - ); - } - }; + let (ident, rev): (ReleaseIdentRow, ReleaseRevRow) = match ( + doi, + wikidata_qid, + isbn13, + pmid, + pmcid, + core_id, + arxiv_id, + jstor_id, + ) { + (Some(doi), None, None, None, None, None, None, None) => { + check_doi(doi)?; + release_ident::table + .inner_join(release_rev::table) + .filter(release_rev::doi.eq(doi)) + .filter(release_ident::is_live.eq(true)) + .filter(release_ident::redirect_id.is_null()) + .first(conn)? + } + (None, Some(wikidata_qid), None, None, None, None, None, None) => { + check_wikidata_qid(wikidata_qid)?; + release_ident::table + .inner_join(release_rev::table) + .filter(release_rev::wikidata_qid.eq(wikidata_qid)) + .filter(release_ident::is_live.eq(true)) + .filter(release_ident::redirect_id.is_null()) + .first(conn)? + } + (None, None, Some(isbn13), None, None, None, None, None) => { + // TODO: check_isbn13(isbn13)?; + release_ident::table + .inner_join(release_rev::table) + .filter(release_rev::isbn13.eq(isbn13)) + .filter(release_ident::is_live.eq(true)) + .filter(release_ident::redirect_id.is_null()) + .first(conn)? + } + (None, None, None, Some(pmid), None, None, None, None) => { + check_pmid(pmid)?; + release_ident::table + .inner_join(release_rev::table) + .filter(release_rev::pmid.eq(pmid)) + .filter(release_ident::is_live.eq(true)) + .filter(release_ident::redirect_id.is_null()) + .first(conn)? + } + (None, None, None, None, Some(pmcid), None, None, None) => { + check_pmcid(pmcid)?; + release_ident::table + .inner_join(release_rev::table) + .filter(release_rev::pmcid.eq(pmcid)) + .filter(release_ident::is_live.eq(true)) + .filter(release_ident::redirect_id.is_null()) + .first(conn)? + } + (None, None, None, None, None, Some(core_id), None, None) => { + // TODO: check_core_id(core_id)?; + release_ident::table + .inner_join(release_rev::table) + .filter(release_rev::core_id.eq(core_id)) + .filter(release_ident::is_live.eq(true)) + .filter(release_ident::redirect_id.is_null()) + .first(conn)? + } + (None, None, None, None, None, None, Some(arxiv_id), None) => { + // TODO: check_arxiv_id(arxiv_id)?; + release_ident::table + .inner_join(release_rev::table) + .filter(release_rev::arxiv_id.eq(arxiv_id)) + .filter(release_ident::is_live.eq(true)) + .filter(release_ident::redirect_id.is_null()) + .first(conn)? + } + (None, None, None, None, None, None, None, Some(jstor_id)) => { + // TODO: check_jstor_id(jstor_id)?; + release_ident::table + .inner_join(release_rev::table) + .filter(release_rev::jstor_id.eq(jstor_id)) + .filter(release_ident::is_live.eq(true)) + .filter(release_ident::redirect_id.is_null()) + .first(conn)? + } + _ => { + return Err( + FatcatError::MissingOrMultipleExternalId("in lookup".to_string()).into(), + ); + } + }; let mut entity = ReleaseEntity::db_from_row(conn, rev, Some(ident), hide_flags)?; entity.db_expand(&conn, expand_flags)?; diff --git a/rust/src/endpoints.rs b/rust/src/endpoints.rs index 8c45ea75..2e467957 100644 --- a/rust/src/endpoints.rs +++ b/rust/src/endpoints.rs @@ -120,7 +120,7 @@ macro_rules! wrap_entity_handlers { let auth_context = self.auth_confectionary.require_auth(&conn, &context.auth_data, Some(stringify!($post_fn)))?; auth_context.require_role(FatcatRole::Editor)?; auth_context.require_editgroup(&conn, editgroup_id)?; - let edit_context = make_edit_context(&conn, auth_context.editor_id, Some(editgroup_id), false)?; + let edit_context = make_edit_context(&conn, auth_context.editor_id, Some(editgroup_id), false, None, None)?; edit_context.check(&conn)?; entity.db_create(&conn, &edit_context)?.into_model() }).map_err(|e| FatcatError::from(e)) { @@ -138,6 +138,8 @@ macro_rules! wrap_entity_handlers { entity_list: &Vec, autoaccept: Option, editgroup_id: Option, + description: Option, + extra: Option, context: &Context, ) -> Box + Send> { let conn = self.db_pool.get().expect("db_pool error"); @@ -155,7 +157,11 @@ macro_rules! wrap_entity_handlers { auth_context.require_editgroup(&conn, eg_id)?; Some(eg_id) } else { None }; - self.$post_batch_handler(&conn, entity_list, autoaccept, auth_context.editor_id, editgroup_id) + let extra: Option = match extra { + Some(v) => serde_json::from_str(&v)?, + None => None, + }; + self.$post_batch_handler(&conn, entity_list, autoaccept, auth_context.editor_id, editgroup_id, description, extra) }).map_err(|e| FatcatError::from(e)) { Ok(edits) => { self.metrics.count("entities.created", edits.len() as i64).ok(); @@ -184,7 +190,7 @@ macro_rules! wrap_entity_handlers { auth_context.require_role(FatcatRole::Editor)?; let entity_id = FatcatId::from_str(&ident)?; auth_context.require_editgroup(&conn, editgroup_id)?; - let edit_context = make_edit_context(&conn, auth_context.editor_id, Some(editgroup_id), false)?; + let edit_context = make_edit_context(&conn, auth_context.editor_id, Some(editgroup_id), false, None, None)?; edit_context.check(&conn)?; entity.db_update(&conn, &edit_context, entity_id)?.into_model() }).map_err(|e| FatcatError::from(e)) { @@ -210,7 +216,7 @@ macro_rules! wrap_entity_handlers { auth_context.require_role(FatcatRole::Editor)?; let entity_id = FatcatId::from_str(&ident)?; auth_context.require_editgroup(&conn, editgroup_id)?; - let edit_context = make_edit_context(&conn, auth_context.editor_id, Some(editgroup_id), false)?; + let edit_context = make_edit_context(&conn, auth_context.editor_id, Some(editgroup_id), false, None, None)?; edit_context.check(&conn)?; $model::db_delete(&conn, &edit_context, entity_id)?.into_model() }).map_err(|e| FatcatError::from(e)) { -- cgit v1.2.3