Skip to content

Commit 2d78711

Browse files
committed
Move validate_user from views module to api module; Define api urls in handball app instead of project; implement user exists api endpoint
1 parent b3e4bc5 commit 2d78711

File tree

3 files changed

+107
-48
lines changed

3 files changed

+107
-48
lines changed

api.py

+86-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
from tastypie.authorization import DjangoAuthorization, Authorization
66
from tastypie.authentication import BasicAuthentication, Authentication, ApiKeyAuthentication
77
from handball.authorization import ManagerAuthorization
8+
from django.contrib.auth import authenticate
9+
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound, HttpResponseBadRequest
10+
from tastypie.serializers import Serializer
11+
from tastypie.utils.mime import determine_format
812

913

1014
class UnionResource(ModelResource):
@@ -142,5 +146,86 @@ class EventResource(ModelResource):
142146
class Meta:
143147
queryset = Event.objects.all()
144148
authorization = Authorization()
145-
authentication = ApiKeyAuthentication()
149+
authentication = Authentication()
146150
include_resource_uri = False
151+
152+
153+
"""
154+
Non-resource api endpoints
155+
"""
156+
157+
158+
def validate_user(request):
159+
"""
160+
Checks a user's basic auth credentials and, if valid, returns the users data
161+
"""
162+
163+
# if not request.META.get('HTTP_AUTHORIZATION'):
164+
# return HttpResponseBadRequest('No HTTP_AUTHORIZATION header found')
165+
166+
# try:
167+
# (auth_type, data) = request.META['HTTP_AUTHORIZATION'].split()
168+
# if auth_type.lower() != 'basic':
169+
# return HttpResponseBadRequest('Wrong auth type. Use basic auth!')
170+
# user_pass = base64.b64decode(data)
171+
# except:
172+
# return HttpResponseBadRequest('Could not decode auth credentials.')
173+
174+
# bits = user_pass.split(':', 1)
175+
176+
# if len(bits) != 2:
177+
# return HttpResponseBadRequest('Could not decode auth credentials.')
178+
179+
# user = authenticate(username=bits[0], password=bits[1])
180+
181+
username = request.POST['username']
182+
password = request.POST['password']
183+
184+
if not username or not password:
185+
return HttpResponseBadRequest()
186+
187+
user = authenticate(username=username, password=password)
188+
189+
if user is None or not user.is_active:
190+
return HttpResponseNotFound('User does not exist or password incorrect.')
191+
192+
person = user.get_profile()
193+
194+
person_resource = PersonResource()
195+
bundle = person_resource.build_bundle(obj=person, request=request)
196+
person_resource.full_dehydrate(bundle)
197+
bundle.data['api_key'] = user.api_key.key
198+
199+
return HttpResponse(person_resource.serialize(None, bundle, 'application/json'))
200+
201+
202+
def user_exists(request):
203+
username = request.GET['username']
204+
email = request.GET['email']
205+
username_exists = True
206+
email_exists = True
207+
208+
try:
209+
User.objects.get(username=username)
210+
except User.DoesNotExist:
211+
username_exists = False
212+
except User.MultipleObjectsReturned:
213+
email_exists = True
214+
215+
try:
216+
User.objects.get(email=email)
217+
except User.DoesNotExist:
218+
email_exists = False
219+
except User.MultipleObjectsReturned:
220+
email_exists = True
221+
222+
data = {
223+
'username': username_exists,
224+
'email': email_exists
225+
}
226+
227+
serializer = Serializer()
228+
229+
format = determine_format(request, serializer, default_format='application/json')
230+
231+
return HttpResponse(serializer.serialize(data, format, {}))

urls.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
from django.conf.urls.defaults import *
2+
from tastypie.api import Api
3+
from handball.api import *
24

35

6+
v1_api = Api(api_name='v1')
7+
v1_api.register(UnionResource())
8+
v1_api.register(ClubResource())
9+
v1_api.register(TeamResource())
10+
v1_api.register(UserResource())
11+
v1_api.register(PersonResource())
12+
v1_api.register(GameResource())
13+
v1_api.register(LeagueResource())
14+
415
urlpatterns = patterns('handball.views',
516
(r'^$', 'index'),
617
(r'^auth/signup/$', 'sign_up'),
718
(r'^auth/activate/([abcdef0123456789]+)$', 'activate'),
8-
(r'^thanks/$', 'thanks'),
9-
(r'^auth/validate/$', 'validate_user')
19+
(r'^thanks/$', 'thanks')
20+
)
21+
22+
urlpatterns += patterns('', (r'^api/', include(v1_api.urls)))
23+
24+
# Non-resource api endpoints
25+
urlpatterns += patterns('handball.api',
26+
(r'^api/v1/auth/validate/$', 'validate_user'),
27+
(r'^api/v1/auth/exists/$', 'user_exists')
28+
#(r'^api/v1/auth/signup/$', 'sign_up')
1029
)

views.py

-45
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from handball.forms import SignUpForm
99
from handball.models import Person
1010
from handball.api import PersonResource
11-
from django.contrib.auth import authenticate
1211
from django.contrib.auth.models import User
1312
from django.utils.translation import ugettext as _
1413

@@ -73,47 +72,3 @@ def activate(request, activation_key):
7372

7473
def thanks(request):
7574
return render_to_response('thanks.html')
76-
77-
78-
def validate_user(request):
79-
"""
80-
Checks a user's basic auth credentials and, if valid, returns the users data
81-
"""
82-
83-
# if not request.META.get('HTTP_AUTHORIZATION'):
84-
# return HttpResponseBadRequest('No HTTP_AUTHORIZATION header found')
85-
86-
# try:
87-
# (auth_type, data) = request.META['HTTP_AUTHORIZATION'].split()
88-
# if auth_type.lower() != 'basic':
89-
# return HttpResponseBadRequest('Wrong auth type. Use basic auth!')
90-
# user_pass = base64.b64decode(data)
91-
# except:
92-
# return HttpResponseBadRequest('Could not decode auth credentials.')
93-
94-
# bits = user_pass.split(':', 1)
95-
96-
# if len(bits) != 2:
97-
# return HttpResponseBadRequest('Could not decode auth credentials.')
98-
99-
# user = authenticate(username=bits[0], password=bits[1])
100-
101-
username = request.POST['username']
102-
password = request.POST['password']
103-
104-
if not username or not password:
105-
return HttpResponseBadRequest()
106-
107-
user = authenticate(username=username, password=password)
108-
109-
if user is None or not user.is_active:
110-
return HttpResponseNotFound('User does not exist or password incorrect.')
111-
112-
person = user.get_profile()
113-
114-
person_resource = PersonResource()
115-
bundle = person_resource.build_bundle(obj=person, request=request)
116-
person_resource.full_dehydrate(bundle)
117-
bundle.data['api_key'] = user.api_key.key
118-
119-
return HttpResponse(person_resource.serialize(None, bundle, 'application/json'))

0 commit comments

Comments
 (0)