Skip to content

Commit

Permalink
refactor with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mxrman committed Feb 10, 2025
1 parent 74e65c3 commit 18ae859
Show file tree
Hide file tree
Showing 26 changed files with 1,205 additions and 54 deletions.
157 changes: 157 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
annotated-types = "==0.7.0"
anyascii = "==0.3.2"
asgiref = "==3.8.1"
astroid = "==3.3.8"
attrs = "==25.1.0"
beautifulsoup4 = "==4.11.2"
"boolean.py" = "==4.0"
boto3 = "==1.34.68"
botocore = "==1.34.109"
cachecontrol = "==0.14.2"
certifi = "==2024.12.14"
cffi = "==1.17.1"
cfgv = "==3.4.0"
charset-normalizer = "==3.4.1"
click = "==8.1.8"
colorama = "==0.4.6"
coreapi = "==2.3.3"
coreschema = "==0.0.4"
cryptography = "==44.0.0"
defusedxml = "==0.7.1"
dill = "==0.3.9"
distlib = "==0.3.9"
django = "==5.1.5"
django-cors-headers = "==4.5.0"
django-db-connection-pool = "==1.2.5"
django-filter = "==24.3"
django-modelcluster = "==6.4"
django-permissionedforms = "==0.1"
django-taggit = "==5.0.0"
django-treebeard = "==4.7.1"
djangorestframework = "==3.15.2"
draftjs-exporter = "==2.1.7"
drf-nested-routers = "==0.94.1"
drf-spectacular = "==0.27.2"
et-xmlfile = "==2.0.0"
exceptiongroup = "==1.2.2"
filelock = "==3.17.0"
filetype = "==1.2.0"
grimp = "==3.5"
gunicorn = "==23.0.0"
html5lib = "==1.1"
identify = "==2.6.6"
idna = "==3.10"
importlib-metadata = "==8.6.1"
inflection = "==0.5.1"
iniconfig = "==2.0.0"
itypes = "==1.2.0"
jinja2 = "==3.1.5"
jmespath = "==1.0.1"
jsonschema = "==4.23.0"
jsonschema-specifications = "==2024.10.1"
kaleido = "==0.2.1"
l18n = "==2021.3"
laces = "==0.1.1"
license-expression = "==30.4.0"
lockfile = "==0.12.2"
markdown = "==3.7"
markdown-it-py = "==2.2.0"
markupsafe = "==3.0.2"
mccabe = "==0.7.0"
mdurl = "==0.1.2"
msgpack = "==1.1.0"
mypy-extensions = "==1.0.0"
nodeenv = "==1.9.1"
openapi-codec = "==1.3.2"
openpyxl = "==3.1.5"
orjson = "==3.10.15"
packageurl-python = "==0.16.0"
packaging = "==24.2"
pathspec = "==0.12.1"
pbr = "==6.0.0"
pillow = "==11.1.0"
pillow-heif = "==0.21.0"
platformdirs = "==4.3.6"
plotly = "==5.24.1"
pluggy = "==1.5.0"
psycopg2 = "==2.9.10"
psycopg2-binary = "==2.9.10"
py-serializable = "==1.1.2"
pycparser = "==2.22"
pydantic = "==2.10.6"
pydantic-core = "==2.27.2"
pygments = "==2.19.1"
pyjwt = "==2.10.1"
pylint-plugin-utils = "==0.8.2"
pyparsing = "==3.2.1"
pyrsistent = "==0.20.0"
python-dateutil = "==2.9.0.post0"
python-dotenv = "==1.0.1"
pytz = "==2024.2"
pyyaml = "==6.0.2"
redis = "==5.2.1"
referencing = "==0.35.1"
requests = "==2.32.3"
rich = "==13.9.4"
rpds-py = "==0.22.3"
"ruamel.yaml" = "==0.18.10"
"ruamel.yaml.clib" = "==0.2.12"
s3transfer = "==0.10.4"
scour = "==0.38.2"
simplejson = "==3.19.3"
six = "==1.17.0"
smmap = "==5.0.2"
sortedcontainers = "==2.4.0"
soupsieve = "==2.6"
sqlalchemy = "==2.0.37"
sqlparams = "==6.2.0"
sqlparse = "==0.5.3"
telepath = "==0.3.1"
tenacity = "==9.0.0"
toml = "==0.10.2"
tomli = "==2.2.1"
tomlkit = "==0.13.2"
typing-extensions = "==4.12.2"
uritemplate = "==4.1.1"
urllib3 = "==2.3.0"
virtualenv = "==20.29.1"
wagtail = "==6.3.2"
wagtail-modeladmin = "==2.1.0"
wagtail-trash = "==3.0.0"
webencodings = "==0.5.1"
willow = "==1.9.0"
zipp = "==3.21.0"
bandit = "==1.8.2"
black = "==25.1.0"
coverage = "==7.6.10"
cyclonedx-python-lib = "==7.6.2"
django-factory-boy = "==1.0.0"
factory-boy = "==3.3.1"
faker = "==35.2.0"
gitdb = "==4.0.12"
gitpython = "==3.1.44"
import-linter = "==2.1"
isort = "==5.13.2"
pip-api = "==0.0.34"
pip-audit = "==2.7.3"
pip-requirements-parser = "==32.0.1"
pre-commit = "==4.1.0"
pylint = "==3.3.4"
pylint-django = "==2.6.1"
pytest = "==8.3.4"
pytest-cov = "==6.0.0"
pytest-django = "==4.9.0"
pytest-random-order = "==1.1.1"
ruff = "==0.9.4"
stevedore = "==5.4.0"

