Skip to content

Commit 059bbd0

Browse files
author
Marco DiDomenico
committed
change settings name and import setup
1 parent 7f3089a commit 059bbd0

File tree

7 files changed

+84
-73
lines changed

7 files changed

+84
-73
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,21 @@ Register Your Application with Replyify
2727
Configure the django-replyify-oauth2 module
2828
-------------------------------------------
2929

30-
From your previously configured app, found at https://app.replyify.com/oauth2/applications:
31-
* _settings.REPLYIFY_CLIENT_ID
32-
* _settings.REPLYIFY_CLIENT_SECRET
33-
* _settings.REPLYIFY_REDIRECT_URI
30+
From your previously configured app, found at https://app.replyify.com/oauth2/applications add the following to your Django settings:
31+
* REPLYIFY_CLIENT_ID
32+
* REPLYIFY_CLIENT_SECRET
33+
* REPLYIFY_REDIRECT_URI
34+
* REPLYIFY_USER_ID_FIELD
35+
36+
And add the following to your `urls.py`
37+
38+
urlpatterns = patterns(
39+
...
40+
url(r'^replyify/', include('replyify_oauth2.urls', namespace='replyify')),
41+
...
42+
)
43+
44+
3445

3546

3647
Using the Replyify API

README.rst

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,20 @@ Register Your Application with Replyify
3131
Configure the django-replyify-oauth2 module
3232
-------------------------------------------
3333

34-
From your previously configured app, found at
35-
https://app.replyify.com/oauth2/applications: \*
36-
\_settings.REPLYIFY\_CLIENT\_ID \* \_settings.REPLYIFY\_CLIENT\_SECRET
37-
\* \_settings.REPLYIFY\_REDIRECT\_URI
34+
From your previously configured app, found at https://app.replyify.com/oauth2/applications add the following to your Django settings:
35+
* REPLYIFY_CLIENT_ID
36+
* REPLYIFY_CLIENT_SECRET
37+
* REPLYIFY_REDIRECT_URI
38+
* REPLYIFY_USER_ID_FIELD
39+
40+
And add the following to your :: urls.py
41+
::
42+
urlpatterns = patterns(
43+
...
44+
url(r'^replyify/', include('replyify_oauth2.urls', namespace='replyify')),
45+
...
46+
)
47+
3848

3949
Using the Replyify API
4050
----------------------

replyify_oauth2/_settings.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

replyify_oauth2/migrations/0001_initial.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33

44
from django.db import migrations, models
55
import django.utils.timezone
6+
from django.conf import settings
67

78

89
class Migration(migrations.Migration):
910

1011
dependencies = [
12+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
1113
]
1214

