Skip to content

Commit

Permalink
Update Untappd plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
mik3y committed Jun 25, 2017
1 parent 36b6b1d commit 324aba9
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 100 deletions.
45 changes: 45 additions & 0 deletions pykeg/contrib/untappd/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2017 Bevbot LLC, All Rights Reserved
#
# This file is part of the Pykeg package of the Kegbot project.
# For more information on Pykeg or Kegbot, see http://kegbot.org/
#
# Pykeg is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Pykeg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pykeg. If not, see <http://www.gnu.org/licenses/>.

import requests
from requests_oauthlib import OAuth2Session


class UntappdClient:
AUTHORIZATION_URL = 'https://untappd.com/oauth/authenticate/'
ACCESS_TOKEN_URL = 'https://untappd.com/oauth/authorize/'

def __init__(self, client_id, client_secret):
self.client_id = client_id
self.client_secret = client_secret

def get_authorization_url(self, redirect_uri):
oauth = OAuth2Session(self.client_id, redirect_uri=redirect_uri)
return oauth.authorization_url(self.AUTHORIZATION_URL)

def handle_authorization_callback(self, response_url, redirect_uri):
oauth = OAuth2Session(self.client_id, redirect_uri=redirect_uri)
token = oauth.fetch_token(self.ACCESS_TOKEN_URL,
authorization_response=response_url,
client_secret=self.client_secret,
method='GET')
return token

def get_user_info(self, access_token):
return requests.get('https://api.untappd.com/v4/user/info',
params={'access_token': access_token}).json()['response']['user']
57 changes: 0 additions & 57 deletions pykeg/contrib/untappd/oauth_client.py

This file was deleted.

8 changes: 6 additions & 2 deletions pykeg/contrib/untappd/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from pykeg.plugin import plugin
from pykeg.plugin import util as plugin_util

from . import client
from . import forms
from . import tasks
from . import views
Expand All @@ -50,8 +51,8 @@ def get_user_settings_view(self):

def get_extra_user_views(self):
return [
('redirect/$', 'pykeg.contrib.untappd.views.auth_redirect', 'redirect'),
('callback/$', 'pykeg.contrib.untappd.views.auth_callback', 'callback'),
('redirect/$', views.auth_redirect, 'redirect'),
('callback/$', views.auth_callback, 'callback'),
]

def handle_new_events(self, events):
Expand Down Expand Up @@ -154,3 +155,6 @@ def get_user_token(self, user):

def save_user_token(self, user, token):
self.datastore.set('user_token:%s' % user.id, token)

def get_client(self):
return client.UntappdClient(*self.get_credentials())
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<h4>Untappd Account</h4>
<div class="well">
<p class="lead">
Untappd is currently linked to <b>{{ profile.first_name }} {{ profile.last_name }}</b>.
Untappd is currently linked to <b>{{ profile.user_name }}</b>.
</p>
<p>
<form method="post" action="{% url "plugin-untappd-redirect" %}">
Expand All @@ -30,7 +30,7 @@ <h4>Untappd Account</h4>
<h4>Link Untappd Account</h4>
<div class="well">
<p class="lead">
Link an Untappd account to your Kegbot account.
Link an Untappd account to your Kegbot account.
<p>
<form method="post" action="{% url "plugin-untappd-redirect" %}">
{% csrf_token %}
Expand All @@ -45,7 +45,7 @@ <h4>Link Untappd Account</h4>
<h4>Settings</h4>
<div class="well">
<p class="lead">
Control how Kegbot will use use your account.
Control how Kegbot will use use your account.
</p>
<form method="post" action="">
{% csrf_token %}
Expand Down
50 changes: 15 additions & 35 deletions pykeg/contrib/untappd/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.shortcuts import redirect
from socialregistration.clients.oauth import OAuthError
from pykeg.web.decorators import staff_member_required
from django.contrib.auth.decorators import login_required
from django.shortcuts import render

from . import forms
from . import oauth_client


@staff_member_required
Expand Down Expand Up @@ -79,43 +77,25 @@ def auth_redirect(request):
return redirect('account-plugin-settings', plugin_name='untappd')

plugin = request.plugins['untappd']
url = request.build_absolute_uri(reverse('plugin-untappd-callback'))
client = get_client(*plugin.get_credentials(), callback_url=url)

request.session['untappd_client'] = client

try:
return redirect(client.get_redirect_url())
except OAuthError as error:
messages.error(request, 'Error: %s' % str(error))
return redirect('account-plugin-settings', plugin_name='untappd')
client = plugin.get_client()
callback_url = request.build_absolute_uri(reverse('plugin-untappd-callback'))
url, state = client.get_authorization_url(callback_url)
return redirect(url)


@login_required
def auth_callback(request):
try:
client = request.session['untappd_client']
del request.session['untappd_client']
token = client.complete(dict(request.GET.items()))
except KeyError:
messages.error(request, 'Session expired.')
except OAuthError as error:
messages.error(request, str(error))
else:
plugin = request.plugins.get('untappd')
profile = client.get_user_info()
token = client.get_access_token()
plugin.save_user_profile(request.user, profile)
plugin.save_user_token(request.user, token)

username = '%s %s' % (profile.get('first_name'), profile.get('last_name'))
messages.success(request, 'Successfully linked to Untappd user %s' % username)
plugin = request.plugins['untappd']
client = plugin.get_client()

return redirect('account-plugin-settings', plugin_name='untappd')
callback_url = request.build_absolute_uri(reverse('plugin-untappd-callback'))
result = client.handle_authorization_callback(request.build_absolute_uri(), callback_url)
token = result['access_token']

profile = client.get_user_info(token)
username = profile['user_name']
plugin.save_user_profile(request.user, profile)
plugin.save_user_token(request.user, token)

def get_client(client_id, client_secret, callback_url):
client = oauth_client.Untappd(callback_url=callback_url)
client.client_id = client_id
client.secret = client_secret
return client
messages.success(request, 'Successfully linked to Untappd user %s' % username)
return redirect('account-plugin-settings', plugin_name='untappd')
4 changes: 1 addition & 3 deletions pykeg/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
KEGBOT_PLUGINS = [
'pykeg.contrib.foursquare.plugin.FoursquarePlugin',
'pykeg.contrib.twitter.plugin.TwitterPlugin',
# 'pykeg.contrib.untappd.plugin.UntappdPlugin',
'pykeg.contrib.untappd.plugin.UntappdPlugin',
'pykeg.contrib.webhook.plugin.WebhookPlugin',
]

Expand Down Expand Up @@ -357,8 +357,6 @@
# Update email addresses.
DEFAULT_FROM_EMAIL = EMAIL_FROM_ADDRESS

# socialregistration (after importing common settings)

if KEGBOT_ENABLE_ADMIN:
INSTALLED_APPS += ('django.contrib.admin',)

Expand Down

0 comments on commit 324aba9

Please sign in to comment.