from django.views import View from django.http import JsonResponse from core.mixins.auth import StaffRequiredMixin from core.models.vessel import Vessel from pycruisemapper import CruiseMapper class VesselsLocationView(StaffRequiredMixin, View): def get(self, request, *args, **kwargs): try: vessel = Vessel.objects.get(id=kwargs["id"]) try: ship = vessel.query_cruisemapper() assert ship.location.latitude and ship.location.longitude data = { "status": "success", "name": ship.name, "location": ship.location.__dict__, "destination": ship.destination, } except AssertionError: data = { "status": "error", "error": f"CruiseMapper does not seem to know the location of {vessel.name}." } except Exception as e: data = { "status": "error", "error": f"Something went wrong fetching data from CruiseMapper: {str(e)}" } except Vessel.DoesNotExist: data = { "status": "error", "error": f"No Vessel object with ID {kwargs['id']} found" } except Exception as e: data = { "status": "error", "error": f"Something unexpected went wrong: {str(e)}" } return JsonResponse(data)