def setUp(self): self.superuser, created = User.objects.get_or_create( username='******', is_superuser=True) self.user1 = User.objects.all()[0] self.user2 = User.objects.all()[1] self.superuser2, x = User.objects.get_or_create(username='******', is_superuser=True) self.regular_user = User.objects.filter( is_active=True, is_superuser=False).exclude( pk__in=[x.pk for x in [self.user1, self.user2]])[0] self.team1 = Team(name='test11', slug='test11') self.team1.save() self.team1_member = TeamMember(team=self.team1, user=self.user1) self.team1_member.save() self.team2 = Team(name='test22', slug='test22') self.team2.save() self.team2_member = TeamMember(team=self.team2, user=self.user2) self.team2_member.save() self.video = Video.objects.all()[0] for user in User.objects.all(): user.set_password(user.username) user.save()
def create_team(request, username): """ Display the TeamCreationForm """ if request.method == 'POST': form = TeamCreationForm(request.POST) if form.is_valid() and request.user.pk == form.cleaned_data['creator']: team = User.objects.create_user(form.cleaned_data['name']) team_creator_attrs = dict( team=team, user=request.user, permission=4, creator=True ) team_creator = TeamMember(**team_creator_attrs) team_creator.save() try: profile = team.get_profile() except Profile.DoesNotExist: profile = Profile(user=team) profile.organization = True profile.creator_id = request.user.pk profile.save() HttpResponseRedirect(reverse('update_team', args=(form.cleaned_data["name"],))) else: form = TeamCreationForm(initial={'creator': request.user.pk}) return render_to_response('teams/team_create.html', {'form': form, 'username': username}, context_instance=RequestContext(request))
def test_application_new(self): def _get_counts(member): email_to = "%s" % (member.user.email) return Message.objects.filter(user=member.user).count() , \ len([x for x in mail.outbox if email_to in x.recipients()]) team, created = Team.objects.get_or_create(name='test', slug='test') applying_user = UserFactory() # creates dummy users: for x in xrange(0, 4): user = UserFactory( username="******" % x, email="*****@*****.**" % x, notify_by_email=True, notify_by_message=True, ) tm = TeamMember(team=team, user=user) if x == 0: tm.role = TeamMember.ROLE_OWNER owner = tm elif x == 1: tm.role = TeamMember.ROLE_ADMIN admin = tm elif x == 2: tm.role = TeamMember.ROLE_MANAGER manager = tm elif x == 3: tm.role = TeamMember.ROLE_CONTRIBUTOR contributor = tm tm.save() # now make sure we count previsou messages owner_messge_count_1, owner_email_count_1 = _get_counts(owner) admin_messge_count_1, admin_email_count_1 = _get_counts(admin) manager_messge_count_1, manager_email_count_1 = _get_counts(manager) contributor_messge_count_1, contributor_email_count_1 = _get_counts( contributor) # now delete and check numers app = Application.objects.create(team=team, user=applying_user) app.save() messages.tasks.application_sent.run(app.pk) # owner and admins should receive email + message owner_messge_count_2, owner_email_count_2 = _get_counts(owner) self.assertEqual(owner_messge_count_1 + 1, owner_messge_count_2) self.assertEqual(owner_email_count_1 + 1, owner_email_count_2) admin_messge_count_2, admin_email_count_2 = _get_counts(admin) self.assertEqual(admin_messge_count_1 + 1, admin_messge_count_2) self.assertEqual(admin_email_count_1 + 1, admin_email_count_2) # manager shoud not manager_messge_count_2, manager_email_count_2 = _get_counts(manager) self.assertEqual(manager_messge_count_1, manager_messge_count_2) self.assertEqual(manager_email_count_1, manager_email_count_2) # contributor shoud not contributor_messge_count_2, contributor_email_count_2 = _get_counts( contributor) self.assertEqual(contributor_messge_count_1, contributor_messge_count_2) self.assertEqual(contributor_email_count_1, contributor_email_count_2)
def create_application(self, team_id, msg, user): if not user.is_authenticated(): return Error(_('You should be authenticated.')) try: if not team_id: raise Team.DoesNotExist team = Team.objects.get(pk=team_id) except Team.DoesNotExist: return Error(_('Team does not exist')) try: tm = TeamMember.objects.get(team=team, user=user) return Error(_(u'You are already a member of this team.')) except TeamMember.DoesNotExist: pass if team.is_open(): TeamMember(team=team, user=user).save() return Msg( _(u'You are now a member of this team because it is open.')) elif team.is_by_application(): application, created = Application.objects.get_or_create(team=team, user=user) application.note = msg application.save() return Msg( _(u'Application sent success. Wait for answer from team.')) else: return Error(_(u'You can\'t join this team by application.'))
def save(self, user): team = super(CreateTeamForm, self).save(False) if self.video: team.video = self.video team.save() TeamMember(team=team, user=user, is_manager=True).save() return team
def create_application(self, team_id, msg, user): if not user.is_authenticated(): return Error(_('You should be authenticated.')) try: if not team_id: raise Team.DoesNotExist team = Team.objects.get(pk=team_id) except Team.DoesNotExist: return Error(_('Team does not exist')) try: TeamMember.objects.get(team=team, user=user) return Error(_(u'You are already a member of this team.')) except TeamMember.DoesNotExist: pass if team.is_open(): TeamMember(team=team, user=user).save() return Msg(_(u'You are now a member of this team.')) elif team.is_by_application(): if msg.strip() == '': return Error(_(u'The "About you" field is required in order to apply.')) application, created = Application.objects.get_or_create(team=team, user=user) application.note = msg application.save() notifier.application_sent.delay(application.pk) return Msg(_(u"Your application has been submitted. " u"You will be notified of the team administrator's response")) else: return Error(_(u"You can't join this team by application."))
def save(self, user): team = super(CreateTeamForm, self).save(False) video = self.fields['video_url'].video if video: team.video = video team.save() TeamMember(team=team, user=user, is_manager=True).save() return team
def save(self, user): team = super(CreateTeamForm, self).save(False) video = self.fields['video_url'].video if video: team.video = video team.save() TeamMember(team=team, user=user, role=TeamMember.ROLE_MANAGER).save() return team
def setUp(self): self.team = Team(name='test', slug='test') self.team.save() self.user = User.objects.all()[:1].get() self.user.is_active = True self.user.changes_notification = True self.user.email = '*****@*****.**' self.user.save() self.tm = TeamMember(team=self.team, user=self.user) self.tm.save() v1 = Video.objects.all()[:1].get() self.tv1 = TeamVideo(team=self.team, video=v1, added_by=self.user) self.tv1.save() v2 = Video.objects.exclude(pk=v1.pk)[:1].get() self.tv2 = TeamVideo(team=self.team, video=v2, added_by=self.user) self.tv2.save()
def create(self, validated_data): team = self.context['team'] if team.members.filter(user=self.user).exists(): self.fail('user-already-member') invite = team.invitations.create(user=self.user, author=self.context['user'], role=validated_data['role']) messages.tasks.team_invitation_sent.delay(invite.id) # return an unsaved TeamMember for serialization purposes return TeamMember(user=self.user, team=team, role=validated_data['role'])
def create_application(self, team_id, msg, user): if not user.is_authenticated(): return Error(_('You should be authenticated.')) try: if not team_id: raise Team.DoesNotExist team = Team.objects.get(pk=team_id) except Team.DoesNotExist: return Error(_('Team does not exist')) try: TeamMember.objects.get(team=team, user=user) return Error(_(u'You are already a member of this team.')) except TeamMember.DoesNotExist: pass if team.is_open(): TeamMember(team=team, user=user).save() return Msg(_(u'You are now a member of this team.')) elif team.is_by_application(): if msg.strip() == '': return Error( _(u'The "About you" field is required in order to apply.')) try: application = team.applications.get(user=user) if application.status == Application.STATUS_PENDING: return Error(_(u'You have already applied to this team.')) elif application.status == Application.STATUS_DENIED: return Error(_(u'Your application has been denied.')) elif application.status == Application.STATUS_MEMBER_REMOVED: return Error(_(u'You have been removed from this team.')) elif application.status == Application.STATUS_MEMBER_LEFT: # the user chose to participate, so we can already approve it application.note = msg application.approve(author=application.user, interface='web UI') return Msg( _(u"Your application has been approved. " u"You are now a member of this team")) except Application.DoesNotExist: application = Application(team=team, user=user) application.note = msg application.save(author=user, interface='web UI') notifier.application_sent.delay(application.pk) return Msg( _(u"Your application has been submitted. " u"You will be notified of the team administrator's response") ) else: return Error(_(u"You can't join this team by application."))
def join_team(request, slug): team = get_object_or_404(Team, slug=slug) user = request.user try: TeamMember.objects.get(team=team, user=user) messages.error(request, _(u'You are already a member of this team.')) except TeamMember.DoesNotExist: if not team.is_open(): messages.error(request, _(u'This team is not open.')) else: TeamMember(team=team, user=user).save() messages.success(request, _(u'You are now a member of this team.')) return redirect(team)
def join(self, team_id, user): if not user.is_authenticated(): return Error(_('You should be authenticated.')) try: team = Team.objects.get(pk=team_id) except Team.DoesNotExist: return Error(_('Team does not exist')) try: TeamMember.objects.get(team=team, user=user) return Error(_(u'You are already a member of this team.')) except TeamMember.DoesNotExist: pass if not team.is_open(): return Error(_(u'This team is not open.')) else: TeamMember(team=team, user=user).save() return Msg(_(u'You are now a member of this team.'))
def test_member_join(self): def _get_counts(member): email_to = "%s" %( member.user.email) return Message.objects.filter(user=member.user).count() , \ len([x for x in mail.outbox if email_to in x.recipients()]) team , created= Team.objects.get_or_create(name='test', slug='test') # creates dummy users: for x in xrange(0,5): user = UserFactory( username="******" % x, email = "*****@*****.**" % x, ) tm = TeamMember(team=team, user=user) if x == 0: tm.role = TeamMember.ROLE_OWNER owner = tm elif x == 1: tm.role = TeamMember.ROLE_ADMIN admin = tm elif x == 2: tm.role = TeamMember.ROLE_MANAGER manager = tm elif x == 3: tm.role = TeamMember.ROLE_CONTRIBUTOR contributor = tm if x < 4: # don't save the last role until we have counts tm.save() else: tm.role= TeamMember.ROLE_CONTRIBUTOR # now make sure we count previsou messages owner_messge_count_1, owner_email_count_1 = _get_counts(owner) admin_messge_count_1, admin_email_count_1 = _get_counts(admin) manager_messge_count_1, manager_email_count_1 = _get_counts(manager) contributor_messge_count_1, contributor_email_count_1 = _get_counts(contributor) # save the last team member and check that each group has appropriate counts tm.save() messages.tasks.team_member_new(tm.pk) # owner and admins should receive email + message owner_messge_count_2, owner_email_count_2 = _get_counts(owner) self.assertEqual(owner_messge_count_1 + 1, owner_messge_count_2) self.assertEqual(owner_email_count_1 + 1, owner_email_count_2) admin_messge_count_2, admin_email_count_2 = _get_counts(admin) self.assertEqual(admin_messge_count_1 + 1, admin_messge_count_2) self.assertEqual(admin_email_count_1 + 1, admin_email_count_2) # manager shoud not manager_messge_count_2, manager_email_count_2 = _get_counts(manager) self.assertEqual(manager_messge_count_1 , manager_messge_count_2) self.assertEqual(manager_email_count_1 , manager_email_count_2) # contributor shoud not contributor_messge_count_2, contributor_email_count_2 = _get_counts(contributor) self.assertEqual(contributor_messge_count_1 , contributor_messge_count_2) self.assertEqual(contributor_email_count_1 , contributor_email_count_2)
def test_member_leave(self): return # fix me now def _get_counts(member): email_to = "%s" % (member.user.email) return Message.objects.filter(user=member.user).count() , \ len([x for x in mail.outbox if email_to in x.recipients()]) team, created = Team.objects.get_or_create(name='test', slug='test') # creates dummy users: for x in xrange(0, 5): user, member = User.objects.get_or_create( username="******" % x, email="*****@*****.**" % x, notify_by_email=True, ) tm = TeamMember(team=team, user=user) if x == 0: tm.role = TeamMember.ROLE_OWNER owner = tm elif x == 1: tm.role = TeamMember.ROLE_ADMIN admin = tm elif x == 2: tm.role = TeamMember.ROLE_MANAGER manager = tm elif x == 3: tm.role = TeamMember.ROLE_CONTRIBUTOR contributor = tm if x < 4: # don't save the last role until we have counts tm.save() else: tm.role = TeamMember.ROLE_CONTRIBUTOR tm.save() # now make sure we count previsou messages owner_messge_count_1, owner_email_count_1 = _get_counts(owner) admin_messge_count_1, admin_email_count_1 = _get_counts(admin) manager_messge_count_1, manager_email_count_1 = _get_counts(manager) contributor_messge_count_1, contributor_email_count_1 = _get_counts( contributor) # now delete and check numers tm_user = tm.user tm_user_pk = tm.user.pk team_pk = tm.team.pk tm.delete() notifier.team_member_leave(team_pk, tm_user_pk) # save the last team member and check that each group has appropriate counts # owner and admins should receive email + message owner_messge_count_2, owner_email_count_2 = _get_counts(owner) self.assertEqual(owner_messge_count_1 + 1, owner_messge_count_2) self.assertEqual(owner_email_count_1 + 1, owner_email_count_2) admin_messge_count_2, admin_email_count_2 = _get_counts(admin) self.assertEqual(admin_messge_count_1 + 1, admin_messge_count_2) self.assertEqual(admin_email_count_1 + 1, admin_email_count_2) # manager shoud not manager_messge_count_2, manager_email_count_2 = _get_counts(manager) self.assertEqual(manager_messge_count_1, manager_messge_count_2) self.assertEqual(manager_email_count_1, manager_email_count_2) # contributor shoud not contributor_messge_count_2, contributor_email_count_2 = _get_counts( contributor) self.assertEqual(contributor_messge_count_1, contributor_messge_count_2) self.assertEqual(contributor_email_count_1, contributor_email_count_2) # now, this has to show up on everybody activitis fed action = Action.objects.get(team=team, user=tm_user, action_type=Action.MEMBER_LEFT) self.assertTrue( Action.objects.for_user(tm.user).filter(pk=action.pk).exists()) self.assertTrue( Action.objects.for_user(owner.user).filter(pk=action.pk).exists()) self.assertTrue( Action.objects.for_user( manager.user).filter(pk=action.pk).exists()) self.assertTrue( Action.objects.for_user( contributor.user).filter(pk=action.pk).exists()) self.assertTrue( Action.objects.for_user(admin.user).filter(pk=action.pk).exists())
class TestCommands(TestCase): fixtures = ["test.json"] def setUp(self): self.team = Team(name='test', slug='test') self.team.save() self.user = User.objects.all()[:1].get() self.user.is_active = True self.user.changes_notification = True self.user.email = '*****@*****.**' self.user.save() self.tm = TeamMember(team=self.team, user=self.user) self.tm.save() v1 = Video.objects.all()[:1].get() self.tv1 = TeamVideo(team=self.team, video=v1, added_by=self.user) self.tv1.save() v2 = Video.objects.exclude(pk=v1.pk)[:1].get() self.tv2 = TeamVideo(team=self.team, video=v2, added_by=self.user) self.tv2.save() def test_new_team_video_notification(self): #mockup for send_templated_email to test context of email import utils send_templated_email = utils.send_templated_email def send_templated_email_mockup(to, subject, body_template, body_dict, *args, **kwargs): send_templated_email_mockup.context = body_dict send_templated_email(to, subject, body_template, body_dict, *args, **kwargs) utils.send_templated_email = send_templated_email_mockup #check initial data self.assertEqual(self.team.teamvideo_set.count(), 2) self.assertEqual(self.team.users.count(), 1) today = datetime.today() date = today - timedelta(hours=24) #test notification about two new videos TeamVideo.objects.filter(pk__in=[self.tv1.pk, self.tv2.pk]).update(created=datetime.today()) self.assertEqual(TeamVideo.objects.filter(created__gte=date).count(), 2) mail.outbox = [] call_command('new_team_video_notification') self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.user.email]) self.assertEqual(len(send_templated_email_mockup.context['team_videos']), 2) #test if user turn off notification self.user.is_active = False self.user.save() mail.outbox = [] call_command('new_team_video_notification') self.assertEqual(len(mail.outbox), 0) self.user.is_active = True self.user.changes_notification = False self.user.save() mail.outbox = [] call_command('new_team_video_notification') self.assertEqual(len(mail.outbox), 0) self.user.changes_notification = True self.user.save() self.tm.changes_notification = False self.tm.save() mail.outbox = [] call_command('new_team_video_notification') self.assertEqual(len(mail.outbox), 0) self.tm.changes_notification = True self.tm.save() #test notification if one video is new past_date = today - timedelta(days=2) TeamVideo.objects.filter(pk=self.tv1.pk).update(created=past_date) self.assertEqual(TeamVideo.objects.filter(created__gte=date).count(), 1) mail.outbox = [] call_command('new_team_video_notification') self.assertEqual(len(mail.outbox), 1) self.assertEqual(len(send_templated_email_mockup.context['team_videos']), 1) self.assertEqual(send_templated_email_mockup.context['team_videos'][0], self.tv2) #test notification if all videos are old TeamVideo.objects.filter(pk__in=[self.tv1.pk, self.tv2.pk]).update(created=past_date) self.assertEqual(TeamVideo.objects.filter(created__gte=date).count(), 0) mail.outbox = [] call_command('new_team_video_notification') self.assertEqual(len(mail.outbox), 0)