move the customer related parts to the customer app
This commit is contained in:
parent
0d3a88e173
commit
95bc3207b3
|
@ -0,0 +1,16 @@
|
|||
from django.http import HttpResponseForbidden
|
||||
|
||||
from .models import Customer
|
||||
|
||||
|
||||
def customer_view_permission(old_function):
|
||||
def new_function(request, pk, *args, **kwargs):
|
||||
customer = Customer.objects.get(pk=pk)
|
||||
user = request.user
|
||||
if user.has_perm('customer.view_customer', customer):
|
||||
return old_function(request, pk)
|
||||
else:
|
||||
return HttpResponseForbidden(
|
||||
"You're not allowed to access this page."
|
||||
)
|
||||
return new_function
|
|
@ -0,0 +1,21 @@
|
|||
import django_tables2 as tables
|
||||
from django_tables2.utils import A
|
||||
|
||||
|
||||
class CustomersTable(tables.Table):
|
||||
name = tables.LinkColumn('customer', args=[A('pk')])
|
||||
nets = tables.LinkColumn('nets', text='Nets', args=[A('pk')],
|
||||
orderable=False)
|
||||
computers = tables.LinkColumn('computers', text='Computers', args=[A('pk')],
|
||||
orderable=False)
|
||||
devices = tables.LinkColumn('devices', text='Devices', args=[A('pk')],
|
||||
orderable=False)
|
||||
backups = tables.LinkColumn('backups', text='Backups', args=[A('pk')],
|
||||
orderable=False)
|
||||
licenses = tables.LinkColumn('licenses', text='Licenses', args=[A('pk')],
|
||||
orderable=False)
|
||||
users = tables.LinkColumn('users', text='Users', args=[A('pk')],
|
||||
orderable=False)
|
||||
|
||||
class Meta:
|
||||
template_name = 'django_tables2/semantic.html'
|
|
@ -0,0 +1,9 @@
|
|||
from django.urls import path
|
||||
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.customers_table_view, name='customers'),
|
||||
path('customer/<int:pk>/', views.CustomerDetailView.as_view(),
|
||||
name='customer'),
|
||||
]
|
|
@ -1,3 +1,27 @@
|
|||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.shortcuts import render
|
||||
from django.views.generic import DetailView
|
||||
|
||||
# Create your views here.
|
||||
from django_tables2 import RequestConfig
|
||||
from guardian.mixins import PermissionRequiredMixin
|
||||
from guardian.shortcuts import get_objects_for_user
|
||||
|
||||
from .models import Customer
|
||||
from .tables import CustomersTable
|
||||
|
||||
|
||||
@login_required
|
||||
def customers_table_view(request):
|
||||
table = CustomersTable(
|
||||
get_objects_for_user(request.user,
|
||||
'customer.view_customer',
|
||||
klass=Customer))
|
||||
RequestConfig(request).configure(table)
|
||||
return render(request, 'inventory/customer_list.html', {'customers': table})
|
||||
|
||||
|
||||
class CustomerDetailView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
|
||||
model = Customer
|
||||
template_name = 'inventory/customer_details.html'
|
||||
permission_required = 'view_customer'
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
from django.http import HttpResponseForbidden
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from customer.models import Customer
|
||||
|
||||
from .models import Backup
|
||||
from .models import Computer
|
||||
from .models import Device
|
||||
|
@ -50,19 +48,6 @@ def connect_device_view_permission(old_function):
|
|||
return new_function
|
||||
|
||||
|
||||
def customer_view_permission(old_function):
|
||||
def new_function(request, pk, *args, **kwargs):
|
||||
customer = Customer.objects.get(pk=pk)
|
||||
user = request.user
|
||||
if user.has_perm('customer.view_customer', customer):
|
||||
return old_function(request, pk)
|
||||
else:
|
||||
return HttpResponseForbidden(
|
||||
"You're not allowed to access this page."
|
||||
)
|
||||
return new_function
|
||||
|
||||
|
||||
def net_view_permission(old_fuction):
|
||||
def new_function(request, pk, *args, **kwargs):
|
||||
net = get_object_or_404(Net, pk=pk)
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import django_tables2 as tables
|
||||
from django_tables2.utils import A
|
||||
|
||||
from .models import Backup
|
||||
from .models import ComputerLicense
|
||||
|
@ -10,25 +9,6 @@ from .models import User
|
|||
from .models import UserLicense
|
||||
|
||||
|
||||
class CustomersTable(tables.Table):
|
||||
name = tables.LinkColumn('customer', args=[A('pk')])
|
||||
nets = tables.LinkColumn('nets', text='Nets', args=[A('pk')],
|
||||
orderable=False)
|
||||
computers = tables.LinkColumn('computers', text='Computers', args=[A('pk')],
|
||||
orderable=False)
|
||||
devices = tables.LinkColumn('devices', text='Devices', args=[A('pk')],
|
||||
orderable=False)
|
||||
backups = tables.LinkColumn('backups', text='Backups', args=[A('pk')],
|
||||
orderable=False)
|
||||
licenses = tables.LinkColumn('licenses', text='Licenses', args=[A('pk')],
|
||||
orderable=False)
|
||||
users = tables.LinkColumn('users', text='Users', args=[A('pk')],
|
||||
orderable=False)
|
||||
|
||||
class Meta:
|
||||
template_name = 'django_tables2/semantic.html'
|
||||
|
||||
|
||||
class ComputersTable(tables.Table):
|
||||
name = tables.Column('Computer', linkify=True)
|
||||
description = tables.Column()
|
||||
|
|
|
@ -4,9 +4,6 @@ from . import views
|
|||
|
||||
urlpatterns = [
|
||||
path('accounts/', include('django.contrib.auth.urls')),
|
||||
path('', views.customers_table_view, name='customers'),
|
||||
path('customer/<int:pk>/', views.CustomerDetailView.as_view(),
|
||||
name='customer'),
|
||||
path('customer/<int:pk>/computers/',
|
||||
views.computers_table_view, name='computers'),
|
||||
path('customer/<int:pk>/devices/', views.devices_table_view,
|
||||
|
|
|
@ -2,19 +2,17 @@ from django.contrib.auth.decorators import login_required
|
|||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.shortcuts import render
|
||||
from django.views.generic import DetailView
|
||||
|
||||
from django_filters.views import FilterView
|
||||
from django_tables2 import RequestConfig
|
||||
from django_tables2.views import SingleTableMixin
|
||||
from guardian.mixins import PermissionRequiredMixin
|
||||
from guardian.shortcuts import get_objects_for_user
|
||||
|
||||
from customer.models import Customer
|
||||
from customer.decorators import customer_view_permission
|
||||
|
||||
from .decorators import backup_view_permission
|
||||
from .decorators import computer_view_permission
|
||||
from .decorators import customer_view_permission
|
||||
from .decorators import device_view_permission
|
||||
from .decorators import net_view_permission
|
||||
from .decorators import user_view_permission
|
||||
|
@ -43,7 +41,6 @@ from .models import UserLicense
|
|||
from .tables import BackupsTable
|
||||
from .tables import ComputerLicensesTable
|
||||
from .tables import ComputersTable
|
||||
from .tables import CustomersTable
|
||||
from .tables import DevicesTable
|
||||
from .tables import NetDetailTable
|
||||
from .tables import NetsTable
|
||||
|
@ -83,22 +80,6 @@ def computer_detail_view(request, pk):
|
|||
return render(request, 'inventory/computer_details.html', context)
|
||||
|
||||
|
||||
class CustomerDetailView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
|
||||
model = Customer
|
||||
template_name = 'inventory/customer_details.html'
|
||||
permission_required = 'view_customer'
|
||||
|
||||
|
||||
@login_required
|
||||
def customers_table_view(request):
|
||||
table = CustomersTable(
|
||||
get_objects_for_user(request.user,
|
||||
'customer.view_customer',
|
||||
klass=Customer))
|
||||
RequestConfig(request).configure(table)
|
||||
return render(request, 'inventory/customer_list.html', {'customers': table})
|
||||
|
||||
|
||||
@login_required
|
||||
@customer_view_permission
|
||||
def computers_table_view(request, pk):
|
||||
|
|
|
@ -19,6 +19,7 @@ from django.urls import path
|
|||
|
||||
urlpatterns = [
|
||||
url(r'', include('inventory.urls')),
|
||||
url(r'', include('customer.urls')),
|
||||
path('admin/', admin.site.urls),
|
||||
url(r'^_nested_admin/', include('nested_admin.urls')),
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue