Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django 1.5 support, Admin interface & docs, backwards compat w/1.3. #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ docs/_build/*
.DS_Store
oauth2app.egg-info/
*.pyc
build/
dist/
# eclipse specific:
.project
.pydevproject
.settings

12 changes: 12 additions & 0 deletions examples/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env python

import os
import sys

if __name__ == "__main__":

os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
32 changes: 17 additions & 15 deletions examples/mysite/apps/account/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
from django.contrib.auth import authenticate
from django.contrib.auth.forms import UserCreationForm
from uni_form.helpers import FormHelper, Submit, Reset
from oauth2app.models import AccessRange


class CreateClientForm(forms.Form):

name = forms.CharField(label="Name", max_length=30)

@property
def helper(self):
form = CreateClientForm()
helper = FormHelper()
reset = Reset('','Reset')
reset = Reset('', 'Reset')
helper.add_input(reset)
submit = Submit('','Create Client')
submit = Submit('', 'Create Client')
helper.add_input(submit)
helper.form_action = '/account/clients'
helper.form_method = 'POST'
Expand All @@ -29,34 +29,34 @@ class ClientRemoveForm(forms.Form):


class SignupForm(UserCreationForm):

email = forms.EmailField(label="Email")

@property
def helper(self):
form = SignupForm()
helper = FormHelper()
reset = Reset('','Reset')
reset = Reset('', 'Reset')
helper.add_input(reset)
submit = Submit('','Sign Up')
submit = Submit('', 'Sign Up')
helper.add_input(submit)
helper.form_action = '/account/signup'
helper.form_method = 'POST'
return helper


class LoginForm(forms.Form):

username = forms.CharField(label="Username", max_length=30)
password = forms.CharField(label="Password", widget=forms.PasswordInput)

@property
def helper(self):
form = LoginForm()
helper = FormHelper()
reset = Reset('','Reset')
reset = Reset('', 'Reset')
helper.add_input(reset)
submit = Submit('','Log In')
submit = Submit('', 'Log In')
helper.add_input(submit)
helper.form_action = '/account/login'
helper.form_method = 'POST'
Expand All @@ -69,7 +69,9 @@ def clean(self):
if username and password:
self.user_cache = authenticate(username=username, password=password)
if self.user_cache is None:
raise forms.ValidationError("Please enter a correct username and password. Note that both fields are case-sensitive.")
raise forms.ValidationError("Please enter a correct username and password. "
"Note that both fields are case-sensitive.")
elif not self.user_cache.is_active:
raise forms.ValidationError("This account is inactive.")
return self.cleaned_data
return self.cleaned_data

13 changes: 7 additions & 6 deletions examples/mysite/apps/account/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

#-*- coding: utf-8 -*-
from django.conf.urls.defaults import patterns, url
from django.conf.urls.defaults import patterns

urlpatterns = patterns('mysite.apps.account.views',
(r'^login/?$', 'login'),
(r'^logout/?$', 'logout'),
(r'^signup/?$', 'signup'),
(r'^clients/?$', 'clients'),
)
(r'^login/?$', 'login'),
(r'^logout/?$', 'logout'),
(r'^signup/?$', 'signup'),
(r'^clients/?$', 'clients'),
)
36 changes: 12 additions & 24 deletions examples/mysite/apps/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from oauth2app.models import Client, AccessRange
from oauth2app.models import Client
from .forms import SignupForm, LoginForm, CreateClientForm, ClientRemoveForm


@login_required
def clients(request):
if request.method == "POST":
Expand All @@ -25,13 +26,8 @@ def clients(request):
form = CreateClientForm()
else:
form = CreateClientForm()
template = {
"form":form,
"clients":Client.objects.filter(user=request.user)}
return render_to_response(
'account/clients.html',
template,
RequestContext(request))
template = {"form": form, "clients": Client.objects.filter(user=request.user)}
return render_to_response('account/clients.html', template, RequestContext(request))


def login(request):
Expand All @@ -45,20 +41,14 @@ def login(request):
return HttpResponseRedirect("/")
else:
form = LoginForm()
template = {"form":form}
return render_to_response(
'account/login.html',
template,
RequestContext(request))


template = {"form": form}
return render_to_response('account/login.html', template, RequestContext(request))


@login_required
def logout(request):
auth.logout(request)
return render_to_response(
'account/logout.html',
{},
RequestContext(request))
return render_to_response('account/logout.html', {}, RequestContext(request))


def signup(request):
Expand All @@ -76,8 +66,6 @@ def signup(request):
return HttpResponseRedirect("/")
else:
form = SignupForm()
template = {"form":form}
return render_to_response(
'account/signup.html',
template,
RequestContext(request))
context = {"form": form}
return render_to_response('account/signup.html', context, RequestContext(request))

10 changes: 5 additions & 5 deletions examples/mysite/apps/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#-*- coding: utf-8 -*-
from django.conf.urls.defaults import patterns, url
from django.conf.urls.defaults import patterns

urlpatterns = patterns('mysite.apps.api.views',
(r'^date_joined/?$', 'date_joined'),
(r'^last_login/?$', 'last_login'),
(r'^email/?$', 'email')
)
(r'^date_joined/?$', 'date_joined'),
(r'^last_login/?$', 'last_login'),
(r'^email/?$', 'email')
)
13 changes: 7 additions & 6 deletions examples/mysite/apps/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from oauth2app.authenticate import JSONAuthenticator, AuthenticationException
from oauth2app.models import AccessRange


def date_joined(request):
scope = AccessRange.objects.get(key="date_joined")
authenticator = JSONAuthenticator(scope=scope)
Expand All @@ -12,19 +13,19 @@ def date_joined(request):
except AuthenticationException:
return authenticator.error_response()
return authenticator.response({
"date_joined":str(authenticator.user.date_joined)})
"date_joined": str(authenticator.user.date_joined)})


def last_login(request):
scope = AccessRange.objects.get(key="last_login")
authenticator = JSONAuthenticator(scope=scope)
try:
authenticator.validate(request)
except AuthenticationException:
return authenticator.error_response()
data = {"date_joined":str(request.user.date_joined)}
#data = {"date_joined": str(request.user.date_joined)}
return authenticator.response({
"last_login":str(authenticator.user.last_login)})
"last_login": str(authenticator.user.last_login)})


def email(request):
Expand All @@ -33,4 +34,4 @@ def email(request):
authenticator.validate(request)
except AuthenticationException:
return authenticator.error_response()
return authenticator.response({"email":authenticator.user.email})
return authenticator.response({"email": authenticator.user.email})
6 changes: 3 additions & 3 deletions examples/mysite/apps/base/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#-*- coding: utf-8 -*-
from django.conf.urls.defaults import patterns, url
from django.conf.urls.defaults import patterns

urlpatterns = patterns('mysite.apps.base.views',
(r'^/?$', 'homepage'),
)
(r'^/?$', 'homepage'),
)
5 changes: 1 addition & 4 deletions examples/mysite/apps/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@ def homepage(request):
access_tokens = access_tokens.select_related()
template["access_tokens"] = access_tokens
template["clients"] = clients
return render_to_response(
'base/homepage.html',
template,
RequestContext(request))
return render_to_response('base/homepage.html', template, RequestContext(request))
12 changes: 4 additions & 8 deletions examples/mysite/apps/client/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#-*- coding: utf-8 -*-
from django.conf.urls.defaults import patterns, url

urlpatterns = patterns('',)#-*- coding: utf-8 -*-


from django.conf.urls.defaults import patterns, url
from django.conf.urls.defaults import patterns

urlpatterns = patterns('',)

urlpatterns = patterns('mysite.apps.client.views',
(r'^(?P<client_id>\w+)/?$', 'client'),
)# Create your views here.
(r'^(?P<client_id>\w+)/?$', 'client'),
) # Create your views here.
20 changes: 9 additions & 11 deletions examples/mysite/apps/client/views.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
#-*- coding: utf-8 -*-


