mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 09:16:10 -06:00
Add bulk sync view for config contexts
This commit is contained in:
parent
e5f660327b
commit
5f73b55623
@ -200,15 +200,18 @@ class ConfigContextTable(NetBoxTable):
|
||||
is_active = columns.BooleanColumn(
|
||||
verbose_name='Active'
|
||||
)
|
||||
is_synced = columns.BooleanColumn(
|
||||
verbose_name='Synced'
|
||||
)
|
||||
|
||||
class Meta(NetBoxTable.Meta):
|
||||
model = ConfigContext
|
||||
fields = (
|
||||
'pk', 'id', 'name', 'weight', 'is_active', 'description', 'regions', 'sites', 'locations', 'roles',
|
||||
'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', 'tenants', 'data_source',
|
||||
'data_file', 'data_synced', 'created', 'last_updated',
|
||||
'pk', 'id', 'name', 'weight', 'is_active', 'is_synced', 'description', 'regions', 'sites', 'locations',
|
||||
'roles', 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups', 'tenants',
|
||||
'data_source', 'data_file', 'data_synced', 'created', 'last_updated',
|
||||
)
|
||||
default_columns = ('pk', 'name', 'weight', 'is_active', 'description')
|
||||
default_columns = ('pk', 'name', 'weight', 'is_active', 'is_synced', 'description')
|
||||
|
||||
|
||||
class ObjectChangeTable(NetBoxTable):
|
||||
|
@ -60,6 +60,7 @@ urlpatterns = [
|
||||
path('config-contexts/add/', views.ConfigContextEditView.as_view(), name='configcontext_add'),
|
||||
path('config-contexts/edit/', views.ConfigContextBulkEditView.as_view(), name='configcontext_bulk_edit'),
|
||||
path('config-contexts/delete/', views.ConfigContextBulkDeleteView.as_view(), name='configcontext_bulk_delete'),
|
||||
path('config-contexts/sync/', views.ConfigContextBulkSyncDataView.as_view(), name='configcontext_bulk_sync'),
|
||||
path('config-contexts/<int:pk>/', include(get_model_urls('extras', 'configcontext'))),
|
||||
|
||||
# Image attachments
|
||||
|
@ -352,7 +352,8 @@ class ConfigContextListView(generic.ObjectListView):
|
||||
filterset = filtersets.ConfigContextFilterSet
|
||||
filterset_form = forms.ConfigContextFilterForm
|
||||
table = tables.ConfigContextTable
|
||||
actions = ('add', 'bulk_edit', 'bulk_delete')
|
||||
template_name = 'extras/configcontext_list.html'
|
||||
actions = ('add', 'bulk_edit', 'bulk_delete', 'bulk_sync')
|
||||
|
||||
|
||||
@register_model_view(ConfigContext)
|
||||
@ -416,6 +417,10 @@ class ConfigContextBulkDeleteView(generic.BulkDeleteView):
|
||||
table = tables.ConfigContextTable
|
||||
|
||||
|
||||
class ConfigContextBulkSyncDataView(generic.BulkSyncDataView):
|
||||
queryset = ConfigContext.objects.all()
|
||||
|
||||
|
||||
class ObjectConfigContextView(generic.ObjectView):
|
||||
base_template = None
|
||||
template_name = 'extras/object_configcontext.html'
|
||||
|
@ -349,6 +349,10 @@ class SyncedDataMixin(models.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@property
|
||||
def is_synced(self):
|
||||
return self.data_file and self.data_synced >= self.data_file.last_updated
|
||||
|
||||
def clean(self):
|
||||
if self.data_file:
|
||||
self.sync_data()
|
||||
|
@ -1,5 +1,6 @@
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.contrib import messages
|
||||
from django.db import transaction
|
||||
from django.db.models import Q
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.utils.translation import gettext as _
|
||||
@ -7,9 +8,12 @@ from django.views.generic import View
|
||||
|
||||
from extras import forms, tables
|
||||
from extras.models import *
|
||||
from utilities.views import ViewTab
|
||||
from utilities.permissions import get_permission_for_model
|
||||
from utilities.views import GetReturnURLMixin, ViewTab
|
||||
from .base import BaseMultiObjectView
|
||||
|
||||
__all__ = (
|
||||
'BulkSyncDataView',
|
||||
'ObjectChangeLogView',
|
||||
'ObjectJournalView',
|
||||
'ObjectSyncDataView',
|
||||
@ -150,3 +154,27 @@ class ObjectSyncDataView(View):
|
||||
messages.success(request, f"Synchronized data for {model._meta._verbose_name} {obj}.")
|
||||
|
||||
return redirect(obj.get_absolute_url())
|
||||
|
||||
|
||||
class BulkSyncDataView(GetReturnURLMixin, BaseMultiObjectView):
|
||||
"""
|
||||
Synchronize multiple instances of a model inheriting from SyncedDataMixin.
|
||||
"""
|
||||
def get_required_permission(self):
|
||||
return get_permission_for_model(self.queryset.model, 'change')
|
||||
|
||||
def post(self, request):
|
||||
selected_objects = self.queryset.filter(
|
||||
pk__in=request.POST.getlist('pk'),
|
||||
data_file__isnull=False
|
||||
)
|
||||
|
||||
with transaction.atomic():
|
||||
for obj in selected_objects:
|
||||
obj.sync_data()
|
||||
obj.save()
|
||||
|
||||
model_name = self.queryset.model._meta.verbose_name_plural
|
||||
messages.success(request, f"Synced {len(selected_objects)} {model_name}")
|
||||
|
||||
return redirect(self.get_return_url(request))
|
||||
|
@ -42,7 +42,7 @@
|
||||
<a href="{{ object.data_file.get_absolute_url }}">{{ object.data_file }}</a>
|
||||
{% elif object.data_path %}
|
||||
<div class="float-end text-warning">
|
||||
<i class="mdi mdi-alert" title="No file exists at specified path."></i>
|
||||
<i class="mdi mdi-alert" title="The data file associated with this object has been deleted."></i>
|
||||
</div>
|
||||
{{ object.data_path }}
|
||||
{% else %}
|
||||
|
10
netbox/templates/extras/configcontext_list.html
Normal file
10
netbox/templates/extras/configcontext_list.html
Normal file
@ -0,0 +1,10 @@
|
||||
{% extends 'generic/object_list.html' %}
|
||||
|
||||
{% block bulk_buttons %}
|
||||
{% if perms.extras.change_configcontext %}
|
||||
<button type="submit" name="_sync" formaction="{% url 'extras:configcontext_bulk_sync' %}" class="btn btn-primary btn-sm">
|
||||
<i class="mdi mdi-sync" aria-hidden="true"></i> Sync Data
|
||||
</button>
|
||||
{% endif %}
|
||||
{{ block.super }}
|
||||
{% endblock %}
|
Loading…
Reference in New Issue
Block a user