def test_adding_new_team_existing_organisation(self): get_user_model().objects.create_user(userid='*****@*****.**') o = Organisation(name='org0001') o.save() # Log in as user form = self.app.get(reverse('login')).form form['userid'] = '*****@*****.**' form.submit() # Now go to the update profile page and check the first team # in the list of teams. form = self.app.get(reverse( 'user-update-teams', kwargs={'slug': 'user0001com'})).form form['teamname'] = 'team0001' form['organisation'].value = o.pk form.submit() # Go back to the users profile page to see if the team is now # on the list of teams response = self.app.get(reverse( 'user-detail', kwargs={'slug': 'user0001com'})) self.assertTrue(response.html.find('a', text='team0001'))
def test_can_create_team(self): o = Organisation(name='New Org') o.save() self.assertTrue(o.pk) t = Team(name='New Team', organisation=o) t.save() self.assertTrue(t.pk)
def test_user_with_full_profile_goes_to_links(self): # This user has a username and teams o = Organisation(name='org0001') o.save() t = Team(name='team0001', organisation=o) t.save() user = get_user_model().objects.create_user( userid='*****@*****.**', name='User 0001', best_way_to_find='In the kitchen', best_way_to_contact='By email', phone='00000000', email='*****@*****.**', ) user.teams.add(t) user.save() # Log in as user form = self.app.get(reverse('login')).form form['userid'] = '*****@*****.**' response = form.submit().follow() self.assertIn( 'All tools', response.html.find('h1').text )
def test_leave_button_visible(self): # Create an organisation, a team and a user and assign that # team to that user. o = Organisation(name="org0001") o.save() t = Team(name="team0001", organisation=o) t.save() u = get_user_model().objects.create_user(userid='*****@*****.**') u.teams.add(t) u.save() # Now we need to log in as that user. form = self.app.get(reverse('login')).form form['userid'] = '*****@*****.**' form.submit().follow() # Now go visit the team page... response = self.app.get(reverse('team-detail', kwargs={'pk': t.pk})) form = response.form self.assertEqual(form.method, 'post') self.assertEqual(form.action, '/teams/' + str(t.pk) + '/leave') # Make sure this button no longer exists self.assertIsNone( response.html.find( 'a', attrs={'href': '/teams/' + str(t.pk) + '/leave'})) # Make sure this button no longer exists self.assertIsNone( response.html.find('a', attrs={'href': '/teams/' + str(t.pk) + '/join'}))
def test_cannot_create_org_and_new_org_team(self): # Create and log in a user get_user_model().objects.create_user(userid='*****@*****.**') form = self.app.get(reverse('login')).form form['userid'] = 'user0001com' form.submit().follow() o = Organisation(name='New Org') o.save() form = self.app.get(reverse('team-create')).form form['name'] = 'New Team' form['organisation'] = str(o.pk) form['new_organisation'] = 'New Org' response = form.submit() html = response.html errors = html.find( "ul", {"class": "form-error-list"} ).find("li").contents self.assertEqual(len(errors), 1) failMessage = "You can't select an existing organisation and " failMessage += "create a new one at the same time." self.assertIn(failMessage, errors[0].text)
def test_adding_new_existing_team(self): get_user_model().objects.create_user(userid='*****@*****.**') o = Organisation(name='org0001') o.save() t = Team(name='team0001', organisation=o) t.save() # Log in as user form = self.app.get(reverse('login')).form form['userid'] = '*****@*****.**' form.submit() # Go to the user's profile page and assert that the team is NOT # showing up in the list of teams they are a member of. response = self.app.get(reverse( 'user-detail', kwargs={'slug': 'user0001com'})) self.assertFalse(response.html.find('a', text='team0001')) # Now go to the update profile page and check the first team # in the list of teams. form = self.app.get(reverse( 'user-update-teams', kwargs={'slug': 'user0001com'})).form form.get('team', index=0).checked = True form.submit() # Go back to the users profile page to see if the team is now # on the list of teams response = self.app.get(reverse( 'user-detail', kwargs={'slug': 'user0001com'})) self.assertTrue(response.html.find('a', text='team0001'))
def test_can_create_team(self): o = Organisation(name="New Org") o.save() self.assertTrue(o.pk) t = Team(name="New Team", organisation=o) t.save() self.assertTrue(t.pk)
def test_user_can_have_multiple_teams_which_have_multiple_users(self): o = Organisation(name='New Org') o.save() t1 = Team(name='Team Awesome', organisation=o) t1.save() t2 = Team(name='Team Great', organisation=o) t2.save() u1 = get_user_model().objects.create_user(userid='teamplayer') u1.teams.add(t1) u1.teams.add(t2) u1.save() u2 = get_user_model().objects.create_user(userid='teamplayer2') u2.teams.add(t2) u2.save() self.assertIn(u1, t1.user_set.all()) self.assertIn(u1, t2.user_set.all()) self.assertNotIn(u2, t1.user_set.all()) self.assertIn(u2, t2.user_set.all()) self.assertEqual(len(t1.user_set.all()), 1) self.assertEqual(len(t2.user_set.all()), 2)
def clean(self): cleaned_data = super(TeamForm, self).clean() # We've been passed both org & new_org, *sigh* if ('organisation' in cleaned_data and cleaned_data['new_organisation'] != ''): raise forms.ValidationError({ "organisation": [self.error_both_fields] } ) # We've been passed neither, *double sigh* if ('organisation' not in cleaned_data and cleaned_data['new_organisation'] == ''): raise forms.ValidationError({ "organisation": [self.error_neither_field] } ) # We've been passed a new organisation if ('organisation' not in cleaned_data and cleaned_data['new_organisation'] != ''): # Check if the team name already exists t_n = cleaned_data.get('name') check_team = Team.objects.filter(name=t_n).exists() # If it doesn't then we carry on dealing with the new org if check_team is False: # Grab the new org name o_n = cleaned_data.get('new_organisation') # See if it exists check_org = \ Organisation.objects.filter(name=o_n).exists() # If it does, then we just grab it, otherwise create it if check_org is True: new_organisation = Organisation.objects.get(name=o_n) else: new_organisation = Organisation() new_organisation.name = cleaned_data['new_organisation'] new_organisation.save() # put the record into the organisation field, which *should* # contain a valid organisaiton record cleaned_data['organisation'] = new_organisation # Just to be neat & tidy, remove the new_organisation field del cleaned_data['new_organisation'] return cleaned_data
def test_join_leave_buttons_work(self): # Create an organisation, a team and a user and assign that # team to that user. o = Organisation(name="org0001") o.save() t = Team(name="team0001", organisation=o) t.save() u = get_user_model().objects.create_user(userid='*****@*****.**') login_user(self, u) response = self.app.get(reverse('team-detail', kwargs={'pk': t.pk})) form = response.form self.assertEqual(form.method, 'post') self.assertEqual(form.action, '/teams/' + str(t.pk) + '/join') response = form.submit().follow() # Make sure the user is now in the team response = self.app.get(reverse('user-detail', kwargs={'slug': u.slug})) self.assertTrue( response.html.find('a', attrs={ 'class': 'main-list-item', 'href': '/teams/' + str(t.pk) })) # Go back to the team detail page so we can submit the link response = self.app.get(reverse('team-detail', kwargs={'pk': t.pk})) form = response.form self.assertEqual(form.method, 'post') self.assertEqual(form.action, '/teams/' + str(t.pk) + '/leave') response = form.submit().follow() # Make sure the user is no longer in the team response = self.app.get(reverse('user-detail', kwargs={'slug': u.slug})) self.assertFalse( response.html.find('a', attrs={ 'class': 'main-list-item', 'href': '/teams/' + str(t.pk) }))
def test_cannot_create_duplicate_organisations(self): o = Organisation(name='New Org') o.save() self.assertTrue(o.pk) o = Organisation(name='Existing Org') with self.assertRaises(IntegrityError): o.save()
def test_leave_button_visible(self): # Create an organisation, a team and a user and assign that # team to that user. o = Organisation(name="org0001") o.save() t = Team(name="team0001", organisation=o) t.save() u = get_user_model().objects.create_user(userid='*****@*****.**') u.teams.add(t) u.save() # Now we need to log in as that user. form = self.app.get(reverse('login')).form form['userid'] = '*****@*****.**' form.submit().follow() # Now go visit the team page... response = self.app.get( reverse( 'team-detail', kwargs={'pk': t.pk} ) ) form = response.form self.assertEqual( form.method, 'post' ) self.assertEqual( form.action, '/teams/' + str(t.pk) + '/leave' ) # Make sure this button no longer exists self.assertIsNone( response.html.find( 'a', attrs={'href': '/teams/' + str(t.pk) + '/leave'} ) ) # Make sure this button no longer exists self.assertIsNone( response.html.find( 'a', attrs={'href': '/teams/' + str(t.pk) + '/join'} ) )
def test_cannot_create_duplicate_organisation(self): # Create and log in a user get_user_model().objects.create_user(userid='*****@*****.**') form = self.app.get(reverse('login')).form form['userid'] = '*****@*****.**' form.submit().follow() o = Organisation(name='alpha') o.save() form = self.app.get(reverse('organisation-list')).form form['name'] = 'alpha' response = form.submit() form = response.context['form'] self.assertIn('Organisation with this Name already exists.', form['name'].errors)
def test_cannot_create_duplicate_organisation(self): # Create and log in a user get_user_model().objects.create_user(userid='*****@*****.**') form = self.app.get(reverse('login')).form form['userid'] = '*****@*****.**' form.submit().follow() o = Organisation(name='alpha') o.save() form = self.app.get(reverse('organisation-list')).form form['name'] = 'alpha' response = form.submit() form = response.context['form'] self.assertIn( 'Organisation with this Name already exists.', form['name'].errors )
def get_context_data(self, **kwargs): context = super(TeamList, self).get_context_data(**kwargs) context['form'] = TeamForm context['top_teams'] = Team.with_most_members() context['top_organisations'] = Organisation.with_most_teams() context['total_teams_in_db'] = Team.objects.count() return context
def test_can_create_team_with_existsing_org(self): # Create and log in a user get_user_model().objects.create_user(userid='*****@*****.**') form = self.app.get(reverse('login')).form form['userid'] = 'user0001com' form.submit().follow() o = Organisation(name='New Org') o.save() team_name = 'New Team skippity bippity bop' form = self.app.get(reverse('team-create')).form form['name'] = team_name form['organisation'] = str(o.pk) response = form.submit().follow() self.assertEquals(response.status_int, 200) html = response.html links = html.findAll("a", text=team_name)[0].contents self.assertIn(team_name, links)
def test_can_click_through_existing_team_link(self): # Create and log in a user get_user_model().objects.create_user(userid='*****@*****.**') form = self.app.get(reverse('login')).form form['userid'] = 'user0001com' form.submit().follow() o = Organisation(name='New Org') o.save() team_name = 'New Team skippity bippity bop' t = Team(name=team_name, organisation=o) t.save() response = self.app.get(reverse('team-list')) response = self.app.get( response.html.find('a', text=team_name).attrs['href']) org_name = response.html.find('h1', attrs={ 'class': 'heading-xlarge' }).get_text(strip=True) self.assertEquals(org_name, 'Team' + team_name)
def test_can_click_through_existing_organisation_link(self): # Create and log in a user get_user_model().objects.create_user(userid='*****@*****.**') form = self.app.get(reverse('login')).form form['userid'] = '*****@*****.**' form.submit().follow() o = Organisation(name='org0001') o.save() response = self.app.get(reverse('organisation-list')) response = self.app.get(response.html.find( 'a', text=o.name ).attrs['href'] ) org_name = response.html.find( 'h1', attrs={'class': 'heading-xlarge'} ).get_text(strip=True) self.assertEquals(org_name, 'Organisation' + o.name)
def test_user_has_username_teams_no_extra_info_redirected(self): # This user has a username and teams o = Organisation(name='org0001') o.save() t = Team(name='team0001', organisation=o) t.save() user = get_user_model().objects.create_user( userid='*****@*****.**', name='User 0001') user.teams.add(t) user.save() # Log in as user form = self.app.get(reverse('login')).form form['userid'] = '*****@*****.**' response = form.submit().follow() # Check that the add more details text is shown self.assertIn( 'enter more details', response.html.find( None, {"class": "user_id"} ).text ) # Check that the link in the nav is heading to the right place self.assertIn( '/users/user0001com/update-profile', response.html.find( 'span', attrs={'data-slug': 'user0001com'} ).find('a').attrs['href'], ) # Make sure we *don't* have an alert summary heading self.assertEquals( response.html.find( 'h3', attrs={'class': 'alert-summary-heading'} ).text, 'Please add additional information' )
def get_context_data(self, **kwargs): context = super(UserList, self).get_context_data(**kwargs) context['top_teams'] = Team.with_most_members() context['top_organisations'] = Organisation.with_most_teams() context['total_users_in_db'] = User.objects.count() if self.has_query(): context['query'] = self.request.GET['q'] context['results_length'] = len(context['object_list']) return context
def get_context_data(self, **kwargs): context = super(UserList, self).get_context_data(**kwargs) context["top_teams"] = Team.with_most_members() context["top_organisations"] = Organisation.with_most_teams() context["total_users_in_db"] = User.objects.count() if self.has_query(): context["query"] = self.request.GET["q"] context["results_length"] = len(context["object_list"]) return context
def create_organisation(name, num_teams=0, num_members=0, usernames={}): o = Organisation(name=name) o.save() user_global_id = 0 for x in range(0, num_teams): t = Team(name='New Team %d' % (x + 1), organisation=o) t.save() for y in range(user_global_id, num_members + user_global_id): if y in usernames.keys(): username = usernames[y] else: username = '******' % (y + 1) u = get_user_model().objects.create_user( userid='teammember%d' % (y + 1), name=username, ) u.teams.add(t) u.save() t.save() # Before we go to the next team, increment start ID for member name user_global_id += num_members return o
def create_team(name, num_members=0, usernames={}): o = Organisation(name="Organisation for %s" % name) o.save() t = Team(name=name, organisation=o) t.save() for x in range(0, num_members): if x in usernames.keys(): username = usernames[x] else: username = '******' % (x + 1) u = get_user_model().objects.create_user( userid='teammember%d' % (x + 1), name=username, ) # if username is not None: # u.username = username u.teams.add(t) u.save() t.save() return t
def test_auto_login_on_landing_for_full_profile(self): o = Organisation(name='org0001') o.save() t = Team(name='team0001', organisation=o) t.save() user = get_user_model().objects.create_user( userid='*****@*****.**', name='User 0001', best_way_to_find='In the kitchen', best_way_to_contact='By email', phone='00000000', email='*****@*****.**', ) user.teams.add(t) user.save() headers = {'KEYCLOAK_USERNAME': '******'} first_step = self.app.get(reverse('home'), headers=headers) self.assertEqual('http://localhost:80/login', first_step.location) second_response = self.app.get(first_step.location, headers=headers) self.assertEqual('http://localhost:80/links', second_response.location)
def test_can_click_through_existing_team_link(self): # Create and log in a user get_user_model().objects.create_user(userid='*****@*****.**') form = self.app.get(reverse('login')).form form['userid'] = 'user0001com' form.submit().follow() o = Organisation(name='New Org') o.save() team_name = 'New Team skippity bippity bop' t = Team(name=team_name, organisation=o) t.save() response = self.app.get(reverse('team-list')) response = self.app.get(response.html.find( 'a', text=team_name ).attrs['href'] ) org_name = response.html.find( 'h1', attrs={'class': 'heading-xlarge'} ).get_text(strip=True) self.assertEquals(org_name, 'Team' + team_name)
def test_auto_login_for_complete_profile_goes_to_links(self): # This user has a username and teams o = Organisation(name='org0001') o.save() t = Team(name='team0001', organisation=o) t.save() user = get_user_model().objects.create_user( userid='*****@*****.**', name='User 0001', best_way_to_find='In the kitchen', best_way_to_contact='By email', phone='00000000', email='*****@*****.**', ) user.teams.add(t) user.save() headers = {'KEYCLOAK_USERNAME': '******'} response = self.app.get(reverse('login'), headers=headers) self.assertEqual( 'http://localhost:80/links', response.location )
def test_top_teams_and_orgs(self): # Create three orgs o1 = Organisation(name='org0001') o1.save() o2 = Organisation(name='org0002') o2.save() o3 = Organisation(name='org0003') o3.save() # Create SEVEN teams!!! Grouped into organistions t1 = Team(name='team0001', organisation=o1) t1.save() t2 = Team(name='team0002', organisation=o1) t2.save() t3 = Team(name='team0003', organisation=o1) t3.save() t4 = Team(name='team0004', organisation=o2) t4.save() t5 = Team(name='team0005', organisation=o2) t5.save() t6 = Team(name='team0006', organisation=o3) t6.save() t7 = Team(name='team0007', organisation=o3) t7.save() # Now we need three users, and throw them into teams u1 = get_user_model().objects.create_user(userid='user0001') u1.save() u1.teams.add(t2, t3, t4) u1.save() u2 = get_user_model().objects.create_user(userid='user0002') u2.save() u2.teams.add(t5, t6) u2.save() u3 = get_user_model().objects.create_user(userid='user0003') u3.save() u3.teams.add(t3, t5, t6, t7) u3.save() # Last and not least we need a tool for everyone to use l1 = Link( name='link0001', is_external=True, owner=u1, destination='http://google.com' ) l1.save() # Now we need to log the activity of each user. # # User 1, is going to use the tool 4 times. for i in range(0, 4): l1.register_usage(user=u1, force_new=True) # User 2, is going to use the tool 2 times. for i in range(0, 2): l1.register_usage(user=u2, force_new=True) # User 3, is going to use the tool 5 times. for i in range(0, 5): l1.register_usage(user=u3, force_new=True) # Login as the first user form = self.app.get(reverse('login')).form form['userid'] = 'user0001' form.submit() # Now lets go an have a look at the details page, and check # out the teams and organisations usage count. details_page = self.app.get( reverse('link-detail', kwargs={'pk': l1.pk})) # Due to the counting above, we know the teams have used the tool # the following amount, so lets check for that... # t1:0, t2:4, t3:9, t4:4, t5:7, t6:7, t7:5 # i.e. there are no members is team 1. # only user 1 is in team 2. # users 1 & 3 are in team 3. usage_by_teams = details_page.html.find( 'ul', id='usage-by-teams' ).findAll('li') self.assertEquals(usage_by_teams[0].text, 'team0003 have collectively used the tool 9 times') self.assertEquals(usage_by_teams[1].text, 'team0005 have collectively used the tool 7 times') self.assertEquals(usage_by_teams[2].text, 'team0006 have collectively used the tool 7 times') self.assertEquals(usage_by_teams[3].text, 'team0007 have collectively used the tool 5 times') self.assertEquals(usage_by_teams[4].text, 'team0002 have collectively used the tool 4 times') # We do a similar thing with organisations, which are tricky # because user 1 is in team 2 & 3, so both those teams get the # 4 times user 1 used the tool. But teams 2 & 3 are both in org 1, # and the code has made sure to only count the useage once, rather # than 4 times from team 2 and 4 again from team 3. This checks # that based on the numbers above the results are as follows. # o1:9, o2:11, o3:7. Ordered that's o2,o1,o3. used_by_organisations = details_page.html.find( 'ul', id='usage-by-organisations' ).findAll('li') self.assertEquals(used_by_organisations[0].text, 'org0002 have collectively used the tool 11 times') self.assertEquals(used_by_organisations[1].text, 'org0001 have collectively used the tool 9 times') self.assertEquals(used_by_organisations[2].text, 'org0003 have collectively used the tool 7 times')
def setUp(self): o = Organisation(name='Existing Org') o.save() t = Team(name='Existing Team', organisation=o) t.save()
def test_top_teams_and_orgs(self): # Create three orgs o1 = Organisation(name='org0001') o1.save() o2 = Organisation(name='org0002') o2.save() o3 = Organisation(name='org0003') o3.save() # Create SEVEN teams!!! Grouped into organistions t1 = Team(name='team0001', organisation=o1) t1.save() t2 = Team(name='team0002', organisation=o1) t2.save() t3 = Team(name='team0003', organisation=o1) t3.save() t4 = Team(name='team0004', organisation=o2) t4.save() t5 = Team(name='team0005', organisation=o2) t5.save() t6 = Team(name='team0006', organisation=o3) t6.save() t7 = Team(name='team0007', organisation=o3) t7.save() # Now we need three users, and throw them into teams u1 = get_user_model().objects.create_user(userid='user0001') u1.save() u1.teams.add(t2, t3, t4) u1.save() u2 = get_user_model().objects.create_user(userid='user0002') u2.save() u2.teams.add(t5, t6) u2.save() u3 = get_user_model().objects.create_user(userid='user0003') u3.save() u3.teams.add(t3, t5, t6, t7) u3.save() # Last and not least we need a tool for everyone to use l1 = Link(name='link0001', is_external=True, owner=u1, destination='http://google.com') l1.save() # Now we need to log the activity of each user. # # User 1, is going to use the tool 4 times. for i in range(0, 4): l1.register_usage(user=u1, force_new=True) # User 2, is going to use the tool 2 times. for i in range(0, 2): l1.register_usage(user=u2, force_new=True) # User 3, is going to use the tool 5 times. for i in range(0, 5): l1.register_usage(user=u3, force_new=True) # Login as the first user form = self.app.get(reverse('login')).form form['userid'] = 'user0001' form.submit() # Now lets go an have a look at the details page, and check # out the teams and organisations usage count. details_page = self.app.get( reverse('link-detail', kwargs={'pk': l1.pk})) # Due to the counting above, we know the teams have used the tool # the following amount, so lets check for that... # t1:0, t2:4, t3:9, t4:4, t5:7, t6:7, t7:5 # i.e. there are no members is team 1. # only user 1 is in team 2. # users 1 & 3 are in team 3. usage_by_teams = details_page.html.find( 'ul', id='usage-by-teams').findAll('li') self.assertEquals(usage_by_teams[0].text, 'team0003 have collectively used the tool 9 times') self.assertEquals(usage_by_teams[1].text, 'team0005 have collectively used the tool 7 times') self.assertEquals(usage_by_teams[2].text, 'team0006 have collectively used the tool 7 times') self.assertEquals(usage_by_teams[3].text, 'team0007 have collectively used the tool 5 times') self.assertEquals(usage_by_teams[4].text, 'team0002 have collectively used the tool 4 times') # We do a similar thing with organisations, which are tricky # because user 1 is in team 2 & 3, so both those teams get the # 4 times user 1 used the tool. But teams 2 & 3 are both in org 1, # and the code has made sure to only count the useage once, rather # than 4 times from team 2 and 4 again from team 3. This checks # that based on the numbers above the results are as follows. # o1:9, o2:11, o3:7. Ordered that's o2,o1,o3. used_by_organisations = details_page.html.find( 'ul', id='usage-by-organisations').findAll('li') self.assertEquals(used_by_organisations[0].text, 'org0002 have collectively used the tool 11 times') self.assertEquals(used_by_organisations[1].text, 'org0001 have collectively used the tool 9 times') self.assertEquals(used_by_organisations[2].text, 'org0003 have collectively used the tool 7 times')
def test_join_leave_buttons_work(self): # Create an organisation, a team and a user and assign that # team to that user. o = Organisation(name="org0001") o.save() t = Team(name="team0001", organisation=o) t.save() u = get_user_model().objects.create_user(userid='*****@*****.**') login_user(self, u) response = self.app.get( reverse( 'team-detail', kwargs={'pk': t.pk} ) ) form = response.form self.assertEqual( form.method, 'post' ) self.assertEqual( form.action, '/teams/' + str(t.pk) + '/join' ) response = form.submit().follow() # Make sure the user is now in the team response = self.app.get( reverse( 'user-detail', kwargs={'slug': u.slug} ) ) self.assertTrue( response.html.find( 'a', attrs={ 'class': 'main-list-item', 'href': '/teams/' + str(t.pk) } ) ) # Go back to the team detail page so we can submit the link response = self.app.get( reverse( 'team-detail', kwargs={'pk': t.pk} ) ) form = response.form self.assertEqual( form.method, 'post' ) self.assertEqual( form.action, '/teams/' + str(t.pk) + '/leave' ) response = form.submit().follow() # Make sure the user is no longer in the team response = self.app.get( reverse( 'user-detail', kwargs={'slug': u.slug} ) ) self.assertFalse( response.html.find( 'a', attrs={ 'class': 'main-list-item', 'href': '/teams/' + str(t.pk) } ) )
def form_valid(self, form): userDetails = form.save(commit=False) # Don't do any of this is the user isn't currently logged in # or different to the currently logged in user if self.request.user.is_authenticated() is False or userDetails.id != self.request.user.id: return HttpResponseRedirect(self.get_success_url()) # If we have been passed a name then we have come from the # user details form, if we don't have a name, then we are dealing # with teams. if form.data.get("name") is not None: userDetails.save() else: user = User.objects.get(pk=userDetails.pk) # Now we need to dump all the current links to teams and # then add them all back in. user.teams.clear() for team in form.data.getlist("team"): user.teams.add(int(team)) # We need to see if we have been passed over a new team name # if so then we have a bunch of work to do around adding that # team team_name = form.data.get("teamname") if team_name is not None and team_name is not "": new_organisation_name = form.data.get("new_organisation") organisation_id = form.data.get("organisation") # Now check to see if this team is using an existing # organisation or a new_organisation. # If it a new organisation then we need to create it. if new_organisation_name is not None and new_organisation_name is not "": check_org = Organisation.objects.filter(name=new_organisation_name).exists() if check_org is True: new_organisation = Organisation.objects.get(name=new_organisation_name) else: new_organisation = Organisation() new_organisation.name = new_organisation_name new_organisation.save() else: # Otherwise we are going to use the organisation we # have been passed over. check_org = Organisation.objects.filter(pk=organisation_id).exists() if check_org is True: new_organisation = Organisation.objects.get(pk=organisation_id) else: # TODO: Raise an error here to display on the form return self.render_to_response(self.get_context_data()) # Either way we now have a new_organisation object that we # can use to create the team. check_team = Team.objects.filter(name=team_name).exists() if check_team is True: new_team = Team.objects.filter(name=team_name) else: new_team = Team(name=team_name, organisation=new_organisation) new_team.save() # Now add the new team to the teams join on the user user.teams.add(new_team.pk) user.save() # If the user wants to add another team, do that here # TODO: add a #team thingy to the URL so we can jump down to the # teams section submit_action = form.data.get("submit_action") if submit_action is not None and submit_action is not "": if submit_action in ["Save and add a new team", "Save and manage team membership"]: return HttpResponseRedirect(reverse("user-update-teams", kwargs={"slug": self.request.user.slug})) # Normally we'd just go back to their profile page. So we'll do # that here. return HttpResponseRedirect(self.get_success_url())
def setUp(self): o = Organisation(name="Existing Org") o.save() t = Team(name="Existing Team", organisation=o) t.save()
def setUp(self): o = Organisation(name='Existing Org') o.save()
def form_valid(self, form): userDetails = form.save(commit=False) # Don't do any of this is the user isn't currently logged in # or different to the currently logged in user if (self.request.user.is_authenticated() is False or userDetails.id != self.request.user.id): return HttpResponseRedirect(self.get_success_url()) # If we have been passed a name then we have come from the # user details form, if we don't have a name, then we are dealing # with teams. if form.data.get('name') is not None: userDetails.save() else: user = User.objects.get(pk=userDetails.pk) # Now we need to dump all the current links to teams and # then add them all back in. user.teams.clear() for team in form.data.getlist('team'): user.teams.add(int(team)) # We need to see if we have been passed over a new team name # if so then we have a bunch of work to do around adding that # team team_name = form.data.get('teamname') if (team_name is not None and team_name is not ''): new_organisation_name = form.data.get('new_organisation') organisation_id = form.data.get('organisation') # Now check to see if this team is using an existing # organisation or a new_organisation. # If it a new organisation then we need to create it. if (new_organisation_name is not None and new_organisation_name is not ''): check_org = Organisation.objects.filter( name=new_organisation_name ).exists() if check_org is True: new_organisation = Organisation.objects.get( name=new_organisation_name ) else: new_organisation = Organisation() new_organisation.name = new_organisation_name new_organisation.save() else: # Otherwise we are going to use the organisation we # have been passed over. check_org = Organisation.objects.filter( pk=organisation_id).exists() if check_org is True: new_organisation = Organisation.objects.get( pk=organisation_id ) else: # TODO: Raise an error here to display on the form return self.render_to_response(self.get_context_data()) # Either way we now have a new_organisation object that we # can use to create the team. check_team = Team.objects.filter(name=team_name).exists() if check_team is True: new_team = Team.objects.filter(name=team_name) else: new_team = Team( name=team_name, organisation=new_organisation ) new_team.save() # Now add the new team to the teams join on the user user.teams.add(new_team.pk) user.save() # If the user wants to add another team, do that here # TODO: add a #team thingy to the URL so we can jump down to the # teams section submit_action = form.data.get('submit_action') if (submit_action is not None and submit_action is not ''): if submit_action in ['Save and add a new team', 'Save and manage team membership']: return HttpResponseRedirect( reverse( 'user-update-teams', kwargs={ 'slug': self.request.user.slug } ) ) # Normally we'd just go back to their profile page. So we'll do # that here. return HttpResponseRedirect(self.get_success_url())
def test_can_create_organisation(self): o = Organisation(name='testy') o.save() self.assertTrue(o.pk)