Skip to content

Commit

Permalink
move utils.dbfields to fields.encrypted
Browse files Browse the repository at this point in the history
  • Loading branch information
amyasnikov committed Jan 15, 2024
1 parent a936407 commit 58296b6
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion validity/api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from netbox.api.serializers import WritableNestedSerializer
from rest_framework.serializers import JSONField, ModelSerializer

from validity.utils.dbfields import EncryptedDict
from validity.fields.encrypted import EncryptedDict


def nested_factory(
Expand Down
1 change: 1 addition & 0 deletions validity/fields/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .encrypted import EncryptedDict, EncryptedDictField, EncryptedObject, EncryptedString
11 changes: 2 additions & 9 deletions validity/utils/dbfields.py → validity/fields/encrypted.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from django import forms
from django.conf import settings
from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import Field, JSONField
Expand Down Expand Up @@ -123,6 +122,8 @@ def to_python(self, value):
return EncryptedDict(value)

def formfield(self, **kwargs):
from validity.forms.helpers import EncryptedDictField as EncryptedDictFormField

return Field.formfield(
self,
**{
Expand All @@ -135,11 +136,3 @@ def formfield(self, **kwargs):

def value_to_string(self, obj: Any) -> Any:
return super().value_to_string(obj).encrypted


class EncryptedDictFormField(forms.JSONField):
def to_python(self, value: Any) -> Any:
value = super().to_python(value)
if isinstance(value, dict):
value = EncryptedDict(value)
return value
12 changes: 11 additions & 1 deletion validity/forms/helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import json
from typing import Any, Sequence

from django.forms import ChoiceField, Select
from django.forms import ChoiceField, JSONField, Select
from utilities.forms import get_field_value

from validity.fields import EncryptedDict


class IntegerChoiceField(ChoiceField):
def to_python(self, value: Any | None) -> Any | None:
Expand All @@ -12,6 +14,14 @@ def to_python(self, value: Any | None) -> Any | None:
return value


class EncryptedDictField(JSONField):
def to_python(self, value: Any) -> Any:
value = super().to_python(value)
if isinstance(value, dict):
value = EncryptedDict(value)
return value


class SelectWithPlaceholder(Select):
def __init__(self, attrs=None, choices=()) -> None:
super().__init__(attrs, choices)
Expand Down
2 changes: 1 addition & 1 deletion validity/migrations/0005_datasources.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from itertools import chain
from django.db import migrations
from django.utils.translation import gettext_lazy as _
from validity.utils.dbfields import EncryptedString
from validity.fields.encrypted import EncryptedString
from django.db import migrations, models
import django.db.models.deletion
from validity.utils.misc import datasource_sync
Expand Down
4 changes: 2 additions & 2 deletions validity/migrations/0007_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import taggit.managers
import utilities.json
import validity.models.base
import validity.utils.dbfields
import validity.fields.encrypted
from django.utils.translation import gettext_lazy as _
import django.core.validators
import django.db.models.deletion
Expand Down Expand Up @@ -132,7 +132,7 @@ class Migration(migrations.Migration):
("name", models.CharField(max_length=255, unique=True)),
("connection_type", models.CharField(max_length=50)),
("public_credentials", models.JSONField(blank=True, default=dict)),
("private_credentials", validity.utils.dbfields.EncryptedDictField(blank=True)),
("private_credentials", validity.fields.encrypted.EncryptedDictField(blank=True)),
("commands", models.ManyToManyField(related_name="pollers", to="validity.command")),
("tags", taggit.managers.TaggableManager(through="extras.TaggedItem", to="extras.Tag")),
],
Expand Down
2 changes: 1 addition & 1 deletion validity/models/polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from django.utils.translation import gettext_lazy as _

from validity.choices import CommandTypeChoices, ConnectionTypeChoices
from validity.fields import EncryptedDictField
from validity.managers import CommandQS, PollerQS
from validity.pollers import get_poller
from validity.subforms import CLICommandForm
from validity.utils.dbfields import EncryptedDictField
from .base import BaseModel, SubformMixin
from .serializer import Serializer

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from validity.utils.dbfields import EncryptedDict, EncryptedString
from validity.fields import EncryptedDict, EncryptedString


@pytest.fixture
Expand Down

0 comments on commit 58296b6

Please sign in to comment.