|
| 1 | +from datetime import datetime |
| 2 | + |
| 3 | +from flask import Blueprint, request |
| 4 | +from flask_login import login_required, current_user |
| 5 | +from marshmallow import ValidationError |
| 6 | + |
| 7 | +from .response import not_found, unauthorized, no_content, bad_request, validation_error |
| 8 | +from ... import acl |
| 9 | +from ...db import transaction, db |
| 10 | +from ...models import OneOnOne, OneOnOneActionItem |
| 11 | +from ...schemas.one_on_one import OneOnOneSchema, GetOneOnOneSchema, CreateOneOnOneSchema, OneOnOneActionItemSchema, \ |
| 12 | + CreateOrEditOneOnOneActionItemSchema |
| 13 | + |
| 14 | +blueprint = Blueprint('api.one_on_ones', __name__) |
| 15 | + |
| 16 | +one_on_one_schema = OneOnOneSchema() |
| 17 | +one_on_one_action_item_schema = OneOnOneActionItemSchema() |
| 18 | +get_one_on_one_schema = GetOneOnOneSchema() |
| 19 | +create_one_on_one_schema = CreateOneOnOneSchema() |
| 20 | +create_or_edit_one_on_one_action_item_schema = CreateOrEditOneOnOneActionItemSchema() |
| 21 | + |
| 22 | + |
| 23 | +@login_required |
| 24 | +@blueprint.route('', methods=['GET']) |
| 25 | +def list_all(): |
| 26 | + return one_on_one_schema.jsonify(OneOnOne.get_by_user(current_user), many=True) |
| 27 | + |
| 28 | + |
| 29 | +@login_required |
| 30 | +@blueprint.route('', methods=['PUT']) |
| 31 | +def create(): |
| 32 | + if not request.is_json(): |
| 33 | + return bad_request() |
| 34 | + |
| 35 | + try: |
| 36 | + one_on_one: OneOnOne = create_one_on_one_schema.load(request.json) |
| 37 | + one_on_one.created_by = current_user |
| 38 | + one_on_one.created_at = datetime.now() |
| 39 | + |
| 40 | + with transaction(): |
| 41 | + OneOnOne.create(one_on_one) |
| 42 | + |
| 43 | + return get_one_on_one_schema.jsonify(one_on_one) |
| 44 | + except ValidationError as e: |
| 45 | + return validation_error(e.messages) |
| 46 | + |
| 47 | + |
| 48 | +@login_required |
| 49 | +@blueprint.route('/<_id>', methods=['GET']) |
| 50 | +def get(_id: int): |
| 51 | + one_on_one = OneOnOne.get(_id) |
| 52 | + |
| 53 | + if not one_on_one: |
| 54 | + return not_found() |
| 55 | + |
| 56 | + if not acl.can_view_one_on_one(one_on_one): |
| 57 | + return unauthorized() |
| 58 | + |
| 59 | + return get_one_on_one_schema.jsonify(one_on_one) |
| 60 | + |
| 61 | + |
| 62 | +@login_required |
| 63 | +@blueprint.route('/<one_on_one_id>/action_items', methods=['GET']) |
| 64 | +def get_action_items(one_on_one_id: int): |
| 65 | + one_on_one = OneOnOne.get(one_on_one_id) |
| 66 | + |
| 67 | + if not one_on_one: |
| 68 | + return not_found() |
| 69 | + |
| 70 | + if not acl.can_view_one_on_one(one_on_one): |
| 71 | + return unauthorized() |
| 72 | + |
| 73 | + return one_on_one_action_item_schema.jsonify(one_on_one.action_items, many=True) |
| 74 | + |
| 75 | + |
| 76 | +@login_required |
| 77 | +@blueprint.route('/<one_on_one_id>/action_items/<action_item_id>', methods=['GET']) |
| 78 | +def get_action_item(one_on_one_id: int, action_item_id: int): |
| 79 | + one_on_one = OneOnOne.get(one_on_one_id) |
| 80 | + |
| 81 | + if not one_on_one: |
| 82 | + return not_found() |
| 83 | + |
| 84 | + if not acl.can_view_one_on_one(one_on_one): |
| 85 | + return unauthorized() |
| 86 | + |
| 87 | + action_item = OneOnOneActionItem.get(action_item_id) |
| 88 | + |
| 89 | + if not action_item: |
| 90 | + return not_found() |
| 91 | + |
| 92 | + if not action_item.one_on_one_id == one_on_one.id: |
| 93 | + return not_found() |
| 94 | + |
| 95 | + return one_on_one_action_item_schema.jsonify(action_item) |
| 96 | + |
| 97 | + |
| 98 | +@login_required |
| 99 | +@blueprint.route('/<one_on_one_id>/action_items/<action_item_id>', methods=['DELETE']) |
| 100 | +def delete_action_item(one_on_one_id: int, action_item_id: int): |
| 101 | + one_on_one = OneOnOne.get(one_on_one_id) |
| 102 | + |
| 103 | + if not one_on_one: |
| 104 | + return not_found() |
| 105 | + |
| 106 | + if not acl.can_view_one_on_one(one_on_one): |
| 107 | + return unauthorized() |
| 108 | + |
| 109 | + action_item = OneOnOneActionItem.get(action_item_id) |
| 110 | + |
| 111 | + if not action_item: |
| 112 | + return not_found() |
| 113 | + |
| 114 | + if not action_item.one_on_one_id == one_on_one.id: |
| 115 | + return not_found() |
| 116 | + |
| 117 | + with transaction(): |
| 118 | + db.session.remove(one_on_one) |
| 119 | + |
| 120 | + return no_content() |
| 121 | + |
| 122 | + |
| 123 | +@login_required |
| 124 | +@blueprint.route('/<one_on_one_id>/action_items', methods=['PUT']) |
| 125 | +def create_action_item(one_on_one_id: int): |
| 126 | + if not request.is_json(): |
| 127 | + return bad_request() |
| 128 | + |
| 129 | + one_on_one = OneOnOne.get(one_on_one_id) |
| 130 | + |
| 131 | + if not one_on_one: |
| 132 | + return not_found() |
| 133 | + |
| 134 | + if not acl.can_view_one_on_one(one_on_one): |
| 135 | + return unauthorized() |
| 136 | + |
| 137 | + try: |
| 138 | + action_item: OneOnOneActionItem = create_or_edit_one_on_one_action_item_schema.load(request.json) |
| 139 | + action_item.one_on_one = one_on_one |
| 140 | + action_item.created_by = current_user |
| 141 | + action_item.created_at = datetime.now() |
| 142 | + |
| 143 | + with transaction(): |
| 144 | + OneOnOneActionItem.create(action_item) |
| 145 | + |
| 146 | + return one_on_one_action_item_schema.jsonify(action_item) |
| 147 | + except ValidationError as e: |
| 148 | + return validation_error(e.messages) |
| 149 | + |
| 150 | + |
| 151 | +@login_required |
| 152 | +@blueprint.route('/<one_on_one_id>/action_items/<action_item_id>', methods=['PATCH']) |
| 153 | +def edit_action_item(one_on_one_id: int, action_item_id: int): |
| 154 | + if not request.is_json(): |
| 155 | + return bad_request() |
| 156 | + |
| 157 | + one_on_one = OneOnOne.get(one_on_one_id) |
| 158 | + |
| 159 | + if not one_on_one: |
| 160 | + return not_found() |
| 161 | + |
| 162 | + if not acl.can_view_one_on_one(one_on_one): |
| 163 | + return unauthorized() |
| 164 | + |
| 165 | + action_item = OneOnOneActionItem.get(action_item_id) |
| 166 | + |
| 167 | + try: |
| 168 | + action_item: OneOnOneActionItem = create_or_edit_one_on_one_action_item_schema.load(request.json, action_item) |
| 169 | + |
| 170 | + with transaction(): |
| 171 | + db.session.add(action_item) |
| 172 | + |
| 173 | + return one_on_one_action_item_schema.jsonify(action_item) |
| 174 | + except ValidationError as e: |
| 175 | + return validation_error(e.messages) |
| 176 | + |
| 177 | + |
| 178 | +@login_required |
| 179 | +@blueprint.route('/<_id>', methods=['DELETE']) |
| 180 | +def delete_one_on_one(_id: int): |
| 181 | + one_on_one = OneOnOne.get(_id) |
| 182 | + |
| 183 | + if not one_on_one: |
| 184 | + return not_found() |
| 185 | + |
| 186 | + if not acl.can_delete_one_on_one(one_on_one): |
| 187 | + return unauthorized() |
| 188 | + |
| 189 | + with transaction(): |
| 190 | + db.session.remove(one_on_one) |
| 191 | + |
| 192 | + return no_content() |
0 commit comments