コード例 #1
0
ファイル: views.py プロジェクト: yananda93/registration
def callback(request, provider=None):
    if not provider:
        messages.error(request, 'Invalid URL')
        return HttpResponseRedirect(reverse('root'))
    if request.user.is_authenticated:
        return HttpResponseRedirect(reverse('root'))
    code = request.GET.get('code', '')
    if not code:
        messages.error(request, 'Invalid URL')
        return HttpResponseRedirect(reverse('root'))
    try:
        access_token = providers.auth_mlh(code, request)
        mlhuser = providers.get_mlh_user(access_token)
    except ValueError as e:
        messages.error(request, str(e))
        return HttpResponseRedirect(reverse('root'))

    user = User.objects.filter(mlh_id=mlhuser.get('id', -1)).first()
    if user:
        auth.login(request, user)
    elif User.objects.filter(email=mlhuser.get('email', None)).first():
        messages.error(
            request,
            'An account with this email already exists. Sign in using your password.'
        )
    else:
        user = User.objects.create_mlhuser(
            email=mlhuser.get('email', None),
            name=mlhuser.get('first_name', '') + ' ' +
            mlhuser.get('last_name', None),
            mlh_id=mlhuser.get('id', None),
        )
        auth.login(request, user)

        # Save extra info
        draft = a_models.DraftApplication()
        draft.user = user
        mlhdiet = mlhuser.get('dietary_restrictions', '')
        diet = mlhdiet if mlhdiet in dict(a_models.DIETS).keys() else 'Others'
        draft.save_dict({
            'degree':
            mlhuser.get('major', ''),
            'university':
            mlhuser.get('school', {}).get('name', ''),
            'phone_number':
            mlhuser.get('phone_number', ''),
            'tshirt_size': [
                k for k, v in a_models.TSHIRT_SIZES
                if v == mlhuser.get('shirt_size', '')
            ][0],
            'diet':
            mlhdiet,
            'other_diet':
            mlhdiet if diet == 'Others' else '',
        })
        draft.save()
    return HttpResponseRedirect(reverse('root'))
コード例 #2
0
def callback(request, provider=None):
    if not provider:
        messages.error(request, 'Invalid URL')
        return HttpResponseRedirect(reverse('root'))
    if request.user.is_authenticated:
        return HttpResponseRedirect(reverse('root'))
    code = request.GET.get('code', '')
    if not code:
        messages.error(request, 'Invalid URL')
        return HttpResponseRedirect(reverse('root'))
    try:
        access_token = providers.auth_mlh(code, request)
        mlhuser = providers.get_mlh_user(access_token)
    except ValueError as e:
        messages.error(request, str(e))
        return HttpResponseRedirect(reverse('root'))

    user = User.objects.filter(mlh_id=mlhuser.get('id', -1)).first()
    if user:
        auth.login(request, user, backend="django.contrib.auth.backends.ModelBackend")
    elif User.objects.filter(email=mlhuser.get('email', None)).first():
        messages.error(request, 'An account with this email already exists. Sign in using your password.')
    else:
        user = User.objects.create_mlhuser(
            email=mlhuser.get('email', None),
            name=mlhuser.get('first_name', '') + ' ' + mlhuser.get('last_name', None),
            mlh_id=mlhuser.get('id', None),
        )
        auth.login(request, user, backend="django.contrib.auth.backends.ModelBackend")

        # Save extra info
        draft = DraftApplication()
        draft.user = user
        mlhgender = mlhuser.get('gender','')
        if mlhgender == "I prefer not to say" or mlhgender not in dict(Application.GENDERS).values():
            mlhgender = "Prefer not to answer"
        mlhyear = mlhuser.get('level_of_study','')
        if mlhyear == "Not Currently a Student":
            mlhyear = Application.C_GRADUATED
        elif mlhyear == "University (Master's / Doctoral)":
            mlhyear = Application.C_GRAD
        else:
            mlhyear = Application.C_FRESHMAN
        draft.save_dict({
            'degree': mlhuser.get('major', ''),
            'university': mlhuser.get('school', {}).get('name', ''),
            'class_status': mlhyear,
            'phone_number': '(' + mlhuser.get('phone_number', '')[2:5] + ') ' + mlhuser.get('phone_number', '')[5:8] + '-' + mlhuser.get('phone_number', '')[8:],
            'gender': [k for k, v in Application.GENDERS if v == mlhgender][0],
        })
        draft.save()
    return HttpResponseRedirect(reverse('root'))
コード例 #3
0
def callback(request, provider=None):
    if not provider:
        messages.error(request, 'Invalid URL')
        return HttpResponseRedirect(reverse('root'))
    if request.user.is_authenticated:
        return HttpResponseRedirect(reverse('root'))
    code = request.GET.get('code', '')
    if not code:
        messages.error(request, 'Invalid URL')
        return HttpResponseRedirect(reverse('root'))
    try:
        access_token = providers.auth_mlh(code, request)
        mlhuser = providers.get_mlh_user(access_token)
    except ValueError as e:
        messages.error(request, str(e))
        return HttpResponseRedirect(reverse('root'))

    user = User.objects.filter(mlh_id=mlhuser.get('id', -1)).first()
    if user:
        auth.login(request, user)
    elif User.objects.filter(email=mlhuser.get('email', None)).first():
        messages.error(request, 'An account with this email already exists. Sign in using your password.')
    else:
        user = User.objects.create_mlhuser(
            email=mlhuser.get('email', None),
            name=mlhuser.get('first_name', '') + ' ' + mlhuser.get('last_name', None),
            mlh_id=mlhuser.get('id', None),
        )
        auth.login(request, user)

        # Save extra info
        draft = a_models.DraftApplication()
        draft.user = user
        mlhdiet = mlhuser.get('dietary_restrictions', '')
        diet = mlhdiet if mlhdiet in dict(a_models.DIETS).keys() else 'Other'
        draft.save_dict({
            'degree': mlhuser.get('major', ''),
            'university': mlhuser.get('school', {}).get('name', ''),
            'phone_number': mlhuser.get('phone_number', ''),
            'tshirt_size': [k for k, v in a_models.TSHIRT_SIZES if v == mlhuser.get('shirt_size', '')][0],
            'diet': mlhdiet,
            'other_diet': mlhdiet if diet == 'Others' else '',
        })
        draft.save()
    return HttpResponseRedirect(reverse('root'))