Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make fields importable from metrics module and add Date
This adds a custom `Date` field which respects `settings.TIMEZONE`. This also exposes elasticsearch_dsl's fields from the metrics module, so that all fields are imported from the same place: ```python from elasticsearch_metrics import metrics class MyMetric(metrics.Metric): my_keyword = metrics.Keyword() my_date = metrics.Date() ``` This approach was the same used in webargs, which uses marshmallow fields under the hood but overrides a single field. Discussion: marshmallow-code/webargs#61 (comment) close #18
- Loading branch information
Showing
8 changed files
with
87 additions
and
44 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,28 @@ | ||
from django.conf import settings | ||
from elasticsearch_dsl import field as edsl_field | ||
|
||
__all__ = ["Date"] | ||
# Expose all fields from elasticsearch_dsl.field | ||
# We do this instead of 'from elasticsearch_dsl.field import *' because elasticsearch_metrics | ||
# has its own subclass of Date | ||
for each in ( | ||
field_name | ||
for field_name in dir(edsl_field) | ||
if field_name != "Date" and not field_name.startswith("_") | ||
): | ||
field = getattr(edsl_field, each) | ||
is_field_subclass = isinstance(field, type) and issubclass(field, edsl_field.Field) | ||
if field is edsl_field.Field or is_field_subclass: | ||
globals()[each] = field | ||
field.__module__ = __name__ | ||
__all__.append(each) | ||
|
||
|
||
class Date(edsl_field.Date): | ||
"""Same as `elasticsearch_dsl.field.Date` except that this respects | ||
the TIMEZONE Django setting. | ||
""" | ||
|
||
def __init__(self, default_timezone=None, *args, **kwargs): | ||
default_timezone = default_timezone or getattr(settings, "TIMEZONE", None) | ||
super(Date, self).__init__(default_timezone=default_timezone, *args, **kwargs) |
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,15 +1,15 @@ | ||
from elasticsearch_metrics.metrics import Metric | ||
from elasticsearch_metrics import metrics | ||
|
||
|
||
class DummyMetric(Metric): | ||
class DummyMetric(metrics.Metric): | ||
pass | ||
|
||
|
||
class DummyMetricWithExplicitTemplateName(Metric): | ||
class DummyMetricWithExplicitTemplateName(metrics.Metric): | ||
class Meta: | ||
template_name = "dummymetric" | ||
|
||
|
||
class DummyMetricWithExplicitTemplatePattern(Metric): | ||
class DummyMetricWithExplicitTemplatePattern(metrics.Metric): | ||
class Meta: | ||
template = "dummymetric-*" |
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,12 @@ | ||
import pytest | ||
from dateutil import tz | ||
|
||
from elasticsearch_metrics import metrics | ||
|
||
|
||
class TestDate: | ||
@pytest.mark.parametrize("timezone", ["America/Chicago", "UTC"]) | ||
def test_respects_timezone_setting(self, settings, timezone): | ||
settings.TIMEZONE = timezone | ||
field = metrics.Date() | ||
assert field._default_timezone == tz.gettz(timezone) |
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