From 2d4282194f04a48c18a31c52b06393c39f68c7fe Mon Sep 17 00:00:00 2001
From: Jan Dittberner <jan@dittberner.info>
Date: Sun, 1 Feb 2015 15:00:44 +0100
Subject: [PATCH] add new view CustomerHostingPackageList

- create view hostingpackages.views.CustomerHostingPackageList to display a
  specific customer's hosting packages
- add docstring to view AllCustomerHostingPackageList
- add URL pattern 'hosting_packages' to hostingpackages.urls
- restructure hostingpackages URL patterns to remove useless parts
- add template hostingpackages/customerhostingpackage_list.html
- change links in template base.html to link to 'hosting_packages'
---
 gnuviechadmin/hostingpackages/urls.py         |  7 ++-
 gnuviechadmin/hostingpackages/views.py        | 36 +++++++++++++++-
 gnuviechadmin/templates/base.html             | 13 ++++--
 .../customerhostingpackage_list.html          | 43 +++++++++++++++++++
 4 files changed, 92 insertions(+), 7 deletions(-)
 create mode 100644 gnuviechadmin/templates/hostingpackages/customerhostingpackage_list.html

diff --git a/gnuviechadmin/hostingpackages/urls.py b/gnuviechadmin/hostingpackages/urls.py
index 2f9b810..e0999e9 100644
--- a/gnuviechadmin/hostingpackages/urls.py
+++ b/gnuviechadmin/hostingpackages/urls.py
@@ -12,6 +12,7 @@ from .views import (
     CreateCustomerHostingPackage,
     CreateHostingPackage,
     CustomerHostingPackageDetails,
+    CustomerHostingPackageList,
     HostingOptionChoices,
 )
 
