academon/core/models/vessel.py

53 lines
1.2 KiB
Python
Raw Normal View History

2022-09-16 12:51:56 +00:00
from django.db import models
2022-09-20 14:02:39 +00:00
from typing import List, Dict
2022-09-16 12:51:56 +00:00
from pycruisemapper.classes import CruiseMapper, Ship
class Vessel(models.Model):
name = models.CharField(max_length=64)
2022-09-18 16:45:35 +00:00
imo = models.IntegerField("IMO", null=True, blank=True)
mmsi = models.IntegerField("MMSI", null=True, blank=True)
2022-09-16 12:51:56 +00:00
2022-09-20 14:02:39 +00:00
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
2022-09-16 12:51:56 +00:00
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