-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates to collection sampling methods (#717)
* fix: handle queries with 0 results when populating collections * fix: better handle 0 result queries in all sampling methods * fix: handle midnight boudary for queries with both start & end hour * feat: support date fields for collection sampling methods * feat: randomize collections with max_num by default * fix: explain changes to max_num field in UI
- Loading branch information
Showing
4 changed files
with
121 additions
and
34 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,28 @@ | ||
import datetime | ||
|
||
from rest_framework import serializers | ||
|
||
|
||
class DateStringField(serializers.CharField): | ||
""" | ||
Field that validates and stores dates as YYYY-MM-DD strings. | ||
Needed for storing dates as strings in JSON fields but keep validation. | ||
""" | ||
|
||
def to_internal_value(self, value: str | None) -> str | None: | ||
if value is None: | ||
return None | ||
|
||
try: | ||
# Validate the date format by parsing it | ||
datetime.datetime.strptime(value, "%Y-%m-%d") | ||
return value | ||
except ValueError as e: | ||
raise serializers.ValidationError("Invalid date format. Use YYYY-MM-DD format.") from e | ||
|
||
@classmethod | ||
def to_date(cls, value: str | None) -> datetime.date | None: | ||
"""Convert a YYYY-MM-DD string to a Python date object for ORM queries.""" | ||
if value is None: | ||
return None | ||
return datetime.datetime.strptime(value, "%Y-%m-%d").date() |
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