Skip to content

Instantly share code, notes, and snippets.

@phoebebright
Last active August 15, 2016 14:01
Show Gist options
  • Save phoebebright/9097696 to your computer and use it in GitHub Desktop.
Save phoebebright/9097696 to your computer and use it in GitHub Desktop.
Add a little bar chart to the admin list to show number of updates taken from reversion data
add field 'updates' to the end of list_display
eg.
class MyAdmin(reversion.VersionAdmin):
class Meta:
model = MyModel
list_display = ('framework_panel', 'level_of_court','seniority', 'updates')
and add this to the same admin class:
def updates(self, obj):
return format_html('<div class="sparks" id="spark_{0}" data-content-type-id="{1}" data-object-id="{0}"></div>',
obj.id,
self.content_type)
updates.allow_tags = True
add this to admin/base_site.html
{% block extrahead %}
<style>
div.bar {
display: inline-block;
width: 5px;
height: 50px;
margin-right: 2px;
background-color: blueviolet;
padding-bottom: 0px;
}
div.sparks {
border-bottom: 1px solid;
border-bottom-color: cornflowerblue;
}
</style>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
(function($) {
$(document).ready(function() {
var updates = d3.selectAll(".sparks");
updates[0].forEach(function(spark) {
d3.json("/get_updates/"+spark.dataset.contentTypeId+"/"+spark.dataset.objectId+"/", function(error, json) {
d3.select("#"+spark.id)
.selectAll(".bar")
.data(json)
.enter()
.append("div")
.attr("class", "bar")
.style("height", function(d) {
var barHeight = d.updates * 10;
return barHeight + "px";
});
});
});
$(".sparks").on("click", function() {
document.location = this.baseURI + this.dataset.objectId+"/history/";
});
});
})(grp.jQuery);
</script>
{% endblock %}
point to your view:
url(r'^get_updates/(?P<content_type>\w+)/(?P<obj_id>\w+)/(?P<daysback>\w+)/', 'get_obj_updates'),
Create a function to extract the data in an appropriate place like views.py
from django.contrib.admin.views.decorators import staff_member_required
from django.views.decorators.cache import cache_control
from django.http import HttpResponse
from django.core.serializers.json import DjangoJSONEncoder
from datetime import timedelta
import json
from reversion.models import Revision, Version
@cache_control(no_cache=True)
@staff_member_required
def get_obj_updates(request, content_type, obj_id, daysback=30):
today = datetime.today()
fromdate = today - timedelta(days=daysback)
# for each date, create list of changes
items = Version.objects.filter(object_id_int=obj_id, content_type=content_type, revision__date_created__gt=fromdate).values('revision__date_created', 'revision__user__username', 'revision__comment').order_by('revision__date_created')
lastdate = items[0]['revision__date_created']
data = [{'updates': 0,
}]*daysback
change_list = []
count = 0
n = 0
num_items = len(items)
for item in items:
n += 1
if lastdate.date() != item['revision__date_created'].date():
data[(lastdate - fromdate).days] = {
'updates': count,
}
count = 0
change_list = []
count += 1
lastdate = item['revision__date_created']
if n==num_items:
data[(lastdate - fromdate).days] = {
'updates': count,
}
jsondata = json.dumps(data, cls=DjangoJSONEncoder)
return HttpResponse(jsondata, mimetype="application/json", content_type="application/json")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment