def show_management_plan(): # We open a connection to the database and create the # `stakeholders`, `deliverables`, and `associations` tables # if they do not already exist. with stakeholders.db.Database(file=file) as db: db.create_stakeholders_table() db.create_deliverables_table() db.create_associations_table() # We request all records from the `associations` table. results = db.select_all_from_associations_table() # We map, reduce, and sort these records. mapped_reduced_sorted = stakeholders.utils.sort( stakeholders.utils.map_reduce(results)) # We return a `200` status code and records serialized # as JSON. return (json.dumps(mapped_reduced_sorted), 200, { "Content-Type": "application/json" })
def test_create_associations_table(file): with stakeholders.db.Database(file=file) as db: db.create_stakeholders_table() db.create_deliverables_table() db.create_associations_table() results = db.select_all_from_associations_table() assert len(results) == 0
def test_create_associations_table_error(file): with pytest.raises(sqlite3.OperationalError) as exception: with stakeholders.db.Database(file=file) as db: db.create_stakeholders_table() db.create_deliverables_table() db.select_all_from_associations_table() assert "no such table: associations" == str(exception.value)
def db(file): with stakeholders.db.Database(file=file) as db: db.create_stakeholders_table() db.insert_into_stakeholders_table(**{ "name": "stakeholder #1", "role": "project sponsor", "sentiment": "😀", "power": "high", "interest": "high", "approach": "monitor closely" }) db.insert_into_stakeholders_table(**{ "name": "stakeholder #2", "role": "customer", "sentiment": "😐", "power": "high", "interest": "low", "approach": "keep satisfied" }) db.create_deliverables_table() db.insert_into_deliverables_table(**{ "name": "deliverable #1", "kind": "interactive", "medium": "in-person meeting", "formality": "formal", "frequency": "daily" }) db.insert_into_deliverables_table(**{ "name": "deliverable #2", "kind": "push", "medium": "video/teleconference", "formality": "casual", "frequency": "weekly" }) db.create_associations_table() db.insert_into_associations_table(**{ "stakeholder_id": 1, "deliverable_id": 1 }) db.insert_into_associations_table(**{ "stakeholder_id": 2, "deliverable_id": 2 }) yield db
def update_stakeholder(): """ This is the endpoint for updating a stakeholder. It accepts a `POST` request. It returns a status code (`200`) and a message (`Success`). """ # We open a connection to the database and create the # `stakeholders` table if it does not already exist. with stakeholders.db.Database(file=file) as db: db.create_stakeholders_table() # We extract the values of `power` and `interest` # from the body of the request and compute # the approach for managing this stakeholder. approach = stakeholders.utils.compute_approach( flask.request.form.get("power"), flask.request.form.get("interest")) # We create a dict with information on the stakeholder. stakeholder = { "id": flask.request.form.get("id"), "name": flask.request.form.get("name"), "role": flask.request.form.get("role"), "sentiment": flask.request.form.get("sentiment"), "power": flask.request.form.get("power"), "interest": flask.request.form.get("interest"), "approach": approach } # We update a record in the `stakeholders` table. db.update_stakeholders_table(**stakeholder) # We return a `200` status code and a `Success` message. return ("Success", 200)
def delete_stakeholder(): """ This is the endpoint for deleting a stakeholder. It accepts a `POST` request. It returns a status code (`200`) and a message (`Success`). """ # We open a connection to the database and create the # `stakeholders` table if it does not already exist. # # We delete a record from the `stakeholders` table. with stakeholders.db.Database(file=file) as db: db.create_stakeholders_table() db.delete_from_stakeholders_table(**flask.request.form.to_dict()) # We return a `200` status code and a `Success` message. return ("Success", 200)
def show_associations(): """ This is the endpoint for showing associations. It accepts a `GET` request. It returns a status code (`200`) and records serialized as JSON. """ # We open a connection to the database and create the # `stakeholders`, `deliverables`, and `associations` tables # if they do not already exist. with stakeholders.db.Database(file=file) as db: db.create_stakeholders_table() db.create_deliverables_table() db.create_associations_table() # If it exists, then we obtain the value of the `id` # query parameter. id = flask.request.args.get("id") # If there is no `id`, then we select all records # from the `associations` table. # # Otherwise, we select a single record # from the `associations` table. if id is None: results = db.select_all_from_associations_table() else: results = db.select_from_associations_table(id=id) # We return a `200` status code and records serialized # as JSON. return (json.dumps(results), 200, { "Content-Type": "application/json" })
def update_association(): """ This is the endpoint for updating an association. It accepts a `POST` request. It returns a status code (`200`) and a message (`Success`). """ # We open a connection to the database and create the # `stakeholders`, `deliverables`, and `associations` tables # if they do not already exist. # # We update a record in the `associations` table. with stakeholders.db.Database(file=file) as db: db.create_stakeholders_table() db.create_deliverables_table() db.create_associations_table() db.update_associations_table(**flask.request.form.to_dict()) # We return a `200` status code and a `Success` message. return ("Success", 200)