def do_save_collaboration(data, organisation, user, current_user_admin=True): _validate_collaboration(data, organisation) administrators = data.get("administrators", []) message = data.get("message", None) data["identifier"] = str(uuid.uuid4()) res = save(Collaboration, custom_json=data, allow_child_cascades=False) administrators = list(filter(lambda admin: admin != user.email, administrators)) collaboration = res[0] for administrator in administrators: invitation = Invitation(hash=generate_token(), message=message, invitee_email=administrator, collaboration_id=collaboration.id, user=user, intended_role="admin", expiry_date=default_expiry_date(), status="open", created_by=user.uid) invitation = db.session.merge(invitation) mail_collaboration_invitation({ "salutation": "Dear", "invitation": invitation, "base_url": current_app.app_config.base_url, "wiki_link": current_app.app_config.wiki_link, "recipient": administrator }, collaboration, [administrator]) if current_user_admin: admin_collaboration_membership = CollaborationMembership(role="admin", user_id=user.id, collaboration_id=collaboration.id, created_by=user.uid, updated_by=user.uid) db.session.merge(admin_collaboration_membership) return res
def collaboration_invites_api(): confirm_external_api_call() organisation = request_context.external_api_organisation data = current_request.get_json() coll_short_name = data["short_name"] collaborations = list(filter(lambda coll: coll.short_name == coll_short_name, organisation.collaborations)) if not collaborations: raise Forbidden(f"Collaboration {coll_short_name} is not part of organisation {organisation.name}") collaboration = collaborations[0] collaboration_admins = list(filter(lambda cm: cm.role == "admin", collaboration.collaboration_memberships)) if len(collaboration_admins) > 0: user = collaboration_admins[0].user elif len(organisation.organisation_memberships) > 0: user = organisation.organisation_memberships[0].user else: user = User.query.filter(User.uid == current_app.app_config.admin_users[0].uid).one() message = data.get("message") intended_role = data.get("intended_role", "member") expiry_date = parse_date(data.get("invitation_expiry_date"), default_expiry_date()) membership_expiry_date = parse_date(data.get("membership_expiry_date")) invites = list(filter(lambda recipient: bool(email_re.match(recipient)), data["invites"])) invites_results = [] for email in invites: invitation = Invitation(hash=generate_token(), message=message, invitee_email=email, collaboration_id=collaboration.id, user=user, intended_role=intended_role, expiry_date=expiry_date, membership_expiry_date=membership_expiry_date, created_by="system", external_identifier=str(uuid.uuid4()), status="open") invitation = db.session.merge(invitation) invites_results.append({ "email": email, "invitation_expiry_date": expiry_date, "status": "open", "invitation_id": invitation.external_identifier }) mail_collaboration_invitation({ "salutation": "Dear", "invitation": invitation, "base_url": current_app.app_config.base_url, "wiki_link": current_app.app_config.wiki_link, "recipient": email }, collaboration, [email]) return invites_results, 201
def do_resend(invitation_id): invitation = _invitation_query() \ .filter(Invitation.id == invitation_id) \ .one() confirm_collaboration_admin(invitation.collaboration_id) invitation.expiry_date = default_expiry_date() invitation.created_at = datetime.date.today(), db.session.merge(invitation) mail_collaboration_invitation({ "salutation": "Dear", "invitation": invitation, "base_url": current_app.app_config.base_url, "wiki_link": current_app.app_config.wiki_link, "recipient": invitation.invitee_email }, invitation.collaboration, [invitation.invitee_email])
def collaboration_invites_preview(): data = current_request.get_json() message = data.get("message", None) intended_role = data.get("intended_role", "member") collaboration = Collaboration.query.get(int(data["collaboration_id"])) confirm_collaboration_admin(collaboration.id) user = User.query.get(current_user_id()) invitation = munchify({ "user": user, "collaboration": collaboration, "intended_role": intended_role, "message": message, "hash": generate_token(), "expiry_date": default_expiry_date(data) }) html = mail_collaboration_invitation({ "salutation": "Dear", "invitation": invitation, "base_url": current_app.app_config.base_url, "wiki_link": current_app.app_config.wiki_link, }, collaboration, [], preview=True) return {"html": html}, 201
def collaboration_invites(): data = current_request.get_json() collaboration_id = data["collaboration_id"] confirm_collaboration_admin(collaboration_id) administrators = data.get("administrators", []) message = data.get("message", None) intended_role = data.get("intended_role") intended_role = "member" if intended_role not in ["admin", "member"] else intended_role group_ids = data.get("groups", []) groups = Group.query \ .filter(Group.collaboration_id == collaboration_id) \ .filter(Group.id.in_(group_ids)) \ .all() collaboration = Collaboration.query.get(collaboration_id) user = User.query.get(current_user_id()) membership_expiry_date = data.get("membership_expiry_date") if membership_expiry_date: membership_expiry_date = datetime.fromtimestamp(data.get("membership_expiry_date")) for administrator in administrators: invitation = Invitation(hash=generate_token(), message=message, invitee_email=administrator, collaboration=collaboration, user=user, status="open", intended_role=intended_role, expiry_date=default_expiry_date(json_dict=data), membership_expiry_date=membership_expiry_date, created_by=user.uid) invitation = db.session.merge(invitation) invitation.groups.extend(groups) db.session.commit() mail_collaboration_invitation({ "salutation": "Dear", "invitation": invitation, "base_url": current_app.app_config.base_url, "wiki_link": current_app.app_config.wiki_link, "recipient": administrator }, collaboration, [administrator]) return None, 201