def user_invite(self, request, **kwargs): if request.method != 'POST': raise ImmediateHttpResponse(response=http.HttpMethodNotAllowed()) try: data = self.deserialize_post_data(request) emails = map(str.strip, data['emails']) agoraid = data['agoraid'] except: raise ImmediateHttpResponse(response=http.HttpBadRequest()) agora = get_object_or_404(Agora, pk=agoraid) welcome_message = data.get('welcome_message', _("Welcome to this agora")) if not agora.has_perms('admin', request.user): raise ImmediateHttpResponse(response=http.HttpMethodNotAllowed()) for email in emails: q = User.objects.filter(Q(email=email) | Q(username=email)) exists = q.exists() if not exists and not validate_email(email): # invalid email address, cannot send an email there! raise ImmediateHttpResponse(response=http.HttpBadRequest()) for email in emails: q = User.objects.filter(Q(email=email) | Q(username=email)) exists = q.exists() if exists: # if user exists in agora, we'll add it directly user = q[0] if user in agora.members.all(): continue # if user exists in agora, we'll add it directly status, resp = rest('/agora/%s/action/' % agoraid, data={ 'action': 'add_membership', 'username': user.username, 'welcome_message': welcome_message }, method="POST", request=request) if status != 200: raise ImmediateHttpResponse(response=http.HttpBadRequest()) else: # send invitation # maximum 30 characters in username username = str(uuid4())[:30] password = str(uuid4()) user = UserenaSignup.objects.create_user( username, email, password, False, False) profile = user.get_profile() profile.lang_code = request.user.get_profile().lang_code profile.extra = {'join_agora_id': agoraid} profile.save() # Mail to the user translation.activate(user.get_profile().lang_code) context = get_base_email_context(request) context.update( dict(agora=agora, other_user=request.user, to=user, invitation_link=reverse( 'register-complete-invite', kwargs=dict(activation_key=user.userena_signup. activation_key)), welcome_message=welcome_message)) email = EmailMultiAlternatives( subject=_('%(site)s - invitation to join %(agora)s') % dict(site=Site.objects.get_current().domain, agora=agora.get_full_name()), body=render_to_string( 'agora_core/emails/join_invitation.txt', context), to=[user.email]) email.attach_alternative( render_to_string('agora_core/emails/join_invitation.html', context), "text/html") email.send() translation.deactivate() # add user to the default agoras if any for agora_name in settings.AGORA_REGISTER_AUTO_JOIN: profile.add_to_agora(agora_name=agora_name, request=request) return self.create_response(request, {})
def user_invite(self, request, **kwargs): if request.method != "POST": raise ImmediateHttpResponse(response=http.HttpMethodNotAllowed()) try: data = self.deserialize_post_data(request) emails = map(str.strip, data["emails"]) agoraid = data["agoraid"] except: raise ImmediateHttpResponse(response=http.HttpBadRequest()) agora = get_object_or_404(Agora, pk=agoraid) welcome_message = data.get("welcome_message", _("Welcome to this agora")) if not agora.has_perms("admin", request.user): raise ImmediateHttpResponse(response=http.HttpMethodNotAllowed()) for email in emails: q = User.objects.filter(Q(email=email) | Q(username=email)) exists = q.exists() if not exists and not validate_email(email): # invalid email address, cannot send an email there! raise ImmediateHttpResponse(response=http.HttpBadRequest()) for email in emails: q = User.objects.filter(Q(email=email) | Q(username=email)) exists = q.exists() if exists: # if user exists in agora, we'll add it directly user = q[0] if user in agora.members.all(): continue # if user exists in agora, we'll add it directly status, resp = rest( "/agora/%s/action/" % agoraid, data={"action": "add_membership", "username": user.username, "welcome_message": welcome_message}, method="POST", request=request, ) if status != 200: raise ImmediateHttpResponse(response=http.HttpBadRequest()) else: # send invitation # maximum 30 characters in username username = str(uuid4())[:30] password = str(uuid4()) user = UserenaSignup.objects.create_user(username, email, password, False, False) profile = user.get_profile() profile.lang_code = request.user.get_profile().lang_code profile.extra = {"join_agora_id": agoraid} profile.save() # Mail to the user translation.activate(user.get_profile().lang_code) context = get_base_email_context(request) context.update( dict( agora=agora, other_user=request.user, to=user, invitation_link=reverse( "register-complete-invite", kwargs=dict(activation_key=user.userena_signup.activation_key) ), welcome_message=welcome_message, ) ) email = EmailMultiAlternatives( subject=_("%(site)s - invitation to join %(agora)s") % dict(site=Site.objects.get_current().domain, agora=agora.get_full_name()), body=render_to_string("agora_core/emails/join_invitation.txt", context), to=[user.email], ) email.attach_alternative( render_to_string("agora_core/emails/join_invitation.html", context), "text/html" ) email.send() translation.deactivate() # add user to the default agoras if any for agora_name in settings.AGORA_REGISTER_AUTO_JOIN: profile.add_to_agora(agora_name=agora_name, request=request) return self.create_response(request, {})
def user_invite(self, request, **kwargs): if request.method != 'POST': raise ImmediateHttpResponse(response=http.HttpMethodNotAllowed()) data = self.deserialize_post_data(request) emails = data['emails'] agoraid = data['agoraid'] agora = get_object_or_404(Agora, pk=agoraid) welcome_message = data.get('welcome_message', _("Welcome to this agora")) if not agora.has_perms('admin', request.user): raise ImmediateHttpResponse(response=http.HttpMethodNotAllowed()) for email in emails: email = email.strip() q = User.objects.filter(Q(email=email)|Q(username=email)) exists = q.exists() if not exists and not validate_email(email): # invalid email address, cannot send an email there! raise ImmediateHttpResponse(response=http.HttpBadRequest()) for email in emails: q = User.objects.filter(Q(email=email)|Q(username=email)) exists = q.exists() if exists: # if user exists in agora, we'll add it directly user = q[0] if user in agora.members.all(): continue # if user exists in agora, we'll add it directly status, resp = rest('/agora/%s/action/' % agoraid, data={'action': 'add_membership', 'username': user.username, 'welcome_message': welcome_message}, method="POST", request=request) if status != 200: raise ImmediateHttpResponse(response=http.HttpBadRequest()) else: # send invitation username = str(uuid4()) password = str(uuid4()) new_user = UserenaSignup.objects.create_user(username, email, password, False, False) profile = new_user.get_profile() profile.lang_code = request.user.get_profile().lang_code profile.extra = {'join_agora_id': agoraid} profile.save() # Mail to the user user = new_user translation.activate(user.get_profile().lang_code) context = get_base_email_context(request) context.update(dict( agora=agora, other_user=request.user, to=user, invitation_link=reverse('register-complete-invite', kwargs=dict(activation_key=user.userena_signup.activation_key)), welcome_message=welcome_message )) email = EmailMultiAlternatives( subject=_('%(site)s - invitation to join %(agora)s') % dict( site=Site.objects.get_current().domain, agora=agora.get_full_name() ), body=render_to_string('agora_core/emails/join_invitation.txt', context), to=[user.email]) email.attach_alternative( render_to_string('agora_core/emails/join_invitation.html', context), "text/html") email.send() translation.deactivate() return self.create_response(request, {})