Added version history to models with the use of a mixin

This commit is contained in:
Tom Price
2014-11-04 03:27:32 +00:00
parent df63d8d0de
commit e59fd8f7a7
4 changed files with 29 additions and 7 deletions

View File

@@ -7,7 +7,9 @@ django.setup()
from django.db import connections from django.db import connections
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.db.utils import ConnectionDoesNotExist from django.db.utils import ConnectionDoesNotExist
from django.db import transaction
from RIGS import models from RIGS import models
import reversion
def setup_cursor(): def setup_cursor():
try: try:
@@ -40,6 +42,8 @@ def import_people():
person, created = models.Person.objects.get_or_create(pk=row[0], name=row[1], phone=row[2], email=email, address=row[4], notes=notes) person, created = models.Person.objects.get_or_create(pk=row[0], name=row[1], phone=row[2], email=email, address=row[4], notes=notes)
if created: if created:
print("Created: " + person.__unicode__()) print("Created: " + person.__unicode__())
with transaction.atomic(), reversion.create_revision():
person.save()
else: else:
print("Found: " + person.__unicode__()) print("Found: " + person.__unicode__())

View File

@@ -16,8 +16,20 @@ class Profile(AbstractUser):
url = "https://www.gravatar.com/avatar/" + hashlib.md5(self.email).hexdigest() + "?d=identicon&s=500" url = "https://www.gravatar.com/avatar/" + hashlib.md5(self.email).hexdigest() + "?d=identicon&s=500"
return url return url
class RevisionMixin(object):
@property
def last_edited_at(self):
version = reversion.get_for_object(self)[0]
return version.revision.date_created
@property
def last_edited_by(self):
version = reversion.get_for_object(self)[0]
return version.revision.user
@reversion.register @reversion.register
class Person(models.Model): class Person(models.Model, RevisionMixin):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
phone = models.CharField(max_length=15, blank=True, null=True) phone = models.CharField(max_length=15, blank=True, null=True)
email = models.EmailField(blank=True, null=True) email = models.EmailField(blank=True, null=True)
@@ -30,4 +42,5 @@ class Person(models.Model):
string = self.name string = self.name
if len(self.notes) > 0: if len(self.notes) > 0:
string += "*" string += "*"
return string return string

View File

@@ -7,7 +7,6 @@
<div class="row"> <div class="row">
{% if not request.is_ajax %} {% if not request.is_ajax %}
<h3>{{ object.name }}</h3> <h3>{{ object.name }}</h3>
<div class="pull-right"> <div class="pull-right">
<a href="{% url 'person_update' object.pk %}" class="btn btn-primary">Edit <span <a href="{% url 'person_update' object.pk %}" class="btn btn-primary">Edit <span
class="glyphicon glyphicon-pencil"></span></a> class="glyphicon glyphicon-pencil"></span></a>
@@ -38,9 +37,16 @@
{% if request.is_ajax %} {% if request.is_ajax %}
{% block footer %} {% block footer %}
<div class="pull-right"> <div class="row">
<a href="{% url 'person_update' object.pk %}" class="btn btn-primary">Edit <span <div class="col-sm-10 align-left">
class="glyphicon glyphicon-pencil"></span></a> Lasted edited at {{ object.last_edited_at|date:"SHORT_DATE_FORMAT" }} by {{ user }}
</div>
<div class="col-sm-2">
<div class="pull-right">
<a href="{% url 'person_update' object.pk %}" class="btn btn-primary">Edit <span
class="glyphicon glyphicon-pencil"></span></a>
</div>
</div>
</div> </div>
{% endblock %} {% endblock %}
{% endif %} {% endif %}

View File

@@ -59,5 +59,4 @@ def paginator(context, adjacent_pages=3):
dict['previous'] = page.previous_page_number() dict['previous'] = page.previous_page_number()
return dict return dict
register.inclusion_tag('pagination.html', takes_context=True)(paginator) register.inclusion_tag('pagination.html', takes_context=True)(paginator)