from django.shortcuts import render_to_response
from django.template import RequestContext
from oauth2app.models import Client, AccessToken, Code
from base64 import b64encode


def client(request, client_id):
client = Client.objects.get(key=client_id)
template = {
"client":client,
"basic_auth":"Basic %s" % b64encode(client.key + ":" + client.secret),
"codes":Code.objects.filter(client=client).select_related(),
"access_tokens":AccessToken.objects.filter(client=client).select_related()}
template["error_description"] = request.GET.get("error_description")
return render_to_response(
'client/client.html',
template,
RequestContext(request))
context = {
"client": client,
"basic_auth": "Basic %s" % b64encode(client.key + ":" + client.secret),
"codes": Code.objects.filter(client=client).select_related(),
"access_tokens": AccessToken.objects.filter(client=client).select_related()}
context["error_description"] = request.GET.get("error_description")
return render_to_response('client/client.html', context, RequestContext(request))

2 changes: 1 addition & 1 deletion examples/mysite/apps/oauth2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@


class AuthorizeForm(forms.Form):
pass
pass
10 changes: 5 additions & 5 deletions examples/mysite/apps/oauth2/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#-*- coding: utf-8 -*-
from django.conf.urls.defaults import patterns, url
from django.conf.urls.defaults import patterns

urlpatterns = patterns('',
(r'^missing_redirect_uri/?$', 'mysite.apps.oauth2.views.missing_redirect_uri'),
(r'^authorize/?$', 'mysite.apps.oauth2.views.authorize'),
(r'^token/?$', 'oauth2app.token.handler'),
)
(r'^missing_redirect_uri/?$', 'mysite.apps.oauth2.views.missing_redirect_uri'),
(r'^authorize/?$', 'mysite.apps.oauth2.views.authorize'),
(r'^token/?$', 'oauth2app.token.handler'),
)
26 changes: 11 additions & 15 deletions examples/mysite/apps/oauth2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,47 @@
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from uni_form.helpers import FormHelper, Submit, Reset
from django.contrib.auth.decorators import login_required

from uni_form.helpers import FormHelper, Submit

from oauth2app.authorize import Authorizer, MissingRedirectURI, AuthorizationException
from oauth2app.authorize import UnvalidatedRequest, UnauthenticatedUser
from .forms import AuthorizeForm


@login_required
def missing_redirect_uri(request):
return render_to_response(
'oauth2/missing_redirect_uri.html',
{},
RequestContext(request))
return render_to_response('oauth2/missing_redirect_uri.html',
{}, RequestContext(request))


@login_required
def authorize(request):
authorizer = Authorizer()
try:
authorizer.validate(request)
except MissingRedirectURI, e:
except MissingRedirectURI:
return HttpResponseRedirect("/oauth2/missing_redirect_uri")
except AuthorizationException, e:
except AuthorizationException:
# The request is malformed or invalid. Automatically
# redirects to the provided redirect URL.
return authorizer.error_redirect()
if request.method == 'GET':
# Make sure the authorizer has validated before requesting the client
# or access_ranges as otherwise they will be None.
template = {
"client":authorizer.client,
"access_ranges":authorizer.access_ranges}
"client": authorizer.client,
"access_ranges": authorizer.access_ranges}
template["form"] = AuthorizeForm()
helper = FormHelper()
no_submit = Submit('connect','No')
no_submit = Submit('connect', 'No')
helper.add_input(no_submit)
yes_submit = Submit('connect', 'Yes')
helper.add_input(yes_submit)
helper.form_action = '/oauth2/authorize?%s' % authorizer.query_string
helper.form_method = 'POST'
template["helper"] = helper
return render_to_response(
'oauth2/authorize.html',
template,
RequestContext(request))
return render_to_response('oauth2/authorize.html', template, RequestContext(request))
elif request.method == 'POST':
form = AuthorizeForm(request.POST)
if form.is_valid():
Expand Down
Loading