def new_register(request): """ This is for a new user who wishes to register independent of twitter and facebook; we use gravatar for these users. """ if request.method == 'POST': form = NewPlayerForm(request.POST) if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] username = form.cleaned_data['username'] email = form.cleaned_data['email'] phone_number = form.cleaned_data['phone_number'] sports = form.cleaned_data['sports'] gender = form.cleaned_data['gender'] password1 = form.cleaned_data['password1'] #password2 = form.cleaned_data['password2'] # unused user = User.objects.create_user(username=username, email=email, password=password1) user.first_name = first_name user.last_name = last_name user.save() player = Player() player.gender = gender player.user = user player.phone_number = phone_number player.image_url = get_gravatar(player.user.email) player.save() player.sports = sports user = authenticate(username=player.user, password=password1) login(request, user) EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' from django.core.mail import EmailMessage from django.template.loader import render_to_string from django.utils.html import strip_tags subject, from_email, to = 'Welcome', '*****@*****.**', user.email html_content = render_to_string('email_signup.html', {'user': user.first_name}) text_content = strip_tags(html_content) msg = EmailMessage(subject, text_content, to=[to]) msg.send() # task here to email user SignUpTask.delay(user) return HttpResponseRedirect('/') else: if 'email' in request.GET: form = NewPlayerForm(initial={'email': request.GET['email']}) else: form = NewPlayerForm() return render(request, 'new_register.html', {'form': form})
def update_or_create_player(self, zuluru_id, name, gender, team): instance = Player.query.filter_by(zuluru_id=zuluru_id).first() if instance: print('Found Player: ', name) else: print('Creating Player: ', name) instance = Player(zuluru_id=zuluru_id) instance.name = name instance.gender = gender instance.team_id = team.id db.session.add(instance) db.session.commit()
def new_register(request): """ This is for a new user who wishes to register independent of twitter and facebook; we use gravatar for these users. """ if request.method == 'POST': form = NewPlayerForm(request.POST) if form.is_valid(): first_name = form.cleaned_data['first_name'] last_name = form.cleaned_data['last_name'] username = form.cleaned_data['username'] email = form.cleaned_data['email'] phone_number = form.cleaned_data['phone_number'] sports = form.cleaned_data['sports'] gender = form.cleaned_data['gender'] password1 = form.cleaned_data['password1'] #password2 = form.cleaned_data['password2'] # unused user = User.objects.create_user(username=username, email=email, password=password1) user.first_name = first_name user.last_name = last_name user.save() player = Player() player.gender = gender player.user = user player.phone_number = phone_number player.image_url = get_gravatar(player.user.email) player.save() player.sports = sports user = authenticate(username=player.user, password=password1) login(request, user) # task here to email user SignUpTask.delay(user) return HttpResponseRedirect('/') else: if 'email' in request.GET: form = NewPlayerForm(initial={'email': request.GET['email']}) else: form = NewPlayerForm() return render(request, 'new_register.html', {'form': form})
def facebook_callback(request): """ This method is responsible for the facebook callback and generating the facebook user. """ if 'error' in request.GET: return HttpResponseRedirect('/') code = request.GET["code"] args = dict(client_id=keys.FACEBOOK_APP_ID, redirect_uri='http://tini.ep.io/facebook/callback', client_secret=keys.FACEBOOK_APP_SECRET, code=code) token_url = 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode( args) data = urllib.urlopen(token_url) response = data.read() data.close() # print 'Token URL:' + token_url # print 'Response:' # print response try: split = response.split('=') access_token = split[1].strip('&expires') expires = split[2] # print 'Access Token: ' + access_token # print 'Expires: ' + expires except: error = json.loads(response) return HttpResponseRedirect('/error') profile = json.load( urllib.urlopen('https://graph.facebook.com/me?' + urllib.urlencode(dict(access_token=access_token)))) args = dict(access_token=access_token) graph_url = 'https://graph.facebook.com/me?' + urllib.urlencode(args) #print graph_url response = urllib.urlopen(graph_url) facebook_user = json.load(response) username = facebook_user.get('username', '') gender = facebook_user.get('gender', '') try: player = Player.objects.get(facebook_id=facebook_user.get('id', None)) except ObjectDoesNotExist: user = User(username=username) user.set_password(access_token) user.first_name = facebook_user['first_name'] user.last_name = facebook_user['last_name'] user.email = facebook_user['email'] player = Player() player.user = user player.gender = get_gender_initial(gender=gender) player.facebook_id = facebook_user['id'] player.facebook_oauth_token = access_token request.session['facebook'] = player form = PlayerForm( initial={ 'username': player.user.username, 'first_name': user.first_name, 'last_name': user.last_name, 'email': player.user.email, 'gender': player.gender }) return render(request, 'register.html', {'form': form}) # player = Player.objects.get(user=user) # print player user = authenticate(username=player.user.username, password=player.facebook_oauth_token) login(request, user) return HttpResponseRedirect('/')
def facebook_callback(request): """ This method is responsible for the facebook callback and generating the facebook user. """ if 'error' in request.GET: return HttpResponseRedirect('/') code = request.GET["code"] args = dict( client_id = keys.FACEBOOK_APP_ID, redirect_uri = 'http://tini.ep.io/facebook/callback', client_secret = keys.FACEBOOK_APP_SECRET, code = code ) token_url = 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(args) data = urllib.urlopen(token_url) response = data.read() data.close() # print 'Token URL:' + token_url # print 'Response:' # print response try: split = response.split('=') access_token = split[1].strip('&expires') expires = split[2] # print 'Access Token: ' + access_token # print 'Expires: ' + expires except: error = json.loads(response) return HttpResponseRedirect('/error') profile = json.load(urllib.urlopen('https://graph.facebook.com/me?' + urllib.urlencode(dict(access_token=access_token)))) args = dict( access_token = access_token ) graph_url = 'https://graph.facebook.com/me?' + urllib.urlencode(args) #print graph_url response = urllib.urlopen(graph_url) facebook_user = json.load(response) username = facebook_user.get('username', '') gender = facebook_user.get('gender', '') try: player = Player.objects.get(facebook_id=facebook_user.get('id', None)) except ObjectDoesNotExist: user = User(username=username) user.set_password(access_token) user.first_name = facebook_user['first_name'] user.last_name = facebook_user['last_name'] user.email = facebook_user['email'] player = Player() player.user = user player.gender = get_gender_initial(gender=gender) player.facebook_id = facebook_user['id'] player.facebook_oauth_token = access_token request.session['facebook'] = player form = PlayerForm(initial={'username': player.user.username, 'first_name': user.first_name, 'last_name': user.last_name, 'email': player.user.email, 'gender': player.gender}) return render(request, 'register.html', {'form': form}) # player = Player.objects.get(user=user) # print player user = authenticate(username=player.user.username, password=player.facebook_oauth_token) login(request, user) return HttpResponseRedirect('/')