Skip to content

Commit

Permalink
Resolves #1138 Add last_updated for tables (#1705)
Browse files Browse the repository at this point in the history
* Add creation and update fields for audit log

* Resolve migration conflicts and fix tests

* Update migration dependencies after rebase

* Update migration test for compatibility
  • Loading branch information
amplifi authored Sep 6, 2017
1 parent 8ff94b5 commit 647dec2
Show file tree
Hide file tree
Showing 15 changed files with 542 additions and 3 deletions.
38 changes: 38 additions & 0 deletions cadasta/accounts/migrations/0009_add_audit_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-05-23 16:27
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0008_add_history_change_reason'),
]

operations = [
migrations.AddField(
model_name='historicaluser',
name='created_date',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='historicaluser',
name='last_updated',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='user',
name='created_date',
field=models.DateTimeField(auto_now_add=True, default='2016-01-01 00:00+00'),
preserve_default=False,
),
migrations.AddField(
model_name='user',
name='last_updated',
field=models.DateTimeField(auto_now=True),
),
]
4 changes: 4 additions & 0 deletions cadasta/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class User(auth_base.AbstractBaseUser, auth.PermissionsMixin):

objects = UserManager()

# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)

history = HistoricalRecords()

USERNAME_FIELD = 'username'
Expand Down
85 changes: 85 additions & 0 deletions cadasta/organization/migrations/0008_add_audit_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-08-08 13:43
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('organization', '0007_add_history_change_reason'),
]

operations = [
migrations.AddField(
model_name='historicalorganization',
name='created_date',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='historicalorganizationrole',
name='created_date',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='historicalorganizationrole',
name='last_updated',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='historicalproject',
name='created_date',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='historicalprojectrole',
name='created_date',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='historicalprojectrole',
name='last_updated',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='organization',
name='created_date',
field=models.DateTimeField(auto_now_add=True, default='2016-01-01 00:00+00'),
preserve_default=False,
),
migrations.AddField(
model_name='organizationrole',
name='created_date',
field=models.DateTimeField(auto_now_add=True, default='2016-01-01 00:00+00'),
preserve_default=False,
),
migrations.AddField(
model_name='organizationrole',
name='last_updated',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='project',
name='created_date',
field=models.DateTimeField(auto_now_add=True, default='2016-01-01 00:00+00'),
preserve_default=False,
),
migrations.AddField(
model_name='projectrole',
name='created_date',
field=models.DateTimeField(auto_now_add=True, default='2016-01-01 00:00+00'),
preserve_default=False,
),
migrations.AddField(
model_name='projectrole',
name='last_updated',
field=models.DateTimeField(auto_now=True),
),
]
16 changes: 16 additions & 0 deletions cadasta/organization/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ class Organization(SlugModel, RandomIDModel):
# TEMPORARY:
logo = models.URLField(null=True)
# logo = TemporalForeignKey('Resource')

# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)

access = models.CharField(
default="public", choices=ACCESS_CHOICES, max_length=8
)
Expand Down Expand Up @@ -117,6 +121,10 @@ class OrganizationRole(RandomIDModel):
user = models.ForeignKey('accounts.User')
admin = models.BooleanField(default=False)

# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)

history = HistoricalRecords()

class Meta:
Expand Down Expand Up @@ -188,6 +196,10 @@ class Project(ResourceModelMixin, SlugModel, RandomIDModel):
)
area = models.FloatField(default=0)

# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)

history = HistoricalRecords()

class Meta:
Expand Down Expand Up @@ -319,6 +331,10 @@ class ProjectRole(RandomIDModel):

history = HistoricalRecords()

# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)

class Meta:
unique_together = ('project', 'user')

Expand Down
10 changes: 7 additions & 3 deletions cadasta/organization/tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _get_apps_for_migration(self, migration_states):
def test_migration(self):
apps_before = self._get_apps_for_migration([
('organization', self.migrate_from),
('spatial', '0005_recalculate_area')])
('spatial', '0009_add_audit_fields')])
Organization = apps_before.get_model('organization', 'Organization')
Project = apps_before.get_model('organization', 'Project')
SpatialUnit = apps_before.get_model('spatial', 'SpatialUnit')
Expand All @@ -41,15 +41,19 @@ def test_migration(self):
type='PA',
geometry='POLYGON((12.323006 51.327645,12.322913 '
'51.327355,12.323114 51.327330,12.323189 '
'51.327624,12.323006 51.327645))')
'51.327624,12.323006 51.327645))',
created_date='2016-01-01 00:00+00',
last_updated='2016-01-01 00:00+00')
su1.refresh_from_db()
su2 = SpatialUnit.objects.create(
id='def',
project=project,
type='PA',
geometry='POLYGON((12.323041 51.32775,12.323012 '
'51.327661,12.323197 51.327638,12.323224 '
'51.327727,12.323041 51.32775))')
'51.327727,12.323041 51.32775))',
created_date='2016-01-01 00:00+00',
last_updated='2016-01-01 00:00+00')
su2.refresh_from_db()

call_command('migrate', 'organization', self.migrate_to)
Expand Down
84 changes: 84 additions & 0 deletions cadasta/party/migrations/0005_add_audit_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-05-23 16:27
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('party', '0004_add_history_change_reason'),
]

operations = [
migrations.AddField(
model_name='historicalparty',
name='created_date',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='historicalparty',
name='last_updated',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='historicalpartyrelationship',
name='created_date',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='historicalpartyrelationship',
name='last_updated',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='historicaltenurerelationship',
name='created_date',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='historicaltenurerelationship',
name='last_updated',
field=models.DateTimeField(blank=True, default='2016-01-01 00:00+00', editable=False),
preserve_default=False,
),
migrations.AddField(
model_name='party',
name='created_date',
field=models.DateTimeField(auto_now_add=True, default='2016-01-01 00:00+00'),
preserve_default=False,
),
migrations.AddField(
model_name='party',
name='last_updated',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='partyrelationship',
name='created_date',
field=models.DateTimeField(auto_now_add=True, default='2016-01-01 00:00+00'),
preserve_default=False,
),
migrations.AddField(
model_name='partyrelationship',
name='last_updated',
field=models.DateTimeField(auto_now=True),
),
migrations.AddField(
model_name='tenurerelationship',
name='created_date',
field=models.DateTimeField(auto_now_add=True, default='2016-01-01 00:00+00'),
preserve_default=False,
),
migrations.AddField(
model_name='tenurerelationship',
name='last_updated',
field=models.DateTimeField(auto_now=True),
),
]
12 changes: 12 additions & 0 deletions cadasta/party/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class Party(ResourceModelMixin, RandomIDModel):
related_name='tenure_relationships'
)

# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)

history = HistoricalRecords()

class Meta:
Expand Down Expand Up @@ -177,6 +181,10 @@ class PartyRelationship(RandomIDModel):

objects = managers.PartyRelationshipManager()

# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)

history = HistoricalRecords()

class TutelaryMeta:
Expand Down Expand Up @@ -243,6 +251,10 @@ class TenureRelationship(ResourceModelMixin,
attributes = JSONAttributeField(default={})
objects = managers.TenureRelationshipManager()

# Audit history
created_date = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)

history = HistoricalRecords()

class TutelaryMeta:
Expand Down
Loading

0 comments on commit 647dec2

Please sign in to comment.