-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a publisher model, use for traceability page initially
This speeds up load times from seconds to ~150ms.
- Loading branch information
Showing
10 changed files
with
142 additions
and
24 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
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,15 @@ | ||
from django.core.management.base import BaseCommand | ||
from django.db import transaction | ||
|
||
from data import get_publisher_stats, publishers_ordered_by_title | ||
from ui.models import Publisher | ||
|
||
|
||
class Command(BaseCommand): | ||
@transaction.atomic | ||
def handle(self, *args, **options): | ||
Publisher.objects.all().delete() | ||
for publisher_title, publisher_slug in publishers_ordered_by_title: | ||
stats_json = dict(get_publisher_stats(publisher_slug)) | ||
publisher = Publisher(title=publisher_title, slug=publisher_slug, stats_json=stats_json) | ||
publisher.save() |
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,62 @@ | ||
# Generated by Django 5.1.4 on 2024-12-20 13:31 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Publisher", | ||
fields=[ | ||
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
("slug", models.CharField()), | ||
("title", models.CharField()), | ||
("stats_json", models.JSONField()), | ||
( | ||
"traceable_sum_commitments_and_disbursements_by_publisher_id_den", | ||
models.GeneratedField( | ||
db_persist=True, | ||
expression=models.F( | ||
"stats_json__traceable_sum_commitments_and_disbursements_by_publisher_id_denominator" | ||
), | ||
output_field=models.JSONField(), | ||
), | ||
), | ||
( | ||
"activities", | ||
models.GeneratedField( | ||
db_persist=True, expression=models.F("stats_json__activities"), output_field=models.JSONField() | ||
), | ||
), | ||
( | ||
"traceable_activities_by_publisher_id", | ||
models.GeneratedField( | ||
db_persist=True, | ||
expression=models.F("stats_json__traceable_activities_by_publisher_id"), | ||
output_field=models.JSONField(), | ||
), | ||
), | ||
( | ||
"traceable_activities_by_publisher_id_denominator", | ||
models.GeneratedField( | ||
db_persist=True, | ||
expression=models.F("stats_json__traceable_activities_by_publisher_id_denominator"), | ||
output_field=models.JSONField(), | ||
), | ||
), | ||
( | ||
"traceable_sum_commitments_and_disbursements_by_publisher_id", | ||
models.GeneratedField( | ||
db_persist=True, | ||
expression=models.F("stats_json__traceable_sum_commitments_and_disbursements_by_publisher_id"), | ||
output_field=models.JSONField(), | ||
), | ||
), | ||
], | ||
), | ||
] |
Empty file.
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,33 @@ | ||
import hashlib | ||
|
||
from django.db import models | ||
|
||
|
||
class Publisher(models.Model): | ||
slug = models.CharField() | ||
title = models.CharField() | ||
stats_json = models.JSONField() | ||
# too long | ||
traceable_sum_commitments_and_disbursements_by_publisher_id_den = models.GeneratedField( | ||
expression=models.F("stats_json__traceable_sum_commitments_and_disbursements_by_publisher_id_denominator"), | ||
output_field=models.JSONField(), | ||
db_persist=True, | ||
) | ||
|
||
@property | ||
def traceable_sum_commitments_and_disbursements_by_publisher_id_denominator(self): | ||
return self.traceable_sum_commitments_and_disbursements_by_publisher_id_den | ||
|
||
|
||
for key in [ | ||
"activities", | ||
"traceable_activities_by_publisher_id", | ||
"traceable_activities_by_publisher_id_denominator", | ||
"traceable_sum_commitments_and_disbursements_by_publisher_id", | ||
]: | ||
Publisher.add_to_class( | ||
key, | ||
models.GeneratedField( | ||
expression=models.F(f"stats_json__{key}"), output_field=models.JSONField(), db_persist=True | ||
), | ||
) |
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
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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
django | ||
django-environ | ||
psycopg | ||
gunicorn | ||
flask | ||
frozen-flask | ||
|
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
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