-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for postgresql, add field classes for date, datetime, int…
…, decimal
- Loading branch information
Showing
23 changed files
with
582 additions
and
309 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
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,2 @@ | ||
from .crypt_model_admin import CryptModelAdmin | ||
from .formfield_overrides import formfield_overrides |
4 changes: 2 additions & 2 deletions
4
django_crypto_fields/admin.py → ..._crypto_fields/admin/crypt_model_admin.py
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,21 @@ | ||
from django.contrib.admin.options import FORMFIELD_FOR_DBFIELD_DEFAULTS | ||
from django.db import models | ||
|
||
from django_crypto_fields.fields import ( | ||
EncryptedCharField, | ||
EncryptedDateField, | ||
EncryptedDateTimeField, | ||
EncryptedIntegerField, | ||
) | ||
|
||
FORMFIELD_FOR_DBFIELD_DEFAULTS.update( | ||
{ | ||
EncryptedCharField: FORMFIELD_FOR_DBFIELD_DEFAULTS[models.CharField], | ||
EncryptedDateField: FORMFIELD_FOR_DBFIELD_DEFAULTS[models.DateField], | ||
EncryptedDateTimeField: FORMFIELD_FOR_DBFIELD_DEFAULTS[models.DateTimeField], | ||
EncryptedIntegerField: FORMFIELD_FOR_DBFIELD_DEFAULTS[models.IntegerField], | ||
} | ||
) | ||
|
||
|
||
formfield_overrides = FORMFIELD_FOR_DBFIELD_DEFAULTS |
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
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,68 @@ | ||
from datetime import date, datetime | ||
from decimal import Decimal | ||
from typing import Any | ||
|
||
from django_crypto_fields.exceptions import ( | ||
DjangoCryptoFieldsDecodingError, | ||
DjangoCryptoFieldsEncodingError, | ||
) | ||
|
||
ENCODING = "utf-8" | ||
DATETIME_STRING = "%Y-%m-%d %H:%M:%S %z" | ||
DATE_STRING = "%Y-%m-%d" | ||
|
||
|
||
def safe_encode(value: str | int | Decimal | float | date | datetime | bytes) -> bytes | None: | ||
if value is None: | ||
return None | ||
if type(value) in [str, int, Decimal, float]: | ||
value = str(value).encode() | ||
elif type(value) in [date, datetime]: | ||
value = safe_encode_date(value) | ||
else: | ||
raise DjangoCryptoFieldsEncodingError( | ||
f"Value must be of type str, date or number. Got {value} is {type(value)}" | ||
) | ||
return value | ||
|
||
|
||
def decode_to_type(value: bytes, to_type: type) -> Any: | ||
if to_type in [date, datetime]: | ||
value = safe_decode_date(value) | ||
elif to_type in [Decimal]: | ||
value = Decimal(value.decode()) | ||
elif to_type in [int, float]: | ||
value = to_type(value.decode()) | ||
elif to_type in [str]: | ||
value = value.decode() | ||
else: | ||
raise DjangoCryptoFieldsDecodingError(f"Unhandled type. Got {to_type}.") | ||
return value | ||
|
||
|
||
def safe_decode_date(value: bytes) -> [date, datetime]: | ||
"""Convert bytes to string and confirm date/datetime format""" | ||
value = value.decode() | ||
try: | ||
value = datetime.strptime(value, "%Y-%m-%d %H:%M:%S %z") | ||
except ValueError: | ||
try: | ||
value = datetime.strptime(value, "%Y-%m-%d") | ||
except ValueError: | ||
raise DjangoCryptoFieldsDecodingError( | ||
f"Decoded string value must be in ISO date or datetime format. Got {value}" | ||
) | ||
return value | ||
|
||
|
||
def safe_encode_date(value: [date, datetime]) -> bytes: | ||
"""Convert date to string and encode.""" | ||
if type(value) is datetime: | ||
value = datetime.strftime(value, DATETIME_STRING) | ||
elif type(value) is date: | ||
value = datetime.strftime(value, DATE_STRING) | ||
else: | ||
raise DjangoCryptoFieldsEncodingError( | ||
f"Value must be either a date or datetime. Got {value}." | ||
) | ||
return value.encode() |
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.