Пример #1
0
def change_org_view(request):
    if request.user.is_authenticated():
        # get all ref data
        title_list = ref.RefTitle.objects.all()
        state_list = ref.RefState.objects.all()
        
        user = request.user
        
        # add new user institution
        if 'change' in request.POST:
            # get all user & user org
            user_profile = get_object_or_404(UserProfile, user = user)
            
            # create a new user institution
            new_user_institution = UserInstitution()
            new_institution_contact = InstitutionContact()
            
            # institution details
            new_user_institution.Name = request.POST['org_name']
            new_user_institution.Department = request.POST['org_dept']
            new_user_institution.Address = request.POST['org_address']
            new_user_institution.City = request.POST['org_city']
            new_user_institution.PostCode = request.POST['org_post']
            new_user_institution.Phone = request.POST['org_phone']
            new_user_institution.Fax = request.POST['org_fax']
            new_user_institution.RequestChange = True
            new_user_institution.StartDate = get_date(request.POST['start_date'])
            if request.POST['end_date'] != '':
                new_user_institution.EndDate = get_date(request.POST['end_date'])
            new_user_institution.ApplicationDate = datetime.date.today()
            
            # set the user it belongs to
            new_user_institution.BelongsTo = user
            
            # set the pending institution
            new_user_institution.PendingInstitution = user
            
            # contact details
            new_institution_contact.ContactFirstName = request.POST['contact_firstname']
            new_institution_contact.ContactLastName = request.POST['contact_lastname']
            new_institution_contact.ContactPhone = request.POST['contact_phone']
            new_institution_contact.ContactEmail = request.POST['contact_email']
            
            # try and get ref data to save
            org_state = request.POST['org_state']
            contact_title = request.POST['contact_title']
            if (org_state):
                new_user_institution.State = ref.RefState.objects.get(State__iexact = org_state)
            if (contact_title):
                new_user_institution.ContactTitle = ref.RefTitle.objects.get(Title__iexact = contact_title)
            
            new_user_institution.save()
            
            # set foreign key relationship
            new_institution_contact.UserInstitution = new_user_institution
            new_institution_contact.save()
            
            # change the access status of the user profile to "Request Change of Organisation"
            # TODO: AccessStatus key will need to be updated
            user_profile.AccessStatus = get_object_or_404(ref.RefAccessStatus, pk=3)
            user_profile.save()
            
            # return user back to the user details page
            return HttpResponseRedirect('/user/')
        
        return render_to_response('users/change_org.html',
                                  {
                                   'title_list': title_list,
                                   'state_list': state_list,
                                   'user': user,
                                   })
    else:
        return render_to_response('home/timeout.html')
