mirror of
https://github.com/nottinghamtec/PyRIGS.git
synced 2026-01-17 05:22:16 +00:00
Versioning module now does magic
Automatic creation of views/urls for anything registered with reversion, with a small amount of hackage to preserve legacy stuff. (and the DAMNED asset IDs!) I would never get distracted...
This commit is contained in:
@@ -22,11 +22,13 @@
|
||||
<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>
|
||||
{% else %}
|
||||
<img class="media-object img-rounded" src="{% static 'imgs/pyrigs-avatar.png' %}" />
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="media-body">
|
||||
<h5>
|
||||
{{ version.revision.user.name }}
|
||||
{{ version.revision.user.name|default:'System' }}
|
||||
<span class="ml-3"><small>{{version.revision.date_created|naturaltime}}</small></span>
|
||||
</h5>
|
||||
{% endif %}
|
||||
|
||||
@@ -2,15 +2,15 @@
|
||||
{% load paginator from filters %}
|
||||
{% load to_class_name from filters %}
|
||||
|
||||
{% block title %}{{ title }} Activity Stream{% endblock %}
|
||||
{% block title %}{{ title|title }} Activity Stream{% endblock %}
|
||||
|
||||
{% block js %}
|
||||
{% include 'partials/version_scripts.html' %}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="col-sm-12">
|
||||
<h3>{{ title }} Activity Stream</h3>
|
||||
<div class="col-12">
|
||||
<h3>{{ title|title }} Activity Stream</h3>
|
||||
{% include 'partials/activity_table_body.html' %}
|
||||
{% paginator %}
|
||||
</div>
|
||||
|
||||
@@ -14,20 +14,14 @@
|
||||
{% for version in object_list %}
|
||||
<tr>
|
||||
<th scope="row">{{ version.revision.date_created }}</th>
|
||||
<td><a href="{{ version.changes.new.get_absolute_url }}">{{version.changes.new|to_class_name}}
|
||||
{% if version.changes.new.asset_id %}
|
||||
{{ version.changes.new.asset_id }}
|
||||
{% else %}
|
||||
{{ version.changes.new.pk|stringformat:"05d" }}
|
||||
{% endif %}
|
||||
</a></td>
|
||||
<td><a href="{{ version.changes.new.get_absolute_url }}">{{ version.changes.new.display_id|default:version.changes.new.pk }} | {{version.changes.new|to_class_name}}</td>
|
||||
<td>{{ version.pk }}|{{ version.revision.pk }}</td>
|
||||
<td>{{ version.revision.user.name|default:"System" }}</td>
|
||||
<td>
|
||||
{% if version.changes.old == None %}
|
||||
{{version.changes.new|to_class_name}} Created
|
||||
{% else %}
|
||||
{% include 'version_changes.html' %}
|
||||
{% include 'partials/version_changes.html' %}
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
{% load to_class_name from filters %}
|
||||
{% load paginator from filters %}
|
||||
|
||||
{% block title %}{{object|to_class_name}} {{ id }} - Revision History{% endblock %}
|
||||
{% block title %}{{object|to_class_name}} {{ object.display_id|default:object.pk }} - Revision History{% endblock %}
|
||||
|
||||
{% block js %}
|
||||
{% include 'partials/version_scripts.html' %}
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
{% block content %}
|
||||
<div class="col-sm-12">
|
||||
<h3><a href="{{ object.get_absolute_url }}">{{object|to_class_name}} {{ id }}</a> - Revision History</h3>
|
||||
<h3><a href="{{ object.get_absolute_url }}">{{object|to_class_name}} {{ object.display_id|default:object.pk }}</a> - Revision History</h3>
|
||||
{% include 'partials/version_history_table.html' %}
|
||||
<div>{% paginator %}</div>
|
||||
</div>
|
||||
|
||||
34
versioning/urls.py
Normal file
34
versioning/urls.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from django.conf.urls import url
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.urls import path
|
||||
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, views, hs
|
||||
from versioning import views
|
||||
from django.views.decorators.cache import cache_page
|
||||
from django.apps import apps
|
||||
|
||||
urlpatterns = [
|
||||
path('rigboard/activity/feed/',
|
||||
cache_page(60 * 10)(permission_required_with_403('RIGS.view_event')(views.ActivityFeed.as_view())),
|
||||
name='activity_feed'),
|
||||
]
|
||||
|
||||
for app in apps.get_app_configs():
|
||||
appname = str(app.label)
|
||||
# Well except this specific hack for legacy URLs...if only the RIGS app had been named 'rigboard'!
|
||||
if appname == 'RIGS':
|
||||
appname = 'rigboard'
|
||||
urlpatterns += [path(appname + '/activity/', permission_required_with_403('RIGS.view_event')(views.ActivityTable.as_view()),
|
||||
name='activity_table', kwargs={'app': appname, 'models': views.get_models(app.label)}),]
|
||||
else:
|
||||
urlpatterns += [path(appname + '/activity/', permission_required_with_403('RIGS.view_event')(views.ActivityTable.as_view()),
|
||||
name=appname + '_activity_table', kwargs={'app': appname, 'models': views.get_models(app.label)}),]
|
||||
for model in views.get_models(app.label):
|
||||
modelname = model.__name__.lower()
|
||||
urlpatterns += [
|
||||
path(appname + '/' + modelname + '/<str:pk>/history/', permission_required_with_403('{}.change_{}'.format(app.label, modelname))(views.VersionHistory.as_view()),
|
||||
name='{}_history'.format(modelname), kwargs={'model': model, 'app': appname,}),
|
||||
]
|
||||
@@ -11,6 +11,8 @@ from django.views import generic
|
||||
from reversion.models import Version, VersionQuerySet
|
||||
from RIGS import models
|
||||
from assets import models as asset_models
|
||||
from django.apps import apps
|
||||
from reversion import revisions as reversion
|
||||
|
||||
logger = logging.getLogger('tec.pyrigs')
|
||||
|
||||
@@ -195,78 +197,3 @@ class RIGSVersion(Version):
|
||||
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()
|
||||
context['id'] = self.get_object().pk
|
||||
|
||||
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, models.RiskAssessment])
|
||||
return versions.order_by("-revision__date_created")
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(ActivityTable, self).get_context_data(**kwargs)
|
||||
context['title'] = 'Rigboard'
|
||||
|
||||
return context
|
||||
|
||||
# TODO Defined by the model, rather than with this list
|
||||
def models_for_feed():
|
||||
return [models.Event, models.Venue, models.Person, models.Organisation, models.EventAuthorisation, models.RiskAssessment, models.EventChecklist,
|
||||
asset_models.Asset, asset_models.Supplier]
|
||||
|
||||
|
||||
# Appears on homepage
|
||||
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_for_feed())
|
||||
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
|
||||
|
||||
98
versioning/views.py
Normal file
98
versioning/views.py
Normal file
@@ -0,0 +1,98 @@
|
||||
import datetime
|
||||
import logging
|
||||
|
||||
from diff_match_patch import diff_match_patch
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.db.models import EmailField, IntegerField, TextField
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.functional import cached_property
|
||||
from django.views import generic
|
||||
from reversion.models import Version, VersionQuerySet
|
||||
from RIGS import models
|
||||
from assets import models as asset_models
|
||||
from django.apps import apps
|
||||
from reversion import revisions as reversion
|
||||
from versioning.versioning import RIGSVersion
|
||||
|
||||
from django.views.decorators.cache import never_cache
|
||||
from django.utils.decorators import method_decorator
|
||||
|
||||
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):
|
||||
#Goddamit, almost got away without specific hacks
|
||||
if self.kwargs['model'].__name__ == 'Asset':
|
||||
return get_object_or_404(self.kwargs['model'], asset_id=self.kwargs['pk'])
|
||||
else:
|
||||
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()
|
||||
if self.kwargs['app'] != 'rigboard':
|
||||
context['override'] = 'base_{}.html'.format(self.kwargs['app'])
|
||||
|
||||
return context
|
||||
|
||||
def get_models(app=None):
|
||||
models = filter(lambda item: not hasattr(item, 'reversion_hide'),reversion.get_registered_models())
|
||||
if app != None:
|
||||
models = filter(lambda item: item in apps.get_app_config(app).get_models(),models)
|
||||
return models
|
||||
|
||||
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(self.kwargs['models'])
|
||||
return versions.order_by("-revision__date_created")
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(ActivityTable, self).get_context_data(**kwargs)
|
||||
context['title'] = self.kwargs['app']
|
||||
if self.kwargs['app'] != 'rigboard':
|
||||
context['override'] = 'base_{}.html'.format(self.kwargs['app'])
|
||||
|
||||
return context
|
||||
|
||||
# Appears on homepage
|
||||
@method_decorator(never_cache, name='dispatch') # Disable browser based caching
|
||||
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(get_models())
|
||||
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