def get_documents_by_date(): """View for table updating (using JQuery and ajax)""" update_table_schema = UpdateTableSchema() errors = update_table_schema.validate(request.args) # if user inputs not number or nothing, than show him all entries if errors: last_n_days = 0 else: args = update_table_schema.dump(request.args) last_n_days = args['last_n_days'] document = Document(connection=connection, cursor=cursor) if last_n_days == 0: documents_by_date = document.get_all_documents() else: documents_by_date = document.get_document_by_date( document_n_days=last_n_days) context = {'all_documents': documents_by_date} return make_response(jsonify(context), StatusCodes.OK)
def get_documents(): """View with documents table""" document = Document(connection=connection, cursor=cursor) all_documents = document.get_all_documents() context = {'all_documents': all_documents} return make_response(jsonify(context), StatusCodes.OK)
def delete_document(document_idx: int): """View for deleting document""" document = Document(connection=connection, cursor=cursor) document.delete_document(document_id=document_idx) all_documents = document.get_all_documents() context = {'all_documents': all_documents} return make_response(jsonify(context), StatusCodes.OK)
def delete_user(user_idx: int): """View for deleting users""" user = User(connection=connection, cursor=cursor) document = Document(connection=connection, cursor=cursor) user.delete_user(user_id=user_idx) document.delete_documents_with_no_users_assigned() all_users = user.get_all_users() context = {'all_users': all_users} return make_response(jsonify(context), StatusCodes.OK)
def add_document(): """View for adding new documents (form)""" if request.method == 'POST': creators_ids = request.form.getlist( 'creators_ids') # if there is no such name, returns empty list controllers_ids = request.form.getlist('controllers_ids') request_args = dict(request.form) request_args.pop('creators_ids') # there is no need in it now request_args.pop('controllers_ids') request_args['creators_ids'] = creators_ids request_args['controllers_ids'] = controllers_ids request_args['date_of_creation'] = datetime.strptime( request_args['date_of_creation'], '%Y-%m-%d') request_args['date_of_registration'] = datetime.strptime( request_args['date_of_registration'], '%Y-%m-%d') add_new_document_schema = AddNewDocument() errors = add_new_document_schema.validate(data=request_args) if errors: abort(StatusCodes.BadRequest, str(errors)) args = add_new_document_schema.dump(request_args) document = Document(connection=connection, cursor=cursor) document.add_document( document_name=args['document_name'], document_type=args['document_type'], date_of_creation=args['date_of_creation'], date_of_registration=args['date_of_registration'], controllers_ids=args['controllers_ids'], creators_ids=args['creators_ids'], ) return Response(status=StatusCodes.Created) return Response(status=StatusCodes.BadRequest)
def get_one_document(idx: int): """View for one document page""" document = Document(connection=connection, cursor=cursor) document_description = document.get_document_by_id(document_id=idx) task = Task(connection=connection, cursor=cursor) all_document_tasks = task.get_task_by_document_id(document_id=idx) if isinstance(document_description['date_of_creation'], datetime): document_description['date_of_creation'] = str( document_description['date_of_creation'].date()) if isinstance(document_description['date_of_registration'], datetime): document_description['date_of_registration'] = str( document_description['date_of_registration'].date()) context = { 'document_description': document_description, 'all_document_tasks': all_document_tasks } return make_response(jsonify(context), StatusCodes.OK)
def change_document(document_idx: int): """View for document changing""" if request.method == 'POST': document = Document(connection=connection, cursor=cursor) document_to_change = document.get_document_by_id( document_id=document_idx) creators_ids = request.form.getlist( 'creators_ids') # if there is no such name, returns empty list controllers_ids = request.form.getlist('controllers_ids') request_args = dict(request.form) request_args.pop('creators_ids') # there is no need in it now request_args.pop('controllers_ids') request_args['creators_ids'] = creators_ids request_args['controllers_ids'] = controllers_ids request_args['date_of_creation'] = datetime.strptime( request_args['date_of_creation'], '%Y-%m-%d') request_args['date_of_registration'] = datetime.strptime( request_args['date_of_registration'], '%Y-%m-%d') add_new_document_schema = AddNewDocument() errors = add_new_document_schema.validate(data=request_args) if errors: abort(StatusCodes.BadRequest, str(errors)) args = add_new_document_schema.dump(request_args) document = Document(connection=connection, cursor=cursor) document.change_document( document_id=document_idx, document_name=args['document_name'], document_type=args['document_type'], date_of_creation=args['date_of_creation'], date_of_registration=args['date_of_registration'], controllers_ids=args['controllers_ids'], creators_ids=args['creators_ids'], ) context = {'idx': document_to_change['id']} return make_response(jsonify(context), StatusCodes.Created) return Response(status=StatusCodes.BadRequest)