-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
296 additions
and
159 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
33 changes: 0 additions & 33 deletions
33
example_project/example/migrations/0002_alter_weatherlocation_latitude_and_more.py
This file was deleted.
Oops, something went wrong.
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,42 +1,42 @@ | ||
"""Models for testing the django_owm app.""" | ||
|
||
from src.django_owm.models import APICallLog as AbstractAPICallLog | ||
from src.django_owm.models import CurrentWeather as AbstractCurrentWeather | ||
from src.django_owm.models import DailyWeather as AbstractDailyWeather | ||
from src.django_owm.models import HourlyWeather as AbstractHourlyWeather | ||
from src.django_owm.models import MinutelyWeather as AbstractMinutelyWeather | ||
from src.django_owm.models import WeatherAlert as AbstractWeatherAlert | ||
from src.django_owm.models import WeatherErrorLog as AbstractWeatherErrorLog | ||
from src.django_owm.models import WeatherLocation as AbstractWeatherLocation | ||
from src.django_owm.models import AbstractAPICallLog | ||
from src.django_owm.models import AbstractCurrentWeather | ||
from src.django_owm.models import AbstractDailyWeather | ||
from src.django_owm.models import AbstractHourlyWeather | ||
from src.django_owm.models import AbstractMinutelyWeather | ||
from src.django_owm.models import AbstractWeatherAlert | ||
from src.django_owm.models import AbstractWeatherErrorLog | ||
from src.django_owm.models import AbstractWeatherLocation | ||
|
||
|
||
class WeatherLocation(AbstractWeatherLocation): # pylint: disable=R0903 | ||
class WeatherLocation(AbstractWeatherLocation): | ||
"""Concrete model for WeatherLocation.""" | ||
|
||
|
||
class CurrentWeather(AbstractCurrentWeather): # pylint: disable=R0903 | ||
"""Concrete model for CurrentWeather.""" | ||
class CurrentWeather(AbstractCurrentWeather): | ||
"""Concrete model for AbstractCurrentWeather.""" | ||
|
||
|
||
class MinutelyWeather(AbstractMinutelyWeather): # pylint: disable=R0903 | ||
"""Concrete model for MinutelyWeather.""" | ||
class MinutelyWeather(AbstractMinutelyWeather): | ||
"""Concrete model for AbstractMinutelyWeather.""" | ||
|
||
|
||
class HourlyWeather(AbstractHourlyWeather): # pylint: disable=R0903 | ||
"""Concrete model for HourlyWeather.""" | ||
class HourlyWeather(AbstractHourlyWeather): | ||
"""Concrete model for AbstractHourlyWeather.""" | ||
|
||
|
||
class DailyWeather(AbstractDailyWeather): # pylint: disable=R0903 | ||
"""Concrete model for DailyWeather.""" | ||
class DailyWeather(AbstractDailyWeather): | ||
"""Concrete model for AbstractDailyWeather.""" | ||
|
||
|
||
class WeatherAlert(AbstractWeatherAlert): # pylint: disable=R0903 | ||
"""Concrete model for WeatherAlert.""" | ||
class WeatherAlert(AbstractWeatherAlert): | ||
"""Concrete model for AbstractWeatherAlert.""" | ||
|
||
|
||
class WeatherErrorLog(AbstractWeatherErrorLog): # pylint: disable=R0903 | ||
"""Concrete model for WeatherErrorLog.""" | ||
class WeatherErrorLog(AbstractWeatherErrorLog): | ||
"""Concrete model for AbstractWeatherErrorLog.""" | ||
|
||
|
||
class APICallLog(AbstractAPICallLog): # pylint: disable=R0903 | ||
"""Concrete model for APICallLog.""" | ||
class APICallLog(AbstractAPICallLog): | ||
"""Concrete model for AbstractAPICallLog.""" |
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
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,20 +1,86 @@ | ||
"""Forms for django_owm.""" | ||
|
||
from decimal import ROUND_HALF_UP | ||
from decimal import Decimal | ||
from decimal import DecimalException | ||
from typing import Optional | ||
from typing import Union | ||
|
||
from django import forms | ||
from django.apps import apps | ||
from django.forms.fields import DecimalField | ||
|
||
from .app_settings import OWM_MODEL_MAPPINGS | ||
from .app_settings import get_model_from_string | ||
from .validators import validate_latitude | ||
from .validators import validate_longitude | ||
|
||
|
||
def quantize_to_2_decimal_places(value: Optional[Union[Decimal, str]]) -> Optional[Decimal]: | ||
"""Quantize a Decimal value to 2 decimal places.""" | ||
if value is not None: | ||
if isinstance(value, str): | ||
try: | ||
value = Decimal(value) | ||
except DecimalException: | ||
return value | ||
if isinstance(value, Decimal): | ||
return value.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP) | ||
else: | ||
raise ValueError("Value must be a Decimal or a string that can be converted to a Decimal.") | ||
return value | ||
|
||
|
||
class TrimmedDecimalField(DecimalField): | ||
"""Custom DecimalField that trims the value to 2 decimal places.""" | ||
|
||
def to_python(self, value): | ||
"""Trim the value to 2 decimal places.""" | ||
value = super().to_python(value) | ||
if value is not None: | ||
return quantize_to_2_decimal_places(value) | ||
return value | ||
|
||
|
||
class WeatherLocationForm(forms.ModelForm): | ||
"""Form for creating or updating a Weather Location.""" | ||
|
||
latitude = TrimmedDecimalField( | ||
max_digits=5, | ||
decimal_places=2, | ||
required=True, | ||
label="Latitude", | ||
help_text="Enter the latitude of the location (e.g., 40.7128)", | ||
) | ||
longitude = TrimmedDecimalField( | ||
max_digits=5, | ||
decimal_places=2, | ||
required=True, | ||
label="Longitude", | ||
help_text="Enter the longitude of the location (e.g., -74.0060)", | ||
) | ||
|
||
class Meta: | ||
"""Meta class for WeatherLocationForm.""" | ||
|
||
model = get_model_from_string(OWM_MODEL_MAPPINGS.get("WeatherLocation")) | ||
model = apps.get_model(OWM_MODEL_MAPPINGS.get("WeatherLocation")) | ||
fields = ["name", "latitude", "longitude"] | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.fields["name"].required = False | ||
|
||
def clean_latitude(self): | ||
"""Clean the input latitude value.""" | ||
latitude = self.cleaned_data.get("latitude") | ||
if latitude is not None: | ||
# Validate the latitude value is a Decimal within the valid range | ||
validate_latitude(latitude) | ||
return latitude | ||
|
||
def clean_longitude(self): | ||
"""Clean the input longitude value.""" | ||
longitude = self.cleaned_data.get("longitude") | ||
if longitude is not None: | ||
# Validate the longitude value is a Decimal within the valid range | ||
validate_longitude(longitude) | ||
return longitude |
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
Oops, something went wrong.