From 680f091cbae886c33f7f12ac6c025e01ec6b1409 Mon Sep 17 00:00:00 2001
From: Jan Dittberner <jan@dittberner.info>
Date: Tue, 20 Jan 2015 00:51:05 +0100
Subject: [PATCH] incomplete create_hosting_package view

- add staff user view create_hosting_package
- add hostingpackages.forms.CreateHostingPackageForm
- add hostingpackages.views.CreateHostingPackage
- add link for staff users on user_dashboard page
- add url pattern
- TODO: implement saving the hosting package, update docs
---
 gnuviechadmin/gnuviechadmin/urls.py           |  5 +-
 gnuviechadmin/hostingpackages/forms.py        | 44 +++++++++++++++++
 gnuviechadmin/hostingpackages/urls.py         | 12 +++++
 gnuviechadmin/hostingpackages/views.py        | 47 +++++++++++++++++++
 .../templates/dashboard/user_dashboard.html   |  5 +-
 .../templates/hostingpackages/base.html       |  1 +
 .../customerhostingpackage_create.html        |  7 +++
 7 files changed, 117 insertions(+), 4 deletions(-)
 create mode 100644 gnuviechadmin/hostingpackages/forms.py
 create mode 100644 gnuviechadmin/hostingpackages/urls.py
 create mode 100644 gnuviechadmin/hostingpackages/views.py
 create mode 100644 gnuviechadmin/templates/hostingpackages/base.html
 create mode 100644 gnuviechadmin/templates/hostingpackages/customerhostingpackage_create.html

diff --git a/gnuviechadmin/gnuviechadmin/urls.py b/gnuviechadmin/gnuviechadmin/urls.py
index c70d6c7..974ba1f 100644
--- a/gnuviechadmin/gnuviechadmin/urls.py
+++ b/gnuviechadmin/gnuviechadmin/urls.py
@@ -3,15 +3,14 @@ from __future__ import absolute_import
 from django.conf.urls import patterns, include, url
 from django.conf import settings
 
-import dashboard.urls
-
 from django.contrib import admin
 admin.autodiscover()
 
 urlpatterns = patterns(
     '',
-    url(r'', include(dashboard.urls)),
+    url(r'', include('dashboard.urls')),
     url(r'^accounts/', include('allauth.urls')),
+    url(r'^hosting/', include('hostingpackages.urls')),
     url(r'^admin/', include(admin.site.urls)),
 )
 
