From 342512036d77e20a2c98f7f9181eba58f48cbf69 Mon Sep 17 00:00:00 2001 From: Levi Date: Thu, 5 May 2016 01:03:35 -0500 Subject: [PATCH] Added DjangoHostingView test, Added RailsHostingView test, Added, NodeJSHostingView test, Changed VMPlan model, Fixed templates to support new relationship between orders and VMplans, Merged Calendar feature with Booking --- dynamicweb/settings/base.py | 4 +- hosting/managers.py | 5 +- hosting/migrations/0014_auto_20160505_0541.py | 21 ++++++ hosting/models.py | 6 +- hosting/templates/hosting/order_detail.html | 5 +- .../hosting/virtual_machine_detail.html | 2 +- hosting/test_views.py | 72 +++++++++++++++++++ membership/models.py | 2 +- utils/stripe_utils.py | 4 +- 9 files changed, 106 insertions(+), 15 deletions(-) create mode 100644 hosting/migrations/0014_auto_20160505_0541.py diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index 69e708d2..9d64dc60 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -446,8 +446,8 @@ AUTH_USER_MODEL = 'membership.CustomUser' # PAYMENT -STRIPE_API_PUBLIC_KEY = 'pk_test_ZRg6P8g5ybiHE6l2RW5pSaYV' # used in frontend to call from user browser -STRIPE_API_PRIVATE_KEY = 'sk_test_uIPMdgXoRGydrcD7fkwcn7dj' # used in backend payment +STRIPE_API_PUBLIC_KEY = 'pk_test_QqBZ50Am8KOxaAlOxbcm9Psl' # used in frontend to call from user browser +STRIPE_API_PRIVATE_KEY = 'sk_test_dqAmbKAij12QCGfkYZ3poGt2' # used in backend payment STRIPE_DESCRIPTION_ON_PAYMENT = "Payment for ungleich GmbH services" # EMAIL MESSAGES diff --git a/hosting/managers.py b/hosting/managers.py index 961b2c6f..c474d61e 100644 --- a/hosting/managers.py +++ b/hosting/managers.py @@ -4,5 +4,6 @@ from django.db import models class VMPlansManager(models.Manager): def active(self, user, **kwargs): - return self.select_related('hostingorder__customer__user').\ - filter(hostingorder__customer__user=user, hostingorder__approved=True, **kwargs) + return self.prefetch_related('hosting_orders__customer__user').\ + filter(hosting_orders__customer__user=user, hosting_orders__approved=True, **kwargs)\ + .distinct() diff --git a/hosting/migrations/0014_auto_20160505_0541.py b/hosting/migrations/0014_auto_20160505_0541.py new file mode 100644 index 00000000..532b25e8 --- /dev/null +++ b/hosting/migrations/0014_auto_20160505_0541.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.4 on 2016-05-05 05:41 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('hosting', '0013_auto_20160505_0302'), + ] + + operations = [ + migrations.AlterField( + model_name='hostingorder', + name='VMPlan', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='hosting_orders', to='hosting.VirtualMachinePlan'), + ), + ] diff --git a/hosting/models.py b/hosting/models.py index 51930a12..dd686312 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -95,10 +95,6 @@ class VirtualMachinePlan(models.Model): name = 'vm-%s' % self.id return name - @cached_property - def orders(self): - return [self.hostingorder] - @classmethod def create(cls, data, user): instance = cls.objects.create(**data) @@ -110,7 +106,7 @@ class HostingOrder(models.Model): ORDER_APPROVED_STATUS = 'Approved' ORDER_DECLINED_STATUS = 'Declined' - VMPlan = models.ForeignKey(VirtualMachinePlan) + VMPlan = models.ForeignKey(VirtualMachinePlan, related_name='hosting_orders') customer = models.ForeignKey(StripeCustomer) billing_address = models.ForeignKey(BillingAddress) created_at = models.DateTimeField(auto_now_add=True) diff --git a/hosting/templates/hosting/order_detail.html b/hosting/templates/hosting/order_detail.html index 4d5dcc18..74b8d5bb 100644 --- a/hosting/templates/hosting/order_detail.html +++ b/hosting/templates/hosting/order_detail.html @@ -23,7 +23,10 @@ Order Date:
{{object.created_at}}

