1
+ import sha
2
+ import datetime
3
+ from random import random
4
+ import pytz
1
5
from tastypie .resources import ModelResource , ALL , ALL_WITH_RELATIONS
2
6
from tastypie import fields
3
7
from handball .models import *
8
+ from handball .forms import SignUpForm
4
9
from django .contrib .auth .models import User
5
10
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
8
12
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
10
15
from tastypie .serializers import Serializer
11
16
from tastypie .utils .mime import determine_format
17
+ from django .utils .translation import ugettext as _
12
18
13
19
14
20
class UnionResource (ModelResource ):
@@ -162,6 +168,9 @@ class Meta:
162
168
163
169
def sign_up (request ):
164
170
form = SignUpForm (request .POST )
171
+ serializer = Serializer ()
172
+ format = determine_format (request , serializer , default_format = 'application/json' )
173
+
165
174
if form .is_valid ():
166
175
username = form .cleaned_data ['username' ]
167
176
password = form .cleaned_data ['password' ]
@@ -175,7 +184,7 @@ def sign_up(request):
175
184
zip_code = form .cleaned_data ['zip_code' ]
176
185
mobile_number = form .cleaned_data ['mobile_number' ]
177
186
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 )
179
188
180
189
profile = form .cleaned_data ['profile' ] or Person .objects .create (user = user )
181
190
profile .first_name = first_name
@@ -186,7 +195,6 @@ def sign_up(request):
186
195
profile .city = city
187
196
profile .zip_code = zip_code
188
197
profile .mobile_number = mobile_number
189
- profile .save ()
190
198
191
199
# Build the activation key
192
200
salt = sha .new (str (random ())).hexdigest ()[:5 ]
@@ -195,21 +203,32 @@ def sign_up(request):
195
203
196
204
# User is unactive until visiting activation link
197
205
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
201
209
202
210
user .save ()
203
- user_profile .save ()
211
+ profile .save ()
204
212
205
- from django .core .mail import send_mail
206
213
subject = _ ('Welcome to ScoreIt!' )
207
214
message = _ ('To activate, please click the following link:\n ' + activation_link )
208
215
sender = _ ('noreply@score-it.de' )
209
216
recipients = [email ]
210
217
send_mail (subject , message , sender , recipients )
211
218
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 , {}))
213
232
214
233
215
234
def validate_user (request ):
0 commit comments