Skip to content

Commit

Permalink
Merge pull request #3 from incuna/remove-django-pgcrypto-fields
Browse files Browse the repository at this point in the history
Remove django-pgcrypto-fields.
  • Loading branch information
todd-dembrey authored Mar 2, 2017
2 parents 26be183 + c8436c5 commit 784442e
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 102 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ Logging for changes made to Django model instances.

Add `model_logging` to your `INSTALLED_APPS`.

This library uses [`django-pgcrypto-fields`](https://github.com/incuna/django-pgcrypto-fields),
which means you will need PGP public and private keys. See the `settings.configure()`
block in `model_logging/tests/run.py` for an (extremely insecure) example.

## Usage

#### Low-level use
Expand Down
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

Upcoming

* BREAKING CHANGE: Remove django-pgcrypto-fields.

### v0.1.0

* Add LogEntry model, together with a supporting serializer and view mixins.
17 changes: 15 additions & 2 deletions model_logging/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import pgcrypto.fields
from unittest.mock import Mock
import logging

"""
The assumption is that a user who is upgrading from 0.1.0 to this version will already have pgcrypto installed. We also check this in 0003 so that they have a verbose error message.
A new user shouldn't migrate through 0001 and should go through the squashed migrations. Hence we pretend that we have a pgcrypto object to allow the migration parsing at startup to be carried out.
"""
try:
import pgcrypto.fields
except ImportError:
logging.warning('Since pgcrypto is not installed. We are now mocking it.')
pgcrypto = Mock()

from django.conf import settings
import django.utils.timezone

Expand All @@ -13,6 +25,7 @@ class Migration(migrations.Migration):
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]


operations = [
migrations.CreateModel(
name='LogEntry',
Expand Down
35 changes: 35 additions & 0 deletions model_logging/migrations/0001_squashed_0004_remove_old_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import django.utils.timezone
from django.conf import settings


class Migration(migrations.Migration):

replaces = [('model_logging', '0001_initial'), ('model_logging', '0002_add_new_data_field'), ('model_logging', '0003_data_migration'), ('model_logging', '0004_remove_old_field')]

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='LogEntry',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, verbose_name='ID', serialize=False)),
('date_created', models.DateTimeField(default=django.utils.timezone.now)),
('operation', models.CharField(max_length=255, choices=[('added', 'Added'), ('removed', 'Removed'), ('modified', 'Modified')])),
('model_path', models.CharField(max_length=255)),
('data', models.TextField(default='')),
('creator', models.ForeignKey(related_name='log_entries_created', to=settings.AUTH_USER_MODEL, null=True)),
('user', models.ForeignKey(related_name='log_entries', to=settings.AUTH_USER_MODEL, null=True)),
],
options={
'ordering': ('date_created',),
},
),
]
21 changes: 21 additions & 0 deletions model_logging/migrations/0002_add_new_data_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('model_logging', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='LogEntry',
name='data_temp',
field=models.TextField(default=''),
),
]
38 changes: 38 additions & 0 deletions model_logging/migrations/0003_data_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
from django.db import migrations

app = 'model_logging'
model = 'LogEntry'


def move_data(apps, schema_editor):
try:
from pgcrypto.fields import TextPGPPublicKeyField
except ImportError:
raise ImportError('Please install django-pgcrypto-fields to perform migration')

LogEntry = apps.get_model(app, model)
for entry in LogEntry.objects.all():
entry.data_temp = entry.data
entry.save()

def move_data_back(apps, schema_editor):
try:
from pgcrypto.fields import TextPGPPublicKeyField
except ImportError:
raise ImportError('Please install django-pgcrypto-fields to perform migration')

LogEntry = apps.get_model(app, model)
for entry in LogEntry.objects.all():
entry.data = entry.data_temp
entry.save()

class Migration(migrations.Migration):

dependencies = [
('model_logging', '0002_add_new_data_field'),
]

operations = [
migrations.RunPython(move_data, reverse_code=move_data_back),
]
25 changes: 25 additions & 0 deletions model_logging/migrations/0004_remove_old_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('model_logging', '0003_data_migration'),
]

operations = [
migrations.RemoveField(
model_name='LogEntry',
name='data',
),
migrations.RenameField(
model_name='LogEntry',
old_name='data_temp',
new_name='data',
),
]
3 changes: 1 addition & 2 deletions model_logging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from pgcrypto.fields import TextPGPPublicKeyField


def get_model_path(model):
Expand Down Expand Up @@ -66,7 +65,7 @@ class LogEntry(models.Model):

operation = models.CharField(choices=OPERATION_CHOICES, max_length=255)
model_path = models.CharField(max_length=255)
data = TextPGPPublicKeyField(default='')
data = models.TextField(default='')
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=True,
Expand Down
57 changes: 0 additions & 57 deletions model_logging/tests/keys/private.key

This file was deleted.

30 changes: 0 additions & 30 deletions model_logging/tests/keys/public.key

This file was deleted.

6 changes: 0 additions & 6 deletions model_logging/tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

INSTALLED_APPS=(
'model_logging',
'pgcrypto',

'django',
'django.contrib.admin',
Expand All @@ -30,11 +29,6 @@
'django.contrib.sessions',
'django.contrib.sites',
),

# Dummy pgcrypto configuration
PGCRYPTO_KEY='so_secure_you_guys',
PUBLIC_PGP_KEY=open('model_logging/tests/keys/public.key').read(),
PRIVATE_PGP_KEY=open('model_logging/tests/keys/private.key').read(),
)


Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


install_requires={
'django-pgcrypto-fields>=1.0.0,<2.0.0',
'simplejson>=3.8.0,<4.0.0',
'djangorestframework>=3.2.4,<4.0.0',
}
Expand Down

0 comments on commit 784442e

Please sign in to comment.