From efaead7d7d43a108ee0cb8e8a3a15741a5c34bc7 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sun, 28 Jun 2020 23:31:35 +0200 Subject: [PATCH] add LicenseWithComputerCreateView --- .../templates/licenses/license_block.html | 2 +- .../license_with_computer_create.html | 14 ++++++++++ licenses/tests/test_license_form_view.py | 24 +++++++++++++++++ licenses/urls.py | 7 +++-- licenses/views.py | 27 +++++++++++++++++++ 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 licenses/templates/licenses/license_with_computer_create.html create mode 100644 licenses/tests/test_license_form_view.py diff --git a/licenses/templates/licenses/license_block.html b/licenses/templates/licenses/license_block.html index 6351a67..b706db7 100644 --- a/licenses/templates/licenses/license_block.html +++ b/licenses/templates/licenses/license_block.html @@ -7,4 +7,4 @@ {% endfor %} -

add

+

add

diff --git a/licenses/templates/licenses/license_with_computer_create.html b/licenses/templates/licenses/license_with_computer_create.html new file mode 100644 index 0000000..843f2cc --- /dev/null +++ b/licenses/templates/licenses/license_with_computer_create.html @@ -0,0 +1,14 @@ +{% extends "core/base.html" %} +{% load crispy_forms_tags %} + +{% block section_title %}Add License{% endblock %} +{% block content %} +
+
+ {% csrf_token %} + {{ form|crispy }} + + Cancel +
+
+{% endblock %} diff --git a/licenses/tests/test_license_form_view.py b/licenses/tests/test_license_form_view.py new file mode 100644 index 0000000..17171b5 --- /dev/null +++ b/licenses/tests/test_license_form_view.py @@ -0,0 +1,24 @@ +from django.test import Client +from mixer.backend.django import mixer +import pytest + + +pytestmark = pytest.mark.django_db + + +def test_license_relation_create_view(create_admin_user): + create_admin_user() + client = Client() + client.login(username="pharma-admin", password="password") + computer = mixer.blend('computers.Computer', customer=mixer.SELECT) + license = mixer.blend('licenses.ComputerLicense') + data = { + 'computer': computer.id, + 'license': license.id, + 'amount': 1 + } + url = '/create/license-with/computer/{}/'.format(computer.id) + response = client.post(url, data) + assert response.status_code == 302 + + diff --git a/licenses/urls.py b/licenses/urls.py index 888e67f..65dc739 100644 --- a/licenses/urls.py +++ b/licenses/urls.py @@ -3,6 +3,9 @@ from django.urls import path from . import views urlpatterns = [ - path('customer//licenses/', views.licenses_table_view, - name='licenses'), + path('customer//licenses/', views.licenses_table_view, + name='licenses'), + path('create/license-with/computer//', + views.LicenseWithComputerCreateView.as_view(), + name='license_with_computer_create'), ] diff --git a/licenses/views.py b/licenses/views.py index 36e0913..70fd707 100644 --- a/licenses/views.py +++ b/licenses/views.py @@ -1,12 +1,19 @@ 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.urls import reverse +from django.views.generic import CreateView from django_tables2 import RequestConfig from customers.decorators import customer_view_permission +from computers.models import Computer + from .models import ComputerLicense from .models import UserLicense +from .models import LicenseWithComputer from .tables import ComputerLicensesTable from .tables import UserLicensesTable # Create your views here. @@ -24,3 +31,23 @@ def licenses_table_view(request, pk): 'licenses/license_list.html', {'user_licenses': user_licenses, 'computer_licenses': computer_licenses}) + + +class LicenseWithComputerCreateView(LoginRequiredMixin, CreateView): + model = LicenseWithComputer + #form_class = ComputerSoftwareRelationCreateForm + template_name = 'licenses/license_with_computer_create.html' + fields = '__all__' + + def get_success_url(self): + return reverse('computer', args=(self.computer.pk,)) + + def get_initial(self): + """ + Set the device and customer dropdown to the device from the previous + view and the customer related to the device. + """ + self.computer = get_object_or_404(Computer, id=self.kwargs.get('pk')) + return { + 'computer': self.computer, + }