mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-17 13:32:15 +00:00
Start to seperate versioning into its own app
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
__author__ = 'ghost'
|
||||
|
||||
import unittest
|
||||
from importer import fix_email
|
||||
|
||||
|
||||
class EmailFixerTest(unittest.TestCase):
|
||||
def test_correct(self):
|
||||
e = fix_email("tom@ghost.uk.net")
|
||||
self.assertEqual(e, "tom@ghost.uk.net")
|
||||
|
||||
def test_partial(self):
|
||||
e = fix_email("psytp")
|
||||
self.assertEqual(e, "psytp@nottingham.ac.uk")
|
||||
|
||||
def test_none(self):
|
||||
old = None
|
||||
new = fix_email(old)
|
||||
self.assertEqual(old, new)
|
||||
|
||||
def test_empty(self):
|
||||
old = ""
|
||||
new = fix_email(old)
|
||||
self.assertEqual(old, new)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -1,50 +0,0 @@
|
||||
{% load static %}
|
||||
|
||||
{% block js %}
|
||||
<script src="{% static 'js/tooltip.js' %}"></script>
|
||||
<script src="{% static 'js/popover.js' %}"></script>
|
||||
<script>
|
||||
$(function () {
|
||||
$('[data-toggle="tooltip"]').tooltip();
|
||||
})
|
||||
</script>
|
||||
<script>
|
||||
$(function () {
|
||||
$('[data-toggle="popover"]').popover().click(function(){
|
||||
if($(this).attr('href')){
|
||||
window.location.href = $(this).attr('href');
|
||||
}
|
||||
});
|
||||
})
|
||||
$(document).ready(function() {
|
||||
$(function () {
|
||||
$( "#activity" ).hide();
|
||||
$( "#activity" ).load( "{% url 'activity_feed' %}", function() {
|
||||
$('#activity_loading').slideUp('slow',function(){
|
||||
$('#activity').slideDown('slow');
|
||||
});
|
||||
var whiteList = $.fn.tooltip.Constructor.Default.whiteList
|
||||
whiteList.ins = []
|
||||
whiteList.del = []
|
||||
$('#activity [data-toggle="popover"]').popover({whiteList: whiteList});
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h4>Recent Changes</h4>
|
||||
</div>
|
||||
<div class="list-group list-group-flush">
|
||||
<div id="activity_loading" class="list-group-item text-center">
|
||||
<div class="spinner-border text-primary" role="status">
|
||||
<span class="sr-only">Loading...</span>
|
||||
</div>
|
||||
</div>
|
||||
<div id="activity">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
{% extends request.is_ajax|yesno:"base_ajax_nomodal.html,base_rigs.html" %}
|
||||
|
||||
{% load static %}
|
||||
{% load humanize %}
|
||||
{% load paginator from filters %}
|
||||
{% load to_class_name from filters %}
|
||||
|
||||
{% block content %}
|
||||
<div class="list-group-item">
|
||||
<div class="media">
|
||||
{% for version in object_list %}
|
||||
{% if not version.withPrevious %}
|
||||
{% if not forloop.first %}
|
||||
</div> {#/.media-body#}
|
||||
</div> {#/.media#}
|
||||
</div>
|
||||
<div class="list-group-item">
|
||||
<div class="media">
|
||||
{% endif %}
|
||||
<div class="align-self-start mr-3">
|
||||
{% if version.revision.user %}
|
||||
<a href="{% url 'profile_detail' pk=version.revision.user.pk %}" class="modal-href">
|
||||
<img class="media-object img-rounded" src="{{ version.revision.user.profile_picture}}" />
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="media-body">
|
||||
<h5>
|
||||
{{ version.revision.user.name }}
|
||||
<span class="ml-3"><small>{{version.revision.date_created|naturaltime}}</small></span>
|
||||
</h5>
|
||||
{% endif %}
|
||||
<p>
|
||||
<small>
|
||||
{% if version.changes.old == None %}
|
||||
Created
|
||||
{% else %}
|
||||
Changed {% include 'version_changes.html' %} in
|
||||
{% endif %}
|
||||
|
||||
{% include 'object_button.html' with object=version.changes.new %}
|
||||
{% if version.revision.comment %}
|
||||
({{ version.revision.comment }})
|
||||
{% endif %}
|
||||
</small>
|
||||
</p>
|
||||
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
@@ -1,71 +0,0 @@
|
||||
{% extends request.is_ajax|yesno:"base_ajax.html,base_rigs.html" %}
|
||||
{% load static %}
|
||||
{% load paginator from filters %}
|
||||
{% load to_class_name from filters %}
|
||||
|
||||
{% block title %}Rigboard Activity Stream{% endblock %}
|
||||
|
||||
{% block js %}
|
||||
<script src="{% static "js/tooltip.js" %}"></script>
|
||||
<script src="{% static "js/popover.js" %}"></script>
|
||||
<script src="{% static "js/moment.js" %}"></script>
|
||||
<script>
|
||||
$(function () {
|
||||
$('[data-toggle="popover"]').popover().click(function(){
|
||||
if($(this).attr('href')){
|
||||
window.location.href = $(this).attr('href');
|
||||
}
|
||||
});
|
||||
$('.date').each(function (index, dateElem) {
|
||||
var $dateElem = $(dateElem);
|
||||
var formatted = moment($dateElem.attr('data-date')).fromNow();
|
||||
$dateElem.text(formatted);
|
||||
});
|
||||
})
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="col-sm-12">
|
||||
<div class="row">
|
||||
<div class="col-sm-12">
|
||||
<h3>Rigboard Activity Stream</h3>
|
||||
</div>
|
||||
</div>
|
||||
{% paginator %}
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<td>Date</td>
|
||||
<td>Object</td>
|
||||
<td>Version ID</td>
|
||||
<td>User</td>
|
||||
<td>Changes</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for version in object_list %}
|
||||
|
||||
<tr>
|
||||
<td>{{ version.revision.date_created }}</td>
|
||||
<td><a href="{{ version.changes.new.get_absolute_url }}">{{version.changes.new|to_class_name}} {{ version.changes.new.pk|stringformat:"05d" }}</a></td>
|
||||
<td>{{ version.pk }}|{{ version.revision.pk }}</td>
|
||||
<td>{{ version.revision.user.name }}</td>
|
||||
<td>
|
||||
{% if version.changes.old == None %}
|
||||
{{version.changes.new|to_class_name}} Created
|
||||
{% else %}
|
||||
{% include 'version_changes.html' %}
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
</div>
|
||||
{% paginator %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -1,19 +0,0 @@
|
||||
{% if version.changes.item_changes or version.changes.field_changes or version.changes.old == None %}
|
||||
{% for change in version.changes.field_changes %}
|
||||
<a tabindex="0" title="Changes to {{ change.field.verbose_name }}" type="button" class="badge badge-secondary" data-container="body" data-html="true" data-trigger='hover' data-toggle="popover" data-content='{% spaceless %}{% include "version_changes_change.html" %}{% endspaceless %}'>{{ change.field.verbose_name }}</a>
|
||||
{% endfor %}
|
||||
{% for itemChange in version.changes.item_changes %}
|
||||
<a tabindex="0" title="Changes to item '{% if itemChange.new %}{{ itemChange.new.name }}{% else %}{{ itemChange.old.name }}{% endif %}'" type="button" class="badge badge-secondary" data-container="body" data-html="true" data-trigger='hover' data-toggle="popover" data-content='{% spaceless %}
|
||||
<ul class="list-group">
|
||||
{% for change in itemChange.field_changes %}
|
||||
<li class="list-group-item">
|
||||
<h4 class="list-group-item-heading">{{ change.field.verbose_name }}</h4>
|
||||
<div class="dont-break-out">{% include 'version_changes_change.html' with change=itemChange %}</div>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% endspaceless %}'>item '{% if itemChange.new %}{{ itemChange.new.name }}{% else %}{{ itemChange.old.name }}{% endif %}'</a>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
nothing useful
|
||||
{% endif %}
|
||||
@@ -1,34 +0,0 @@
|
||||
{# pass in variable "change" to this template #}
|
||||
{% if change.linebreaks and change.new and change.old %}
|
||||
{% for diff in change.diff %}
|
||||
{% if diff.type == "insert" %}
|
||||
<ins class="dont-break-out">{{ diff.text|linebreaksbr }}</ins>
|
||||
{% elif diff.type == "delete" %}
|
||||
<del class="dont-break-out">{{diff.text|linebreaksbr}}</del>
|
||||
{% else %}
|
||||
<span class="dont-break-out">{{diff.text|linebreaksbr}}</span>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
{% if change.old %}
|
||||
<del{% if change.long %} class="overflow-ellipsis"{% endif %}>
|
||||
{% if change.linebreaks %}
|
||||
{{change.old|linebreaksbr}}
|
||||
{% else %}
|
||||
{{change.old}}
|
||||
{% endif %}
|
||||
</del>
|
||||
{% endif %}
|
||||
{% if change.new and change.old %}
|
||||
<br/>
|
||||
{% endif %}
|
||||
{% if change.new %}
|
||||
<ins{% if change.long %} class="overflow-ellipsis"{% endif %}>
|
||||
{% if change.linebreaks %}
|
||||
{{change.new|linebreaksbr}}
|
||||
{% else %}
|
||||
{{change.new}}
|
||||
{% endif %}
|
||||
</ins>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
@@ -1,60 +0,0 @@
|
||||
{% extends request.is_ajax|yesno:"base_ajax.html,base_rigs.html" %}
|
||||
{% load to_class_name from filters %}
|
||||
{% load paginator from filters %}
|
||||
{% load static %}
|
||||
|
||||
{% block title %}{{object|to_class_name}} {{ object.pk|stringformat:"05d" }} - Revision History{% endblock %}
|
||||
|
||||
{% block js %}
|
||||
<script src="{% static "js/tooltip.js" %}"></script>
|
||||
<script src="{% static "js/popover.js" %}"></script>
|
||||
<script>
|
||||
$(function () {
|
||||
$('[data-toggle="popover"]').popover().click(function(){
|
||||
if($(this).attr('href')){
|
||||
window.location.href = $(this).attr('href');
|
||||
}
|
||||
});
|
||||
})
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="col-sm-12">
|
||||
<div class="row">
|
||||
<div class="col-sm-12">
|
||||
<h3><a href="{{ object.get_absolute_url }}">{{object|to_class_name}} {{ object.pk|stringformat:"05d" }}</a> - Revision History</h3>
|
||||
</div>
|
||||
</div>
|
||||
<div>{% paginator %}</div>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<td>Date</td>
|
||||
<td>Version ID</td>
|
||||
<td>User</td>
|
||||
<td>Changes</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for version in object_list %}
|
||||
<tr>
|
||||
<td>{{ version.revision.date_created }}</td>
|
||||
<td>{{ version.pk }}|{{ version.revision.pk }}</td>
|
||||
<td>{{ version.revision.user.name }}</td>
|
||||
<td>
|
||||
{% if version.changes.old is None %}
|
||||
{{object|to_class_name}} Created
|
||||
{% else %}
|
||||
{% include 'version_changes.html' %}
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div>{% paginator %}</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -5,7 +5,8 @@ from django.views.decorators.clickjacking import xframe_options_exempt
|
||||
from django.views.generic import RedirectView
|
||||
from PyRIGS.decorators import (api_key_required, has_oembed,
|
||||
permission_required_with_403)
|
||||
from RIGS import finance, ical, models, rigboard, versioning, views
|
||||
from RIGS import finance, ical, models, rigboard, views
|
||||
from versioning import versioning
|
||||
|
||||
urlpatterns = [
|
||||
path('', login_required(views.Index.as_view()), name='index'),
|
||||
|
||||
@@ -1,257 +0,0 @@
|
||||
|
||||
|
||||
import logging
|
||||
import datetime
|
||||
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.views import generic
|
||||
from django.utils.functional import cached_property
|
||||
from django.db.models import IntegerField, EmailField, TextField
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
from reversion.models import Version, VersionQuerySet
|
||||
from diff_match_patch import diff_match_patch
|
||||
|
||||
from RIGS import models
|
||||
|
||||
logger = logging.getLogger('tec.pyrigs')
|
||||
|
||||
|
||||
class FieldComparison(object):
|
||||
def __init__(self, field=None, old=None, new=None):
|
||||
self.field = field
|
||||
self._old = old
|
||||
self._new = new
|
||||
|
||||
def display_value(self, value):
|
||||
if isinstance(self.field, IntegerField) and self.field.choices is not None and len(self.field.choices) > 0:
|
||||
return [x[1] for x in self.field.choices if x[0] == value][0]
|
||||
if self.field.name == "risk_assessment_edit_url":
|
||||
return "completed" if value else ""
|
||||
return value
|
||||
|
||||
@property
|
||||
def old(self):
|
||||
return self.display_value(self._old)
|
||||
|
||||
@property
|
||||
def new(self):
|
||||
return self.display_value(self._new)
|
||||
|
||||
@property
|
||||
def long(self):
|
||||
if isinstance(self.field, EmailField):
|
||||
return True
|
||||
return False
|
||||
|
||||
@property
|
||||
def linebreaks(self):
|
||||
if isinstance(self.field, TextField):
|
||||
return True
|
||||
return False
|
||||
|
||||
@property
|
||||
def diff(self):
|
||||
oldText = str(self.display_value(self._old)) or ""
|
||||
newText = str(self.display_value(self._new)) or ""
|
||||
dmp = diff_match_patch()
|
||||
diffs = dmp.diff_main(oldText, newText)
|
||||
dmp.diff_cleanupSemantic(diffs)
|
||||
|
||||
outputDiffs = []
|
||||
|
||||
for (op, data) in diffs:
|
||||
if op == dmp.DIFF_INSERT:
|
||||
outputDiffs.append({'type': 'insert', 'text': data})
|
||||
elif op == dmp.DIFF_DELETE:
|
||||
outputDiffs.append({'type': 'delete', 'text': data})
|
||||
elif op == dmp.DIFF_EQUAL:
|
||||
outputDiffs.append({'type': 'equal', 'text': data})
|
||||
return outputDiffs
|
||||
|
||||
|
||||
class ModelComparison(object):
|
||||
|
||||
def __init__(self, old=None, new=None, version=None, excluded_keys=[]):
|
||||
# recieves two objects of the same model, and compares them. Returns an array of FieldCompare objects
|
||||
try:
|
||||
self.fields = old._meta.get_fields()
|
||||
except AttributeError:
|
||||
self.fields = new._meta.get_fields()
|
||||
|
||||
self.old = old
|
||||
self.new = new
|
||||
self.excluded_keys = excluded_keys
|
||||
self.version = version
|
||||
|
||||
@cached_property
|
||||
def revision(self):
|
||||
return self.version.revision
|
||||
|
||||
@cached_property
|
||||
def field_changes(self):
|
||||
changes = []
|
||||
for field in self.fields:
|
||||
field_name = field.name
|
||||
|
||||
if field_name in self.excluded_keys:
|
||||
continue # if we're excluding this field, skip over it
|
||||
|
||||
try:
|
||||
oldValue = getattr(self.old, field_name, None)
|
||||
except ObjectDoesNotExist:
|
||||
oldValue = None
|
||||
|
||||
try:
|
||||
newValue = getattr(self.new, field_name, None)
|
||||
except ObjectDoesNotExist:
|
||||
newValue = None
|
||||
|
||||
bothBlank = (not oldValue) and (not newValue)
|
||||
if oldValue != newValue and not bothBlank:
|
||||
comparison = FieldComparison(field, oldValue, newValue)
|
||||
changes.append(comparison)
|
||||
|
||||
return changes
|
||||
|
||||
@cached_property
|
||||
def fields_changed(self):
|
||||
return len(self.field_changes) > 0
|
||||
|
||||
@cached_property
|
||||
def item_changes(self):
|
||||
# Recieves two event version objects and compares their items, returns an array of ItemCompare objects
|
||||
|
||||
item_type = ContentType.objects.get_for_model(models.EventItem)
|
||||
old_item_versions = self.version.parent.revision.version_set.filter(content_type=item_type)
|
||||
new_item_versions = self.version.revision.version_set.filter(content_type=item_type)
|
||||
|
||||
comparisonParams = {'excluded_keys': ['id', 'event', 'order']}
|
||||
|
||||
# Build some dicts of what we have
|
||||
item_dict = {} # build a list of items, key is the item_pk
|
||||
for version in old_item_versions: # put all the old versions in a list
|
||||
if version.field_dict["event_id"] == int(self.new.pk):
|
||||
compare = ModelComparison(old=version._object_version.object, **comparisonParams)
|
||||
item_dict[version.object_id] = compare
|
||||
|
||||
for version in new_item_versions: # go through the new versions
|
||||
if version.field_dict["event_id"] == int(self.new.pk):
|
||||
try:
|
||||
compare = item_dict[version.object_id] # see if there's a matching old version
|
||||
compare.new = version._object_version.object # then add the new version to the dictionary
|
||||
except KeyError: # there's no matching old version, so add this item to the dictionary by itself
|
||||
compare = ModelComparison(new=version._object_version.object, **comparisonParams)
|
||||
|
||||
item_dict[version.object_id] = compare # update the dictionary with the changes
|
||||
|
||||
changes = []
|
||||
for (_, compare) in list(item_dict.items()):
|
||||
if compare.fields_changed:
|
||||
changes.append(compare)
|
||||
|
||||
return changes
|
||||
|
||||
@cached_property
|
||||
def items_changed(self):
|
||||
return len(self.item_changes) > 0
|
||||
|
||||
@cached_property
|
||||
def anything_changed(self):
|
||||
return self.fields_changed or self.items_changed
|
||||
|
||||
|
||||
class RIGSVersionManager(VersionQuerySet):
|
||||
def get_for_multiple_models(self, model_array):
|
||||
content_types = []
|
||||
for model in model_array:
|
||||
content_types.append(ContentType.objects.get_for_model(model))
|
||||
|
||||
return self.filter(content_type__in=content_types).select_related("revision").order_by("-revision__date_created")
|
||||
|
||||
|
||||
class RIGSVersion(Version):
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
objects = RIGSVersionManager.as_manager()
|
||||
|
||||
@cached_property
|
||||
def parent(self):
|
||||
thisId = self.object_id
|
||||
|
||||
versions = RIGSVersion.objects.get_for_object_reference(self.content_type.model_class(), thisId).select_related("revision", "revision__user").all()
|
||||
|
||||
try:
|
||||
previousVersion = versions.filter(revision_id__lt=self.revision_id).latest('revision__date_created')
|
||||
except ObjectDoesNotExist:
|
||||
return False
|
||||
|
||||
return previousVersion
|
||||
|
||||
@cached_property
|
||||
def changes(self):
|
||||
return ModelComparison(
|
||||
version=self,
|
||||
new=self._object_version.object,
|
||||
old=self.parent._object_version.object if self.parent else None
|
||||
)
|
||||
|
||||
|
||||
class VersionHistory(generic.ListView):
|
||||
model = RIGSVersion
|
||||
template_name = "version_history.html"
|
||||
paginate_by = 25
|
||||
|
||||
def get_queryset(self, **kwargs):
|
||||
return RIGSVersion.objects.get_for_object(self.get_object()).select_related("revision", "revision__user").all().order_by("-revision__date_created")
|
||||
|
||||
def get_object(self, **kwargs):
|
||||
return get_object_or_404(self.kwargs['model'], pk=self.kwargs['pk'])
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(VersionHistory, self).get_context_data(**kwargs)
|
||||
context['object'] = self.get_object()
|
||||
|
||||
return context
|
||||
|
||||
|
||||
class ActivityTable(generic.ListView):
|
||||
model = RIGSVersion
|
||||
template_name = "activity_table.html"
|
||||
paginate_by = 25
|
||||
|
||||
def get_queryset(self):
|
||||
versions = RIGSVersion.objects.get_for_multiple_models([models.Event, models.Venue, models.Person, models.Organisation, models.EventAuthorisation])
|
||||
return versions.order_by("-revision__date_created")
|
||||
|
||||
|
||||
class ActivityFeed(generic.ListView):
|
||||
model = RIGSVersion
|
||||
template_name = "activity_feed_data.html"
|
||||
paginate_by = 25
|
||||
|
||||
def get_queryset(self):
|
||||
versions = RIGSVersion.objects.get_for_multiple_models([models.Event, models.Venue, models.Person, models.Organisation, models.EventAuthorisation])
|
||||
return versions.order_by("-revision__date_created")
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
# Call the base implementation first to get a context
|
||||
context = super(ActivityFeed, self).get_context_data(**kwargs)
|
||||
|
||||
maxTimeDelta = datetime.timedelta(hours=1)
|
||||
|
||||
items = []
|
||||
|
||||
for thisVersion in context['object_list']:
|
||||
thisVersion.withPrevious = False
|
||||
if len(items) >= 1:
|
||||
timeDiff = items[-1].revision.date_created - thisVersion.revision.date_created
|
||||
timeTogether = timeDiff < maxTimeDelta
|
||||
sameUser = thisVersion.revision.user_id == items[-1].revision.user_id
|
||||
thisVersion.withPrevious = timeTogether & sameUser
|
||||
|
||||
items.append(thisVersion)
|
||||
|
||||
return context
|
||||
Reference in New Issue
Block a user