|
| 1 | +from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship |
| 2 | +from flask_rest_jsonapi.exceptions import ObjectNotFound |
| 3 | + |
| 4 | +from app.api.helpers.db import safe_query_kwargs |
| 5 | +from app.api.helpers.errors import UnprocessableEntityError |
| 6 | +from app.api.helpers.permission_manager import has_access |
| 7 | +from app.api.helpers.permissions import jwt_required |
| 8 | +from app.api.helpers.utilities import require_relationship |
| 9 | +from app.api.schema.station import StationSchema |
| 10 | +from app.models import db |
| 11 | +from app.models.event import Event |
| 12 | +from app.models.microlocation import Microlocation |
| 13 | +from app.models.station import Station |
| 14 | + |
| 15 | + |
| 16 | +class StationList(ResourceList): |
| 17 | + """Create and List Station""" |
| 18 | + |
| 19 | + def query(self, view_kwargs): |
| 20 | + """ |
| 21 | + query method for different view_kwargs |
| 22 | + :param view_kwargs: |
| 23 | + :return: |
| 24 | + """ |
| 25 | + query_ = self.session.query(Station) |
| 26 | + if view_kwargs.get('event_id'): |
| 27 | + event = safe_query_kwargs(Event, view_kwargs, 'event_id') |
| 28 | + query_ = query_.filter_by(event_id=event.id) |
| 29 | + |
| 30 | + elif view_kwargs.get('microlocation_id'): |
| 31 | + event = safe_query_kwargs(Microlocation, view_kwargs, 'microlocation_id') |
| 32 | + query_ = query_.filter_by(microlocation_id=event.id) |
| 33 | + |
| 34 | + return query_ |
| 35 | + |
| 36 | + view_kwargs = True |
| 37 | + schema = StationSchema |
| 38 | + data_layer = { |
| 39 | + 'session': db.session, |
| 40 | + 'model': Station, |
| 41 | + 'methods': {'query': query}, |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | +class StationDetail(ResourceDetail): |
| 46 | + """Station detail by id""" |
| 47 | + |
| 48 | + @staticmethod |
| 49 | + def before_patch(args, kwargs, data): |
| 50 | + """ |
| 51 | + before patch method |
| 52 | + :param args: |
| 53 | + :param kwargs: |
| 54 | + :param data: |
| 55 | + :return: |
| 56 | + """ |
| 57 | + require_relationship(['event'], data) |
| 58 | + if not has_access('is_coorganizer', event=data['event']): |
| 59 | + raise ObjectNotFound( |
| 60 | + {'parameter': 'event'}, |
| 61 | + f"Event: {data['event']} not found {args} {kwargs}", |
| 62 | + ) |
| 63 | + |
| 64 | + if data.get('microlocation'): |
| 65 | + require_relationship(['microlocation'], data) |
| 66 | + if not has_access('is_coorganizer', microlocation=data['microlocation']): |
| 67 | + raise ObjectNotFound( |
| 68 | + {'parameter': 'microlocation'}, |
| 69 | + f"Microlocation: {data['microlocation']} not found", |
| 70 | + ) |
| 71 | + else: |
| 72 | + if data['station_type'] in ('check in', 'check out', 'daily'): |
| 73 | + raise ObjectNotFound( |
| 74 | + {'parameter': 'microlocation'}, |
| 75 | + "Microlocation: microlocation_id is missing from your request.", |
| 76 | + ) |
| 77 | + station = Station.query.filter_by( |
| 78 | + station_type=data.get('station_type'), |
| 79 | + microlocation_id=data.get('microlocation'), |
| 80 | + event_id=data.get('event'), |
| 81 | + ).first() |
| 82 | + if station: |
| 83 | + raise UnprocessableEntityError( |
| 84 | + { |
| 85 | + 'station_type': data.get('station_type'), |
| 86 | + 'microlocation_id': data.get('microlocation'), |
| 87 | + 'event_id': data.get('event'), |
| 88 | + }, |
| 89 | + "A Station already exists for the provided Event ID" |
| 90 | + ", Microlocation ID and Station type", |
| 91 | + ) |
| 92 | + |
| 93 | + schema = StationSchema |
| 94 | + data_layer = { |
| 95 | + 'session': db.session, |
| 96 | + 'model': Station, |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +class StationRelationship(ResourceRelationship): |
| 101 | + """Station Relationship (Required)""" |
| 102 | + |
| 103 | + decorators = (jwt_required,) |
| 104 | + methods = ['GET', 'PATCH'] |
| 105 | + schema = StationSchema |
| 106 | + data_layer = {'session': db.session, 'model': Station} |
| 107 | + |
| 108 | + |
| 109 | +class StationListPost(ResourceList): |
| 110 | + """Create and List Station""" |
| 111 | + |
| 112 | + @staticmethod |
| 113 | + def before_post(args, kwargs, data): |
| 114 | + """ |
| 115 | + method to check for required relationship with event and microlocation |
| 116 | + :param data: |
| 117 | + :param args: |
| 118 | + :param kwargs: |
| 119 | + :return: |
| 120 | + """ |
| 121 | + require_relationship(['event'], data) |
| 122 | + if not has_access('is_coorganizer', event=data['event']): |
| 123 | + raise ObjectNotFound( |
| 124 | + {'parameter': 'event'}, |
| 125 | + f"Event: {data['event']} not found {args} {kwargs}", |
| 126 | + ) |
| 127 | + |
| 128 | + if data.get('microlocation'): |
| 129 | + require_relationship(['microlocation'], data) |
| 130 | + if not has_access('is_coorganizer', microlocation=data['microlocation']): |
| 131 | + raise ObjectNotFound( |
| 132 | + {'parameter': 'microlocation'}, |
| 133 | + f"Microlocation: {data['microlocation']} not found", |
| 134 | + ) |
| 135 | + else: |
| 136 | + if data['station_type'] in ('check in', 'check out', 'daily'): |
| 137 | + raise ObjectNotFound( |
| 138 | + {'parameter': 'microlocation'}, |
| 139 | + "Microlocation: missing from your request.", |
| 140 | + ) |
| 141 | + |
| 142 | + def before_create_object(self, data, view_kwargs): |
| 143 | + """ |
| 144 | + function to check if station already exist |
| 145 | + @param data: |
| 146 | + @param view_kwargs: |
| 147 | + """ |
| 148 | + station = ( |
| 149 | + self.session.query(Station) |
| 150 | + .filter_by( |
| 151 | + station_type=data.get('station_type'), |
| 152 | + microlocation_id=data.get('microlocation'), |
| 153 | + event_id=data.get('event'), |
| 154 | + ) |
| 155 | + .first() |
| 156 | + ) |
| 157 | + if station: |
| 158 | + raise UnprocessableEntityError( |
| 159 | + { |
| 160 | + 'station_type': data.get('station_type'), |
| 161 | + 'microlocation_id': data.get('microlocation'), |
| 162 | + 'event_id': data.get('event'), |
| 163 | + 'view_kwargs': view_kwargs, |
| 164 | + }, |
| 165 | + "A Station already exists for the provided Event ID" |
| 166 | + ", Microlocation ID and Station type", |
| 167 | + ) |
| 168 | + |
| 169 | + schema = StationSchema |
| 170 | + methods = [ |
| 171 | + 'POST', |
| 172 | + ] |
| 173 | + data_layer = { |
| 174 | + 'session': db.session, |
| 175 | + 'model': Station, |
| 176 | + 'methods': {'before_create_object': before_create_object}, |
| 177 | + } |
0 commit comments