Skip to content

Commit

Permalink
exempted for user.
Browse files Browse the repository at this point in the history
  • Loading branch information
soasme committed Apr 28, 2017
1 parent 01fd591 commit 6cd82b2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
15 changes: 10 additions & 5 deletions wikisensei/subscription/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@
from __future__ import unicode_literals

from django.contrib import admin
from .models import Customer, Subscription, SubscriptionEvent, WebhookEvent
from django_extensions.admin import ForeignKeyAutocompleteAdmin
from .models import Customer, Subscription, SubscriptionEvent, WebhookEvent, SubscriptionExemption

class CustomerAdmin(admin.ModelAdmin):
class CustomerAdmin(ForeignKeyAutocompleteAdmin):
pass

class SubscriptionAdmin(admin.ModelAdmin):
class SubscriptionAdmin(ForeignKeyAutocompleteAdmin):
pass

class SubscriptionEventAdmin(admin.ModelAdmin):
class SubscriptionEventAdmin(ForeignKeyAutocompleteAdmin):
pass

class WebhookEventAdmin(admin.ModelAdmin):
class WebhookEventAdmin(ForeignKeyAutocompleteAdmin):
pass

class SubscriptionExemptionAdmin(ForeignKeyAutocompleteAdmin):
pass

admin.site.register(Customer, CustomerAdmin)
admin.site.register(Subscription, SubscriptionAdmin)
admin.site.register(SubscriptionEvent, SubscriptionEventAdmin)
admin.site.register(WebhookEvent, WebhookEventAdmin)
admin.site.register(SubscriptionExemption, SubscriptionExemptionAdmin)
26 changes: 26 additions & 0 deletions wikisensei/subscription/migrations/0004_subscriptionexemption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-04-27 10:24
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('subscription', '0003_webhookevent'),
]

operations = [
migrations.CreateModel(
name='SubscriptionExemption',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
7 changes: 7 additions & 0 deletions wikisensei/subscription/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ class Subscription(models.Model):
def __str__(self):
return 'subscription-' + self.user.username

class SubscriptionExemption(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)

def __str__(self):
return 'exemption-' + self.user.username

class SubscriptionEvent(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
data = models.TextField()
Expand Down
11 changes: 10 additions & 1 deletion wikisensei/subscription/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
from django.conf import settings
import stripe

from .models import Customer, Subscription, SubscriptionEvent
from .models import (
Customer, Subscription, SubscriptionEvent,
WebhookEvent, SubscriptionExemption,
)

stripe.api_key = settings.STRIPE['secret_key']

Expand Down Expand Up @@ -116,3 +119,9 @@ def add_webhook_event(json_data):
query = dict(stripe_id=id, data=json.dumps(json_data))
event, _ = WebhookEvent.objects.get_or_create(**query)
return event

def is_exempted(user):
try:
return bool(SubscriptionExemption.objects.get(user=user))
except SubscriptionExemption.DoesNotExist:
return False
4 changes: 4 additions & 0 deletions wikisensei/web/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from wikisensei.wiki.services import is_private as is_wiki_private
from wikisensei.prof.services import is_all_wikis_private
from wikisensei.subscription.services import get_subscription_by_user
from wikisensei.subscription.services import is_exempted

class ViewPrivateWikiPermission(permissions.BasePermission):
message = 'Viewing private wiki not allowed.'
Expand Down Expand Up @@ -32,6 +33,9 @@ def has_permission(self, request, view):
if not user.is_authenticated:
return False

if is_exempted(user):
return True

if not get_subscription_by_user(user):
return False

Expand Down
2 changes: 2 additions & 0 deletions wikisensei/web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def get(self, request, pk, version=None):
'title': self.page_title or wiki.title,
})



class WikiCreate(APIView):
renderer_classes = [TemplateHTMLRenderer]
template_name = 'wiki/create.html'
Expand Down

0 comments on commit 6cd82b2

Please sign in to comment.