本文整理汇总了Python中corehq.apps.users.models.Invitation类的典型用法代码示例。如果您正苦于以下问题:Python Invitation类的具体用法?Python Invitation怎么用?Python Invitation使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Invitation类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: invite_web_user
def invite_web_user(request, domain, template="users/invite_web_user.html"):
role_choices = UserRole.role_choices(domain)
if request.method == "POST":
form = AdminInvitesUserForm(request.POST,
excluded_emails=[user.username for user in WebUser.by_domain(domain)],
role_choices=role_choices
)
if form.is_valid():
data = form.cleaned_data
# create invitation record
data["invited_by"] = request.couch_user.user_id
data["invited_on"] = datetime.utcnow()
data["domain"] = domain
invite = Invitation(**data)
invite.save()
invite.send_activation_email()
messages.success(request, "Invitation sent to %s" % invite.email)
return HttpResponseRedirect(reverse("web_users", args=[domain]))
else:
form = AdminInvitesUserForm(role_choices=role_choices)
context = _users_context(request, domain)
context.update(
registration_form=form
)
return render_to_response(request, template, context)
示例2: setUpClass
def setUpClass(cls):
cls.invitations = []
for kwargs in [
{'domain': 'domain_1', 'email': '[email protected]'},
{'domain': 'domain_1', 'email': '[email protected]', 'is_accepted': True},
{'domain': 'domain_2', 'email': '[email protected]'},
]:
inv = Invitation(**kwargs)
inv.save()
cls.invitations.append(inv)
示例3: reinvite_web_user
def reinvite_web_user(request, domain):
invitation_id = request.POST["invite"]
try:
invitation = Invitation.get(invitation_id)
invitation.invited_on = datetime.utcnow()
invitation.save()
invitation.send_activation_email()
return json_response({"response": _("Invitation resent"), "status": "ok"})
except ResourceNotFound:
return json_response({"response": _("Error while attempting resend"), "status": "error"})
示例4: resend_pending_invitations
def resend_pending_invitations():
from corehq.apps.users.models import Invitation
days_to_resend = (15, 29)
days_to_expire = 30
domains = Domain.get_all()
for domain in domains:
invitations = Invitation.by_domain(domain.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)
示例5: accept_invitation
def accept_invitation(request, domain, invitation_id):
if request.GET.get('switch') == 'true':
logout(request)
return redirect_to_login(request.path)
if request.GET.get('create') == 'true':
logout(request)
return HttpResponseRedirect(request.path)
invitation = Invitation.get(invitation_id)
assert(invitation.domain == domain)
if invitation.is_accepted:
messages.error(request, "Sorry that invitation has already been used up. "
"If you feel this is a mistake please ask the inviter for "
"another invitation.")
return HttpResponseRedirect(reverse("login"))
if request.user.is_authenticated():
# if you are already authenticated, just add the domain to your
# list of domains
if request.couch_user.username != invitation.email:
messages.error(request, "The invited user %s and your user %s do not match!" % (invitation.email, request.couch_user.username))
if request.method == "POST":
couch_user = CouchUser.from_django_user(request.user)
couch_user.add_domain_membership(domain=domain)
couch_user.set_role(domain, invitation.role)
couch_user.save()
invitation.is_accepted = True
invitation.save()
messages.success(request, "You have been added to the %s domain" % domain)
return HttpResponseRedirect(reverse("domain_homepage", args=[domain,]))
else:
return render_to_response(request, 'users/accept_invite.html', {'domain': domain,
"invited_user": invitation.email if request.couch_user.username != invitation.email else ""})
else:
# if you're not authenticated we need you to fill out your information
if request.method == "POST":
form = NewWebUserRegistrationForm(request.POST)
if form.is_valid():
user = activate_new_user(form, is_domain_admin=False, domain=invitation.domain)
user.set_role(domain, invitation.role)
user.save()
invitation.is_accepted = True
invitation.save()
messages.success(request, "User account for %s created! You may now login." % form.cleaned_data["email"])
return HttpResponseRedirect(reverse("login"))
else:
form = NewWebUserRegistrationForm(initial={'email': invitation.email})
return render_to_response(request, "users/accept_invite.html", {"form": form})
示例6: invite_web_user_form
def invite_web_user_form(self):
role_choices = UserRole.role_choices(self.domain)
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 Invitation.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)
示例7: invite_web_user_form
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 Invitation.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)
示例8: select
def select(request, domain_select_template='domain/select.html', do_not_redirect=False):
domains_for_user = Domain.active_for_user(request.user)
if not domains_for_user:
from corehq.apps.registration.views import track_domainless_new_user
track_domainless_new_user(request)
return redirect('registration_domain')
email = request.couch_user.get_email()
open_invitations = [e for e in Invitation.by_email(email) if not e.is_expired]
additional_context = {
'domains_for_user': domains_for_user,
'open_invitations': open_invitations,
'current_page': {'page_name': _('Select A Project')},
}
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:
from corehq.apps.dashboard.views import dashboard_default
return dashboard_default(request, last_visited_domain)
except Http404:
pass
del request.session['last_visited_domain']
return render(request, domain_select_template, additional_context)
示例9: __call__
def __call__(self, request, invitation_id, **kwargs):
logging.warning("Don't use this view in more apps until it gets cleaned up.")
# add the correct parameters to this instance
self.request = request
self.inv_id = invitation_id
if "domain" in kwargs:
self.domain = kwargs["domain"]
if request.GET.get("switch") == "true":
logout(request)
return redirect_to_login(request.path)
if request.GET.get("create") == "true":
logout(request)
return HttpResponseRedirect(request.path)
try:
invitation = Invitation.get(invitation_id)
except ResourceNotFound:
messages.error(
request,
_(
"Sorry, it looks like your invitation has expired. "
"Please check the invitation link you received and try again, or request a "
"project administrator to send you the invitation again."
),
)
return HttpResponseRedirect(reverse("login"))
if invitation.is_accepted:
messages.error(
request,
_(
"Sorry, that invitation has already been used up. "
"If you feel this is a mistake please ask the inviter for "
"another invitation."
),
)
return HttpResponseRedirect(reverse("login"))
self.validate_invitation(invitation)
if invitation.is_expired:
return HttpResponseRedirect(reverse("no_permissions"))
context = self.added_context()
if request.user.is_authenticated():
is_invited_user = request.couch_user.username.lower() == invitation.email.lower()
if self.is_invited(invitation, request.couch_user) and not request.couch_user.is_superuser:
if is_invited_user:
# if this invite was actually for this user, just mark it accepted
messages.info(
request, _("You are already a member of {entity}.").format(entity=self.inviting_entity)
)
invitation.is_accepted = True
invitation.save()
else:
messages.error(
request,
_(
"It looks like you are trying to accept an invitation for "
"{invited} but you are already a member of {entity} with the "
"account {current}. Please sign out to accept this invitation "
"as another user."
).format(
entity=self.inviting_entity, invited=invitation.email, current=request.couch_user.username
),
)
return HttpResponseRedirect(self.redirect_to_on_success)
if not is_invited_user:
messages.error(
request,
_("The invited user {invited} and your user {current} do not match!").format(
invited=invitation.email, current=request.couch_user.username
),
)
if request.method == "POST":
couch_user = CouchUser.from_django_user(request.user)
self._invite(invitation, couch_user)
track_workflow(
request.couch_user.get_email(),
"Current user accepted a project invitation",
{"Current user accepted a project invitation": "yes"},
)
meta = get_meta(request)
track_existing_user_accepted_invite_on_hubspot.delay(request.couch_user, request.COOKIES, meta)
return HttpResponseRedirect(self.redirect_to_on_success)
else:
mobile_user = CouchUser.from_django_user(request.user).is_commcare_user()
context.update(
{
"mobile_user": mobile_user,
"invited_user": invitation.email if request.couch_user.username != invitation.email else "",
}
)
return render(request, self.template, context)
else:
if request.method == "POST":
form = NewWebUserRegistrationForm(request.POST)
if form.is_valid():
#.........这里部分代码省略.........
示例10: delete_invitation
def delete_invitation(request, domain):
invitation_id = request.POST["id"]
invitation = Invitation.get(invitation_id)
invitation.delete()
return json_response({"status": "ok"})
示例11: test_by_domain
def test_by_domain(self):
self.assertEqual(len(Invitation.by_domain('domain_1')), 1)
self.assertEqual(len(Invitation.by_domain('domain_2')), 1)
self.assertEqual(len(Invitation.by_domain('domain_3')), 0)
示例12: test_by_email
def test_by_email(self):
self.assertEqual(len(Invitation.by_email('[email protected]')), 1)
self.assertEqual(len(Invitation.by_email('[email protected]')), 1)
self.assertEqual(len(Invitation.by_email('[email protected]')), 0)