Exemplo n.º 1
0
def create(request): 
    context = {}
    context['alert_count'] = Alerts.objects.filter(soft_delete=False).count()
    
    cleaner = forms.CharField()
    context['utc_offsets'] = utc_offsets
    
    #check if the current user is allowed to create new users
    this_profile = UserProfile.objects.get(user = request.user)
    if this_profile.oauth_scope == 'read write':
        context['can_edit'] = True
    else:
        return profiles(request)
    
    if request.method == "POST":
            
        user_profile_create_form = UserProfileEditForm(request.POST)
        user_create_form = UserEditForm(request.POST, instance=request.user)
        if user_create_form.is_valid() and user_profile_create_form.is_valid():
            context['edit_msg'] = " New User successfully created."
            
            #get new django user from POST data
            username = cleaner.clean(request.POST['username'])
            first_name = user_create_form.cleaned_data.get("first_name")
            last_name = user_create_form.cleaned_data.get("last_name")
            email = user_create_form.cleaned_data.get("email")
            pw1 = cleaner.clean(request.POST['inputpassword1']) 
            pw2 = cleaner.clean(request.POST['inputpassword2'])
            
            if pw1 == pw2:
                #passwords match so create new user
                new_user = User(username=username,
                                first_name=first_name,
                                last_name=last_name,
                                email=email,
                               )
                new_user.set_password(pw1)               
                new_user.save()
                
                oauth_scope = user_profile_create_form.cleaned_data.get("oauth_scope")
                utc_offset = user_profile_create_form.cleaned_data['utc_offset']
                new_user_profile = UserProfile(user=new_user,
                                               oauth_scope=oauth_scope,
                                               utc_offset=utc_offset
                                               )
                new_user_profile.save()                               
            
            return profiles(request) 
            
        else:
          context['edit_msg'] = "Received data is not valid." 
          return render(request, 'accounts/create_profile.html', context)
        
    return render(request, 'accounts/create_profile.html', context)    
Exemplo n.º 2
0
def edit(request, upk): 
    context = {}
    context['alert_count'] = Alerts.objects.filter(soft_delete=False).count()
    
    cleaner = forms.CharField()
    context['utc_offsets'] = utc_offsets
    
    #get the user of the site
    this_profile = UserProfile.objects.get(user = request.user)
    this_user = User.objects.get(id=request.user.id)    
    
    
    # the user that's being edited    
    upk = int(cleaner.clean(upk))
    edit_user_profile = UserProfile.objects.get(user=upk)
    edit_user = User.objects.get(id=edit_user_profile.user.id) 
    
    context['user_data'] = edit_user_profile
    
    if 'write' in this_profile.oauth_scope.split():
        context['can_edit'] = True
        if request.method == "POST":
                
            user_profile_edit_form = UserProfileEditForm(request.POST)
            user_edit_form = UserEditForm(request.POST, instance=request.user)
            if user_edit_form.is_valid() and user_profile_edit_form.is_valid():
                #set default edit message
                context['edit_msg'] = "User data successfully saved."
                
                #Clean independent form values
                user_id = cleaner.clean(request.POST['user_id'])
                username = cleaner.clean(request.POST['username'])
                
                valid_ids = True
                
                if edit_user.id != upk:
                    #if user's id's don't match then return to form.
                    context['edit_msg'] = "User data could not be saved."
                    valid_ids = False
                    
                if not valid_ids:
                    #if errors exist return to page and display error message    
                    return render(request, 'accounts/edit_profile.html', context)
                    
                # save the 'user' data that has been edited
                edit_user.username = username
                edit_user.first_name = user_edit_form.cleaned_data['first_name']
                edit_user.last_name = user_edit_form.cleaned_data['last_name']
                edit_user.email = user_edit_form.cleaned_data['email']
                edit_user.save() 
                
                # Save the 'profile' data
                edit_user_profile.oauth_scope = user_profile_edit_form.cleaned_data['oauth_scope']
                edit_user_profile.utc_offset = user_profile_edit_form.cleaned_data['utc_offset']
                edit_user_profile.save()                               
                
                # reload from the database to update all data going to the template
                edit_user_profile = UserProfile.objects.get(user=upk)
                
                context['user_data'] = edit_user_profile
                
            else:
              context['edit_msg'] = "Input data is not valid."    

    
    # If any of the following fields is empty then set it to value of its generic label 
    # for populating the placeholder values in the form.
    if not edit_user_profile.user.first_name :
        edit_user_profile.user.first_name = 'First name'
    if not edit_user_profile.user.last_name :
        edit_user_profile.user.last_name = 'Last name' 
    if not edit_user_profile.user.email :
        user_data.user.email = 'Email address'
    return render(request, 'accounts/edit_profile.html', context)