Added CBVs for SecretRoles

This commit is contained in:
Jeremy Stretch 2016-05-16 12:07:12 -04:00
parent a07aa69321
commit b481935b9a
8 changed files with 125 additions and 5 deletions

View File

@ -29,6 +29,21 @@ def validate_rsa_key(key, is_secret=True):
raise forms.ValidationError("Error validating RSA key. Please ensure that your key supports PKCS#1 OAEP.")
#
# Secret roles
#
class SecretRoleForm(forms.ModelForm, BootstrapMixin):
class Meta:
model = SecretRole
fields = ['name', 'slug']
class SecretRoleBulkDeleteForm(ConfirmationForm):
pk = forms.ModelMultipleChoiceField(queryset=SecretRole.objects.all(), widget=forms.MultipleHiddenInput)
#
# Secrets
#

View File

@ -173,6 +173,9 @@ class SecretRole(models.Model):
def __unicode__(self):
return self.name
def get_absolute_url(self):
return "{}?role={}".format(reverse('secrets:secret_list'), self.slug)
class Secret(models.Model):
"""

View File

@ -1,7 +1,32 @@
import django_tables2 as tables
from django_tables2.utils import Accessor
from .models import Secret
from .models import SecretRole, Secret
SECRETROLE_EDIT_LINK = """
{% if perms.secrets.change_secretrole %}<a href="{% url 'secrets:secretrole_edit' slug=record.slug %}">Edit</a>{% endif %}
"""
#
# Secret roles
#
class SecretRoleTable(tables.Table):
pk = tables.CheckBoxColumn(visible=False, default='')
name = tables.LinkColumn(verbose_name='Name')
secret_count = tables.Column(verbose_name='Secrets')
slug = tables.Column(verbose_name='Slug')
edit = tables.TemplateColumn(template_code=SECRETROLE_EDIT_LINK, verbose_name='')
class Meta:
model = SecretRole
fields = ('pk', 'name', 'secret_count', 'slug', 'edit')
empty_text = "No secret roles were found."
attrs = {
'class': 'table table-hover',
}
#

View File

@ -3,6 +3,14 @@ from django.conf.urls import url
from . import views
urlpatterns = [
# Secret roles
url(r'^secret-roles/$', views.SecretRoleListView.as_view(), name='secretrole_list'),
url(r'^secret-roles/add/$', views.SecretRoleEditView.as_view(), name='secretrole_add'),
url(r'^secret-roles/delete/$', views.SecretRoleBulkDeleteView.as_view(), name='secretrole_bulk_delete'),
url(r'^secret-roles/(?P<slug>[\w-]+)/edit/$', views.SecretRoleEditView.as_view(), name='secretrole_edit'),
# Secrets
url(r'^secrets/$', views.SecretListView.as_view(), name='secret_list'),
url(r'^secrets/import/$', views.secret_import, name='secret_import'),
url(r'^secrets/edit/$', views.SecretBulkEditView.as_view(), name='secret_bulk_edit'),
@ -10,4 +18,5 @@ urlpatterns = [
url(r'^secrets/(?P<pk>\d+)/$', views.secret, name='secret'),
url(r'^secrets/(?P<pk>\d+)/edit/$', views.secret_edit, name='secret_edit'),
url(r'^secrets/(?P<pk>\d+)/delete/$', views.SecretDeleteView.as_view(), name='secret_delete'),
]

View File

@ -3,17 +3,45 @@ from django.contrib.auth.decorators import permission_required, login_required
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.core.urlresolvers import reverse
from django.db import transaction, IntegrityError
from django.db.models import Count
from django.shortcuts import get_object_or_404, redirect, render
from django.utils.decorators import method_decorator
from dcim.models import Device
from utilities.views import BulkEditView, BulkDeleteView, ObjectListView, ObjectDeleteView
from utilities.views import BulkEditView, BulkDeleteView, ObjectListView, ObjectEditView, ObjectDeleteView
from .decorators import userkey_required
from .filters import SecretFilter
from .forms import SecretForm, SecretImportForm, SecretBulkEditForm, SecretBulkDeleteForm, SecretFilterForm
from .models import Secret, UserKey
from .tables import SecretTable
from .forms import SecretRoleForm, SecretRoleBulkDeleteForm, SecretForm, SecretImportForm, SecretBulkEditForm,\
SecretBulkDeleteForm, SecretFilterForm
from .models import SecretRole, Secret, UserKey
from .tables import SecretRoleTable, SecretTable
#
# Secret roles
#
class SecretRoleListView(ObjectListView):
queryset = SecretRole.objects.annotate(secret_count=Count('secrets'))
table = SecretRoleTable
edit_permissions = ['secrets.change_secretrole', 'secrets.delete_secretrole']
template_name = 'secrets/secretrole_list.html'
class SecretRoleEditView(PermissionRequiredMixin, ObjectEditView):
permission_required = 'secrets.change_secretrole'
model = SecretRole
form_class = SecretRoleForm
success_url = 'secrets:secretrole_list'
cancel_url = 'secrets:secretrole_list'
class SecretRoleBulkDeleteView(PermissionRequiredMixin, BulkDeleteView):
permission_required = 'secrets.delete_secretrole'
cls = SecretRole
form = SecretRoleBulkDeleteForm
default_redirect_url = 'secrets:secretrole_list'
#

View File

@ -180,6 +180,11 @@
{% if perms.secrets.add_secret %}
<li><a href="{% url 'secrets:secret_import' %}"><i class="glyphicon glyphicon-import" aria-hidden="true"></i> Import Secrets</a></li>
{% endif %}
<li class="divider"></li>
<li><a href="{% url 'secrets:secretrole_list' %}"><i class="glyphicon glyphicon-search" aria-hidden="true"></i> Secret Roles</a></li>
{% if perms.secrets.add_secretrole %}
<li><a href="{% url 'secrets:secretrole_add' %}"><i class="glyphicon glyphicon-plus" aria-hidden="true"></i> Add a Secret Role</a></li>
{% endif %}
</ul>
</li>
{% endif %}

View File

@ -0,0 +1,14 @@
{% load render_table from django_tables2 %}
{% if perms.secrets.delete_secretrole %}
<form method="post" class="form form-horizontal">
{% csrf_token %}
<input type="hidden" name="redirect_url" value="{{ request.path }}{% if request.GET %}?{{ request.GET.urlencode }}{% endif %}" />
{% render_table table table_template|default:'table.html' %}
<button type="submit" name="_delete" formaction="{% url 'secrets:secretrole_bulk_delete' %}" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
Delete Selected
</button>
</form>
{% else %}
{% render_table table table_template|default:'table.html' %}
{% endif %}

View File

@ -0,0 +1,21 @@
{% extends '_base.html' %}
{% load helpers %}
{% block title %}Secret Roles{% endblock %}
{% block content %}
<div class="pull-right">
{% if perms.dcim.add_devicerole %}
<a href="{% url 'secrets:secretrole_add' %}" class="btn btn-primary">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
Add a secret role
</a>
{% endif %}
</div>
<h1>Device Roles</h1>
<div class="row">
<div class="col-md-12">
{% include 'secrets/inc/secretrole_table.html' %}
</div>
</div>
{% endblock %}