Skip to content

Commit 9c89487

Browse files
committed
Finish implementing signup api backend
1 parent 6e32e0a commit 9c89487

File tree

4 files changed

+51
-33
lines changed

4 files changed

+51
-33
lines changed

Diff for: api.py

+30-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
import sha
2+
import datetime
3+
from random import random
4+
import pytz
15
from tastypie.resources import ModelResource, ALL, ALL_WITH_RELATIONS
26
from tastypie import fields
37
from handball.models import *
8+
from handball.forms import SignUpForm
49
from django.contrib.auth.models import User
510
from tastypie.authorization import DjangoAuthorization, Authorization
6-
from tastypie.authentication import BasicAuthentication, Authentication, ApiKeyAuthentication
7-
from handball.authorization import ManagerAuthorization
11+
from tastypie.authentication import Authentication, ApiKeyAuthentication
812
from django.contrib.auth import authenticate
9-
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound, HttpResponseBadRequest
13+
from django.core.mail import send_mail
14+
from django.http import HttpResponse, HttpResponseNotFound, HttpResponseBadRequest
1015
from tastypie.serializers import Serializer
1116
from tastypie.utils.mime import determine_format
17+
from django.utils.translation import ugettext as _
1218

1319

1420
class UnionResource(ModelResource):
@@ -162,6 +168,9 @@ class Meta:
162168

163169
def sign_up(request):
164170
form = SignUpForm(request.POST)
171+
serializer = Serializer()
172+
format = determine_format(request, serializer, default_format='application/json')
173+
165174
if form.is_valid():
166175
username = form.cleaned_data['username']
167176
password = form.cleaned_data['password']
@@ -175,7 +184,7 @@ def sign_up(request):
175184
zip_code = form.cleaned_data['zip_code']
176185
mobile_number = form.cleaned_data['mobile_number']
177186

178-
user = User.objects.create(username=username, password=password, email=email)
187+
user = User.objects.create(username=username, password=password, first_name=first_name, last_name=last_name, email=email)
179188

180189
profile = form.cleaned_data['profile'] or Person.objects.create(user=user)
181190
profile.first_name = first_name
@@ -186,7 +195,6 @@ def sign_up(request):
186195
profile.city = city
187196
profile.zip_code = zip_code
188197
profile.mobile_number = mobile_number
189-
profile.save()
190198

191199
# Build the activation key
192200
salt = sha.new(str(random())).hexdigest()[:5]
@@ -195,21 +203,32 @@ def sign_up(request):
195203

196204
# User is unactive until visiting activation link
197205
user.is_active = False
198-
user_profile.activation_key = activation_key
199-
user_profile.key_expires = key_expires
200-
activation_link = 'http://127.0.0.1/activate/' + activation_key
206+
profile.activation_key = activation_key
207+
profile.key_expires = key_expires
208+
activation_link = 'http://127.0.0.1:8000/auth/activate/' + activation_key
201209

202210
user.save()
203-
user_profile.save()
211+
profile.save()
204212

205-
from django.core.mail import send_mail
206213
subject = _('Welcome to ScoreIt!')
207214
message = _('To activate, please click the following link:\n' + activation_link)
208215
sender = _('noreply@score-it.de')
209216
recipients = [email]
210217
send_mail(subject, message, sender, recipients)
211218

212-
return HttpResponse()
219+
user_resource = UserResource()
220+
person_resource = PersonResource()
221+
222+
data = {
223+
'user': user_resource.get_resource_uri(user),
224+
'profile': person_resource.get_resource_uri(profile),
225+
'activation_key': activation_key
226+
}
227+
228+
return HttpResponse(serializer.serialize(data, format, {}))
229+
230+
else:
231+
return HttpResponseBadRequest(serializer.serialize(form.errors, format, {}))
213232

214233

215234
def validate_user(request):

Diff for: forms.py

+19-19
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ class SignUpForm(forms.Form):
99
email = forms.EmailField(label=_('Email'), required=True)
1010
password = forms.CharField(widget=forms.PasswordInput,
1111
label=_('Password'), required=True)
12-
password_repeat = forms.CharField(widget=forms.PasswordInput,
13-
label=_('Repeat Password'), required=True)
12+
# password_repeat = forms.CharField(widget=forms.PasswordInput,
13+
# label=_('Repeat Password'), required=True)
1414

1515
gender = forms.ChoiceField(choices=(('male', _('male')), ('female', _('female'))))
1616
first_name = forms.CharField(label=_('First Name'), required=True)
1717
last_name = forms.CharField(label=_('Last Name'), required=True)
18-
pass_number = forms.CharField(label=_('Pass Number'))
19-
address = forms.CharField(label=_('Address'))
20-
city = forms.CharField(label=_('City'))
21-
zip_code = forms.CharField(label=_('Zip Code'))
22-
mobile_number = forms.CharField(label=_('Mobile Number'))
18+
pass_number = forms.IntegerField(label=_('Pass Number'), required=False)
19+
address = forms.CharField(label=_('Address'), required=False)
20+
city = forms.CharField(label=_('City'), required=False)
21+
zip_code = forms.IntegerField(label=_('Zip Code'), required=False)
22+
mobile_number = forms.CharField(label=_('Mobile Number'), required=False)
2323

2424
profile = forms.ModelChoiceField(required=False,
2525
queryset=Person.objects.filter(first_name=first_name).filter(last_name=last_name).filter(user__isnull=True))
@@ -50,18 +50,18 @@ def clean_pass_number(self):
5050
raise forms.ValidationError(
5151
_('A player with this pass number is already registered.'))
5252

53-
def clean(self):
54-
cleaned_data = super(SignUpForm, self).clean()
55-
password = cleaned_data.get('password')
56-
password_repeat = cleaned_data.get('password_repeat')
53+
# def clean(self):
54+
# cleaned_data = super(SignUpForm, self).clean()
55+
# password = cleaned_data.get('password')
56+
# password_repeat = cleaned_data.get('password_repeat')
5757

58-
# check if passwords match
59-
if password and password_repeat and password != password_repeat:
60-
msg = _('The passwords do not match!')
61-
self._errors['password'] = self.error_class([msg])
62-
self._errors['password_repeat'] = self.error_class([msg])
58+
# # check if passwords match
59+
# if password and password_repeat and password != password_repeat:
60+
# msg = _('The passwords do not match!')
61+
# self._errors['password'] = self.error_class([msg])
62+
# self._errors['password_repeat'] = self.error_class([msg])
6363

64-
del cleaned_data['password']
65-
del cleaned_data['password_repeat']
64+
# del cleaned_data['password']
65+
# del cleaned_data['password_repeat']
6666

67-
return cleaned_data
67+
# return cleaned_data

Diff for: urls.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424
# Non-resource api endpoints
2525
urlpatterns += patterns('handball.api',
2626
(r'^api/v1/auth/validate/$', 'validate_user'),
27-
(r'^api/v1/auth/unique/$', 'is_unique')
28-
#(r'^api/v1/auth/signup/$', 'sign_up')
27+
(r'^api/v1/auth/unique/$', 'is_unique'),
28+
(r'^api/v1/auth/signup/$', 'sign_up')
2929
)

Diff for: views.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sha
2-
import base64
32
import datetime
43
import pytz
54
from random import random

0 commit comments

Comments
 (0)