def test_item_converter(self): ctx = ApplicationContext(TestableDAOContext()) print 'Creating organization and contact item.' org = ctx.get_object('OrganizationDAO') org_dto = OrganizationDTO(name="Univerisityee of Nyeebraska-Lincoln") org.create_update(org_dto) org_model = DTOConverter.from_dto(Organization, org_dto) contact_item = ScrapedContact( first_name='Bee', last_name='Yee', organization={'name': "Univerisityee of Nyeebraska-Lincoln"}) print 'Converting contact to model.' converter = ctx.get_object('ModelConverter') model_contact = converter.to_model(Contact, contact_item) self.assertEqual(model_contact.organization.name, org_model.name)
def signup(request): """ Sends a request to the Signup page. Returns: A rendered page of the Signup form if the user is not logged in already. """ if 'user_id' in request.session: user_id = request.session['user_id'] logger.error('Bad request for signup made by user={0}'.format(user_id)) return HttpResponseRedirect('/') error = '' form = SignupForm(request.POST or None) if request.method == 'POST': user_dao = ctx.get_object('UserDAO') if form.is_valid(): data = form.cleaned_data password = make_password(data['password']) new_user = User(first_name=data['first_name'], last_name=data['last_name'], email=data['email'], password=password, background=data['background'], account_type=int(data['account_type'])) org_dao = ctx.get_object('OrganizationDAO') if 'org_type' in data and data['org_type']: new_user.org_type = int(data['org_type']) if 'organization' in data and data['organization']: existing_org = org_dao.find(name=data['organization']) if existing_org: new_user.organization = existing_org else: new_org = OrganizationDTO() new_org.name = data['organization'] if new_user.org_type: new_org.types.append(new_user.org_type) else: new_org.types.append(OrgTypesEnum.UNKNOWN) new_user.organization = org_dao.create_update(new_org) try: user_dto = DTOConverter.to_dto(UserDTO, new_user) ret_user = user_dao.create_update(user_dto) request.session['name'] = str(new_user.first_name) + ' ' + str( new_user.last_name) request.session['user_id'] = str(ret_user.id) request.session['account_type'] = str(ret_user.account_type) request.session.set_expiry(SESSION_TIMEOUT) return HttpResponseRedirect('/') except: logger.error('Error occurred during signup') error = 'Oops! We had a little trouble signing you up. Please try again later.' return render(request, 'user/signup.html', {'form': form, 'error': error})
def manage_account(request): """ Sends a request to the Account Preferences page. Returns: A rendered page with editable account settings if the user is logged in. """ if 'user_id' not in request.session: logger.error('Request made for account-preferences without login') return HttpResponseRedirect('/login') user_id = request.session['user_id'] user_dao = ctx.get_object('UserDAO') user = user_dao.find(id=user_id) user_dict = _create_user_dict(user) user_dict['user_id'] = str(user_id) form = ManageForm(request.POST or None, initial=user_dict) error = '' success = '' if request.method == 'POST': if form.is_valid(): data = form.cleaned_data password = data['password'] user.first_name = data['first_name'] user.last_name = data['last_name'] user.account_type = int(data['account_type']) user.background = data['background'] user.email = data['email'] if 'org_type' in data and data['org_type']: user.org_type = int(data['org_type']) if password: user.password = make_password(password) org_dao = ctx.get_object('OrganizationDAO') if 'organization' in data and data['organization']: existing_org = org_dao.find(name=data['organization']) if existing_org: user.organization = existing_org else: new_org = OrganizationDTO() new_org.name = data['organization'] if user.org_type: new_org.types.append(user.org_type) else: new_org.types.append(OrgTypesEnum.UNKNOWN) user.organization = org_dao.create_update(new_org) try: ret_user = user_dao.create_update(user) success = 'Account settings changed successfully' request.session[ 'name'] = ret_user.first_name + ' ' + ret_user.last_name request.session['account_type'] = ret_user.account_type except: logger.error( 'Error occurred during account update for user={0}'.format( user_id)) error = 'Oops! We had a little trouble updating your account. Please try again later.' return render(request, 'user/manage.html', { 'form': form, 'error': error, 'success': success })