@@ -20,10 +21,12 @@ urlpatterns = patterns(
     '',
     url(r'^create$', CreateHostingPackage.as_view(),
         name='create_hosting_package'),
-    url(r'^(?P<user>[\w0-9@.+-_]+)/create$',
+    url(r'^(?P<user>[\w0-9@.+-]+)/$',
+        CustomerHostingPackageList.as_view(), name='hosting_packages'),
+    url(r'^(?P<user>[\w0-9@.+-]+)/create$',
         CreateCustomerHostingPackage.as_view(),
         name='create_customer_hosting_package'),
-    url(r'^(?P<user>[\w0-9@.+-_]+)/hostingpackage/(?P<pk>\d+)/$',
+    url(r'^(?P<user>[\w0-9@.+-]+)/(?P<pk>\d+)/$',
         CustomerHostingPackageDetails.as_view(),
         name='hosting_package_details'),
     url(r'^allpackages/',
diff --git a/gnuviechadmin/hostingpackages/views.py b/gnuviechadmin/hostingpackages/views.py
index de6b029..abe265e 100644
--- a/gnuviechadmin/hostingpackages/views.py
+++ b/gnuviechadmin/hostingpackages/views.py
@@ -103,10 +103,13 @@ class CustomerHostingPackageDetails(StaffOrSelfLoginRequiredMixin, DetailView):
     """
     model = CustomerHostingPackage
     context_object_name = 'hostingpackage'
+    customer = None
 
     def get_customer_object(self):
-        return get_object_or_404(
-            get_user_model(), username=self.kwargs['user'])
+        if self.customer is None:
+            self.customer = get_object_or_404(
+                get_user_model(), username=self.kwargs['user'])
+        return self.customer
 
     def get_context_data(self, **kwargs):
         context = super(CustomerHostingPackageDetails, self).get_context_data(
@@ -128,10 +131,39 @@ class CustomerHostingPackageDetails(StaffOrSelfLoginRequiredMixin, DetailView):
 class AllCustomerHostingPackageList(
     LoginRequiredMixin, StaffuserRequiredMixin, ListView
 ):
+    """
+    This view is used for showing a list of all hosting packages.
+
+    """
     model = CustomerHostingPackage
     template_name_suffix = '_admin_list'
 
 
+class CustomerHostingPackageList(StaffOrSelfLoginRequiredMixin, ListView):
+    """
+    This view is used for showing a list of a customer's hosting packages.
+
+    """
+    model = CustomerHostingPackage
+    customer = None
+
+    def get_customer_object(self):
+        if self.customer is None:
+            self.customer = get_object_or_404(
+                get_user_model(), username=self.kwargs['user'])
+        return self.customer
+
+    def get_context_data(self, **kwargs):
+        context = super(CustomerHostingPackageList, self).get_context_data(
+            **kwargs)
+        context['customer'] = self.get_customer_object()
+        return context
+
+    def get_queryset(self):
+        return super(CustomerHostingPackageList, self).get_queryset().filter(
+            customer__username=self.kwargs['user'])
+
+
 class HostingOptionChoices(
     LoginRequiredMixin, StaffuserRequiredMixin, DetailView
 ):
diff --git a/gnuviechadmin/templates/base.html b/gnuviechadmin/templates/base.html
index a3f8bc1..3927ac2 100644
--- a/gnuviechadmin/templates/base.html
+++ b/gnuviechadmin/templates/base.html
@@ -41,13 +41,20 @@
             <span class="icon-bar"></span>
             <span class="icon-bar"></span>
           </button>
-          <a class="navbar-brand" href="{% url "dashboard" %}">gnuviechadmin</a>
+          <a class="navbar-brand" href="{% if user.is_authenticated %}{% url 'customer_dashboard' slug=user.username %}{% else %}{% url 'dashboard' %}{% endif %}" title="{% trans "Dashboard" %}">gnuviechadmin</a>
         </div>
         <div class="collapse navbar-collapse">
           <ul class="nav navbar-nav">
-            <li class="active"><a href="{% if user.is_authenticated %}{% url 'customer_dashboard' slug=user.username %}{% else %}{% url 'dashboard' %}{% endif %}">{% trans "Dashboard" %}</a></li>
             {% if user.is_staff %}
-            <li><a href="{% url 'all_hosting_packages' %}">{% trans "All hosting packages" %}</a></li>
+            <li class="dropdown{% if active_item == 'hostingpackage' %} active{% endif %}">
+              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">{% trans "Hosting" %} <span class="caret"></span></a>
+              <ul class="dropdown-menu" role="menu">
+                <li><a href="{% url 'hosting_packages' user=user.username %}">{% trans "Your hosting packages" %}</a></li>
+                <li><a href="{% url 'all_hosting_packages' %}">{% trans "All hosting packages" %}</a></li>
+              </ul>
+            </li>
+            {% else %}
+            <li{% if active_item == 'hostingpackage' %} class="active"{% endif %}><a href="{% url 'hosting_packages' user=user.username %}">{% trans "Hosting" %}</a></li>
             {% endif %}
             <li class="dropdown">
               <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="glyphicon glyphicon-link"></i> {% trans "Links" %} <span class="caret"></span></a>
diff --git a/gnuviechadmin/templates/hostingpackages/customerhostingpackage_list.html b/gnuviechadmin/templates/hostingpackages/customerhostingpackage_list.html
new file mode 100644
index 0000000..fb90f1d
--- /dev/null
+++ b/gnuviechadmin/templates/hostingpackages/customerhostingpackage_list.html
@@ -0,0 +1,43 @@
+{% extends "hostingpackages/base.html" %}
+{% load i18n %}
+{% block title %}{{ block.super }} - {% spaceless %}
+{% if user == customer %}
+{% trans "Your hosting packages" %}
+{% else %}
+{% blocktrans with customer=customer.get_full_name %}Hosting Packages of {{ customer }}{% endblocktrans %}
+{% endif %}
+{% endspaceless %}{% endblock title %}
+
+{% block page_title %}{% spaceless %}
+{% if user == customer %}
+{% trans "Your hosting packages" %}
+{% else %}
+{% blocktrans with customer=customer.get_full_name %}Hosting Packages <small>of {{ customer }}</small>{% endblocktrans %}
+{% endif %}
+{% endspaceless %}{% endblock page_title %}
+
+{% block content %}
+{% if customerhostingpackage_list %}
+<table class="table table-condensed">
+  <thead>
+    <tr>
+      <th>{% trans "Name" %}</th>
+      <th>{% trans "Setup date" %}</th>
+    </tr>
+  </thead>
+  <tbody>
+    {% for package in customerhostingpackage_list %}
+    <tr>
+      <td><a href="{{ package.get_absolute_url }}">{{ package.name }}</a></td>
+      <td>{{ package.created }}</td>
+    </tr>
+    {% endfor %}
+  </tbody>
+</table>
+{% else %}
+<p class="text-info">{% if user == customer %}{% trans "You have no hosting packages setup yet." %}{% else %}{% trans "There are no hosting packages setup for this customer yet." %}{% endif %}</p>
+{% endif %}
+{% if user.is_staff %}
+<p><a href="{% url 'create_customer_hosting_package' user=customer.username %}" class="btn btn-primary">{% trans "Add hosting package" %}</a></p>
+{% endif %}
+{% endblock content %}