Beispiel #1
0
def add_scout(request, scout_id = None):  
    if request.method == 'GET':
        edit = int(request.GET.get('edit','0')) == 1
        boyscout = Position.objects.get(name='Boy Scout')
        scout = None
        unit = None
        try:
            unit = request.user.profile.unit
        except:
            pass
        
        patrol = request.REQUEST.get('patrol')
        if patrol and unit:
            try:
                patrol = Patrol.objects.get(name=patrol, unit=unit)
            except Patrol.DoesNotExist:
                pass
        

        if scout_id is not None:
            scout = Userprofile.objects.get(pk=scout_id)
            form = NewScoutForm(instance=scout, initial={"first_name":scout.user.first_name,
                                                         "last_name":scout.user.last_name,
                                                         "email":scout.user.email})
        else:
            form = NewScoutForm(initial={'email':'noemail', 'unit':unit, 'patrol':patrol})
        
            
        return 'userprofile/add_scout.html', {'form':form, 'boyscout':boyscout, 'scout':scout, 'edit':edit}
    
    else:
        try:
            scout = NewScoutForm(request.POST)
            if request.POST.get('scout_id',False):
                scoutid = request.POST.get('scout_id')
                scout = Userprofile.objects.get(pk=scoutid)
                scoutform = NewScoutForm(request.POST, instance=scout)
            else:
                scoutform = NewScoutForm(request.POST)
        except Exception,e:
            print 'error', e
            
        try:    
            if scoutform.is_valid():
                scoutform.save()
            else:
                return HttpJsonFormError(scoutform.errors)
            return HttpJsonSuccess()
        except Exception, e:
            print e
Beispiel #2
0
def oauth_callback(request):
    invite = request.session.get("req")
    if request.method == 'GET':
        code = request.GET.get('code',None)
        if 'google_access_token' in request.session:
            del request.session['google_access_token']
    
        if code is not None:
            request.session['google_oauth_code'] = code
            data =  get_token_from_code(code)
            request.session['google_access_token'] = data['access_token']
            user = authenticate(token=request.session['google_access_token'])
            if user is None:
                profile = get_google_profile(request.session['google_access_token'])
                #if an existing user exists with this email address, update googlie id
                try:
                    user = User.objects.get(email=profile['email'])
                    up = Userprofile.objects.get(user=user)
                    up.google_id = profile["id"]
                    up.save()
                    user = USTLogin(request, token=request.session['google_access_token'], google_id=profile["id"])                    
                    if user:
                        return HttpResponseRedirect("/user")
                except User.DoesNotExist:
                    pass

                form = NewScoutForm({'first_name':profile["given_name"],
                                     'last_name':profile["family_name"],
                                     'nickname':profile["given_name"],
                                     'login_name':profile["email"],
                                     'bd_string':profile.get('birthday',''),
                                     'email':profile["email"],
                                     'google_id':profile["id"],
                                     'position':7,
                                     })
                if form.is_valid():
                    up = form.save()
                    user = USTLogin(request, token=request.session['google_access_token'], google_id=profile["id"])                    
                    return HttpResponseRedirect("/")
                    
                    
                else:
                    return 'oauth.html', {'form':form, 'profile':True}
            else:
                login(request, user)
                return HttpResponseRedirect("/user")
        else:
            print 'in here four'
            form = NewScoutForm(initial={
                                         'position':7,#Guardian
                                         })
            return 'oauth.html', {'form':form}  
        
    elif request.method == 'POST':
        invite = request.session.get("req")

        scoutform = NewScoutForm(request.POST)
       
        if scoutform.is_valid():
            profile = scoutform.save()
                    
            if len(profile.google_id) > 0:
                user = USTLogin(request, token=request.session['google_access_token'], google_id=profile.google_id)                    
 
                #user = authenticate(token=request.session['google_access_token'], google_id=profile.google_id)
            else:
                user = USTLogin(request, username=profile.user.username, password=scoutform.cleaned_data['password'])
                #user = authenticate(username=profile.user.username, password=scoutform.cleaned_data['password'])
            
            return HttpResponseRedirect("/")
        else:
            print 'form errors: %s'%scoutform.errors
            return 'oauth.html', {'form':scoutform}