def list_publications(project, collection_id, order_by="id"): """ List all publications in a given collection """ project_id = get_project_id_from_name(project) connection = db_engine.connect() collections = get_table("publication_collection") publications = get_table("publication") statement = select([collections]).where( collections.c.id == int_or_none(collection_id)).order_by(str(order_by)) rows = connection.execute(statement).fetchall() if len(rows) != 1: return jsonify({"msg": "Could not find collection in database."}), 404 elif rows[0]["project_id"] != int_or_none(project_id): return jsonify({ "msg": "Found collection not part of project {!r} with ID {}.".format( project, project_id) }), 400 statement = select([publications ]).where(publications.c.publication_collection_id == int_or_none(collection_id)).order_by( str(order_by)) rows = connection.execute(statement).fetchall() result = [] for row in rows: result.append(dict(row)) connection.close() return jsonify(result)
def get_publication_facsimiles(project, publication_id): """ List all fascimilies for the given publication """ connection = db_engine.connect() publication_facsimiles = get_table("publication_facsimile") facsimile_collections = get_table("publication_facsimile_collection") # join in facsimile_collections to we can get the collection title as well tables = join( publication_facsimiles, facsimile_collections, publication_facsimiles.c.publication_facsimile_collection_id == facsimile_collections.c.id) statement = select([publication_facsimiles, facsimile_collections.c.title])\ .where(publication_facsimiles.c.publication_id == int_or_none(publication_id))\ .where(publication_facsimiles.c.deleted != 1)\ .select_from(tables) rows = connection.execute(statement).fetchall() result = [] for row in rows: result.append(dict(row)) connection.close() return jsonify(result)
def edit_facsimile(project): """ Edit a facsimile object in the database POST data MUST be in JSON format. """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 facsimile_id = request_data.get("id", None) facsimile = get_table("publication_facsimile") connection = db_engine.connect() facsimile_query = select( [facsimile.c.id]).where(facsimile.c.id == int_or_none(facsimile_id)) facsimile_row = connection.execute(facsimile_query).fetchone() if facsimile_row is None: return jsonify({ "msg": "No facsimile with an ID of {} exists.".format(facsimile_id) }), 404 # facsimile_collection_id = request_data.get("facsimile_collection_id", None) page = request_data.get("page", None) priority = request_data.get("priority", None) type = request_data.get("type", None) values = {} if page is not None: values["page_nr"] = page if type is not None: values["type"] = type if priority is not None: values["priority"] = priority values["date_modified"] = datetime.now() if len(values) > 0: try: update = facsimile.update().where( facsimile.c.id == int(facsimile_id)).values(**values) connection.execute(update) return jsonify({ "msg": "Updated facsimile {} with values {}".format( int(facsimile_id), str(values)), "facsimile_id": int(facsimile_id) }) except Exception as e: result = {"msg": "Failed to update facsimile.", "reason": str(e)} return jsonify(result), 500 finally: connection.close() else: connection.close() return jsonify("No valid update values given."), 400
def connect_event(event_id): """ Link an event to a location, subject, or tag through event_connection POST data MUST be in JSON format. POST data MUST contain at least one of the following: subject_id: ID for the subject involved in the given event location_id: ID for the location involved in the given event tag_id: ID for the tag involved in the given event """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 events = get_table("event") connection = db_engine.connect() select_event = select([events]).where(events.c.id == int_or_none(event_id)) event_exists = connection.execute(select_event).fetchall() if len(event_exists) != 1: return jsonify({"msg": "Event ID not found in database"}), 404 event_connections = get_table("event_connection") insert = event_connections.insert() new_event_connection = { "event_id": int(event_id), "subject_id": int(request_data["subject_id"]) if request_data.get( "subject_id", None) else None, "location_id": int(request_data["location_id"]) if request_data.get( "location_id", None) else None, "tag_id": int(request_data["tag_id"]) if request_data.get("tag_id", None) else None } try: result = connection.execute(insert, **new_event_connection) new_row = select([ event_connections ]).where(event_connections.c.id == result.inserted_primary_key[0]) new_row = dict(connection.execute(new_row).fetchone()) result = { "msg": "Created new event_connection with ID {}".format( result.inserted_primary_key[0]), "row": new_row } return jsonify(result), 201 except Exception as e: result = { "msg": "Failed to create new event_connection", "reason": str(e) } return jsonify(result), 500 finally: connection.close()
def get_publication_group(project, group_id): """ Get all data for a single publication group """ connection = db_engine.connect() groups = get_table("publication_group") statement = select([groups]).where(groups.c.id == int_or_none(group_id)) rows = connection.execute(statement).fetchall() result = dict(rows[0]) connection.close() return jsonify(result)
def get_publication(project, publication_id): """ Get a publication object from the database """ connection = db_engine.connect() publications = get_table("publication") statement = select( [publications]).where(publications.c.id == int_or_none(publication_id)) rows = connection.execute(statement).fetchall() result = dict(rows[0]) connection.close() return jsonify(result)
def add_publication_to_group(project, publication_id): """ Add a publication to a publication_group POST data MUST be in JSON format POST data MUST contain the following: group_id: numerical ID for the publication_group """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 if "group_id" not in request_data: return jsonify({"msg": "group_id not in POST data."}), 400 group_id = int_or_none(request_data["group_id"]) connection = db_engine.connect() publications = get_table("publication") statement = publications.update().where( publications.c.id == int_or_none(publication_id)).values( publication_group_id=group_id) transaction = connection.begin() try: connection.execute(statement) statement = select([ publications ]).where(publications.c.id == int_or_none(publication_id)) updated = dict(connection.execute(statement).fetchone()) transaction.commit() result = {"msg": "Updated publication object", "row": updated} return jsonify(result) except Exception as e: transaction.rollback() result = {"msg": "Failed to create new object", "reason": str(e)} return jsonify(result), 500 finally: connection.close()
def get_publication_versions(project, publication_id): """ List all versions of the given publication """ connection = db_engine.connect() publication_versions = get_table("publication_version") statement = select([publication_versions]).where( publication_versions.c.publication_id == int_or_none(publication_id)) rows = connection.execute(statement).fetchall() result = [] for row in rows: result.append(dict(row)) connection.close() return jsonify(result)
def get_publications_in_group(project, group_id): """ List all publications in a given publication_group """ connection = db_engine.connect() publications = get_table("publication") statement = select([ publications.c.id, publications.c.name ]).where(publications.c.publication_group_id == int_or_none(group_id)) result = [] for row in connection.execute(statement).fetchall(): result.append(dict(row)) connection.close() return jsonify(result)
def edit_version(project, version_id): """ Takes "title", "filename", "published", "sort_order", "type" as JSON data "type" denotes version type, 1=base text, 2=other variant Returns "msg" and "version_id" on success, otherwise 40x """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 title = request_data.get("title", None) filename = request_data.get("filename", None) published = request_data.get("published", None) sort_order = request_data.get("sort_order", None) version_type = request_data.get("type", None) versions = get_table("publication_version") query = select([versions]).where(versions.c.id == int_or_none(version_id)) connection = db_engine.connect() result = connection.execute(query).fetchone() if result is None: connection.close() return jsonify("No such version exists."), 404 values = {} if title is not None: values["name"] = title if filename is not None: values["original_filename"] = filename if published is not None: values["published"] = published if sort_order is not None: values["sort_order"] = sort_order if version_type is not None: values["type"] = version_type values["date_modified"] = datetime.now() if len(values) > 0: update = versions.update().where(versions.c.id == int(version_id)).values(**values) connection.execute(update) connection.close() return jsonify({ "msg": "Updated version {} with values {}".format(int(version_id), str(values)), "manuscript_id": int(version_id) }) else: connection.close() return jsonify("No valid update values given."), 400
def list_facsimile_collection_links(project, collection_id): """ List all publication_facsimile objects in the given publication_facsimile_collection """ connection = db_engine.connect() facsimiles = get_table("publication_facsimile") statement = select( [facsimiles]).where(facsimiles.c.publication_facsimile_collection_id == int_or_none(collection_id)) rows = connection.execute(statement).fetchall() result = [] for row in rows: result.append(dict(row)) connection.close() return jsonify(result)
def get_event_occurrences(event_id): """ Get a list of all event_occurrence in the database, optionally limiting to a given event """ event_occurrences = get_table("event_occurrence") connection = db_engine.connect() statement = select([ event_occurrences ]).where(event_occurrences.c.event_id == int_or_none(event_id)) rows = connection.execute(statement).fetchall() result = [] for row in rows: result.append(dict(row)) connection.close() return jsonify(result)
def get_event_connections(event_id): """ List all event_connections for a given event, to find related locations, subjects, and tags """ event_connections = get_table("event_connection") connection = db_engine.connect() statement = select([ event_connections ]).where(event_connections.c.event_id == int_or_none(event_id)) rows = connection.execute(statement).fetchall() result = [] for row in rows: result.append(dict(row)) connection.close() return jsonify(result)
def get_publication_version(project, publication_id): """ Get a publication object from the database """ connection = db_engine.connect() publication_v = get_table("publication_version") statement = select([ publication_v ]).where(publication_v.c.publication_id == int_or_none(publication_id)) rows = connection.execute(statement).fetchall() result = [] for row in rows: result.append(dict(row)) connection.close() return jsonify(result)
def get_publication_collection_info(project, collection_id): """ Returns published status for publication_collection and associated introduction and title objects Also returns the original_filename for the introduction and title objects """ collections = get_table("publication_collection") intros = get_table("publication_collection_introduction") titles = get_table("publication_collection_title") query = select([collections]).where(collections.c.id == int_or_none(collection_id)) connection = db_engine.connect() collection_result = connection.execute(query).fetchone() if collection_result is None: connection.close() return jsonify("No such publication collection exists"), 404 intro_id = int_or_none(collection_result["publication_collection_introduction_id"]) title_id = int_or_none(collection_result["publication_collection_title_id"]) intro_query = select([intros.c.published, intros.c.original_filename]).where(intros.c.id == intro_id) title_query = select([titles.c.published, titles.c.original_filename]).where(titles.c.id == title_id) intro_result = connection.execute(intro_query).fetchone() title_result = connection.execute(title_query).fetchone() connection.close() result = { "collection_id": int(collection_id), "collection_published": collection_result["published"], "intro_id": intro_id, "intro_published": None if intro_result is None else intro_result["published"], "intro_original_filename": None if intro_result is None else intro_result["original_filename"], "title_id": title_id, "title_published": None if title_result is None else title_result["published"], "title_original_filename": None if title_result is None else title_result["original_filename"] } return jsonify(result)
def edit_facsimile_collection(project, collection_id): """ Takes "title", "numberOfPages", "startPageNumber", "description" as JSON data Returns "msg" and "facs_coll_id" on success, otherwise 40x """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 title = request_data.get("title", None) page_count = request_data.get("numberOfPages", None) start_page = request_data.get("startPageNumber", None) description = request_data.get("description", None) external_url = request_data.get("external_url", None) collections = get_table("publication_facsimile_collection") query = select([collections]).where(collections.c.id == int_or_none(collection_id)) connection = db_engine.connect() result = connection.execute(query).fetchone() if result is None: connection.close() return jsonify("No such facsimile collection exists."), 404 values = {} if title is not None: values["title"] = title if page_count is not None: values["number_of_pages"] = page_count if start_page is not None: values["start_page_number"] = start_page if description is not None: values["description"] = description if external_url is not None: values["external_url"] = external_url values["date_modified"] = datetime.now() if len(values) > 0: update = collections.update().where(collections.c.id == int(collection_id)).values(**values) connection.execute(update) connection.close() return jsonify({ "msg": "Updated facsimile collection {} with values {}".format(int(collection_id), str(values)), "facs_coll_id": int(collection_id) }) else: connection.close() return jsonify("No valid update values given."), 400
def edit_publication(project, publication_id): """ Takes "title", "genre", "filename", "published" as JSON data Returns "msg" and "publication_id" on success, otherwise 40x """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 title = request_data.get("title", None) genre = request_data.get("genre", None) filename = request_data.get("filename", None) published = request_data.get("published", None) publications = get_table("publication") query = select([publications.c.id]).where(publications.c.id == int_or_none(publication_id)) connection = db_engine.connect() result = connection.execute(query) if len(result.fetchall()) != 1: connection.close() return jsonify("No such publication exists."), 404 values = {} if title is not None: values["name"] = title if genre is not None: values["genre"] = genre if filename is not None: values["original_filename"] = filename if published is not None: values["published"] = published values["date_modified"] = datetime.now() if len(values) > 0: update = publications.update().where(publications.c.id == int(publication_id)).values(**values) connection.execute(update) connection.close() return jsonify({ "msg": "Updated project {} with values {}".format(publication_id, str(values)), "publication_id": publication_id }) else: connection.close() return jsonify("No valid update values given."), 400
def add_version(project, publication_id): """ Takes "title", "filename", "published", "sort_order", "type" as JSON data "type" denotes version type, 1=base text, 2=other variant Returns "msg" and "version_id" on success, otherwise 40x """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 title = request_data.get("title", None) filename = request_data.get("filename", None) published = request_data.get("published", None) sort_order = request_data.get("sort_order", None) version_type = request_data.get("type", None) publications = get_table("publication") versions = get_table("publication_version") query = select([publications]).where(publications.c.id == int_or_none(publication_id)) connection = db_engine.connect() result = connection.execute(query).fetchone() if result is None: connection.close() return jsonify("No such publication exists."), 404 values = {"publication_id": int(publication_id)} if title is not None: values["name"] = title if filename is not None: values["original_filename"] = filename if published is not None: values["published"] = published if sort_order is not None: values["sort_order"] = sort_order if version_type is not None: values["type"] = version_type insert = versions.insert().values(**values) result = connection.execute(insert) return jsonify({ "msg": "Created new version object.", "version_id": int(result.inserted_primary_key[0]) }), 201
def get_publication_comments(project, publication_id): """ List all comments for the given publication """ connection = db_engine.connect() publications = get_table("publication") publication_comments = get_table("publication_comment") statement = select([ publications.c.publication_comment_id ]).where(publications.c.id == int_or_none(publication_id)) comment_ids = connection.execute(statement).fetchall() comment_ids = [int(row[0]) for row in comment_ids] statement = select([publication_comments ]).where(publication_comments.c.id.in_(comment_ids)) rows = connection.execute(statement).fetchall() result = [] for row in rows: result.append(dict(row)) connection.close() return jsonify(result)
def list_publication_collections(project): """ List all publication_collection objects for a given project """ project_id = get_project_id_from_name(project) connection = db_engine.connect() # collections = get_table("publication_collection") statement = """ SELECT pc.id, pc.name as title, pc.published, pc.date_created, pc.date_modified, pc.date_published_externally, pc.legacy_id, pc.project_id, pc.publication_collection_title_id, pc.publication_collection_introduction_id, pc.name, pct.original_filename AS collection_title_filename, pci.original_filename AS collection_intro_filename, pct.published AS collection_title_published, pci.published AS collection_intro_published FROM publication_collection pc LEFT JOIN publication_collection_title pct ON pct.id = pc.publication_collection_title_id LEFT JOIN publication_collection_introduction pci ON pci.id = pc.publication_collection_introduction_id WHERE pc.project_id=:project_id AND pc.published>=1 ORDER BY pc.id """ statement = text(statement).bindparams(project_id=int_or_none(project_id)) # statement = select([collections]).where(collections.c.project_id == int_or_none(project_id)) rows = connection.execute(statement).fetchall() result = [] for row in rows: result.append(dict(row)) connection.close() return jsonify(result)
def edit_subject(project, subject_id): """ Edit a subject object in the database POST data MUST be in JSON format POST data CAN contain: type: subject type description: subject description first_name: Subject first or given name last_name: Subject surname preposition: preposition for subject full_name: Subject full name legacy_id: Legacy id for subject date_born: Subject date of birth date_deceased: Subject date of death """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 subjects = get_table("subject") connection = db_engine.connect() subject_query = select([subjects.c.id ]).where(subjects.c.id == int_or_none(subject_id)) subject_row = connection.execute(subject_query).fetchone() if subject_row is None: return jsonify( {"msg": "No subject with an ID of {} exists.".format(subject_id)}), 404 subject_type = request_data.get("type", None) description = request_data.get("description", None) first_name = request_data.get("first_name", None) last_name = request_data.get("last_name", None) preposition = request_data.get("preposition", None) full_name = request_data.get("full_name", None) legacy_id = request_data.get("legacy_id", None) date_born = request_data.get("date_born", None) date_deceased = request_data.get("date_deceased", None) values = {} if subject_type is not None: values["type"] = subject_type if description is not None: values["description"] = description if first_name is not None: values["first_name"] = first_name if last_name is not None: values["last_name"] = last_name if preposition is not None: values["preposition"] = preposition if full_name is not None: values["full_name"] = full_name if legacy_id is not None: values["legacy_id"] = legacy_id if date_born is not None: values["date_born"] = date_born if date_deceased is not None: values["date_deceased"] = date_deceased values["date_modified"] = datetime.now() if len(values) > 0: try: update = subjects.update().where( subjects.c.id == int(subject_id)).values(**values) connection.execute(update) return jsonify({ "msg": "Updated subject {} with values {}".format( int(subject_id), str(values)), "subject_id": int(subject_id) }) except Exception as e: result = {"msg": "Failed to update subject.", "reason": str(e)} return jsonify(result), 500 finally: connection.close() else: connection.close() return jsonify("No valid update values given."), 400
def link_file_to_publication(project, publication_id): """ Link an XML file to a publication, creating the appropriate publication_comment, publication_manuscript, or publication_version object. POST data MUST be in JSON format POST data MUST contain the following: file_type: one of [comment, manuscript, version] indicating which type of file the given file_path points to file_path: path to the file to be linked POST data SHOULD also contain the following: datePublishedExternally: date of external publication published: 0 or 1, is this file published and ready for viewing publishedBy: person responsible for publishing POST data MAY also contain: legacyId: legacy ID for this publication file object type: Type of file link, for Manuscripts and Versions sectionId: Publication section or chapter number, for Manuscripts and Versions """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 if "file_path" not in request_data or "file_type" not in request_data or request_data.get( "file_type", None) not in ["comment", "manuscript", "version"]: return jsonify( {"msg": "POST data JSON doesn't contain required data."}), 400 file_type = request_data["file_type"] connection = db_engine.connect() if file_type == "comment": comments = get_table("publication_comment") publications = get_table("publication") transaction = connection.begin() new_comment = { "original_file_name": request_data.get("file_path"), "date_published_externally": request_data.get("datePublishedExternally", None), "published": request_data.get("published", None), "published_by": request_data.get("publishedBy", None), "legacy_id": request_data.get("legacyId", None) } ins = comments.insert() try: result = connection.execute(ins, **new_comment) new_row = select([ comments ]).where(comments.c.id == result.inserted_primary_key[0]) new_row = dict(connection.execute(new_row).fetchone()) # update publication object in database with new publication_comment ID update_stmt = publications.update().where(publications.c.id == int_or_none(publication_id)). \ values(publications.c.publication_comment_id == result.inserted_primary_key[0]) connection.execute(update_stmt) transaction.commit() result = { "msg": "Created new publication_comment with ID {}".format( result.inserted_primary_key[0]), "row": new_row } return jsonify(result), 201 except Exception as e: transaction.rollback() result = { "msg": "Failed to create new publication_comment object", "reason": str(e) } return jsonify(result), 500 finally: connection.close() else: new_object = { "original_file_name": request_data.get("file_path"), "publication_id": int(publication_id), "date_published_externally": request_data.get("datePublishedExternally", None), "published": request_data.get("published", None), "published_by": request_data.get("publishedBy", None), "legacy_id": request_data.get("legacyId", None), "type": request_data.get("type", None), "section_id": request_data.get("sectionId", None) } if file_type == "manuscript": table = get_table("publication_manuscript") else: table = get_table("publication_version") ins = table.insert() try: result = connection.execute(ins, **new_object) new_row = select( [table]).where(table.c.id == result.inserted_primary_key[0]) new_row = dict(connection.execute(new_row).fetchone()) result = { "msg": "Created new publication{} with ID {}".format( file_type.capitalize(), result.inserted_primary_key[0]), "row": new_row } return jsonify(result), 201 except Exception as e: result = {"msg": "Failed to create new object", "reason": str(e)} return jsonify(result), 500 finally: connection.close()
def edit_facsimile_collection(project, facsimile_collection_id): """ Edit a facsimile_collection object in the database POST data MUST be in JSON format. """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 facsimile_collections = get_table("publication_facsimile_collection") connection = db_engine.connect() facsimile_collections_query = select([facsimile_collections.c.id]).where( facsimile_collections.c.id == int_or_none(facsimile_collection_id)) facsimile_collections_row = connection.execute( facsimile_collections_query).fetchone() if facsimile_collections_row is None: return jsonify({ "msg": "No facsimile collection with an ID of {} exists.".format( facsimile_collection_id) }), 404 title = request_data.get("title", None) number_of_pages = request_data.get("number_of_pages", 0) start_page_number = request_data.get("start_page_number", 0) description = request_data.get("description", None) folder_path = request_data.get("folder_path", None) page_comment = request_data.get("page_comment", None) external_url = request_data.get("external_url", None) values = {} if title is not None: values["title"] = title if number_of_pages is not None: values["number_of_pages"] = number_of_pages if start_page_number is not None: values["start_page_number"] = start_page_number if description is not None: values["description"] = description if folder_path is not None: values["folder_path"] = folder_path if page_comment is not None: values["page_comment"] = page_comment if external_url is not None: values["external_url"] = external_url values["date_modified"] = datetime.now() if len(values) > 0: try: update = facsimile_collections.update().where( facsimile_collections.c.id == int( facsimile_collection_id)).values(**values) connection.execute(update) return jsonify({ "msg": "Updated facsimile_collection {} with values {}".format( int(facsimile_collection_id), str(values)), "facsimile_collection_id": int(facsimile_collection_id) }) except Exception as e: result = { "msg": "Failed to update facsimile_collections.", "reason": str(e) } return jsonify(result), 500 finally: connection.close() else: connection.close() return jsonify("No valid update values given."), 400
def new_publication(project, collection_id): """ Create a new publication object as part of the given publication_collection POST data MUST be in JSON format. POST data SHOULD contain the following: name: publication name POST data MAY also contain the following: publicationComment_id: ID for related publicationComment object datePublishedExternally: date of external publication for publication published: publish status for publication legacyId: legacy ID for publication publishedBy: person responsible for publishing the publication originalFilename: filepath to publication XML file genre: Genre for this publication publicationGroup_id: ID for related publicationGroup, used to group publications for easy publishing of large numbers of publications originalPublicationDate: Date of original publication for physical equivalent """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 project_id = get_project_id_from_name(project) connection = db_engine.connect() collections = get_table("publication_collection") publications = get_table("publication") statement = select([ collections.c.project_id ]).where(collections.c.id == int_or_none(collection_id)) result = connection.execute(statement).fetchall() if len(result) != 1: return jsonify({"msg": "publication_collection not found."}), 404 if result[0]["project_id"] != project_id: return jsonify({ "msg": "publication_collection {} does not belong to project {!r}".format( collection_id, project) }), 400 insert = publications.insert() publication = { "name": request_data.get("name", None), "publication_comment_id": request_data.get("publicationComment_id", None), "date_published_externally": request_data.get("datePublishedExternally", None), "published": request_data.get("published", None), "legacy_id": request_data.get("legacyId", None), "published_by": request_data.get("publishedBy", None), "original_filename": request_data.get("originalFileName", None), "genre": request_data.get("genre", None), "publication_group_id": request_data.get("publicationGroup_id", None), "original_publication_date": request_data.get("originalPublicationDate", None), "publication_collection_id": int_or_none(collection_id) } try: result = connection.execute(insert, **publication) new_row = select([ publications ]).where(publications.c.id == result.inserted_primary_key[0]) new_row = dict(connection.execute(new_row).fetchone()) result = { "msg": "Created new publication with ID {}".format( result.inserted_primary_key[0]), "row": new_row } return jsonify(result), 201 except Exception as e: result = {"msg": "Failed to create new publication", "reason": str(e)} return jsonify(result), 500 finally: connection.close()
def new_event_occurrence(event_id): """ Add a new event_occurrence to the database POST data MUST be in JSON format. POST data SHOULD contain the following: type: event occurrence type description: event occurrence description POST data SHOULD also contain at least one of the following: publication_id: ID for publication the event occurs in publicationVersion_id: ID for publication version the event occurs in publicationManuscript_id: ID for publication manuscript the event occurs in publicationFacsimile_id: ID for publication facsimile the event occurs in publicationComment_id: ID for publication comment the event occurs in publicationFacsimile_page: Number for publication facsimile page the event occurs in """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 events = get_table("event") connection = db_engine.connect() select_event = select([events]).where(events.c.id == int_or_none(event_id)) event_exists = connection.execute(select_event).fetchall() if len(event_exists) != 1: return jsonify({"msg": "Event ID not found in database"}), 404 event_occurrences = get_table("event_occurrence") insert = event_occurrences.insert() new_occurrence = { "event_id": int(event_id), "type": request_data.get("type", None), "description": request_data.get("description", None), "publication_id": int(request_data["publication_id"]) if request_data.get( "publication_id", None) else None, "publication_version_id": int(request_data["publicationVersion_id"]) if request_data.get( "publicationVersion_id", None) else None, "publication_manuscript_id": int(request_data["publicationManuscript_id"]) if request_data.get( "publicationManuscript_id", None) else None, "publication_facsimile_id": int(request_data["publicationFacsimile_id"]) if request_data.get( "publicationFacsimile_id", None) else None, "publication_comment_id": int(request_data["publicationComment_id"]) if request_data.get( "publicationComment_id", None) else None, "publication_facsimile_page": int(request_data["publicationFacsimile_page"]) if request_data.get( "publicationFacsimile_page", None) else None, } try: result = connection.execute(insert, **new_occurrence) new_row = select([ event_occurrences ]).where(event_occurrences.c.id == result.inserted_primary_key[0]) new_row = dict(connection.execute(new_row).fetchone()) result = { "msg": "Created new event_occurrence with ID {}".format( result.inserted_primary_key[0]), "row": new_row } return jsonify(result), 201 except Exception as e: result = { "msg": "Failed to create new event_occurrence", "reason": str(e) } return jsonify(result), 500 finally: connection.close()
def new_publication_event_occurrence(publication_id): """ Add a new event_occurrence to the publication POST data MUST be in JSON format. POST data MUST contain the following: publication_id: ID for publication the event occurs in tag_id: ID for publication the event occurs in POST data MAY contain the following: publicationFacsimile_page: Number for publication facsimile page the event occurs in """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 event_occ = get_table("event_occurrence") connection = db_engine.connect() select_event = select([ event_occ.c.event_id ]).where(event_occ.c.publication_id == int_or_none(publication_id)).where( event_occ.c.deleted != 1) result = connection.execute(select_event).fetchone() if int_or_none(result["event_id"]) is None: event_id = int_or_none(result) else: event_id = int_or_none(result["event_id"]) # No existing connection between publication and event, we need to create an event if event_id is None: # create event events = get_table("event") new_event = { "type": "publication", "description": "publication->tag", } try: insert = events.insert() result = connection.execute(insert, **new_event) event_id = result.inserted_primary_key[0] except Exception as e: result = {"msg": "Failed to create new event", "reason": str(e)} return jsonify(result), 500 # Create the occurrence, connection between publication and event insert = event_occ.insert() new_occurrence = { "event_id": int(event_id), "type": request_data.get("type", None), "description": request_data.get("description", None), "publication_id": int(request_data["publication_id"]) if request_data.get( "publication_id", None) else None, "publication_facsimile_page": int(request_data["publication_facsimile_page"]) if request_data.get("publication_facsimile_page", None) else None, } try: result = connection.execute(insert, **new_occurrence) new_row = select([ event_occ ]).where(event_occ.c.id == result.inserted_primary_key[0]) new_row = dict(connection.execute(new_row).fetchone()) except Exception as e: result = { "msg": "Failed to create new event_occurrence", "reason": str(e) } return jsonify(result), 500 # Create the connection between tag and event event_conn = get_table("event_connection") insert = event_conn.insert() new_connection = { "event_id": int(event_id), "tag_id": request_data.get("tag_id", None) } try: result = connection.execute(insert, **new_connection) new_row = select([ event_conn ]).where(event_conn.c.id == result.inserted_primary_key[0]) new_row = dict(connection.execute(new_row).fetchone()) except Exception as e: result = { "msg": "Failed to create new event_connection", "reason": str(e) } return jsonify(result), 500 finally: connection.close() else: try: new_connection = { "event_id": int(event_id), "tag_id": request_data.get("tag_id", None) } event_conn = get_table("event_connection") insert = event_conn.insert() result = connection.execute(insert, **new_connection) new_row = select([ event_conn ]).where(event_conn.c.id == result.inserted_primary_key[0]) new_row = dict(connection.execute(new_row).fetchone()) result = { "msg": "Created new event_connection with ID {}".format( result.inserted_primary_key[0]), "row": new_row } return jsonify(result), 201 except Exception as e: result = { "msg": "Failed to create new event_connection", "reason": str(e) } return jsonify(result), 500 finally: connection.close()
def link_facsimile_collection_to_publication(project, collection_id): """ Link a publication_facsimile_collection to a publication through publication_facsimile table POST data MUST be in JSON format. POST data MUST contain the following: publication_id: ID for the publication to link to POST data MAY also contain the following: publicationManuscript_id: ID for the specific publication manuscript to link to publicationVersion_id: ID for the specific publication version to link to sectionId: Section or chapter number for this particular facsimile pageNr: Page number for link priority: Priority number for this link """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 if "publication_id" not in request_data: return jsonify({"msg": "No publication_id in POST data."}), 400 connection = db_engine.connect() publication_id = int_or_none(request_data["publication_id"]) project_id = get_project_id_from_name(project) publication_facsimiles = get_table("publication_facsimile") publication_collections = get_table("publication_collection") publications = get_table("publication") statement = select([publications.c.publication_collection_id ]).where(publications.c.id == publication_id) result = connection.execute(statement).fetchall() if len(result) != 1: return jsonify({ "msg": "Could not find publication collection for publication, unable to verify that publication belongs to {!r}" .format(project) }), 404 publication_collection_id = int_or_none( result[0]["publication_collection_id"]) statement = select([ publication_collections.c.project_id ]).where(publication_collections.c.id == publication_collection_id) result = connection.execute(statement).fetchall() if len(result) != 1: return jsonify({ "msg": "Could not find publication collection for publication, unable to verify that publication belongs to {!r}" .format(project) }), 404 if result[0]["project_id"] != project_id: return jsonify({ "msg": "Publication {} appears to not belong to project {!r}".format( publication_id, project) }), 400 insert = publication_facsimiles.insert() new_facsimile = { "publication_facsimile_collection_id": collection_id, "publication_id": publication_id, "publication_manuscript_id": request_data.get("publication_manuscript_id", None), "publication_version_id": request_data.get("publication_version_id", None), "page_nr": request_data.get("page", 0), "section_id": request_data.get("section_id", 0), "priority": request_data.get("priority", 0), "type": request_data.get("type", 0) } try: result = connection.execute(insert, **new_facsimile) new_row = select([ publication_facsimiles ]).where(publication_facsimiles.c.id == result.inserted_primary_key[0]) new_row = dict(connection.execute(new_row).fetchone()) result = { "msg": "Created new publication_facsimile with ID {}".format( result.inserted_primary_key[0]), "row": new_row } return jsonify(result), 201 except Exception as e: result = { "msg": "Failed to create new publication_facsimile", "reason": str(e) } return jsonify(result), 500 finally: connection.close()
def edit_tag(project, tag_id): """ Update tag object to the database POST data MUST be in JSON format. POST data SHOULD contain: type: tag type name: tag name POST data CAN also contain: description: tag description legacy_id: Legacy id for tag """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 tags = get_table("tag") connection = db_engine.connect() tag_query = select([tags.c.id]).where(tags.c.id == int_or_none(tag_id)) tag_row = connection.execute(tag_query).fetchone() if tag_row is None: return jsonify( {"msg": "No tag with an ID of {} exists.".format(tag_id)}), 404 type = request_data.get("type", None) name = request_data.get("name", None) description = request_data.get("description", None) legacy_id = request_data.get("legacy_id", None) values = {} if type is not None: values["type"] = type if name is not None: values["name"] = name if description is not None: values["description"] = description if legacy_id is not None: values["legacy_id"] = legacy_id values["date_modified"] = datetime.now() if len(values) > 0: try: update = tags.update().where(tags.c.id == int(tag_id)).values( **values) connection.execute(update) return jsonify({ "msg": "Updated tag {} with values {}".format(int(tag_id), str(values)), "tag_id": int(tag_id) }) except Exception as e: result = {"msg": "Failed to update tag.", "reason": str(e)} return jsonify(result), 500 finally: connection.close() else: connection.close() return jsonify("No valid update values given."), 400
def edit_location(project, location_id): """ Edit a location object in the database POST data MUST be in JSON format. POST data CAN contain: name: location name description: location description legacy_id: legacy id for location latitude: latitude coordinate for location longitude: longitude coordinate for location """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 locations = get_table("location") connection = db_engine.connect() location_query = select( [locations.c.id]).where(locations.c.id == int_or_none(location_id)) location_row = connection.execute(location_query).fetchone() if location_row is None: return jsonify({ "msg": "No location with an ID of {} exists.".format(location_id) }), 404 name = request_data.get("name", None) description = request_data.get("description", None) legacy_id = request_data.get("legacy_id", None) latitude = request_data.get("latitude", None) longitude = request_data.get("longitude", None) city = request_data.get("city", None) region = request_data.get("region", None) source = request_data.get("source", None) alias = request_data.get("alias", None) deleted = request_data.get("deleted", 0) country = request_data.get("country", None) values = {} if name is not None: values["name"] = name if description is not None: values["description"] = description if legacy_id is not None: values["legacy_id"] = legacy_id if latitude is not None: values["latitude"] = latitude if longitude is not None: values["longitude"] = longitude if city is not None: values["city"] = city if country is not None: values["country"] = country if region is not None: values["region"] = region if source is not None: values["source"] = source if alias is not None: values["alias"] = alias if deleted is not None: values["deleted"] = deleted values["date_modified"] = datetime.now() if len(values) > 0: try: update = locations.update().where( locations.c.id == int(location_id)).values(**values) connection.execute(update) return jsonify({ "msg": "Updated location {} with values {}".format( int(location_id), str(values)), "location_id": int(location_id) }) except Exception as e: result = {"msg": "Failed to update location.", "reason": str(e)} return jsonify(result), 500 finally: connection.close() else: connection.close() return jsonify("No valid update values given."), 400
def edit_work_manifestation(project, man_id): """ Update work_manifestation object to the database POST data MUST be in JSON format. POST data SHOULD contain: type: manifestation type title: manifestation title POST data CAN also contain: description: tag description legacy_id: Legacy id for tag """ request_data = request.get_json() if not request_data: return jsonify({"msg": "No data provided."}), 400 manifestations = get_table("work_manifestation") references = get_table("work_reference") connection = db_engine.connect() # get manifestation data query = select([manifestations.c.id ]).where(manifestations.c.id == int_or_none(man_id)) row = connection.execute(query).fetchone() if row is None: return jsonify({ "msg": "No manifestation with an ID of {} exists.".format(man_id) }), 404 # get reference data reference = request_data.get("reference", None) reference_id = request_data.get("reference_id", None) type = request_data.get("type", None) title = request_data.get("title", None) description = request_data.get("description", None) legacy_id = request_data.get("legacy_id", None) source = request_data.get("source", None) translated_by = request_data.get("translated_by", None) journal = request_data.get("journal", None) publication_location = request_data.get("publication_location", None) publisher = request_data.get("publisher", None) published_year = request_data.get("published_year", None) volume = request_data.get("volume", None) total_pages = request_data.get("total_pages", None) isbn = request_data.get("isbn", None) values = {} if type is not None: values["type"] = type if title is not None: values["title"] = title if description is not None: values["description"] = description if legacy_id is not None: values["legacy_id"] = legacy_id if source is not None: values["source"] = source if translated_by is not None: values["translated_by"] = translated_by if journal is not None: values["journal"] = journal if publication_location is not None: values["publication_location"] = publication_location if publisher is not None: values["publisher"] = publisher if published_year is not None: values["published_year"] = published_year if volume is not None: values["volume"] = volume if total_pages is not None: values["total_pages"] = total_pages if isbn is not None: values["isbn"] = isbn values["date_modified"] = datetime.now() reference_values = {} if reference is not None: reference_values["reference"] = reference if len(values) > 0: try: update = manifestations.update().where( manifestations.c.id == int(man_id)).values(**values) connection.execute(update) if len(reference_values) > 0: update_ref = references.update().where( references.c.id == int(reference_id)).values( **reference_values) connection.execute(update_ref) return jsonify({ "msg": "Updated manifestation {} with values {}".format( int(man_id), str(values)), "man_id": int(man_id) }) except Exception as e: result = { "msg": "Failed to update manifestation.", "reason": str(e) } return jsonify(result), 500 finally: connection.close() else: connection.close() return jsonify("No valid update values given."), 400