Skip to content

Commit 45e4460

Browse files
author
Marco DiDomenico
committed
update admin and migrations
1 parent 059bbd0 commit 45e4460

File tree

9 files changed

+51
-31
lines changed

9 files changed

+51
-31
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
include LICENSE
22
include README.rst
3+
recursive-include docs *

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ And add the following to your `urls.py`
4141
...
4242
)
4343

44+
In your templates you can initiate the OAuth2 workflow like this:
45+
46+
<a href="{% url 'replyify:authorize' %}">Connect to Replyify</a>
47+
4448

4549

4650

README.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ Configure the django-replyify-oauth2 module
3232
-------------------------------------------
3333

3434
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
35+
- REPLYIFY_CLIENT_ID
36+
- REPLYIFY_CLIENT_SECRET
37+
- REPLYIFY_REDIRECT_URI
38+
- REPLYIFY_USER_ID_FIELD
3939

40-
And add the following to your :: urls.py
40+
And add the following to your `urls.py`
4141
::
4242
urlpatterns = patterns(
43-
...
44-
url(r'^replyify/', include('replyify_oauth2.urls', namespace='replyify')),
45-
...
46-
)
43+
...
44+
url(r'^replyify/', include('replyify_oauth2.urls', namespace='replyify')),
45+
...
46+
)
4747

4848

4949
Using the Replyify API

docs/.gitkeep

Whitespace-only changes.

replyify_oauth2/admin.py

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

99
from django.contrib import admin
10+
from django.contrib.auth import get_user_model
11+
from django.conf import settings
1012
from .models import Credentials
1113

12-
admin.site.register(Credentials)
14+
15+
def user_search_fields():
16+
User = get_user_model()
17+
USERNAME_FIELD = getattr(User, 'USERNAME_FIELD', None)
18+
fields = []
19+
if USERNAME_FIELD is not None:
20+
# Using a Django 1.5+ User model
21+
fields = [
22+
'user__{}'.format(USERNAME_FIELD),
23+
'user__{}'.format(settings.REPLYIFY_USER_ID_FIELD)
24+
]
25+
return fields
26+
27+
28+
class CredentialsAdmin(admin.ModelAdmin):
29+
search_fields = ['access_token'] + user_search_fields()
30+
raw_id_fields = ('user',)
31+
32+
33+
admin.site.register(Credentials, CredentialsAdmin)

replyify_oauth2/apps.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414

1515
class ReplyifyOAuth2Config(AppConfig):
1616
name = 'replyify_oauth2'
17+
verbose_name = 'Replyify OAuth2'
18+
label = 'replyify_oauth2'

replyify_oauth2/migrations/0001_initial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Migration(migrations.Migration):
1616
migrations.CreateModel(
1717
name='Credentials',
1818
fields=[
19-
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
19+
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True, related_name='replyify_credentials')),
2020
('user', models.OneToOneField(null=True, to=settings.AUTH_USER_MODEL)),
2121
('access_token', models.CharField(max_length=50)),
2222
('refresh_token', models.CharField(max_length=50)),

replyify_oauth2/models.py

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

1414

1515
class Credentials(models.Model):
16-
user = models.OneToOneField(getattr(settings, 'AUTH_USER_MODEL', 'auth.User'), on_delete=models.CASCADE)
16+
user = models.OneToOneField(getattr(settings, 'AUTH_USER_MODEL', 'auth.User'), on_delete=models.CASCADE, related_name='replyify_credentials')
1717
access_token = models.CharField(max_length=50)
1818
refresh_token = models.CharField(max_length=50)
1919
expires = models.DateTimeField(default=timezone.now)

replyify_oauth2/views.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
# This file is subject to the terms and conditions defined in
66
# file 'LICENSE.md', which is part of this source code package.
77
#
8-
9-
try:
10-
from . import settings
11-
except ImportError:
12-
import settings
138
from django.contrib.auth.decorators import login_required
149
from django.http import HttpResponse
1510
from django.shortcuts import redirect
1611
from django.utils import timezone
1712
from django.utils.crypto import get_random_string
1813
from .models import Credentials
14+
from . import settings
1915
from datetime import timedelta
2016
import requests
2117

@@ -51,7 +47,16 @@ def callback(request=None):
5147
raise Exception(request.GET['error'])
5248

5349
uid = _check_state(request)
54-
data = _exchange_auth_code(request)
50+
code = request.GET['code']
51+
data = {
52+
'grant_type': 'authorization_code',
53+
'code': code,
54+
'client_id': settings.REPLYIFY_CLIENT_ID,
55+
'redirect_uri': settings.REPLYIFY_REDIRECT_URI
56+
}
57+
url = settings.REPLYIFY_TOKEN_URL
58+
r = requests.post(url=url, data=data)
59+
data = r.json()
5560
creds = _store_credentials(uid, data)
5661

5762
return HttpResponse(creds)
@@ -98,19 +103,6 @@ def _check_state(request=None):
98103
return uid
99104

100105

101-
def _exchange_auth_code(request=None):
102-
code = request.GET['code']
103-
data = {
104-
'grant_type': 'authorization_code',
105-
'code': code,
106-
'client_id': settings.REPLYIFY_CLIENT_ID,
107-
'redirect_uri': settings.REPLYIFY_REDIRECT_URI
108-
}
109-
url = settings.REPLYIFY_TOKEN_URL
110-
r = requests.post(url=url, data=data)
111-
return r.json()
112-
113-
114106
def _store_credentials(user, data=None):
115107
assert user is not None
116108
creds, _ = Credentials.objects.get_or_create(user=user)

0 commit comments

Comments
 (0)