Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/pgcrypto/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@ def formfield(self, **kwargs):
return super().formfield(**defaults)


class EncryptedBooleanField(BaseEncryptedField):
description = _("Boolean")
field_cast = "::boolean"

def formfield(self, **kwargs):
defaults = {"form_class": forms.BooleanField}
defaults.update(kwargs)
return super().formfield(**defaults)

def to_python(self, value):
if value in self.empty_values:
return None
unencrypted_value = super().to_python(value)
return unencrypted_value == 'True'


class EncryptedLookup(Lookup):
patterns = {
"contains": "%%%s%%",
Expand Down
6 changes: 4 additions & 2 deletions testapp/fixtures/employees.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"name": "John Smith",
"ssn": "999-05-6728",
"salary": "52000.00",
"date_hired": "1999-01-23"
"date_hired": "1999-01-23",
"is_vip": false
},
"model": "testapp.employee",
"pk": 1
Expand All @@ -15,7 +16,8 @@
"ssn": "666-27-9811",
"salary": "75248.77",
"date_hired": "1996-02-28",
"email": "[email protected]"
"email": "[email protected]",
"is_vip": true
},
"model": "testapp.employee",
"pk": 2
Expand Down
2 changes: 2 additions & 0 deletions testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Employee(models.Model):
)
email = pgcrypto.EncryptedEmailField(unique=True, null=True)
date_modified = pgcrypto.EncryptedDateTimeField(auto_now=True)
is_vip = pgcrypto.EncryptedBooleanField()

def __str__(self):
return self.name
Expand All @@ -30,6 +31,7 @@ class RawEmployee(models.Model):
date_hired = models.TextField()
email = models.TextField(null=True)
date_modified = models.TextField()
is_vip = models.BooleanField()

class Meta:
db_table = "testapp_employee"
Expand Down
1 change: 1 addition & 0 deletions testapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def test_formfields(self):
"date_hired": forms.DateField,
"email": forms.EmailField,
"date_modified": forms.DateTimeField,
"is_vip": forms.BooleanField,
}
actual = {
f.name: type(f.formfield())
Expand Down