1315
operations = [
1416
migrations.CreateModel(
1517
name='Credentials',
1618
fields=[
1719
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
18-
('uid', models.CharField(max_length=50)),
20+
('user', models.OneToOneField(null=True, to=settings.AUTH_USER_MODEL)),
1921
('access_token', models.CharField(max_length=50)),
2022
('refresh_token', models.CharField(max_length=50)),
2123
('expires', models.DateTimeField(default=django.utils.timezone.now)),

replyify_oauth2/models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,25 @@
77
#
88

99
from __future__ import unicode_literals
10+
from django.conf import settings
1011
from django.db import models
1112
from django.utils import timezone
1213

1314

1415
class Credentials(models.Model):
15-
uid = models.CharField(max_length=50)
16+
user = models.OneToOneField(getattr(settings, 'AUTH_USER_MODEL', 'auth.User'), on_delete=models.CASCADE)
1617
access_token = models.CharField(max_length=50)
1718
refresh_token = models.CharField(max_length=50)
1819
expires = models.DateTimeField(default=timezone.now)
1920
scope = models.CharField(max_length=50)
2021
token_type = models.CharField(max_length=50)
22+
updated = models.DateTimeField(auto_now=True)
23+
created = models.DateTimeField(auto_now_add=True)
2124

2225
def expired(self):
2326
if timezone.now() > self.expires:
2427
return True
2528
return False
29+
30+
def __unicode__(self):
31+
return '<Replyify Creds: {}>'.format(self.access_token)

replyify_oauth2/settings.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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.md', which is part of this source code package.
7+
#
8+
from django.conf import settings
9+
10+
REPLYIFY_CLIENT_ID = getattr(settings, 'REPLYIFY_CLIENT_ID', None)
11+
REPLYIFY_CLIENT_SECRET = getattr(settings, 'REPLYIFY_CLIENT_SECRET', None)
12+
REPLYIFY_REDIRECT_URI = getattr(settings, 'REPLYIFY_REDIRECT_URI', None)
13+
14+
REPLYIFY_AUTH_URL = getattr(settings, 'REPLYIFY_AUTH_URL', 'https://app.replyify.com/oauth2/authorize')
15+
REPLYIFY_TOKEN_URL = getattr(settings, 'REPLYIFY_TOKEN_URL', 'https://app.replyify.com/oauth2/token')
16+
17+
REPLYIFY_USER_ID_FIELD = getattr(settings, 'REPLYIFY_USER_ID_FIELD', 'id')

replyify_oauth2/views.py

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
# file 'LICENSE.md', which is part of this source code package.
77
#
88

9-
from datetime import timedelta
10-
11-
import requests
9+
try:
10+
from . import settings
11+
except ImportError:
12+
import settings
1213
from django.contrib.auth.decorators import login_required
1314
from django.http import HttpResponse
1415
from django.shortcuts import redirect
1516
from django.utils import timezone
1617
from django.utils.crypto import get_random_string
17-
18-
import _settings
1918
from .models import Credentials
19+
from datetime import timedelta
20+
import requests
2021

2122

2223
def index(request=None):
@@ -25,9 +26,9 @@ def index(request=None):
2526

2627
@login_required
2728
def authorize(request=None):
28-
uid = _get_uid(request)
29-
client_id = _settings.REPLYIFY_CLIENT_ID
30-
redirect_uri = _settings.REPLYIFY_REDIRECT_URI
29+
uid = getattr(request.user, settings.REPLYIFY_USER_ID_FIELD)
30+
client_id = settings.REPLYIFY_CLIENT_ID
31+
redirect_uri = settings.REPLYIFY_REDIRECT_URI
3132
response_type = 'code'
3233

3334
state = get_random_string(20, "abcdefghijklmnopqrstuvwxyz0123456789")
@@ -39,7 +40,7 @@ def authorize(request=None):
3940
"response_type={0}".format(response_type),
4041
"state={0}".format(state)
4142
]
42-
url = "{0}?{1}".format(_settings.REPLYIFY_AUTH_URL, "&".join(args))
43+
url = "{0}?{1}".format(settings.REPLYIFY_AUTH_URL, "&".join(args))
4344

4445
return redirect(url)
4546

@@ -61,39 +62,29 @@ def refresh(request=None):
6162
if 'error' in request.GET:
6263
raise Exception(request.GET['error'])
6364

64-
uid = _get_uid(request)
65-
6665
try:
67-
creds = Credentials.objects.get(uid=uid)
66+
creds = Credentials.objects.get(user=request.user)
6867

6968
data = {
7069
'grant_type': 'refresh_token',
71-
'client_id': _settings.REPLYIFY_CLIENT_ID,
72-
'client_secret': _settings.REPLYIFY_CLIENT_SECRET,
70+
'client_id': settings.REPLYIFY_CLIENT_ID,
71+
'client_secret': settings.REPLYIFY_CLIENT_SECRET,
7372
'refresh_token': creds.refresh_token
7473
}
7574

76-
url = _settings.REPLYIFY_TOKEN_URL
75+
url = settings.REPLYIFY_TOKEN_URL
7776
r = requests.post(url=url, data=data)
7877
data = r.json()
7978

80-
creds = _store_credentials(uid=uid, data=data)
79+
creds = _store_credentials(user=request.user, data=data)
8180
return HttpResponse(creds)
8281

8382
except Credentials.DoesNotExist:
8483
authorize(request)
8584

8685

87-
def _get_uid(request=None):
88-
if hasattr(request.user, 'guid'):
89-
uid = request.user.guid
90-
else:
91-
uid = request.user.id
92-
return uid
93-
94-
9586
def _check_state(request=None):
96-
uid = _get_uid(request)
87+
uid = getattr(request.user, settings.REPLYIFY_USER_ID_FIELD)
9788
msg = "Something fishy is happening. Abort ..."
9889

9990
if 'state' not in request.session:
@@ -112,32 +103,21 @@ def _exchange_auth_code(request=None):
112103
data = {
113104
'grant_type': 'authorization_code',
114105
'code': code,
115-
'client_id': _settings.REPLYIFY_CLIENT_ID,
116-
'redirect_uri': _settings.REPLYIFY_REDIRECT_URI
106+
'client_id': settings.REPLYIFY_CLIENT_ID,
107+
'redirect_uri': settings.REPLYIFY_REDIRECT_URI
117108
}
118-
url = _settings.REPLYIFY_TOKEN_URL
109+
url = settings.REPLYIFY_TOKEN_URL
119110
r = requests.post(url=url, data=data)
120111
return r.json()
121112

122113

123-
def _store_credentials(uid=None, data=None):
124-
try:
125-
creds = Credentials.objects.get(uid=uid)
126-
creds.access_token = data['access_token']
127-
creds.refresh_token = data['refresh_token']
128-
creds.expires = timezone.now() + timedelta(seconds=data['expires_in'])
129-
creds.scope = data['scope']
130-
creds.token_type = data['token_type']
131-
creds.save()
132-
133-
except Credentials.DoesNotExist:
134-
creds = Credentials.objects.create(
135-
uid=uid,
136-
access_token=data['access_token'],
137-
refresh_token=data['refresh_token'],
138-
expires=timezone.now() + timedelta(seconds=data['expires_in']),
139-
scope=data['scope'],
140-
token_type=data['token_type']
141-
)
142-
114+
def _store_credentials(user, data=None):
115+
assert user is not None
116+
creds, _ = Credentials.objects.get_or_create(user=user)
117+
creds.access_token = data['access_token']
118+
creds.refresh_token = data['refresh_token']
119+
creds.expires = timezone.now() + timedelta(seconds=data['expires_in'])
120+
creds.scope = data['scope']
121+
creds.token_type = data['token_type']
122+
creds.save()
143123
return creds

0 commit comments

Comments
 (0)