Skip to content

Commit

Permalink
Merge c610b23 into 285a7f9
Browse files Browse the repository at this point in the history
  • Loading branch information
atodorov committed Dec 9, 2020
2 parents 285a7f9 + c610b23 commit accb2ef
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
9 changes: 9 additions & 0 deletions tcms_github_marketplace/tests/test_views_createview.py
Expand Up @@ -110,6 +110,15 @@ def test_invalid_schema_name_shows_errors(self):
self.assertFalse(
tcms_tenants.models.Tenant.objects.filter(schema_name='kiwi-tcms').exists())

def test_visit_not_logged_in(self):
self.client.logout()
response = self.client.get(self.create_tenant_url)

# requires login
self.assertRedirects(
response,
reverse('tcms-login') + '?next=%s' % self.create_tenant_url)

def test_visit_without_purchase(self):
"""
If user visits the Create Tenant page
Expand Down
24 changes: 13 additions & 11 deletions tcms_github_marketplace/views.py
Expand Up @@ -230,26 +230,28 @@ def get(self, request, *args, **kwargs): # pylint: disable=unused-argument

@method_decorator(login_required, name='dispatch')
class CreateTenant(NewTenantView):
purchase = None
organization = None

def dispatch(self, request, *args, **kwargs):
"""
Jump over NewTenantView class b/c it requires the tcms_tenants.add_tenant
permission while on Marketplace we allow everyone who had paid their subscription
to create tenants!
"""
return super(NewTenantView, self).dispatch(request, *args, **kwargs) # pylint: disable=bad-super-call

def setup(self, request, *args, **kwargs):
super().setup(request, *args, **kwargs)

# we take the most recent purchase event for this user
# where they purchase a paid plan
# pylint: disable=attribute-defined-outside-init
self.purchase = Purchase.objects.filter(
sender=request.user.email,
action='purchased',
payload__marketplace_purchase__plan__monthly_price_in_cents__gt=0,
).order_by('-received_on').first()
self.organization = utils.organization_from_purchase(self.purchase)
if not self.purchase:
self.purchase = Purchase.objects.filter(
sender=request.user.email,
action='purchased',
payload__marketplace_purchase__plan__monthly_price_in_cents__gt=0,
).order_by('-received_on').first()
if not self.organization:
self.organization = utils.organization_from_purchase(self.purchase)

return super(NewTenantView, self).dispatch(request, *args, **kwargs) # pylint: disable=bad-super-call

def get(self, request, *args, **kwargs):
"""
Expand Down

0 comments on commit accb2ef

Please sign in to comment.