Skip to content

Commit

Permalink
Refactored lookups and mixins (#63)
Browse files Browse the repository at this point in the history
* Added `__range` lookup for Date / DateTime fields (#59)
* Remove compatibility for `Django 1.8, 1.9, and 1.10` (#62)
* Improved `setup.py`:
    * check for Python 3.5+
    * updated classifiers
* Improved `make` file for release to use `twine`
* Added additional shields to `README`
* Updated Travis config to include Python 3.5 and 3.6
* Refactored lookups and mixins
  • Loading branch information
peterfarrell authored Oct 9, 2018
1 parent bac2044 commit aef1c4b
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 378 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
### 2.3.0

* Added `__range` lookup for Date / DateTime fields (#59)
* Remove compatibility for `Django 1.8, 1.9, and 1.10`
* Remove compatibility for `Django 1.8, 1.9, and 1.10` (#62)
* Improved `setup.py`:
* check for Python 3.5+
* updated classifiers
* Improved `make` file for release to use `twine`
* Added additional shields to `README`
* Updated Travis config to include Python 3.5 and 3.6
* Refactored lookups and mixins

## 2.2.0

Expand Down
9 changes: 6 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ We welcome contributions in many forms:

## Releasing to PyPI

This section only applies to maintainers
This section only applies to maintainers.

* Run `pip install setuptools twine`
* Run `make release`
In your virtual environment, run

* `pip install pip --upgrade`
* `pip install setuptools wheel twine`
* `make release`
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ encrypt and decrypt data for fields.
pip install django-pgcrypto-fields
```

In your Django `settings.py`, add `pgcrypto` to `INSTALLED_APPS`:

```python
INSTALLED_APPS = (
'pgcrypto',
# Other apps
)
```

## Fields

`django-pgcrypto-fields` has 3 kinds of fields:
Expand Down
4 changes: 4 additions & 0 deletions pgcrypto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
PGP_PUB_ENCRYPT_SQL = "pgp_pub_encrypt(%s, dearmor('{}'))".format(
settings.PUBLIC_PGP_KEY,
)
PGP_PUB_DECRYPT_SQL = "pgp_pub_decrypt(%s, dearmor('{}'))".format(
settings.PRIVATE_PGP_KEY,
)
PGP_SYM_ENCRYPT_SQL = "pgp_sym_encrypt(%s, '{}')".format(settings.PGCRYPTO_KEY)
PGP_SYM_DECRYPT_SQL = "pgp_sym_decrypt(%s, '{}')".format(settings.PGCRYPTO_KEY)
31 changes: 6 additions & 25 deletions pgcrypto/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,7 @@ class PGPSymmetricKeySQL:
)


class EncryptionBase(Aggregate):
"""Base class to add a custom aggregate method to a query."""

def add_to_query(self, query, alias, col, source, is_summary):
"""Add the aggregate to the query. This method will be removed in Django 1.10.
`alias` is `{self.lookup}__decrypt` where 'decrypt' is `self.name.lower()`.
`self.lookup` is defined in `models.Aggregate.__init__`.
"""
aggregate = self.sql(
col,
source=source,
is_summary=is_summary,
**self.extra
)
query.aggregates[alias] = aggregate


class PGPPublicKeyAggregate(PGPPublicKeySQL, EncryptionBase):
class PGPPublicKeyAggregate(PGPPublicKeySQL, Aggregate):
"""PGP public key based aggregation.
`pgp_pub_encrypt` and `dearmor` are pgcrypto functions which encrypt
Expand All @@ -74,7 +55,7 @@ class PGPPublicKeyAggregate(PGPPublicKeySQL, EncryptionBase):
name = 'decrypted'


class PGPSymmetricKeyAggregate(PGPSymmetricKeySQL, EncryptionBase):
class PGPSymmetricKeyAggregate(PGPSymmetricKeySQL, Aggregate):
"""PGP symmetric key based aggregation.
`pgp_sym_encrypt` is a pgcrypto functions, encrypts the field's value
Expand All @@ -83,7 +64,7 @@ class PGPSymmetricKeyAggregate(PGPSymmetricKeySQL, EncryptionBase):
name = 'decrypted'


class DatePGPPublicKeyAggregate(EncryptionBase):
class DatePGPPublicKeyAggregate(Aggregate):
"""PGP public key based aggregation.
`pgp_sym_encrypt` is a pgcrypto functions, encrypts the field's value
Expand All @@ -97,7 +78,7 @@ class DatePGPPublicKeyAggregate(EncryptionBase):
)


class DatePGPSymmetricKeyAggregate(EncryptionBase):
class DatePGPSymmetricKeyAggregate(Aggregate):
"""PGP symmetric key based aggregation.
`pgp_sym_encrypt` is a pgcrypto functions, encrypts the field's value
Expand All @@ -111,7 +92,7 @@ class DatePGPSymmetricKeyAggregate(EncryptionBase):
)


class DateTimePGPPublicKeyAggregate(EncryptionBase):
class DateTimePGPPublicKeyAggregate(Aggregate):
"""PGP public key based aggregation.
`pgp_sym_encrypt` is a pgcrypto functions, encrypts the field's value
Expand All @@ -125,7 +106,7 @@ class DateTimePGPPublicKeyAggregate(EncryptionBase):
)


class DateTimePGPSymmetricKeyAggregate(EncryptionBase):
class DateTimePGPSymmetricKeyAggregate(Aggregate):
"""PGP symmetric key based aggregation.
`pgp_sym_encrypt` is a pgcrypto functions, encrypts the field's value
Expand Down
99 changes: 33 additions & 66 deletions pgcrypto/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,15 @@
HMAC_SQL,
INTEGER_PGP_PUB_ENCRYPT_SQL,
INTEGER_PGP_SYM_ENCRYPT_SQL,
PGP_PUB_ENCRYPT_SQL,
PGP_SYM_ENCRYPT_SQL,
)
from pgcrypto.lookups import (
DatePGPPublicKeyEXACT,
DatePGPPublicKeyGT,
DatePGPPublicKeyGTE,
DatePGPPublicKeyLT,
DatePGPPublicKeyLTE,
DatePGPPublicKeyRANGE,
DatePGPSymmetricKeyEXACT,
DatePGPSymmetricKeyGT,
DatePGPSymmetricKeyGTE,
DatePGPSymmetricKeyLT,
DatePGPSymmetricKeyLTE,
DatePGPSymmetricKeyRANGE,
DateTimePGPPublicKeyEXACT,
DateTimePGPPublicKeyGT,
DateTimePGPPublicKeyGTE,
DateTimePGPPublicKeyLT,
DateTimePGPPublicKeyLTE,
DateTimePGPPublicKeyRANGE,
DateTimePGPSymmetricKeyEXACT,
DateTimePGPSymmetricKeyGT,
DateTimePGPSymmetricKeyGTE,
DateTimePGPSymmetricKeyLT,
DateTimePGPSymmetricKeyLTE,
DateTimePGPSymmetricKeyRANGE,
DigestLookup,
HMACLookup,
DateTimeExactLookup,
DateTimeGteLookup,
DateTimeGtLookup,
DateTimeLteLookup,
DateTimeLtLookup,
DateTimeRangeLookup,
HashLookup,
)
from pgcrypto.mixins import (
DatePGPPublicKeyFieldMixin,
Expand All @@ -54,20 +33,19 @@ class TextDigestField(HashMixin, models.TextField):
encrypt_sql = DIGEST_SQL


TextDigestField.register_lookup(DigestLookup)
TextDigestField.register_lookup(HashLookup)


class TextHMACField(HashMixin, models.TextField):
"""Text HMAC field for postgres."""
encrypt_sql = HMAC_SQL


TextHMACField.register_lookup(HMACLookup)
TextHMACField.register_lookup(HashLookup)


class EmailPGPPublicKeyField(EmailPGPPublicKeyFieldMixin, models.EmailField):
"""Email PGP public key encrypted field."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL


class IntegerPGPPublicKeyField(PGPPublicKeyFieldMixin, models.IntegerField):
Expand All @@ -77,40 +55,34 @@ class IntegerPGPPublicKeyField(PGPPublicKeyFieldMixin, models.IntegerField):

class TextPGPPublicKeyField(PGPPublicKeyFieldMixin, models.TextField):
"""Text PGP public key encrypted field."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL


class DatePGPPublicKeyField(DatePGPPublicKeyFieldMixin, models.TextField):
"""Date PGP public key encrypted field for postgres."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL
cast_sql = 'cast(%s as DATE)'


DatePGPPublicKeyField.register_lookup(DatePGPPublicKeyEXACT)
DatePGPPublicKeyField.register_lookup(DatePGPPublicKeyGT)
DatePGPPublicKeyField.register_lookup(DatePGPPublicKeyGTE)
DatePGPPublicKeyField.register_lookup(DatePGPPublicKeyLT)
DatePGPPublicKeyField.register_lookup(DatePGPPublicKeyLTE)
DatePGPPublicKeyField.register_lookup(DatePGPPublicKeyRANGE)
DatePGPPublicKeyField.register_lookup(DateTimeExactLookup)
DatePGPPublicKeyField.register_lookup(DateTimeLtLookup)
DatePGPPublicKeyField.register_lookup(DateTimeLteLookup)
DatePGPPublicKeyField.register_lookup(DateTimeGtLookup)
DatePGPPublicKeyField.register_lookup(DateTimeGteLookup)
DatePGPPublicKeyField.register_lookup(DateTimeRangeLookup)


class DateTimePGPPublicKeyField(DateTimePGPPublicKeyFieldMixin, models.TextField):
"""DateTime PGP public key encrypted field for postgres."""
encrypt_sql = PGP_PUB_ENCRYPT_SQL
cast_sql = 'cast(%s as TIMESTAMP)'


DateTimePGPPublicKeyField.register_lookup(DateTimePGPPublicKeyEXACT)
DateTimePGPPublicKeyField.register_lookup(DateTimePGPPublicKeyGT)
DateTimePGPPublicKeyField.register_lookup(DateTimePGPPublicKeyGTE)
DateTimePGPPublicKeyField.register_lookup(DateTimePGPPublicKeyLT)
DateTimePGPPublicKeyField.register_lookup(DateTimePGPPublicKeyLTE)
DateTimePGPPublicKeyField.register_lookup(DateTimePGPPublicKeyRANGE)
DateTimePGPPublicKeyField.register_lookup(DateTimeExactLookup)
DateTimePGPPublicKeyField.register_lookup(DateTimeLtLookup)
DateTimePGPPublicKeyField.register_lookup(DateTimeLteLookup)
DateTimePGPPublicKeyField.register_lookup(DateTimeGtLookup)
DateTimePGPPublicKeyField.register_lookup(DateTimeGteLookup)
DateTimePGPPublicKeyField.register_lookup(DateTimeRangeLookup)


class EmailPGPSymmetricKeyField(EmailPGPSymmetricKeyFieldMixin, models.EmailField):
"""Email PGP symmetric key encrypted field."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL


class IntegerPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.IntegerField):
Expand All @@ -120,32 +92,27 @@ class IntegerPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.IntegerField

class TextPGPSymmetricKeyField(PGPSymmetricKeyFieldMixin, models.TextField):
"""Text PGP symmetric key encrypted field for postgres."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL


class DatePGPSymmetricKeyField(DatePGPSymmetricKeyFieldMixin, models.TextField):
"""Date PGP symmetric key encrypted field for postgres."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL
cast_sql = 'cast(%s as DATE)'


DatePGPSymmetricKeyField.register_lookup(DatePGPSymmetricKeyEXACT)
DatePGPSymmetricKeyField.register_lookup(DatePGPSymmetricKeyGT)
DatePGPSymmetricKeyField.register_lookup(DatePGPSymmetricKeyGTE)
DatePGPSymmetricKeyField.register_lookup(DatePGPSymmetricKeyLT)
DatePGPSymmetricKeyField.register_lookup(DatePGPSymmetricKeyLTE)
DatePGPSymmetricKeyField.register_lookup(DatePGPSymmetricKeyRANGE)
DatePGPSymmetricKeyField.register_lookup(DateTimeExactLookup)
DatePGPSymmetricKeyField.register_lookup(DateTimeLtLookup)
DatePGPSymmetricKeyField.register_lookup(DateTimeLteLookup)
DatePGPSymmetricKeyField.register_lookup(DateTimeGtLookup)
DatePGPSymmetricKeyField.register_lookup(DateTimeGteLookup)
DatePGPSymmetricKeyField.register_lookup(DateTimeRangeLookup)


class DateTimePGPSymmetricKeyField(DateTimePGPSymmetricKeyFieldMixin, models.TextField):
"""DateTime PGP symmetric key encrypted field for postgres."""
encrypt_sql = PGP_SYM_ENCRYPT_SQL
cast_sql = 'cast(%s as TIMESTAMP)'


DateTimePGPSymmetricKeyField.register_lookup(DateTimePGPSymmetricKeyEXACT)
DateTimePGPSymmetricKeyField.register_lookup(DateTimePGPSymmetricKeyGT)
DateTimePGPSymmetricKeyField.register_lookup(DateTimePGPSymmetricKeyGTE)
DateTimePGPSymmetricKeyField.register_lookup(DateTimePGPSymmetricKeyLT)
DateTimePGPSymmetricKeyField.register_lookup(DateTimePGPSymmetricKeyLTE)
DateTimePGPSymmetricKeyField.register_lookup(DateTimePGPSymmetricKeyRANGE)
DateTimePGPSymmetricKeyField.register_lookup(DateTimeExactLookup)
DateTimePGPSymmetricKeyField.register_lookup(DateTimeLtLookup)
DateTimePGPSymmetricKeyField.register_lookup(DateTimeLteLookup)
DateTimePGPSymmetricKeyField.register_lookup(DateTimeGtLookup)
DateTimePGPSymmetricKeyField.register_lookup(DateTimeGteLookup)
DateTimePGPSymmetricKeyField.register_lookup(DateTimeRangeLookup)
Loading

0 comments on commit aef1c4b

Please sign in to comment.