Skip to content

Commit a0b45c7

Browse files
author
Marco DiDomenico
committed
add decorator and consistant file comments
1 parent 032cba8 commit a0b45c7

File tree

15 files changed

+66
-17
lines changed

15 files changed

+66
-17
lines changed

README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ In templates
6363

6464
Note: you can pass `next` query parameter to the authorize view to direct the user to correct page after OAuth flow has completed successfully. Default will send user to '/'
6565

66+
In views as a decorator: this will kick off the Authorization flow or Refresh request (if token is expired) and will send the user back to the original requested url on completion
67+
::
68+
from replyify_oauth2.decorators import replyify_auth_required
69+
70+
@replyify_auth_required
71+
def my_view_that_needs_replyify(request):
72+
...
6673

6774
Using the Replyify API
6875
----------------------
File renamed without changes.

replyify_oauth2/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
#
55
# This file is subject to the terms and conditions defined in
6-
# file 'LICENSE.md', which is part of this source code package.
6+
# file 'LICENSE', which is part of this source code package.
77
#
8-
98
from django import VERSION as DJANGO_VERSION
109
if DJANGO_VERSION >= (1, 7):
1110
default_app_config = 'replyify_oauth2.apps.ReplyifyOAuth2Config'

replyify_oauth2/admin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
#
55
# This file is subject to the terms and conditions defined in
6-
# file 'LICENSE.md', which is part of this source code package.
6+
# file 'LICENSE', which is part of this source code package.
77
#
8-
98
from django.contrib import admin
109
from django.contrib.auth import get_user_model
1110
from . import settings

replyify_oauth2/apps.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
#
55
# This file is subject to the terms and conditions defined in
6-
# file 'LICENSE.md', which is part of this source code package.
6+
# file 'LICENSE', which is part of this source code package.
77
#
8-
98
from __future__ import unicode_literals
109
from django import VERSION as DJANGO_VERSION
1110

replyify_oauth2/decorators.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
#
5+
# This file is subject to the terms and conditions defined in
6+
# file 'LICENSE', which is part of this source code package.
7+
#
8+
from functools import wraps
9+
from django.core.urlresolvers import reverse
10+
from django.shortcuts import redirect
11+
from .models import Credentials
12+
13+
14+
def replyify_auth_required(func):
15+
def decorator(func):
16+
def inner_decorator(request, *args, **kwargs):
17+
try:
18+
if Credentials.objects.get(user=request.user).is_valid():
19+
return func(request, *args, **kwargs)
20+
except Credentials.DoesNotExsit:
21+
return redirect(reverse('replyfy:authorize') + '?next=' + request.GET.get('next', request.path))
22+
return redirect(reverse('replyfy:refresh') + '?next=' + request.GET.get('next', request.path))
23+
return wraps(func)(inner_decorator)
24+
return decorator(func)

replyify_oauth2/migrations/0001_initial.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
#!/usr/bin/env python
12
# -*- coding: utf-8 -*-
2-
# Generated by Django 1.11 on 2017-04-11 21:18
3+
4+
#
5+
# This file is subject to the terms and conditions defined in
6+
# file 'LICENSE', which is part of this source code package.
7+
#
38
from __future__ import unicode_literals
49

510
from django.conf import settings

replyify_oauth2/migrations/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
#
55
# This file is subject to the terms and conditions defined in
6-
# file 'LICENSE.md', which is part of this source code package.
6+
# file 'LICENSE', which is part of this source code package.
77
#

replyify_oauth2/models.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
#
55
# This file is subject to the terms and conditions defined in
6-
# file 'LICENSE.md', which is part of this source code package.
6+
# file 'LICENSE', which is part of this source code package.
77
#
8-
98
from __future__ import unicode_literals
109
from django.conf import settings
1110
from django.db import models
@@ -22,10 +21,13 @@ class Credentials(models.Model):
2221
updated = models.DateTimeField(auto_now=True)
2322
created = models.DateTimeField(auto_now_add=True)
2423

25-
def expired(self):
24+
def is_expired(self):
2625
if timezone.now() > self.expires:
2726
return True
2827
return False
2928

29+
def is_valid(self):
30+
return not self.is_expired()
31+
3032
def __unicode__(self):
3133
return '<Replyify Creds: {}>'.format(self.access_token)

replyify_oauth2/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#
55
# This file is subject to the terms and conditions defined in
6-
# file 'LICENSE.md', which is part of this source code package.
6+
# file 'LICENSE', which is part of this source code package.
77
#
88
from django.conf import settings
99

0 commit comments

Comments
 (0)