def create_invite(form): invitation = Invitation(email=form.email.data, should_be_admin=bool(form.should_be_admin.data)) existing_user = User.query.filter(User.email == invitation.email).first() if existing_user: raise Exception( "A user with this email address is already registered.") # We need the user id to generate the token, so we have to commit # opportunistically here. db.session.add(invitation) db.session.commit() try: token = invitation.generate_secure_token() resp = send_email( form.email.data, "Invitation to edit the Pan-Dene Comparative Lexicon", "auth/email/invite", token=token) if not (resp.status_code >= 200 and resp.status_code <= 299): raise Exception("Got {} from Sendgrid".format(resp.status_code)) except Exception as e: db.session.delete(invitation) db.session.commit() raise e
def test_delete(self) -> None: db.session.delete = Mock() db.session.commit = Mock() invitation = Invitation(id=123) invitation.delete() db.session.delete.assert_called_once_with(invitation) db.session.commit.assert_called_once_with()
def test_save(self) -> None: db.session.add = Mock() db.session.commit = Mock() invitation = Invitation(id=123) invitation.save() db.session.add.assert_called_once_with(invitation) db.session.commit.assert_called_once_with()
def test_get_all(self, mock_query: Mock) -> None: expected = [Invitation(id=123)] all = mock_query.return_value.all all.return_value = expected result = Invitation.get_all() all.assert_called_once_with() assert result == expected
def new_invitation(): request_dict = InvitationSchema.validate_request() request_dict['userIntID'] = g.user_id request_dict['tenantID'] = g.tenant_uid invitation = Invitation() invitation_dict = invitation.create(request_dict).to_dict() user = User.query.filter(User.id == g.user_id).first_or_404() username = user.email email_title = current_app.config.get('EMAIL_TITLE') site_domain = current_app.config.get('SITE_DOMAIN') site_name = current_app.config.get('SITE_NAME') send_invite_mail(invitation, username, email_title, site_domain, site_name) return jsonify(invitation_dict), 201
def test_find_by_email(self, mock_query: Mock) -> None: expected = Invitation(id=123) filter_by = mock_query.return_value.filter_by first = filter_by.return_value.first first.return_value = expected result = Invitation.find_by_email('name-xyz', None) filter_by.assert_called_once_with(email='name-xyz') first.assert_called_once_with() assert result == expected
def test_find_by_email_with_id(self, mock_query: Mock) -> None: expected = Invitation(id=123) filter_by = mock_query.return_value.filter_by filter = filter_by.return_value.filter first = filter.return_value.first first.return_value = expected result = Invitation.find_by_email('name-xyz', 654) filter_by.assert_called_once_with(email='name-xyz') filter.assert_called_once_with(_BinaryExpressionMatcher(Invitation.id != 654)) first.assert_called_once_with() assert result == expected
def create(self, validated_data): user = self.context['request'].user invitation = Invitation(inviter=user) invitation.save() EmailService().send_email( "*****@*****.**", "welcome to shooka", "hi\n you invited to " + user.company.name + "\nclick this link and complete your registration \n " "127.0.0.1:3000/register/agent/" + invitation.key) validated_data["username"] = validated_data["email"] validated_data["invitation"] = invitation validated_data["is_active"] = False validated_data["company"] = user.company return super(AgentSerializer, self).create(validated_data)
def test_get(self, mock_get_all: Mock, app: Flask) -> None: mock_get_all.return_value = [ Invitation(id=12, email='email', is_admin=True), Invitation(id=23, email='email2', is_admin=False), ] app.register_blueprint(invitations.module) client = app.test_client() response = client.get( '/invitations', headers=build_authorization_headers(app, is_admin=True), ) mock_get_all.assert_called_once_with() assert response.data == (b'[{"id": 12, "email": "email", "is_admin": true}, ' b'{"id": 23, "email": "email2", "is_admin": false}]\n') assert response.status_code == HTTPStatus.OK
def post(user): """send invitation to phone number. confirmation code is deterministic based on team info""" data = request.get_json() try: number = parse(data["phone_number"], "US") except NumberParseException: message = "The number supplied does not seem to be valid. Please try again." print(message) return make_response(jsonify({"message": message}), 400) number = f"+{number.country_code}{number.national_number}" # generate a confirmation code team = db.session.query(Team).filter(Team.id == data["team_id"]).one() code = encode(team) # format message message = f"{user.username} invited you to join their team {team.name} on the Bricks app." # send message to number with Twilio recipient = {"phone_number": number} send_message(recipient, message) send_message( recipient, "Download the app here: https://itunes.apple.com/us/app/stack-a-brick/id1456194944#?platform=iphone" ) send_message(recipient, "Use this code to join their team:") send_message(recipient, code) # add invitation to db invitation = Invitation(user=user, team=team, invitee_phone=number, code=code) db.session.add(invitation) db.session.commit() db.session.close() message = f"Invitation sent to {number}" return make_response(jsonify({"message": message}), 200)
def invite_user(): form = InviteForm() # users can only add users one privilege level below them form.role.choices = [(role.id, role.title) for role in Role.query.all() if role.level > current_user.roles[0].level] if form.validate_on_submit(): # the method is POST and the form is valid token = random_base64(lambda t: Invitation.get(t) is None) invitation = Invitation( token, form.email.data, Role.get_by_id(form.role.data), current_user ) # invite_link: http://<host>/signup?invite=<token> invite_link = url_for('auth.signup', _external=True, invite=token) # prepare and send invitation email try: send_email( subject="Asset Tracker Invitation", sender=(current_user.name, current_user.email), recipients=[form.email.data], body="You've been invited to join Asset Tracker. Follow \ this link to sign up: %s" % invite_link, html="You've been invited to join Asset Tracker. Follow \ this link to sign up:<br> <a href=\"%s\">%s</a>" % \ (invite_link, invite_link) ) db.session.add(invitation) db.session.commit() flash("Invitation sent to %s" % form.email.data, 'success') except Exception, e: if current_app.config.get('DEBUG'): raise e else: flash("Failed to send invite due to a %s error" % e.__class__.__name__, 'danger') return render_template('auth/invite.html', form=form) return redirect(url_for('index'))
def new_invitation(): form = InvitationForm() if form.validate_on_submit(): recipients = form.recipients.data.split(',') for recipient in recipients: receiver = User.query.filter_by(username=recipient).first() invitation = Invitation( sender=current_user, recipient=receiver, invitation_header=form.invitation_header.data, invitation_body=form.invitation_body.data, invitation_footer=form.invitation_footer.data, accepted=True) db.session.add(invitation) db.session.commit() flash('Invitation Created') return redirect(url_for('home')) return render_template('new_invitation.html', title='New Invitation', form=form)
def signup(): form = SignUpForm() token = request.args.get('invite') invite = Invitation.get(token) if token and not invite: return render_template( 'error/generic.html', message="The invite is invalid" ) if invite is not None: if User.query.filter_by(email=invite.invitee).first() is not None: return render_template( 'error/generic.html', message="Email belongs to an existing user" ) if form.validate_on_submit(): if invite is None: role_short = 'staff' else: role_short = invite.role.short if form.email.data != invite.invitee: return render_template( 'error/generic.html', message="Email doesn't match invite email" ) user = User(form.email.data, form.password.data, form.name.data, role_short) db.session.add(user) db.session.commit() login_user(user) flash("Sign up successful", 'success') return redirect(url_for('index')) if invite is not None: form.email.data = invite.invitee else: flash('Signing up without an inivite defaults to staff member account', 'info') return render_template('auth/signup.html', form=form)
db.session.add(demo_5_mme) db.session.add(demo_6_att) db.session.add(demo_7_abs) five_peace_band = Band(name="Five Peace Band") five_peace_band.owner = demo five_peace_band.style = jazz fpb_owner = UserBand(is_confirmed=True) fpb_owner.user = demo fpb_owner.band = five_peace_band db.session.add(five_peace_band) db.session.add(fpb_owner) kenny_to_kenny = Invitation(message="") kenny_to_kenny.sender = demo kenny_to_kenny.recipient = demo_2 kenny_to_kenny.band = five_peace_band fpb_pending = UserBand(is_confirmed=False) fpb_pending.user = demo_2 fpb_pending.band = five_peace_band db.session.add(kenny_to_kenny) db.session.add(fpb_pending) atcq_tribute = Band(name="A Tribe Called Quest Tribute Group") atcq_owner = UserBand(is_confirmed=True) atcq_tribute.owner = demo_3 atcq_tribute.style = hiphop