Example #1
0
def registerUser(request):
    '''
    This registers both the initial hospital plus
    the initial admin account
    
    1. Check if it's a post request
    2. Validate all entries 
        i. Validate the fields for the hospital form
        ii. Validate the fields for the user form
    3. Get the hospital_id and insert it into the user hospital field
    4. 
    '''
    if request.user.is_authenticated():
        # User already have an account: no registration
        return render_to_response('/project/register', {'has_account': True})
    
    manipulator = RegistrationForm()
    if request.POST:
        new_data = request.POST.copy()
        errors = manipulator.get_validation_errors(new_data)
        
        if not errors:
            # save the user
            manipulator.do_html2python(new_data)
            new_user = manipulator.save(new_data)
            
            # Building the activation key for the account
            salt = sha.new(str(random.random())).hexdigest()[:5]
            activation_key = sha.new(salt+new_user.username).hexdigest()
            key_expires = datetime.datetime.today() + datetime.timedelta(2)
            
            # Creating user
            new_personnel = Personnel(
                                first_name = "",
                                last_name = "",
                                hospital = "",
                                personnel_type = "",
                                activation_key = "",
                                key_expires ="",
                            )
            new_personnel.save()
            
            
            # send email to user
            email_subject = 'Your new localhost:8000 account confirmation code'
            email_body = "Hello, %s, and thanks for signing up for an account from our service!\n\nTo activate your account, click this link within 48 hours:\n\nhttp://localhost:8000/accounts/confirm/%s" % (new_user.username, new_personnel.activation_key)
            send_mail(email_subject, email_body, '*****@*****.**', [new_user.email])
            
            return render_to_response('register.html', {'created': True})
        else:
            errors = new_data = {}
        form = forms.FormWrapper(manipulator, new_data, errors)
    return render_to_response('register.html',{'hospitalForm': hospitalForm, 'userForm':userForm})
Example #2
0
def registerUser(request):
    if request.POST:
        hospitalForm = HospitalRegistrationForm(request.POST)
        userForm = RegistrationForm(request.POST)
        
        if hospitalForm.is_valid() and userForm.is_valid():
            # Fields from hospital form
            hospital_name = hospitalForm.cleaned_data['hospital_name']
            postal_address = hospitalForm.cleaned_data['postal_address']
            location = hospitalForm.cleaned_data['location']
            email = hospitalForm.cleaned_data['email']
            contact_number = hospitalForm.cleaned_data['contact_number']
            
            # Fields from user Form
            first_name = userForm.cleaned_data['first_name']
            last_name = userForm.cleaned_data['last_name']
            personnel_type = userForm.cleaned_data['personnel_type']
            user_email = userForm.cleaned_data['email']
            
            username = userForm.cleaned_data['username']
            password = userForm.cleaned_data['password']
            
            #### other fields for the personnnel
            # Build activation_key
            salt = sha.new(str(random.random())).hexdigest()[:5]
            activation_key = sha.new(salt+username).hexdigest()
            
            key_expires = datetime.datetime.today() + datetime.timedelta(2)
            
            #create User object
            user = User.objects.create_user(username, user_email, password)
            
            #create hospital object
            hospital = Hospital.objects.create(
                            hospital_name = hospital_name, 
                            postal_address = postal_address,
                            location = location,
                            email = email,
                            contact_number = contact_number
                        )
            
            # Now we can create Personnel object passing hospital and user objects
            personnel = Personnel.objects.create(
                            first_name = first_name,
                            last_name = last_name,
                            personnel_type = personnel_type,
                            email = user_email,
                            activation_key = activation_key,
                            key_expires = key_expires,
                            hospital = hospital,
                            user = user
                        )
            
            # user.is_active = False;
            ## Save the User object
            user.save()
            ## Save the Hospital object
            hospital.save()
            ## Save the personnel object
            personnel.save()
            print "hospitalForm is valid"
            return HttpResponseRedirect('/project/loginUser')
    else:
        hospitalForm = HospitalRegistrationForm()
        userForm = RegistrationForm()
        keyForm = KeyForm()
    #userForm = forms.FormWrapper(userForm, new_data, errors)
    return render_to_response('register.html',{'hospitalForm':hospitalForm, 'userForm':userForm,})# 'keyForm':keyForm})