|
| 1 | +from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship |
| 2 | +from flask_rest_jsonapi.exceptions import ObjectNotFound |
| 3 | + |
| 4 | +from app.api.helpers.permission_manager import has_access |
| 5 | +from app.api.helpers.permissions import jwt_required |
| 6 | +from app.api.helpers.utilities import require_relationship |
| 7 | +from app.api.schema.station_store_pax import StationStorePaxSchema |
| 8 | +from app.models import db |
| 9 | +from app.models.session import Session |
| 10 | +from app.models.station import Station |
| 11 | +from app.models.station_store_pax import StationStorePax |
| 12 | + |
| 13 | + |
| 14 | +class StationStorePaxList(ResourceList): |
| 15 | + """Create and List Station Store Pax""" |
| 16 | + |
| 17 | + def query(self, view_kwargs): |
| 18 | + """ |
| 19 | + query method for different view_kwargs |
| 20 | + :param view_kwargs: |
| 21 | + :return: |
| 22 | + """ |
| 23 | + query_ = self.session.query(StationStorePax) |
| 24 | + if view_kwargs.get('station_id'): |
| 25 | + query_ = query_.join(Station).filter_by(id=view_kwargs.get('station_id')) |
| 26 | + if view_kwargs.get('session_id'): |
| 27 | + query_ = query_.join(Session).filter_by(id=view_kwargs.get('session_id')) |
| 28 | + |
| 29 | + return query_ |
| 30 | + |
| 31 | + view_kwargs = True |
| 32 | + decorators = (jwt_required,) |
| 33 | + methods = [ |
| 34 | + 'GET', |
| 35 | + ] |
| 36 | + schema = StationStorePaxSchema |
| 37 | + data_layer = { |
| 38 | + 'session': db.session, |
| 39 | + 'model': StationStorePax, |
| 40 | + 'methods': {'query': query}, |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +class StationStorePaxDetail(ResourceDetail): |
| 45 | + """StationStorePax detail by id""" |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def before_patch(_args, _kwargs, data): |
| 49 | + """ |
| 50 | + before patch method |
| 51 | + :param _args: |
| 52 | + :param kwargs: |
| 53 | + :param data: |
| 54 | + :return: |
| 55 | + """ |
| 56 | + if data['station']: |
| 57 | + require_relationship(['station'], data) |
| 58 | + if not has_access('is_coorganizer', station=data['station']): |
| 59 | + raise ObjectNotFound( |
| 60 | + {'parameter': 'station'}, |
| 61 | + f"Station: {data['station']} not found", |
| 62 | + ) |
| 63 | + |
| 64 | + if data['session']: |
| 65 | + require_relationship(['session'], data) |
| 66 | + if not has_access('is_coorganizer', session=data['session']): |
| 67 | + raise ObjectNotFound( |
| 68 | + {'parameter': 'session'}, |
| 69 | + f"Session: {data['session']} not found", |
| 70 | + ) |
| 71 | + |
| 72 | + schema = StationStorePaxSchema |
| 73 | + data_layer = { |
| 74 | + 'session': db.session, |
| 75 | + 'model': StationStorePax, |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | +class StationStorePaxRelationship(ResourceRelationship): |
| 80 | + """StationStorePax Relationship (Required)""" |
| 81 | + |
| 82 | + decorators = (jwt_required,) |
| 83 | + methods = ['GET', 'PATCH'] |
| 84 | + schema = StationStorePaxSchema |
| 85 | + data_layer = {'session': db.session, 'model': StationStorePax} |
| 86 | + |
| 87 | + |
| 88 | +class StationStorePaxListPost(ResourceList): |
| 89 | + """Create and List StationStorePax""" |
| 90 | + |
| 91 | + @staticmethod |
| 92 | + def before_post(_args, _kwargs, data): |
| 93 | + """ |
| 94 | + method to check for required relationship with event and microlocation |
| 95 | + :param data: |
| 96 | + :return: |
| 97 | + """ |
| 98 | + require_relationship(['station'], data) |
| 99 | + if not has_access('is_coorganizer', station=data['station']): |
| 100 | + raise ObjectNotFound( |
| 101 | + {'parameter': 'station'}, |
| 102 | + f"Station: {data['station']} not found", |
| 103 | + ) |
| 104 | + |
| 105 | + require_relationship(['session'], data) |
| 106 | + if not has_access('is_coorganizer', session=data['session']): |
| 107 | + raise ObjectNotFound( |
| 108 | + {'parameter': 'session'}, |
| 109 | + f"Session: {data['session']} not found", |
| 110 | + ) |
| 111 | + |
| 112 | + schema = StationStorePaxSchema |
| 113 | + methods = [ |
| 114 | + 'POST', |
| 115 | + ] |
| 116 | + data_layer = {'session': db.session, 'model': StationStorePax} |
0 commit comments