academon/core/models/vessel.py
2022-09-26 14:27:52 +00:00

52 lines
1.2 KiB
Python

from django.db import models
from typing import List, Dict
from pycruisemapper.classes import CruiseMapper, Ship
class Vessel(models.Model):
name = models.CharField(max_length=64)
imo = models.IntegerField("IMO", null=True, blank=True)
mmsi = models.IntegerField("MMSI", null=True, blank=True)
def get_names(self) -> List[str]:
names = [self.name]
for alias in self.vesselalias_set:
names.append(alias.alias)
return names
def name_dict(cls) -> Dict:
names = {}
for vessel in cls.objects.all():
for name in vessel.get_names():
names[name] = vessel
return names
def query_cruisemapper(self) -> Ship:
cm = CruiseMapper()
all_ships = cm.get_ships()
try:
if self.imo:
filtered = filter(lambda x: x.imo == self.imo, all_ships)
elif self.mmsi:
filtered = filter(lambda x: x.mmsi == self.mmsi, all_ships)
else:
return False
ship = list(filtered)[0]
except:
return False
try:
ship = cm.fill_ship(ship)
except:
pass
return ship