def create_document_section(doc_id: str): """ Append new document section using doc_id. Section is appended at the end of document with empty values. Return the new section number. Parameters ---------- doc_id the document id string Returns ------- ApiResult JSON Object with message of the updated document id and the new number of sections. TellSpaceAuthError Exception Class with authorization error message. Raised when the collaborator is banned or not approved. """ email = get_jwt_identity() collab: Collaborator = Collaborator.objects.get(email=email) if not collab.banned: doc: DocumentCase = DocumentCase.objects.get(id=doc_id, creatoriD=str(collab.id)) new_section = Section() new_section.secTitle = f'Section No. {len(doc.section) + 1}' new_section.content = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod...' doc.section.append(new_section) doc.save() return ApiResult(message=f'Created new section for {doc.id}. Total No. of sections {len(doc.section)}') raise TellSpaceAuthError(msg='Authorization Error. Collaborator is banned or has not been approved by the admin.')
def edit_document_title(doc_id: str): """ Edit the document title using doc_id and valid request body values. Parameters ---------- doc_id the document id string Returns ------- ApiResult JSON Object with message of the updated document with the new title. TellSpaceAuthError Exception Class with authorization error message. Raised when the collaborator is banned or not approved. """ if request.json == {}: raise TellSpaceApiError(msg='No request body data.', status=400) email = get_jwt_identity() body = TitleValidator().load(request.json) collab: Collaborator = Collaborator.objects.get(email=email) if not collab.banned and collab.approved: doc = DocumentCase.objects.get(id=doc_id, creatoriD=str(collab.id)) doc.title = body['title'] doc.save() return ApiResult(message=f'Updated document {doc.id} title to: {doc.title}') raise TellSpaceAuthError(msg='Authorization Error. Collaborator is banned or has not been approved by the admin.')
def remove_document(doc_id: str): """ Removes a document using the document id from the route parameters. Parameters ---------- doc_id the document id string Returns ------- ApiResult JSON Object containing the id of the removed document. TellSpaceAuthError Exception Class with authorization error message. Raised when the collaborator is banned or not approved. """ # Get user identity email = get_jwt_identity() # Extract collaborator with identity collab: Collaborator = Collaborator.objects.get(email=email) if not collab.banned and collab.approved: doc = DocumentCase.objects.get(id=doc_id, creatoriD=str(collab.id)) doc.delete() return ApiResult(id=str(doc.id)) raise TellSpaceAuthError(msg='Authorization Error. Collaborator is banned or has not been approved by the admin.')
def get_damages(): """ GET request to return all the available options of the filter category damages. Returns ------- Message: json All the options to filter of the damages category """ return ApiResult(message=get_damage_list())
def get_infrastructures(): """ GET request to return all the available options of the filter category infrastructure. Returns ------- Message: json All the options to filter of the infrastructure category """ return ApiResult(message=get_infrastructure_list())
def edit_document_timeline(doc_id: str): """ Edit the document timeline using doc_id and valid request body values. Verifies if for all given timeline pairs the start date is NOT larger than the end-date. Parameters ---------- doc_id the document id string Returns ------- ApiResult JSON Object with message of the updated document and its id. TellSpaceAuthError Exception Class with authorization error message. Raised when the collaborator is banned or not approved. """ email = get_jwt_identity() if request.json == {}: raise TellSpaceApiError(msg='No request body data.', status=400) body = TimelineValidator().load(request.json) # Check that event_start_date is NOT larger than event_end_date for time_pair in body['timeline']: if time_pair['event_start_date'] > time_pair['event_end_date']: raise TellSpaceApiError( msg='Invalid Timeline Pair. ' 'Start Date is larger than the end date. One of the dates is larger than today.', status=500 ) collab: Collaborator = Collaborator.objects.get(email=email) # If collaborator is NOT banned and its approved, then do the thing if not collab.banned and collab.approved: doc: DocumentCase = DocumentCase.objects.get(id=doc_id, creatoriD=str(collab.id)) new_timeline = [] new_time_pair = Timeline() for time_pair in body['timeline']: new_time_pair.eventStartDate = str(time_pair['event_start_date']) new_time_pair.eventEndDate = str(time_pair['event_end_date']) new_time_pair.event = time_pair['event'] new_timeline.append(new_time_pair) doc.timeline = new_timeline doc.save() return ApiResult(message=f'Updated document {doc.id} timeline.') raise TellSpaceAuthError(msg='Authorization Error. Collaborator is banned or has not been approved by the admin.')
def edit_document_section(doc_id, section_nbr): """ Edit the document section with valid request body values. Parameters ---------- doc_id the document id string section_nbr the document section number to be removed Returns ------- ApiResult JSON Object with message of the updated section and the document id. TellSpaceAuthError Exception Class with authorization error message. Raised when the collaborator is banned or not approved. """ if request.json == {}: raise TellSpaceApiError(msg='No request body data.', status=400) # No such thing as section 0 or negative if int(section_nbr) <= 0: raise TellSpaceApiError(msg='No section found.') email = get_jwt_identity() body: EditSectionValidator = EditSectionValidator().load(request.json) collab: Collaborator = Collaborator.objects.get(email=email) if not collab.banned: doc: DocumentCase = DocumentCase.objects.get(id=doc_id, creatoriD=str(collab.id)) # Create section to replace existent one section = Section() section.secTitle = body['section_title'] section.content = body['section_text'] # Remember that lists start with index 0 doc.section[int(section_nbr) - 1] = section doc.save() return ApiResult( message=f'Edited section {section_nbr} from the document {doc.id}.' ) raise TellSpaceAuthError(msg='Authorization Error. Collaborator is banned or has not been approved by the admin.')
def create_document(): """ Create a new document using the information from the request body. Returns ------- ApiResult JSON object with the id of the newly created document. TellSpaceAuthError Exception Class with authorization error message. Raised when the collaborator is banned or not approved. """ if not (request.method == 'POST'): raise TellSpaceApiError(msg='Method not allowed.', status=400) if request.json == {}: raise TellSpaceApiError(msg='No request body data.', status=400) body: CreateDocumentValidator = CreateDocumentValidator().load(request.json) email = get_jwt_identity() collab: Collaborator = Collaborator.objects.get(email=email) # TODO: Verify that the infrastrucutres and damage types exist in the database # TODO: Verify if tags exist in the database, if not... add them and create document. if not collab.banned and collab.approved: doc = DocumentCase() doc.title = body['title'] doc.creatoriD = str(collab.id) doc.location = [] doc.description = body['description'] doc.incidentDate = str(body['incident_date']) doc.creationDate = datetime.today().strftime('%Y-%m-%d') doc.lastModificationDate = datetime.today().strftime('%Y-%m-%d') doc.language = body['language'] doc.tagsDoc = [] doc.infrasDocList = body['infrastructure_type'] doc.damageDocList = [] doc.author = [] doc.actor = [] doc.section = [] doc.timeline = [] doc.published = True doc.save() return ApiResult(docId=str(doc.id)) raise TellSpaceAuthError(msg='Authorization Error. Collaborator is banned or has not been approved by the admin.')
def edit_document_authors(doc_id: str): """ Edit the document authors using doc_id and valid request body values. Parameters ---------- doc_id the document id string Returns ------- ApiResult JSON Object with message containing the id of the updated document. TellSpaceAuthError Exception Class with authorization error message. Raised when the collaborator is banned or not approved. """ # Get user identity email = get_jwt_identity() # Verify request parameters if request.json == {}: raise TellSpaceApiError(msg='No request body data.', status=400) body = AuthorsValidator().load(request.json) # Extract collaborator with identity collab: Collaborator = Collaborator.objects.get(email=email) if not collab.banned: doc: DocumentCase = DocumentCase.objects.get(id=doc_id, creatoriD=str(collab.id)) authors_list = [] for a in body['authors']: new_author = Author() new_author.author_FN = a['first_name'] new_author.author_LN = a['last_name'] new_author.author_email = a['email'] new_author.author_faculty = a['faculty'] authors_list.append(new_author) doc.author = authors_list doc.save() return ApiResult(id=str(doc.id)) raise TellSpaceAuthError(msg='Authorization Error. Collaborator is banned or has not been approved by the admin.')
def edit_document_tags(doc_id: str): """ Edit the document tags using doc_id and valid request body values. Parameters ---------- doc_id the document id string Returns ------- ApiResult JSON Object with message containing the id of the updated document. TellSpaceAuthError Exception Class with authorization error message. Raised when the collaborator is banned or not approved. """ # Get user identity email = get_jwt_identity() # Verify request parameters if request.json == {}: raise TellSpaceApiError(msg='No request body data.', status=400) body = TagsValidator().load(request.json) # Extract collaborator with identity collab: Collaborator = Collaborator.objects.get(email=email) if not collab.banned: doc: DocumentCase = DocumentCase.objects.get(id=doc_id, creatoriD=str(collab.id)) # If tags exists DO NOT exist in the tags collection, add it for tag in body['tags']: if not Tag.objects(tagItem=tag): newTag = Tag(tagItem=tag) newTag.save() doc.tagsDoc = body['tags'] doc.save() return ApiResult(message=f'Edited tags of the document {doc.id}.') raise TellSpaceAuthError(msg='Authorization Error. Collaborator is banned or has not been approved by the admin.')
def edit_document_infrastructure_types(doc_id: str): """ Edit the document infrastructure_types using doc_id and valid request body values. Parameters ---------- doc_id the document id string Returns ------- ApiResult JSON Object with message containing the id of the updated document. TellSpaceAuthError Exception Class with authorization error message. Raised when the collaborator is banned or not approved. """ if request.json == {}: raise TellSpaceApiError(msg='No request body data.', status=400) # Get user identity email = get_jwt_identity() # Verify request parameters body = InfrastructureTypesValidator().load(request.json) # Extract collaborator with identity collab: Collaborator = Collaborator.objects.get(email=email) # Double check if the given infrastrucutres are in the Infrastrucutre Collection for infra in body['infrastructure_types']: Infrastructure.objects.get(infrastructureType=infra) if not collab.banned: doc: DocumentCase = DocumentCase.objects.get(id=doc_id, creatoriD=str(collab.id)) doc.infrasDocList = body['infrastructure_types'] doc.save() return ApiResult( message=f'Edited infrastructure types of the document {doc.id}.' ) raise TellSpaceAuthError(msg='Authorization Error. Collaborator is banned or has not been approved by the admin.')