Don't use request.project in the pricing page cc: #336

This commit is contained in:
Pēteris Caune 2020-02-26 10:27:45 +02:00
parent bb808852d9
commit 9c3f7101db
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
6 changed files with 40 additions and 25 deletions

View File

@ -8,18 +8,27 @@ class PricingTestCase(BaseTestCase):
def test_anonymous(self):
r = self.client.get("/pricing/")
self.assertContains(r, "Unlimited Team Members", status_code=200)
self.assertNotContains(r, "jumbotron")
# A subscription object should have NOT been created
assert Subscription.objects.count() == 0
self.assertFalse(Subscription.objects.exists())
def test_authenticated(self):
self.client.login(username="alice@example.org", password="password")
r = self.client.get("/pricing/")
self.assertContains(r, "Unlimited Team Members", status_code=200)
self.assertContains(r, "jumbotron")
# A subscription object still should have NOT been created
assert Subscription.objects.count() == 0
self.assertFalse(Subscription.objects.exists())
def test_authenticated_for_project(self):
self.client.login(username="alice@example.org", password="password")
r = self.client.get("/projects/%s/pricing/" % self.project.code)
self.assertContains(r, "Unlimited Team Members", status_code=200)
self.assertContains(r, "jumbotron")
@override_settings(USE_PAYMENTS=True)
def test_pricing_is_visible_for_all(self):
@ -32,18 +41,9 @@ class PricingTestCase(BaseTestCase):
def test_it_offers_to_switch(self):
self.client.login(username="bob@example.org", password="password")
r = self.client.get("/pricing/")
r = self.client.get("/projects/%s/pricing/" % self.project.code)
self.assertContains(r, "To manage billing for this project")
def test_it_handles_null_project(self):
self.profile.current_project = None
self.profile.save()
self.client.login(username="alice@example.org", password="password")
r = self.client.get("/pricing/")
self.assertContains(r, "Unlimited Team Members")
def test_it_shows_active_plan(self):
self.sub = Subscription(user=self.alice)
self.sub.subscription_id = "test-id"
@ -55,3 +55,6 @@ class PricingTestCase(BaseTestCase):
r = self.client.get("/pricing/")
self.assertContains(r, "Business ($20 / month)", status_code=200)
r = self.client.get("/projects/%s/pricing/" % self.project.code)
self.assertContains(r, "Business ($20 / month)", status_code=200)

View File

@ -3,6 +3,7 @@ from django.urls import path
from . import views
urlpatterns = [
path("projects/<uuid:code>/pricing/", views.pricing, name="hc-p-pricing"),
path("pricing/", views.pricing, name="hc-pricing"),
path("accounts/profile/billing/", views.billing, name="hc-billing"),
path(

View File

@ -1,10 +1,11 @@
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseBadRequest, JsonResponse
from django.http import Http404, HttpResponseBadRequest, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.views.decorators.http import require_POST
from hc.api.models import Check
from hc.front.views import _get_project_for_user
from hc.payments.forms import InvoiceEmailingForm
from hc.payments.models import Subscription
@ -15,18 +16,26 @@ def token(request):
return JsonResponse({"client_token": sub.get_client_token()})
def pricing(request):
if request.user.is_authenticated:
if request.project and request.project.owner != request.user:
ctx = {"page": "pricing"}
def pricing(request, code=None):
project = None
if code:
if not request.user.is_authenticated:
raise Http404()
project = _get_project_for_user(request, code)
if project.owner != request.user:
ctx = {"page": "pricing", "project": project}
return render(request, "payments/pricing_not_owner.html", ctx)
# Don't use Subscription.objects.for_user method here, so a
# subscription object is not created just by viewing a page.
sub = Subscription.objects.filter(user_id=request.user.id).first()
sub = None
if request.user.is_authenticated:
# Don't use Subscription.objects.for_user method here, so a
# subscription object is not created just by viewing a page.
sub = Subscription.objects.filter(user_id=request.user.id).first()
ctx = {
"page": "pricing",
"project": project,
"sub": sub,
"enable_whatsapp": settings.TWILIO_USE_WHATSAPP,
}

View File

@ -119,7 +119,11 @@
<ul id="global-links" class="nav navbar-nav navbar-right">
{% if show_pricing %}
<li {% if page == 'pricing' %} class="active" {% endif %}>
{% if project %}
<a href="{% url 'hc-p-pricing' project.code %}">Pricing</a>
{% else %}
<a href="{% url 'hc-pricing' %}">Pricing</a>
{% endif %}
</li>
{% endif %}

View File

@ -12,9 +12,7 @@
{% endblock %}
{% block content %}
<!-- Plans -->
<section id="plans" {% if request.user.is_authenticated %} data- {% endif %}>
<section id="plans">
<div class="container">
{% if request.user.is_authenticated %}
<div class="row">

View File

@ -11,10 +11,10 @@
<div class="col-md-12">
<div class="jumbotron">
<p>
You are currently viewing project <strong>{{ request.project }}</strong>.
You are currently viewing project <strong>{{ project }}</strong>.
</p>
<p>
To manage billing for this project, please log in as <strong>{{ request.project.owner.email }}</strong>.
To manage billing for this project, please log in as <strong>{{ project.owner.email }}</strong>.
</p>
<br />