diff --git a/customers/templates/customers/customer_create.html b/customers/templates/customers/customer_create.html new file mode 100644 index 0000000..af164d4 --- /dev/null +++ b/customers/templates/customers/customer_create.html @@ -0,0 +1,14 @@ +{% extends "core/base.html" %} +{% load crispy_forms_tags %} + +{% block section_title %}Create Customer{% endblock %} +{% block content %} +
+
+ {% csrf_token %} + {{ form|crispy }} + + Cancel +
+
+{% endblock %} diff --git a/customers/templates/customers/customer_list.html b/customers/templates/customers/customer_list.html index 14cb1c0..05ab22a 100644 --- a/customers/templates/customers/customer_list.html +++ b/customers/templates/customers/customer_list.html @@ -2,5 +2,19 @@ {% load render_table from django_tables2 %} {% block section_title %}List of Customers{% endblock %} {% block content %} -{% render_table customers %} +{% if request.user.is_superuser %} +
+
+
+ +
+
+
+{% endif %} +
+
+ {% render_table customers %} +
+
+ {% endblock %} diff --git a/customers/tests/test_customer_form.py b/customers/tests/test_customer_form.py new file mode 100644 index 0000000..33741c2 --- /dev/null +++ b/customers/tests/test_customer_form.py @@ -0,0 +1,16 @@ +from django.test import Client + +import pytest + + +pytestmark = pytest.mark.django_db + + +def test_customer_create_view(create_admin_user): + create_admin_user() + data = {'name': 'Big Pharma', + 'description': 'Some text.'} + client = Client() + client.login(username="pharma-admin", password="password") + response = client.post('/create/customer/', data) + assert response.status_code == 302 diff --git a/customers/urls.py b/customers/urls.py index e9e7491..df2ccd4 100644 --- a/customers/urls.py +++ b/customers/urls.py @@ -6,4 +6,7 @@ urlpatterns = [ path('', views.customers_table_view, name='customers'), path('customer//', views.CustomerDetailView.as_view(), name='customer'), + path('create/customer/', + views.CustomerCreateView.as_view(), + name='customer_create'), ] diff --git a/customers/views.py b/customers/views.py index 14d2c5d..fd602ee 100644 --- a/customers/views.py +++ b/customers/views.py @@ -1,6 +1,8 @@ from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin from django.shortcuts import render +from django.urls import reverse +from django.views.generic import CreateView from django.views.generic import DetailView from django_tables2 import RequestConfig @@ -26,3 +28,15 @@ class CustomerDetailView(LoginRequiredMixin, model = Customer template_name = 'customers/customer_details.html' permission_required = 'view_customer' + + +class CustomerCreateView(LoginRequiredMixin, CreateView): + """ + A view to create a customer. + """ + model = Customer + template_name = 'customers/customer_create.html' + fields = '__all__' + + def get_success_url(self): + return reverse('customer', args=(self.object.pk,))