def signin(request): #Check if user logging player = get_current_user(request) if player: return redirect(dashboard) if request.method == 'POST': form = SignInForm(request.POST) if form.is_valid(): email = request.POST['email'] password = request.POST['password'] player = get_or_none(Player, email=email) if player and player.check_password(password): if player.is_active: request.session['player'] = player set_lang(request, player.language_code) return redirect(dashboard) else: from django.forms.util import ErrorList errors = form._errors.setdefault("email", ErrorList()) errors.append(_('Account is not active. Please check your email !')) else: from django.forms.util import ErrorList errors = form._errors.setdefault("email", ErrorList()) errors.append(_('User or password incorrect')) else: form = None return index(request, form)
def signup(request): #Check if user logging player = get_current_user(request) if player: return redirect('/account/dashboard') if request.method == 'POST': form = SignUpForm(request.POST) #Check if data is valid if form.is_valid(): email = form.cleaned_data['email'] password = form.cleaned_data['password'] #Create new account player = Player(username=email, email=email, password=password, is_active=False) player.set_password(password) player.save() player_activation = player.create_activation() player_activation.save() html_content = render_to_string('email/signup.html', {'code': player_activation.code, 'id': player_activation.id, 'default_url': DEFAULT_URL}) msg = EmailMessage(_('WiDoo Sport Registration'), html_content, EMAIL_HOST_USER, [email]) msg.content_subtype = "html" msg.send() return redirect('/?status=success') else: form = SignUpForm() return index(request, None, form)
def index(request, signin_form=None, signup_form=None): #Check if user logging player = get_current_user(request) #if player: # return redirect(dashboard) is_signup = False status = request.GET.get('status') if status == 'success' or signup_form is not None: is_signup = True if signin_form is None: signin_form = SignInForm() if signup_form is None: signup_form = SignUpForm() return render_to_response('home/index.html', { 'signin_form': signin_form, 'signup_form': signup_form, 'is_signup': is_signup, 'status': status }, context_instance=RequestContext(request))
def signin(request): #Check if user logging player = get_current_user(request) if player: return redirect(dashboard) if request.method == 'POST': form = SignInForm(request.POST) if form.is_valid(): email = request.POST['email'] password = request.POST['password'] player = get_or_none(Player, email=email) if player and player.check_password(password): if player.is_active: request.session['player'] = player set_lang(request, player.language_code) return redirect(dashboard) else: from django.forms.util import ErrorList errors = form._errors.setdefault("email", ErrorList()) errors.append( _('Account is not active. Please check your email !')) else: from django.forms.util import ErrorList errors = form._errors.setdefault("email", ErrorList()) errors.append(_('User or password incorrect')) else: form = None return index(request, form)
def logout(request): _init_session(request) current_player = get_current_user(request) if current_player is None: return Response(ReturnObject(RetCode._FAIL, _('You session is ended'), None).to_json()) try: #Django logout method from django.contrib.auth import logout logout(request) return Response(ReturnObject(RetCode._SUCCESS, _('Logout successfully'), None).to_json()) except KeyError: return Response(ReturnObject(RetCode._SYSTEM_ERROR, _('Error'), None).to_json())
def login_facebook(request): if 'access_token' not in request.GET: return Response(ReturnObject(RetCode._FAIL, _('Access token not exist in request'), None).to_json()) try: access_token = request.GET.get('access_token') facebook = Pyfb(FACEBOOK_APP_ID) facebook.set_access_token(access_token) result = _render_user(facebook, request) if result != '': return Response(ReturnObject(RetCode._FAIL, result, None).to_json()) else: current_player = get_current_user(request) return Response(ReturnObject(RetCode._SUCCESS, _('Login successfully'), current_player).to_json()) except Exception, error: return Response(ReturnObject(RetCode._SYSTEM_ERROR, _('System error, access token is wrong'), None).to_json())
def set_lang(request, lang_code): from django import http from django.utils.translation import check_for_language response = http.HttpResponseRedirect('/') if not check_for_language(lang_code): lang_code = "en" if hasattr(request, 'session'): request.session['django_language'] = lang_code else: response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code) current_player = get_current_user(request) if current_player is not None: current_player.language_code = lang_code current_player.save() return response
def logout(request): _init_session(request) current_player = get_current_user(request) if current_player is None: return Response( ReturnObject(RetCode._FAIL, _('You session is ended'), None).to_json()) try: #Django logout method from django.contrib.auth import logout logout(request) return Response( ReturnObject(RetCode._SUCCESS, _('Logout successfully'), None).to_json()) except KeyError: return Response( ReturnObject(RetCode._SYSTEM_ERROR, _('Error'), None).to_json())
def index(request, signin_form=None, signup_form=None): #Check if user logging player = get_current_user(request) #if player: # return redirect(dashboard) is_signup = False status = request.GET.get('status') if status == 'success' or signup_form is not None: is_signup = True if signin_form is None: signin_form = SignInForm() if signup_form is None: signup_form = SignUpForm() return render_to_response('home/index.html', {'signin_form': signin_form, 'signup_form': signup_form, 'is_signup': is_signup, 'status': status}, context_instance=RequestContext(request))
def signup(request): #Check if user logging player = get_current_user(request) if player: return redirect('/account/dashboard') if request.method == 'POST': form = SignUpForm(request.POST) #Check if data is valid if form.is_valid(): email = form.cleaned_data['email'] password = form.cleaned_data['password'] #Create new account player = Player(username=email, email=email, password=password, is_active=False) player.set_password(password) player.save() player_activation = player.create_activation() player_activation.save() html_content = render_to_string( 'email/signup.html', { 'code': player_activation.code, 'id': player_activation.id, 'default_url': DEFAULT_URL }) msg = EmailMessage(_('WiDoo Sport Registration'), html_content, EMAIL_HOST_USER, [email]) msg.content_subtype = "html" msg.send() return redirect('/?status=success') else: form = SignUpForm() return index(request, None, form)
def login_facebook(request): if 'access_token' not in request.GET: return Response( ReturnObject(RetCode._FAIL, _('Access token not exist in request'), None).to_json()) try: access_token = request.GET.get('access_token') facebook = Pyfb(FACEBOOK_APP_ID) facebook.set_access_token(access_token) result = _render_user(facebook, request) if result != '': return Response( ReturnObject(RetCode._FAIL, result, None).to_json()) else: current_player = get_current_user(request) return Response( ReturnObject(RetCode._SUCCESS, _('Login successfully'), current_player).to_json()) except Exception, error: return Response( ReturnObject(RetCode._SYSTEM_ERROR, _('System error, access token is wrong'), None).to_json())
def _render_user(facebook, request): me = facebook.get_myself() fb_id = me.id current_player = get_current_user(request) if current_player is not None: if fb_id != current_player.fb_id: #Check exit fb_id in table Player exit_fb_id = get_or_none(Player, fb_id=fb_id) if exit_fb_id is not None: if exit_fb_id.is_active: del request.session['access_token'] return _('This facebook account is using by other player. Please try another!') else: player = merge_users(current_player, exit_fb_id) request.session['player'] = player else: player = get_or_none(Player, pk=current_player.id) player.fb_id = fb_id if hasattr(me, 'first_name'): player.first_name = me.first_name if hasattr(me, 'last_name'): player.last_name = me.last_name if hasattr(me, 'gender'): if me.gender == 'male': player.gender = 1 else: player.gender = 0 if hasattr(me, 'birthday'): player.birth_date = datetime.strptime(me.birthday, "%m/%d/%Y").strftime("%Y-%m-%d") player.save() request.session['player'] = player else: player = get_or_none(Player, fb_id=fb_id) if player is None: set_username = False player = Player() #Check if account with email exist if hasattr(me, 'email'): email = me.email player = get_or_none(Player, email=email) if player is None: player = Player() player.email = email player.username = email set_username = True if hasattr(me, 'first_name'): player.first_name = me.first_name if hasattr(me, 'last_name'): player.last_name = me.last_name if hasattr(me, 'gender'): if me.gender == 'male': player.gender = 1 else: player.gender = 0 if hasattr(me, 'birthday'): player.birth_date = datetime.strptime(me.birthday, "%m/%d/%Y").strftime("%Y-%m-%d") if not set_username: player.username = fb_id player.fb_id = fb_id player.is_active = True player.save() else: if not player.is_active: #Merge account when account with email exist and account with facebook exist if hasattr(me, 'email'): email = me.email email_account = get_or_none(Player, ~Q(id=player.id), email=email) if email_account is not None: player = merge_users(email_account, player) elif player.email is None or player.email == '': player.email = email player.is_active = True player.save() request.session['player'] = player return ''