示例#1
0
文件: views.py 项目: selseo/hospital
def edituser(request, userl_slug):
    ##### THIS METHOD MUST EDIT#####
    ## It looks like viewusermethod but you should to edit to make it can edit user profile in database ##
    global userDoc
    user_info = {}
    try:
        userl = UserProfile.objects.get(slug=userl_slug)
        user_info['firstname'] = userl.firstname
        user_info['lastname'] = userl.lastname
        user_info['role'] = userl.role
        if userl.role==1:
            user_info['department']=Doctor.objects.get(userprofile=userl).department
            
            userDoc=Doctor.objects.get(userprofile=userl)
        userAccount = userl.user

    except UserProfile.DoesNotExist:
        return HttpResponseRedirect('/default/viewuserlist/')

    
    

    # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':
        # Attempt to grab information from the raw form information.
        # Note that we make use of both UserForm and UserProfileForm.
        user_form = UserForm(data=request.POST)
        admin_user_form = AdminCreateUser(data=request.POST)
        admin_doctor_form= AdminCreateDoctor(data=request.POST)
        # If the two forms are valid...
        if admin_user_form.is_valid() :
            # Save the user's form data to the database.
            #user = user_form.save()
            userprofile = userl
            #user = user_form.save()
            userprofile.firstname = request.POST['firstname']
            userprofile.lastname = request.POST['lastname']
            userprofile.role = request.POST['role']
            userprofile.status = True
            # Now we hash the password with the set_password method.
            # Once hashed, we can update the user object.
            if(request.POST['password']):
                userAccount.set_password(request.POST['password'])
            #if userprofile.role==1:
            if (request.POST['department']) :
                userDoc.department=request.POST['department']
                userDoc.userprofile=userprofile
                userDoc.save()
            userAccount.save()
            userprofile.save()
            
            # Now sort out the UserProfile instance.
            # Since we need to set the user attribute ourselves, we set commit=False.
            # This delays saving the model until we're ready to avoid integrity problems.
            # userl = admin_user_form.save(commit=False)
            # userl.user = user
            

            # Did the user provide a profile picture?
            # If so, we need to get it from the input form and put it in the UserProfile model.
            #if 'picture' in request.FILES:
            #    profile.picture = request.FILES['picture']

            # Now we save the UserProfile model instance.
            


            # Update our variable to tell the template registration was successful.
            registered = True
            return HttpResponseRedirect('/default/viewuserlist/')

        # Invalid form or forms - mistakes or something else?
        # Print problems to the terminal.
        # They'll also be shown to the user.
        else:
            print (user_form.errors, admin_user_form.errors,admin_doctor_form.errors)

    # Not a HTTP POST, so we render our form using two ModelForm instances.
    # These forms will be blank, ready for user input.
    else:
        user_form = UserForm(initial={'username': userAccount.username,'password':userAccount.password})
       
        admin_user_form = AdminCreateUser(initial={
            'firstname': user_info['firstname'],
            'lastname':user_info['lastname'],
            'role':user_info['role']})
        if user_info['role']==1:
            admin_doctor_form= AdminCreateDoctor(initial={
                'department': user_info['department']})



    if user_info['role']==1:        
        return render(request,
            'admin/editUser.html',
            {'user_form': user_form, 'admin_user_form': admin_user_form,'admin_doctor_form': admin_doctor_form,'registered': registered,'userl':userl} )
    else :
        admin_doctor_form= AdminCreateDoctor(data=request.POST)
        return render(request,
            'admin/editUser.html',
            {'user_form': user_form, 'admin_user_form': admin_user_form,'admin_doctor_form':admin_doctor_form,'registered': registered,'userl':userl} )
示例#2
0
文件: views.py 项目: selseo/hospital
def admin_create_user(request):

    # A boolean value for telling the template whether the registration was successful.
    # Set to False initially. Code changes value to True when registration succeeds.
    registered = False

    # If it's a HTTP POST, we're interested in processing form data.
    if request.method == 'POST':
        # Attempt to grab information from the raw form information.
        # Note that we make use of both UserForm and UserProfileForm.
        user_form = UserForm(data=request.POST)
        admin_user_form = AdminCreateUser(data=request.POST)
        admin_doctor_form= AdminCreateDoctor(data=request.POST)
        # If the two forms are valid...
        if user_form.is_valid() and admin_user_form.is_valid() :
            # Save the user's form data to the database.
            user = user_form.save()

            # Now we hash the password with the set_password method.
            # Once hashed, we can update the user object.
            user.set_password(user.password)
            user.save()

            # Now sort out the UserProfile instance.
            # Since we need to set the user attribute ourselves, we set commit=False.
            # This delays saving the model until we're ready to avoid integrity problems.
            profile = admin_user_form.save(commit=False)
            profile.user = user
            profile.status=True

            # Did the user provide a profile picture?
            # If so, we need to get it from the input form and put it in the UserProfile model.
            #if 'picture' in request.FILES:
            #    profile.picture = request.FILES['picture']

            # Now we save the UserProfile model instance.
            profile.save()
            if profile.role==1 and admin_doctor_form.is_valid() :
                doctor=admin_doctor_form.save(commit=False)
                doctor.userprofile=profile
                doctor.save()



            # Update our variable to tell the template registration was successful.
            registered = True
            #return render(request,'theme/login.html', {'registered': registered} )
            return HttpResponseRedirect('/default/viewuserlist',{'a':'a'})

        # Invalid form or forms - mistakes or something else?
        # Print problems to the terminal.
        # They'll also be shown to the user.
        else:
            print (user_form.errors, admin_user_form.errors,admin_doctor_form.errors)

    # Not a HTTP POST, so we render our form using two ModelForm instances.
    # These forms will be blank, ready for user input.
    else:
        user_form = UserForm()
        admin_user_form = AdminCreateUser()
        admin_doctor_form=AdminCreateDoctor()
    # Render the template depending on the context.
    role = getUserProfile(request.user).role

    return render(request,
            'admin/addUser.html',
            {'user_form': user_form, 'admin_user_form': admin_user_form,'admin_doctor_form': admin_doctor_form,'registered': registered,'role':role} )