Skip to content

Commit

Permalink
refactor(models): split models file in separate files for better over…
Browse files Browse the repository at this point in the history
…view
  • Loading branch information
zyv committed Dec 26, 2023
1 parent 902ffe4 commit 424a53a
Show file tree
Hide file tree
Showing 25 changed files with 301 additions and 267 deletions.
5 changes: 4 additions & 1 deletion logbook/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.contrib import admin

from .models import Aerodrome, Aircraft, Certificate, LogEntry, Pilot
from .models.aerodrome import Aerodrome
from .models.aircraft import Aircraft
from .models.log_entry import LogEntry
from .models.pilot import Certificate, Pilot
from .templatetags.logbook_utils import duration


Expand Down
3 changes: 2 additions & 1 deletion logbook/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .models import AircraftType, FunctionType, LaunchType, SpeedUnit
from .models.aircraft import AircraftType, SpeedUnit
from .models.log_entry import FunctionType, LaunchType
from .statistics.currency import CurrencyStatus


Expand Down
2 changes: 1 addition & 1 deletion logbook/fixtures/import_aerodromes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from django_countries import countries

from logbook.models import Aerodrome
from logbook.models.aerodrome import Aerodrome

CHECK_ICAO_CODE = False

Expand Down
37 changes: 23 additions & 14 deletions logbook/management/commands/import_flightlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

from django.core.management.base import BaseCommand

from logbook import models
import logbook.models.aerodrome
import logbook.models.aircraft
import logbook.models.log_entry
import logbook.models.pilot


class Fields(enum.StrEnum):
Expand Down Expand Up @@ -40,7 +43,7 @@ def add_arguments(self, parser):
def handle(self, *args, **options):
if options["init"]:
self.stdout.write(self.style.WARNING("Removing old database records..."))
models.LogEntry.objects.all().delete()
logbook.models.log_entry.LogEntry.objects.all().delete()

self.stdout.write(self.style.SUCCESS("Importing FlightLog records..."))

Expand All @@ -65,30 +68,36 @@ def handle(self, *args, **options):
assert (pic_time + dual_time) == duration_recorded, f"{duration_recorded}, {pic_time}, {dual_time}"
assert bool(pic_time) ^ bool(dual_time)

time_function = models.FunctionType.PIC if pic_time else models.FunctionType.DUAL
time_function = (
logbook.models.log_entry.FunctionType.PIC
if pic_time
else logbook.models.log_entry.FunctionType.DUAL
)
registration = row[Fields.REGISTRATION]

aircraft = models.Aircraft.objects.get(registration=registration)
aircraft = logbook.models.aircraft.Aircraft.objects.get(registration=registration)

me = models.Pilot.objects.get(last_name="Zaytsev")
recorded_copilot = models.Pilot.objects.get(last_name=row[Fields.COPILOT].strip())
me = logbook.models.pilot.Pilot.objects.get(last_name="Zaytsev")
recorded_copilot = logbook.models.pilot.Pilot.objects.get(last_name=row[Fields.COPILOT].strip())

pilot, copilot = (
(me, recorded_copilot) if time_function is models.FunctionType.PIC else (recorded_copilot, me)
(me, recorded_copilot)
if time_function is logbook.models.log_entry.FunctionType.PIC
else (recorded_copilot, me)
)

from_aerodrome = models.Aerodrome.objects.get(icao_code=row[Fields.FROM].strip())
to_aerodrome = models.Aerodrome.objects.get(icao_code=row[Fields.TO].strip())
from_aerodrome = logbook.models.aerodrome.Aerodrome.objects.get(icao_code=row[Fields.FROM].strip())
to_aerodrome = logbook.models.aerodrome.Aerodrome.objects.get(icao_code=row[Fields.TO].strip())

remarks = row[Fields.NOTE].strip()

launch_type = (
{
"Self": models.LaunchType.SELF,
"Tow": models.LaunchType.TOW,
"Winch": models.LaunchType.WINCH,
"Self": logbook.models.log_entry.LaunchType.SELF,
"Tow": logbook.models.log_entry.LaunchType.TOW,
"Winch": logbook.models.log_entry.LaunchType.WINCH,
}[remarks]
if aircraft.type == models.AircraftType.GLD
if aircraft.type == logbook.models.aircraft.AircraftType.GLD
else ""
)

Expand Down Expand Up @@ -117,7 +126,7 @@ def handle(self, *args, **options):
"remarks": remarks,
}

entry, created = models.LogEntry.objects.update_or_create(
entry, created = logbook.models.log_entry.LogEntry.objects.update_or_create(
departure_time=departure_time,
from_aerodrome=from_aerodrome,
defaults=defaults,
Expand Down
235 changes: 0 additions & 235 deletions logbook/models.py

This file was deleted.

Empty file added logbook/models/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions logbook/models/aerodrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.db import models
from django_countries.fields import CountryField


class Aerodrome(models.Model):
name = models.CharField(max_length=75)
city = models.CharField(max_length=75)
country = CountryField()
icao_code = models.CharField(max_length=4, unique=True)
latitude = models.DecimalField(max_digits=9, decimal_places=6)
longitude = models.DecimalField(max_digits=9, decimal_places=6)
elevation = models.IntegerField()
priority = models.IntegerField()

class Meta:
ordering = ("priority",)

def __str__(self):
return f"{self.icao_code} ({self.name})"
Loading

0 comments on commit 424a53a

Please sign in to comment.