-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: summarize fixtures command (#92)
* fix: summarize fixtures command * fix: remove new lines
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
© Ocado Group | ||
Created on 22/02/2024 at 09:23:32(+00:00). | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
© Ocado Group | ||
Created on 22/02/2024 at 09:23:49(+00:00). | ||
""" |
84 changes: 84 additions & 0 deletions
84
codeforlife/user/management/commands/summarize_fixtures.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
""" | ||
© Ocado Group | ||
Created on 22/02/2024 at 09:24:27(+00:00). | ||
""" | ||
|
||
import json | ||
import os | ||
import typing as t | ||
from dataclasses import dataclass | ||
from itertools import groupby | ||
|
||
from django.apps import apps | ||
from django.core.management.base import BaseCommand | ||
|
||
from ....types import JsonDict | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Fixture: | ||
"""A data model fixture.""" | ||
|
||
model: str | ||
pk: t.Any | ||
fields: JsonDict | ||
|
||
|
||
FixtureDict = t.Dict[str, t.List[Fixture]] | ||
|
||
|
||
# pylint: disable-next=missing-class-docstring | ||
class Command(BaseCommand): | ||
help = "Summarizes all the listed fixtures by default" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("app_labels", nargs="?", type=str) | ||
|
||
def _write_pks_per_model(self, fixtures: t.List[Fixture], indents: int = 0): | ||
def get_model(fixture: Fixture): | ||
return fixture.model | ||
|
||
fixtures.sort(key=get_model) | ||
|
||
self.stdout.write(f'{" " * indents}Primary keys per model:') | ||
|
||
for model, group in groupby(fixtures, key=get_model): | ||
pks = [fixture.pk for fixture in group] | ||
pks.sort() | ||
|
||
self.stdout.write(f'{" " * (indents + 1)}- {model}: {pks}') | ||
|
||
def write_pks_per_model(self, fixtures: FixtureDict): | ||
"""Write all the sorted primary keys per model.""" | ||
self._write_pks_per_model( | ||
[ | ||
fixture | ||
for file_fixtures in fixtures.values() | ||
for fixture in file_fixtures | ||
] | ||
) | ||
|
||
def write_pks_per_file(self, fixtures: FixtureDict): | ||
"""Write all the sorted primary keys per file, per model.""" | ||
self.stdout.write("Primary keys per file:") | ||
|
||
for file, file_fixtures in fixtures.items(): | ||
self.stdout.write(f" - {file}") | ||
self._write_pks_per_model(file_fixtures, indents=2) | ||
|
||
def handle(self, *args, **options): | ||
fixtures: FixtureDict = {} | ||
for app_label in [*(options["app_labels"] or []), "user"]: | ||
app_config = apps.app_configs[app_label] | ||
fixtures_path = os.path.join(app_config.path, "fixtures") | ||
|
||
for fixture_name in os.listdir(fixtures_path): | ||
fixture_path = os.path.join(fixtures_path, fixture_name) | ||
with open(fixture_path, "r", encoding="utf-8") as fixture: | ||
fixtures[fixture_path] = [ | ||
Fixture(**fixture) for fixture in json.load(fixture) | ||
] | ||
|
||
self.write_pks_per_model(fixtures) | ||
self.stdout.write() | ||
self.write_pks_per_file(fixtures) |