Exemplo n.º 1
0
class LocationController(BaseController):
    def __init__(self, request):
        BaseController.__init__(self, request)
        self.location_repo = LocationRepo()

    def list_locations(self):
        locations = self.location_repo.fetch_all()
        location_list = [location.serialize() for location in locations.items]
        return self.handle_response(
            "OK",
            payload={
                "locations": location_list,
                "meta": self.pagination_meta(locations),
            },
        )

    def get_location(self, location_id):
        location = self.location_repo.get(location_id)
        if location:
            return self.handle_response(
                "OK", payload={"location": location.serialize()})
        return self.handle_response("Invalid or Missing location_id",
                                    status_code=400)

    def create_location(self):
        name, zone = self.request_params("name", "zone")
        location = self.location_repo.new_location(name=name, zone=zone)
        return self.handle_response("OK",
                                    payload={"location": location.serialize()},
                                    status_code=201)

    def update_location(self, location_id):
        name, zone = self.request_params("name", "zone")
        location = self.location_repo.get(location_id)

        if location:
            location = self.location_repo.update(location,
                                                 **dict(name=name, zone=zone))
            return self.handle_response(
                "OK",
                payload={"location": location.serialize()},
                status_code=201)

        return self.handle_response("Location Not Found", status_code=404)

    def delete_location(self, location_id):
        location = self.location_repo.get(location_id)

        if location and not location.is_deleted:
            location = self.location_repo.update(location,
                                                 **dict(is_deleted=True))
            return self.handle_response(
                "Location deleted successfully",
                payload={"location": location.serialize()},
                status_code=200,
            )

        return self.handle_response("Location Not Found", status_code=404)
Exemplo n.º 2
0
class LocationController(BaseController):
    def __init__(self, request):
        BaseController.__init__(self, request)
        self.location_repo = LocationRepo()

    def get_locations(self):
        locations = self.location_repo.filter_by(**{'is_deleted': 'false'})
        location_list = [location.serialize() for location in locations.items]
        return self.handle_response('OK',
                                    payload={
                                        'locations': location_list,
                                        'meta': self.pagination_meta(locations)
                                    })

    def get_location(self, location_id):
        location = self.location_repo.get(location_id)
        if location:
            location = location.serialize()
            return self.handle_response('OK', payload={'location': location})
        else:
            return self.handle_response(
                'Bad Request - Invalid or missing location_id',
                status_code=400)

    def create_location(self):
        location_code, location_name = self.request_params(
            'locationCode', 'location')
        location = self.location_repo.create_location(location_code,
                                                      location_name)
        return self.handle_response('OK',
                                    payload={'location': location.serialize()},
                                    status_code=201)

    def update_location(self, location_id):
        location_code, location_name = self.request_params(
            'locationCode', 'location')

        location = self.location_repo.get(location_id)
        if location:
            updates = {}
            if location_code:
                updates['location_code'] = location_code
            if location_name:
                updates['location'] = location_name

            self.location_repo.update(location, **updates)
            return self.handle_response(
                'OK', payload={'location': location.serialize()})
        return self.handle_response(
            'Invalid or incorrect location_id provided', status_code=400)

    def delete_location(self, location_id):
        location = self.location_repo.get(location_id)
        updates = {}
        if location:
            updates['is_deleted'] = True

            self.location_repo.update(location, **updates)
            return self.handle_response('OK', payload={"status": "success"})
        return self.handle_response(
            'Invalid or incorrect location_id provided', status_code=400)