Status:
- {{object.status}}

+ {{object.status}} +

diff --git a/hosting/templates/hosting/virtual_machine_detail.html b/hosting/templates/hosting/virtual_machine_detail.html index 87edd71f..e51e1d4e 100644 --- a/hosting/templates/hosting/virtual_machine_detail.html +++ b/hosting/templates/hosting/virtual_machine_detail.html @@ -100,7 +100,7 @@ - {% for order in virtual_machine.orders %} + {% for order in virtual_machine.hosting_orders.all %} {{order.id}} {{order.created_at}} diff --git a/hosting/test_views.py b/hosting/test_views.py index e69de29b..b2dfe9ae 100644 --- a/hosting/test_views.py +++ b/hosting/test_views.py @@ -0,0 +1,72 @@ +from django.test import TestCase +from django.core.urlresolvers import reverse +from django.core.urlresolvers import resolve +from .models import VirtualMachineType +from .views import DjangoHostingView, RailsHostingView, NodeJSHostingView + + +class ProcessVMSelectionTestMixin(object): + + def url_resolve_to_view_correctly(self): + found = resolve(self.url) + self.assertEqual(found.func.__name__, self.view.__name__) + + def test_get(self): + response = self.client.get(self.url) + self.assertEqual(response.status_code, 200) + self.assertEqual(self.view.get_context_data(), self.expected_context) + self.assertEqual(response.context['hosting'], self.expected_context['hosting']) + self.assertTemplateUsed(response, self.expected_template) + + def test_anonymous_post(self): + response = self.client.post(self.url) + self.assertRedirects(response, expected_url=reverse('hosting:login'), + status_code=302, target_status_code=200) + + +class DjangoHostingViewTest(TestCase, ProcessVMSelectionTestMixin): + + def setUp(self): + self.url = reverse('django.hosting') + self.view = DjangoHostingView() + self.expected_template = 'hosting/django.html' + self.expected_context = { + 'hosting': "django", + 'hosting_long': "Django", + 'domain': "django-hosting.ch", + 'google_analytics': "UA-62285904-6", + 'email': "info@django-hosting.ch", + 'vm_types': VirtualMachineType.get_serialized_vm_types(), + } + + +class RailsHostingViewTest(TestCase, ProcessVMSelectionTestMixin): + + def setUp(self): + self.url = reverse('rails.hosting') + self.view = RailsHostingView() + self.expected_template = 'hosting/rails.html' + self.expected_context = { + 'hosting': "rails", + 'hosting_long': "Ruby On Rails", + 'domain': "rails-hosting.ch", + 'google_analytics': "UA-62285904-5", + 'email': "info@rails-hosting.ch", + 'vm_types': VirtualMachineType.get_serialized_vm_types(), + } + + +class NodeJSHostingViewTest(TestCase, ProcessVMSelectionTestMixin): + + def setUp(self): + self.url = reverse('node.hosting') + self.view = NodeJSHostingView() + self.expected_template = 'hosting/nodejs.html' + self.expected_context = { + 'hosting': "nodejs", + 'hosting_long': "NodeJS", + 'domain': "node-hosting.ch", + 'google_analytics': "UA-62285904-7", + 'email': "info@node-hosting.ch", + 'vm_types': VirtualMachineType.get_serialized_vm_types(), + } diff --git a/membership/models.py b/membership/models.py index a607906c..75962660 100644 --- a/membership/models.py +++ b/membership/models.py @@ -131,7 +131,7 @@ class StripeCustomer(models.Model): Check if there is a registered stripe customer with that email or create a new one """ - + stripe_customer = None try: stripe_utils = StripeUtils() stripe_customer = cls.objects.get(user__email=email) diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index c0ea630b..ce14d417 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -52,8 +52,6 @@ def handleStripeError(f): return handleProblems - - class StripeUtils(object): CURRENCY = 'chf' INTERVAL = 'month' @@ -71,7 +69,7 @@ class StripeUtils(object): customer = stripe.Customer.retrieve(id) except stripe.InvalidRequestError: customer = self.create_customer(token, user.email) - user.stripecustomer.stripe_id = customer.get('id') + user.stripecustomer.stripe_id = customer.get('response_object').get('id') user.stripecustomer.save() return customer