Beispiel #1
0
def register(request):
    '''
    Required: handle
    Get name and email from sign in service
    '''

    #   Check for errors
    #+++++++++++++++++++
    if 'error' in request.GET:
        return HttpResponse('Access Denied')

    auth = Authentication(request)
    if request.method == 'POST':
        if not request.POST.get('service') or not request.POST.get('handle'):
            return redirect('www:index', _anchor='registration')

        #   TODO: Input Validation
        request.session['redirect'] = 'www:register'
        if not re.match('^[A-Za-z_\d]+$', request.POST.get('handle')):
            return HttpResponse('Invalid handle')

        request.session['handle'] = request.POST.get('handle')
        service = request.POST.get('service')

        try:
            return auth.login_step_1(
                service, reverse('www:oauth', kwargs={'service': service}))

        except FIRSTAuthError as e:
            return HttpResponse(
                ('Error: {}<br /><a href="/#login">Try logging '
                 'in again</a>').format(e))

        return HttpResponse('No post data provided')

    if request.method == 'GET':
        if auth.is_logged_in:
            if ('info' not in request.session
                    or 'email' not in request.session['info']):
                raise FIRSTAuthError('Email not set')

            user = Authentication.get_user_data(
                request.session['info']['email'])
            if not user:
                if 'handle' in request.session:
                    user = auth.register_user()
                    if not user:
                        return HttpResponse('Error creating user')

                return redirect(reverse('www:profile'))

            return HttpResponse('User already exists')

        return HttpResponse('Not logged in')
Beispiel #2
0
def profile(request):
    '''
    Should show the user's name, email, ranking and API key
    '''
    if 'info' not in request.session:
        return redirect(reverse('www:login'))

    info = request.session['info']
    user = Authentication.get_user_data(info['email'])

    if not user:
        return redirect(reverse('www:index'))

    count = Function.objects.filter(metadata__user=user).count()
    data = {
        'title': 'Profile',
        'user': user.dump(True),
        'metadata_count': count
    }
    return render(request, 'www/profile.html', data)