def setUpClass(cls): super(InvitationTest, cls).setUpClass() cls.invitations = [ SQLInvitation(domain='domain_1', email='*****@*****.**', invited_by='*****@*****.**', invited_on=datetime.utcnow()), SQLInvitation(domain='domain_1', email='*****@*****.**', invited_by='*****@*****.**', invited_on=datetime.utcnow(), is_accepted=True), SQLInvitation(domain='domain_2', email='*****@*****.**', invited_by='*****@*****.**', invited_on=datetime.utcnow()), ] for inv in cls.invitations: inv.save()
def invite_web_user_form(self): role_choices = _get_editable_role_choices(self.domain, self.request.couch_user, allow_admin_role=True) loc = None domain_request = DomainRequest.objects.get( id=self.request_id) if self.request_id else None initial = { 'email': domain_request.email if domain_request else None, } if 'location_id' in self.request.GET: from corehq.apps.locations.models import SQLLocation loc = SQLLocation.objects.get( location_id=self.request.GET.get('location_id')) if self.request.method == 'POST': current_users = [ user.username for user in WebUser.by_domain(self.domain) ] pending_invites = [ di.email for di in SQLInvitation.by_domain(self.domain) ] return AdminInvitesUserForm(self.request.POST, excluded_emails=current_users + pending_invites, role_choices=role_choices, domain=self.domain) return AdminInvitesUserForm(initial=initial, role_choices=role_choices, domain=self.domain, location=loc)
def resend_pending_invitations(): from corehq.apps.users.models import SQLInvitation days_to_resend = (15, 29) days_to_expire = 30 domains = Domain.get_all() for domain_obj in domains: invitations = SQLInvitation.by_domain(domain_obj.name) for invitation in invitations: days = (datetime.utcnow() - invitation.invited_on).days if days in days_to_resend: invitation.send_activation_email(days_to_expire - days)
def select(request, do_not_redirect=False, next_view=None): domains_for_user = Domain.active_for_user(request.user) if not domains_for_user: return redirect('registration_domain') email = request.couch_user.get_email() open_invitations = [ e for e in SQLInvitation.by_email(email) if not e.is_expired ] # next_view must be a url that expects exactly one parameter, a domain name next_view = next_view or request.GET.get('next_view') additional_context = { 'domains_for_user': domains_for_user, 'open_invitations': [] if next_view else open_invitations, 'current_page': { 'page_name': _('Select A Project') }, 'next_view': next_view or 'domain_homepage', 'hide_create_new_project': bool(next_view), } domain_select_template = "domain/select.html" last_visited_domain = request.session.get('last_visited_domain') if open_invitations \ or do_not_redirect \ or not last_visited_domain: return render(request, domain_select_template, additional_context) else: domain_obj = Domain.get_by_name(last_visited_domain) if domain_obj and domain_obj.is_active: # mirrors logic in login_and_domain_required if (request.couch_user.is_member_of(domain_obj) or (request.user.is_superuser and not domain_obj.restrict_superusers) or domain_obj.is_snapshot): try: return HttpResponseRedirect( reverse(next_view or 'dashboard_default', args=[last_visited_domain])) except Http404: pass del request.session['last_visited_domain'] return render(request, domain_select_template, additional_context)
def post(self, request, *args, **kwargs): if self.invite_web_user_form.is_valid(): # If user exists and has already requested access, just add them to the project # Otherwise, send an invitation create_invitation = True data = self.invite_web_user_form.cleaned_data domain_request = DomainRequest.by_email(self.domain, data["email"]) if domain_request is not None: domain_request.is_approved = True domain_request.save() user = CouchUser.get_by_username(domain_request.email) if user is not None: domain_request.send_approval_email() create_invitation = False user.add_as_web_user(self.domain, role=data["role"], location_id=data.get( "supply_point", None), program_id=data.get("program", None)) messages.success(request, "%s added." % data["email"]) else: track_workflow(request.couch_user.get_email(), "Sent a project invitation", {"Sent a project invitation": "yes"}) send_hubspot_form(HUBSPOT_INVITATION_SENT_FORM, request) messages.success(request, "Invitation sent to %s" % data["email"]) if create_invitation: data["invited_by"] = request.couch_user.user_id data["invited_on"] = datetime.utcnow() data["domain"] = self.domain invite = SQLInvitation(**data) invite.save() invite.send_activation_email() return HttpResponseRedirect( reverse(ListWebUsersView.urlname, args=[self.domain])) return self.get(request, *args, **kwargs)
def invitations(self): invitations = SQLInvitation.by_domain(self.domain) for invitation in invitations: invitation.role_label = self.role_labels.get(invitation.role, "") return invitations
def test_by_email(self): self.assertEqual(len(SQLInvitation.by_email('*****@*****.**')), 1) self.assertEqual(len(SQLInvitation.by_email('*****@*****.**')), 1) self.assertEqual(len(SQLInvitation.by_email('*****@*****.**')), 0)
def test_by_domain(self): self.assertEqual(len(SQLInvitation.by_domain('domain_1')), 1) self.assertEqual(len(SQLInvitation.by_domain('domain_2')), 1) self.assertEqual(len(SQLInvitation.by_domain('domain_3')), 0)