diff --git a/gnuviechadmin/hostingpackages/forms.py b/gnuviechadmin/hostingpackages/forms.py
new file mode 100644
index 0000000..bc8a8d9
--- /dev/null
+++ b/gnuviechadmin/hostingpackages/forms.py
@@ -0,0 +1,44 @@
+"""
+This module contains the form classes related to hosting packages.
+
+"""
+from __future__ import absolute_import, unicode_literals
+
+from django import forms
+from django.core.urlresolvers import reverse
+from django.utils.translation import ugettext as _
+
+from crispy_forms.helper import FormHelper
+from crispy_forms.layout import (
+    Layout,
+    Submit,
+)
+
+from .models import CustomerHostingPackage
+
+
+class CreateHostingPackageForm(forms.ModelForm):
+    """
+    This form class is used for creating new customer hosting packages.
+
+    """
+    class Meta:
+        model = CustomerHostingPackage
+        fields = ['template', 'name', 'description']
+
+    def __init__(self, instance, *args, **kwargs):
+        username = kwargs.pop('user')
+        super(CreateHostingPackageForm, self).__init__(
+            *args, **kwargs
+        )
+        self.fields['description'].widget.attrs['rows'] = 2
+        self.helper = FormHelper()
+        self.helper.form_action = reverse(
+            'create_hosting_package', kwargs={'user': username}
+        )
+        self.helper.layout = Layout(
+            'template',
+            'name',
+            'description',
+            Submit('submit', _('Add Hosting Package')),
+        )
diff --git a/gnuviechadmin/hostingpackages/urls.py b/gnuviechadmin/hostingpackages/urls.py
new file mode 100644
index 0000000..9e7f00d
--- /dev/null
+++ b/gnuviechadmin/hostingpackages/urls.py
@@ -0,0 +1,12 @@
+from __future__ import absolute_import, unicode_literals
+
+from django.conf.urls import patterns, url
+
+from .views import CreateHostingPackage
+
+
+urlpatterns = patterns(
+    '',
+    url(r'^(?P<user>[\w0-9@.+-_]+)/create$', CreateHostingPackage.as_view(),
+        name='create_hosting_package'),
+)
diff --git a/gnuviechadmin/hostingpackages/views.py b/gnuviechadmin/hostingpackages/views.py
new file mode 100644
index 0000000..3a4a077
--- /dev/null
+++ b/gnuviechadmin/hostingpackages/views.py
@@ -0,0 +1,47 @@
+"""
+This module defines views related to hosting packages.
+
+"""
+from __future__ import absolute_import, unicode_literals
+
+from django.core.urlresolvers import reverse
+from django.views.generic.edit import CreateView
+from django.contrib.auth import get_user_model
+
+from braces.views import (
+    LoginRequiredMixin,
+    StaffuserRequiredMixin,
+)
+
+from .forms import CreateHostingPackageForm
+from .models import CustomerHostingPackage
+
+
+class CreateHostingPackage(
+    LoginRequiredMixin, StaffuserRequiredMixin, CreateView
+):
+    """
+    Create a hosting package.
+
+    """
+    model = CustomerHostingPackage
+    raise_exception = True
+    template_name_suffix = '_create'
+    form_class = CreateHostingPackageForm
+
+    def get_form_kwargs(self):
+        kwargs = super(CreateHostingPackage, self).get_form_kwargs()
+        kwargs.update(self.kwargs)
+        return kwargs
+
+    def get_context_data(self, **kwargs):
+        context = super(CreateHostingPackage, self).get_context_data(**kwargs)
+        customer = get_user_model().objects.get(username=self.kwargs['user'])
+        context['customer'] = customer
+        return context
+
+    def get_success_url(self):
+        return reverse('customer_dashboard', slug=self.kwargs['user'])
+
+    def form_valid(self, form):
+        return super(CreateHostingPackage, self).form_valid(form)
diff --git a/gnuviechadmin/templates/dashboard/user_dashboard.html b/gnuviechadmin/templates/dashboard/user_dashboard.html
index c12627a..fc2509a 100644
--- a/gnuviechadmin/templates/dashboard/user_dashboard.html
+++ b/gnuviechadmin/templates/dashboard/user_dashboard.html
@@ -39,7 +39,10 @@
           </tbody>
         </table>
         {% else %}
-        <p class="text-info">{% trans "You have no hosting packages yet." %}</p>
+        <p class="text-info">{% if user == object %}{% trans "You have no hosting packages yet." %}{% else %}{% trans "This user has no hosting packages assigned yet." %}{% endif %}</p>
+        {% endif %}
+        {% if user.is_staff %}
+        <a href="{% url "create_hosting_package" user=object.username %}" class="btn btn-primary">{% trans "Add hosting package" %}</a>
         {% endif %}
       </div>
     </div>
diff --git a/gnuviechadmin/templates/hostingpackages/base.html b/gnuviechadmin/templates/hostingpackages/base.html
new file mode 100644
index 0000000..94d9808
--- /dev/null
+++ b/gnuviechadmin/templates/hostingpackages/base.html
@@ -0,0 +1 @@
+{% extends "base.html" %}
diff --git a/gnuviechadmin/templates/hostingpackages/customerhostingpackage_create.html b/gnuviechadmin/templates/hostingpackages/customerhostingpackage_create.html
new file mode 100644
index 0000000..1cadd46
--- /dev/null
+++ b/gnuviechadmin/templates/hostingpackages/customerhostingpackage_create.html
@@ -0,0 +1,7 @@
+{% extends "hostingpackages/base.html" %}
+{% load i18n crispy_forms_tags %}
+{% block title %}{{ block.super }} - {% blocktrans with full_name=customer.get_full_name %}Add hosting package for Customer {{ full_name }}{% endblocktrans %}{% endblock title %}
+{% block page_title %}{% blocktrans with full_name=customer.get_full_name %}Add Hosting Package for Customer {{ full_name }}{% endblocktrans %}{% endblock page_title %}
+{% block content %}
+{% crispy form %}
+{% endblock content %}