[dev-packages]

[requires]
python_version = "3.12"
1 change: 1 addition & 0 deletions metrics/api/serializers/headlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class Meta:
"period_start",
"period_end",
"metric_value",
"is_private",
]

@classmethod
Expand Down
3 changes: 2 additions & 1 deletion metrics/api/serializers/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from metrics.utils.auth import serializer_permissions


# @serializer_permissions(["topic"])
@serializer_permissions()
class CoreTimeSeriesSerializer(serializers.ModelSerializer):
"""This serializer returns a set of serialized fields from the `CoreTimesSeries` and related models.
Expand Down Expand Up @@ -50,6 +50,7 @@ class Meta:
"date",
"metric_value",
"in_reporting_delay_period",
"is_private",
]

@classmethod
Expand Down
9 changes: 4 additions & 5 deletions metrics/api/views/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def _handle_json(
serializer = self._get_serializer_class(
queryset=queryset, metric_group=metric_group, request=request,
)

response = Response(serializer.data)
data = [item for item in serializer.data if item is not None]
response = Response(data)
response["Content-Type"] = "application/json"
response["Content-Disposition"] = "attachment; filename=chart_download.json"
return response
Expand All @@ -102,10 +102,9 @@ def _handle_csv(

return write_data_to_csv(file=response, core_time_series_queryset=queryset)


# @extend_schema(request=DownloadsSerializer, tags=[DOWNLOADS_API_TAG])
# @cache_response()
@authorised_route
@extend_schema(request=DownloadsSerializer, tags=[DOWNLOADS_API_TAG])
@cache_response()
def post(self, request, *args, **kwargs):
"""This endpoint will return the query output in json/csv format
Expand Down
3 changes: 3 additions & 0 deletions metrics/data/managers/core_models/headline.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def get_headlines_released_from_embargo(
sex: str,
age: str,
geography_code: str = "",
is_private: bool = False,
) -> Self:
"""Filters by the given `topic_name` and `metric_name`
Expand Down Expand Up @@ -267,6 +268,7 @@ def query_for_data(
stratum_name: str = "",
sex: str = "",
age: str = "",
is_private: bool = False,
):
"""Filters for a N-item list of dicts by the given params if `fields_to_export` is used.
Expand Down Expand Up @@ -314,6 +316,7 @@ def query_for_data(
stratum_name=stratum_name,
age=age,
sex=sex,
is_private=is_private,
)[:1]

if fields_to_export:
Expand Down
6 changes: 4 additions & 2 deletions metrics/data/managers/core_models/time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
"""

import datetime
from typing import Self
from typing import Self, List

from django.db import models
from django.utils import timezone


class CoreTimeSeriesQuerySet(models.QuerySet):
"""Custom queryset which can be used by the `CoreTimeSeriesManager`"""

Expand Down Expand Up @@ -146,6 +145,7 @@ def query_for_data(
stratum_name: str | None = None,
sex: str | None = None,
age: str | None = None,
is_private: bool = False,
) -> models.QuerySet:
"""Filters for a N-item list of dicts by the given params if `fields_to_export` is used.
Expand Down Expand Up @@ -475,6 +475,7 @@ def query_for_data(
stratum_name: str | None = None,
sex: str | None = None,
age: str | None = None,
is_private: bool,
) -> CoreTimeSeriesQuerySet:
"""Filters for a 2-item object by the given params. Slices all values older than the `date_from`.
Expand Down Expand Up @@ -551,6 +552,7 @@ def query_for_data(
stratum_name=stratum_name,
sex=sex,
age=age,
is_private=is_private,
)

def query_for_superseded_data(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.1.5 on 2025-02-03 18:11

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("data", "0034_remove_subtheme_name_unique_constraint"),
]

operations = [
migrations.AddField(
model_name="coreheadline",
name="is_private",
field=models.BooleanField(default=True),
),
migrations.AddField(
model_name="coretimeseries",
name="is_private",
field=models.BooleanField(default=True),
),
]
2 changes: 2 additions & 0 deletions metrics/data/models/core_models/headline.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@


class CoreHeadline(models.Model):
is_private = models.BooleanField(default=True)

metric = models.ForeignKey(
to=Metric,
on_delete=models.SET_NULL,
Expand Down
2 changes: 2 additions & 0 deletions metrics/data/models/core_models/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@


class CoreTimeSeries(models.Model):
is_private = models.BooleanField(default=True)

metric = models.ForeignKey(
to=Metric,
on_delete=models.SET_NULL,
Expand Down
1 change: 1 addition & 0 deletions metrics/domain/exports/csv_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"date": "date",
"metric_value": "metric_value",
"in_reporting_delay_period": "in_reporting_delay_period",
"is_private": "is_private",
}

HEADLINE_FIELDS = {
Expand Down
2 changes: 2 additions & 0 deletions metrics/domain/models/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class PlotParameters(BaseModel):
override_y_axis_choice_to_none: bool = False
use_smooth_lines: bool = True
use_markers: bool = False
is_private: bool = False

@property
def topic_name(self) -> str:
Expand Down Expand Up @@ -122,6 +123,7 @@ def to_dict_for_query(self) -> dict[str, str]:
"geography_type_name": self.geography_type_name or "",
"sex": self.sex or "",
"age": self.age or "",
"is_private": self.is_private,
}

if self.is_timeseries_data:
Expand Down
Loading

0 comments on commit 18ae859

Please sign in to comment.