Пример #2
0
def signup_view(request):
    # load ref data
    title_list = ref.RefTitle.objects.all()
    state_list = ref.RefState.objects.all()
    usage_list = ref.RefUsageIntention.objects.all()
    
    # init var
    firstName = ''; lastName = ''; username = ''; user_email = ''; password = ''; confirm = ''; user_phone = '';
    user_mobile = ''; user_title = ''; usage = ''; org_name = ''; org_address = ''; org_city = ''; org_dept = '';
    org_state = ''; org_post = ''; org_phone = ''; org_fax = ''; contact_title = ''; contact_firstname = '';
    contact_lastname = ''; contact_phone = ''; contact_email = '';
    
    # init password_error and email_exist bools for validating
    # password and email.
    password_error = False
    email_exist = False
    upload_format_error = False
    username_exist = False
    
    # init form
    #form = UploadFileForm()
    if (request.POST):
        # get user entered details
        # user details
        firstName = request.POST['firstname']
        lastName = request.POST['lastname']
        username = request.POST['username']
        user_email = request.POST['user_email']
        password = request.POST['password']
        confirm = request.POST['confirm_password']
        user_phone = request.POST['user_phone']
        user_mobile = request.POST['user_mobile']
        user_title = request.POST['user_title']
        usage = request.POST['usage']
        
        # organisation
        org_name = request.POST['org_name']
        org_dept = request.POST['org_dept']
        org_address = request.POST['org_address']
        org_city = request.POST['org_city']
        org_state = request.POST['org_state']
        org_post = request.POST['org_post']
        org_phone = request.POST['org_phone']
        org_fax = request.POST['org_fax']
        contact_title = request.POST['contact_title']
        contact_firstname = request.POST['contact_firstname']
        contact_lastname = request.POST['contact_lastname']
        contact_phone = request.POST['contact_phone']
        contact_email = request.POST['contact_email']
        
        # validate password, email and username
        if bool(password != confirm): 
            password_error = True
        if bool(User.objects.filter(email__iexact=user_email)):
            email_exist = True
        if bool(User.objects.filter(username__iexact=username)):
            username_exist = True
        
        # validate file upload extension
        if 'file' in request.FILES:
            uploaded_file = request.FILES['file']
            if not valid_file_extensions(uploaded_file.name):
                upload_format_error = True
        
        # I know this is stupid but I can't get the 'or' to work
        # for some reason it won't let me do this: if not password_error and not email_exist:
        if not password_error: 
            if not email_exist: 
                if not username_exist:
                    if not upload_format_error:
                        # create user model objects
                        user = User()
                        user_profile = UserProfile()
                        user_institution = UserInstitution()
                        contact = InstitutionContact()
                        
                        try:
                            # save user basic details - password will be encrypted
                            # email will be used as the users username
                            user.username = username
                            user.email = user_email
                            user.first_name = firstName
                            user.last_name = lastName
                            user.set_password(password)
                            user.is_staff = False
                            user.is_active = False
                            user.is_superuser = False

                            # need to save user first before we can assign it to the UserProfile and UserInstitution
                            user.save()

                            # create a user profile
                            user_profile.user = user
                            user_profile.Phone = user_phone
                            user_profile.Mobile = user_mobile
                            user_profile.IsHVPAdmin = False
                            user_profile.IsLabLeader = False

                            # set access level and status
                            user_profile.AccessStatus = get_object_or_404(ref.RefAccessStatus, pk=1)

                            # try and get ref to save
                            if (user_title):
                                user_profile.Title = ref.RefTitle.objects.get(Title__iexact = user_title)
                            if (usage):
                                user_profile.UsageIntention = ref.RefUsageIntention.objects.get(UsageIntention__iexact = usage)     

                            user_profile.save()


                            # create UserInstitution
                            user_institution.User = user
                            user_institution.PendingInstitution = user;
                            user_institution.BelongsTo = user;
                            user_institution.Institution = user;

                            user_institution.Name = org_name
                            user_institution.Department = org_dept
                            user_institution.Address = org_address
                            user_institution.City = org_city
                            if (org_state):
                                user_institution.State = ref.RefState.objects.get(State__iexact = org_state)
                            user_institution.PostCode = org_post
                            user_institution.Phone = org_phone
                            user_institution.Fax = org_fax

                            user_institution.ApplicationDate = date.today()

                            user_institution.save()


                            # try and get ref to save
                            if (contact_title):
                                contact.ContactTitle = ref.RefTitle.objects.get(Title__iexact = contact_title)                        
                            contact.ContactFirstName = contact_firstname
                            contact.ContactLastName = contact_lastname
                            contact.ContactPhone = contact_phone
                            contact.ContactEmail = contact_email

                            contact.UserInstitution = user_institution

                            contact.save()
                            
                            # send email
                            send_new_signup_email(user)
                            
                        except:
                            # in case of error! we delete objects if they have an id, the id
                            # will mean the object was saved with an auto incremented id.
                            # We use delete as there is no rollback feature in django :(
                            if user.id != None:
                                user.delete()
                            if user_profile.id != None:
                                user_profile.delete()
                            if user_institution.id != None:
                                user_institution.delete()
                            if contact.id != None:
                                contact.delete()
                            # TODO: if signup failed and user uploaded a file need to delete the
                            # file from the 'uploads/' directory and removed the record from 
                            # UserDocument table.  
                            
                            raise Http404('Something has gone horribly wrong! Please try again or contact the HVP system admin.') 
                        
                        return render_to_response('users/signup_complete.html')
        
    return render_to_response('users/signup.html',
                              {
                               #'form': form,
                               'title_list': title_list,
                               'state_list': state_list,
                               'usage_list': usage_list,
                               'password_error': password_error,
                               'email_exist': email_exist,
                               'username_exist': username_exist,
                               'upload_format_error': upload_format_error,
                               'firstName': firstName,
                               'lastName': lastName,
                               'username': username,
                               'user_email': user_email,
                               'user_phone': user_phone,
                               'user_mobile': user_mobile,
                               'user_title': user_title,
                               'usage': usage,
                               'org_name': org_name,
                               'org_dept': org_dept,
                               'org_address': org_address,
                               'org_city': org_city,
                               'org_state': org_state,
                               'org_post': org_post,
                               'org_phone': org_phone,
                               'org_fax': org_fax,
                               'contact_title': contact_title,
                               'contact_firstname': contact_firstname, 
                               'contact_lastname': contact_lastname,
                               'contact_phone': contact_phone,
                               'contact_email': contact_email
                               })