def delete_invite(id): invitation = Invite.get_invite(id) if not invitation: return jsonify(success=False, reason='no-such-invitation') # do not delete users that are not invite pending (bug #123) email = invitation['recipient'] user = User.get_user(email) if user and user.get('status') == "invited": db.session.remove(user) db.session.commit() invites.remove({'id': id}) return jsonify(success=True)
def update_invite(id): timenow = datetime.datetime.utcnow() action = request.json['action'].lower() invitation = Invite.get_invite(id) if invitation: max_time_allowed = invitation.get('max_time_allowed') \ or backend_config.get('invitation').get('max_time_allowed') recipient = invitation['recipient'] recipient_name = invitation['recipient_name'] sender = invitation['sender'] sender_name = invitation['sender_name'] sent_on = invitation['sent_on'] accepted_on = invitation['accepted_on'] expire_on = invitation['expire_on'] user = User.get_user(recipient) if user is None: return jsonify(success=False, reason="user-not-created") if accepted_on is not None: return jsonify(success=False, reason="invitation-has-been-used") if not action in ('resend', 'accept', 'decline'): return jsonify(success=False, reason='invalid-action') if action == 'resend': new_id = str(uuid.uuid4()) base_url = request.json['base_url'] send_email('invite', invitation, extra_data={'base_url': base_url}) # generate new record sent_on = datetime.datetime.utcnow() expire_on = sent_on + datetime.timedelta(seconds=max_time_allowed) invitation['id'] = new_id invitation['sent_on'] = sent_on invitation['expire_on'] = expire_on invites.update({'id': id}, {'$set': {'sent_on': invitation['sent_on'], 'id': invitation['id']}}) return jsonify(success=True, invite=sanitize_invite(invitation)) elif action == 'accept': # if time now is ahead of expire_on, the delta is negative if (expire_on - timenow).seconds < 0: invitation['status'] = 'expired' invites.update({'id': id}, {'$set': {'status': 'expired'}}) return jsonify(success=False, reason='invitation-expired') else: invitation['status'] = 'used' invitation['accepted_on'] = datetime.datetime.utcnow() invites.update({'id': id},{'$set': {'accepted_on': invitation['accepted_on'], 'status': 'used'}}) user = User.get_user(recipient) user.status = 'active' user.email = request.json['login'] db.session.commit() # if user's persona email is different invitation['recipient'] = request.json['login'] # notify inviter if he chooses to receive such notification if "accept" in invitation['notify_when']: send_email('accept', invitation) return jsonify(success=True, invite=sanitize_invite(invitation)) elif action == 'decline': invitation['status'] = 'declined' invites.update({'id': id}, {'$set': {'status': 'declined'}}) db.session.remove(user) db.commit() # notify inviter if he chooses to if "decline" in invitation['notify_when']: send_email('decline', invitation) return jsonify(success=True, invite=sanitize_invite(invitation)) else: return jsonify(success=False, reason='invitation-does-not-exist')
def get_invite(id): invitation = Invite.get_invite(id) if invitation: return jsonify(success=True, invite=sanitize_invite(invitation)) else: return jsonify(success=False, reason='invitation-does-not-exist')