|
| 1 | +from auth.forms import SignUpForm |
| 2 | +import sha |
| 3 | +import datetime |
| 4 | +from random import random |
| 5 | +import pytz |
| 6 | +from django.contrib.auth import authenticate |
| 7 | +from django.contrib.auth.models import User |
| 8 | +from django.core.mail import send_mail |
| 9 | +from tastypie.serializers import Serializer |
| 10 | +from tastypie.utils.mime import determine_format |
| 11 | +from django.utils.translation import ugettext as _ |
| 12 | +from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotFound |
| 13 | +from tastypie.resources import ModelResource, ALL_WITH_RELATIONS |
| 14 | +from tastypie import fields |
| 15 | +from auth.models import Profile |
| 16 | +from tastypie.authorization import DjangoAuthorization, Authorization |
| 17 | +from tastypie.authentication import Authentication, ApiKeyAuthentication |
| 18 | + |
| 19 | + |
| 20 | +class UserResource(ModelResource): |
| 21 | + |
| 22 | + class Meta: |
| 23 | + queryset = User.objects.all() |
| 24 | + excludes = ['email', 'password'] |
| 25 | + |
| 26 | + |
| 27 | +class ProfileResource(ModelResource): |
| 28 | + user = fields.OneToOneField(UserResource, 'user') |
| 29 | + |
| 30 | + class Meta: |
| 31 | + queryset = Profile.objects.all() |
| 32 | + authorization = Authorization() |
| 33 | + authentication = Authentication() |
| 34 | + excludes = ['activation_key', 'key_expires'] |
| 35 | + filtering = { |
| 36 | + 'user': ALL_WITH_RELATIONS, |
| 37 | + 'first_name': ['exact'], |
| 38 | + 'last_name': ['exact'] |
| 39 | + } |
| 40 | + |
| 41 | + def dehydrate(self, bundle): |
| 42 | + bundle.data['display_name'] = str(bundle.obj) |
| 43 | + |
| 44 | + |
| 45 | +def sign_up(request): |
| 46 | + form = SignUpForm(request.POST) |
| 47 | + serializer = Serializer() |
| 48 | + format = determine_format(request, serializer, |
| 49 | + default_format='application/json') |
| 50 | + |
| 51 | + if form.is_valid(): |
| 52 | + username = form.cleaned_data['username'] |
| 53 | + password = form.cleaned_data['password'] |
| 54 | + email = form.cleaned_data['email'] |
| 55 | + first_name = form.cleaned_data['first_name'] |
| 56 | + last_name = form.cleaned_data['last_name'] |
| 57 | + |
| 58 | + user = User.objects.create(username=username, password=password, |
| 59 | + first_name=first_name, last_name=last_name, email=email, |
| 60 | + is_active=False) |
| 61 | + user.save() |
| 62 | + |
| 63 | + auth_profile = user.get_profile() |
| 64 | + |
| 65 | + # Build the activation key |
| 66 | + salt = sha.new(str(random())).hexdigest()[:5] |
| 67 | + activation_key = sha.new(salt + user.username).hexdigest() |
| 68 | + key_expires = datetime.datetime.now(pytz.utc) + datetime.timedelta(2) |
| 69 | + |
| 70 | + # User is unactive until visiting activation link |
| 71 | + auth_profile.activation_key = activation_key |
| 72 | + auth_profile.key_expires = key_expires |
| 73 | + activation_link = 'http://127.0.0.1:8000/auth/activate/' + activation_key |
| 74 | + |
| 75 | + auth_profile.save() |
| 76 | + |
| 77 | + subject = _('Welcome to ScoreIt!') |
| 78 | + message = _('To activate, please click the following link:\n' + activation_link) |
| 79 | + sender = _('noreply@score-it.de') |
| 80 | + recipients = [email] |
| 81 | + send_mail(subject, message, sender, recipients) |
| 82 | + |
| 83 | + user_resource = UserResource() |
| 84 | + profile_resource = ProfileResource() |
| 85 | + |
| 86 | + data = { |
| 87 | + 'user': user_resource.get_resource_uri(user), |
| 88 | + 'profile': profile_resource.get_resource_uri(auth_profile), |
| 89 | + 'activation_key': activation_key |
| 90 | + } |
| 91 | + |
| 92 | + return HttpResponse(serializer.serialize(data, format, {})) |
| 93 | + |
| 94 | + else: |
| 95 | + return HttpResponseBadRequest(serializer.serialize(form.errors, format, {})) |
| 96 | + |
| 97 | + |
| 98 | +def validate_user(request): |
| 99 | + """ |
| 100 | + Checks a user's basic auth credentials and, if valid, returns the users data |
| 101 | + """ |
| 102 | + |
| 103 | + # if not request.META.get('HTTP_AUTHORIZATION'): |
| 104 | + # return HttpResponseBadRequest('No HTTP_AUTHORIZATION header found') |
| 105 | + |
| 106 | + # try: |
| 107 | + # (auth_type, data) = request.META['HTTP_AUTHORIZATION'].split() |
| 108 | + # if auth_type.lower() != 'basic': |
| 109 | + # return HttpResponseBadRequest('Wrong auth type. Use basic auth!') |
| 110 | + # user_pass = base64.b64decode(data) |
| 111 | + # except: |
| 112 | + # return HttpResponseBadRequest('Could not decode auth credentials.') |
| 113 | + |
| 114 | + # bits = user_pass.split(':', 1) |
| 115 | + |
| 116 | + # if len(bits) != 2: |
| 117 | + # return HttpResponseBadRequest('Could not decode auth credentials.') |
| 118 | + |
| 119 | + # user = authenticate(username=bits[0], password=bits[1]) |
| 120 | + |
| 121 | + username = request.POST['username'] |
| 122 | + password = request.POST['password'] |
| 123 | + |
| 124 | + if not username or not password: |
| 125 | + return HttpResponseBadRequest() |
| 126 | + |
| 127 | + user = authenticate(username=username, password=password) |
| 128 | + |
| 129 | + if user is None or not user.is_active: |
| 130 | + return HttpResponseNotFound('User does not exist or password incorrect.') |
| 131 | + |
| 132 | + auth_profile = user.get_profile() |
| 133 | + |
| 134 | + profile_resource = ProfileResource() |
| 135 | + bundle = profile_resource.build_bundle(obj=auth_profile, request=request) |
| 136 | + profile_resource.full_dehydrate(bundle) |
| 137 | + bundle.data['api_key'] = user.api_key.key |
| 138 | + |
| 139 | + return HttpResponse(profile_resource.serialize(None, bundle, 'application/json')) |
| 140 | + |
| 141 | + |
| 142 | +def is_unique(request): |
| 143 | + data = {} |
| 144 | + |
| 145 | + if 'username' in request.GET: |
| 146 | + username = request.GET['username'] |
| 147 | + |
| 148 | + try: |
| 149 | + User.objects.get(username=username) |
| 150 | + unique = False |
| 151 | + except User.DoesNotExist: |
| 152 | + unique = True |
| 153 | + except User.MultipleObjectsReturned: |
| 154 | + unique = False |
| 155 | + |
| 156 | + data['username'] = unique |
| 157 | + |
| 158 | + if 'email' in request.GET: |
| 159 | + email = request.GET['email'] |
| 160 | + |
| 161 | + try: |
| 162 | + User.objects.get(email=email) |
| 163 | + unique = False |
| 164 | + except User.DoesNotExist: |
| 165 | + unique = True |
| 166 | + except User.MultipleObjectsReturned: |
| 167 | + unique = False |
| 168 | + |
| 169 | + data['email'] = unique |
| 170 | + |
| 171 | + serializer = Serializer() |
| 172 | + |
| 173 | + format = determine_format(request, serializer, default_format='application/json') |
| 174 | + |
| 175 | + return HttpResponse(serializer.serialize(data, format, {})) |
0 commit comments