def htmlInvitation( self, invitation, invitee=None, fullDoc=False, includeLink=True ): """ Returns an HTML document rendering the invitation. :param invitation: Invitation to render :type invitation: dict :param invitee: Invited user :type invitee: dict or None :returns: html document """ from girderformindlogger.models.applet import Applet from girderformindlogger.models.profile import Profile from girderformindlogger.models.protocol import Protocol from girderformindlogger.models.token import Token from girderformindlogger.models.user import User from girderformindlogger.exceptions import GirderException from girderformindlogger.api.rest import getApiUrl from girderformindlogger.utility import context as contextUtil, \ mail_utils accept = ( "To accept or decline, visit <a href=\"{u}\">{u}</a>".format( u="https://web.mindlogger.org/#/invitation/{}".format(str( invitation['_id'] )) ) ) if includeLink else "" applet = Applet().load(ObjectId(invitation['appletId']), force=True) appletName = applet.get( 'displayName', 'a new applet' ) try: skin = contextUtil.getSkin() except: skin = {} instanceName = skin.get("name", "MindLogger") role = invitation.get("role", "user") try: coordinator = Profile().coordinatorProfile( applet['_id'], invitation["invitedBy"] ) except: coordinator = None displayProfile = Profile().displayProfileFields(invitation, invitee) description = applet.get('meta', {}).get( 'applet', {} ).get( "schema:desciription", Protocol().load( applet['meta']['protocol']['_id'].split('protocol/')[-1], force=True ).get('meta', {}).get('protocol') if 'protocol' in applet.get( 'meta', {} ) else {} ).get("schema:description", "") managers = mail_utils.htmlUserList( Applet().listUsers(applet, 'manager', force=True) ) coordinators = mail_utils.htmlUserList( Applet().listUsers(applet, 'coordinator', force=True) ) reviewers = mail_utils.htmlUserList( Applet().listUsers(applet, 'reviewer', force=True) ) body = """ {greeting}ou were invited {byCoordinator}to be {role} of <b>{appletName}</b>{instanceName}. <br/> Below are the users that have access to your data: {reviewers} {managers} {coordinators} <br/> {accept} """.format( accept=accept, appletName=appletName, byCoordinator="by {} ({}) ".format( coordinator.get("displayName", "an anonymous entity"), "<a href=\"mailto:{email}\">{email}</a>".format( email=coordinator["email"] ) if "email" in coordinator and coordinator["email"] is not None else "email not available" ) if isinstance(coordinator, dict) else "", coordinators="<h3>Users who can change this applet's settings, " "but who cannot change who can see your data: </h3>{}" "".format( coordinators if len( coordinators ) else "<ul><li>None</li></ul>" ), greeting="Welcome to MindLogger! Y", instanceName=" on {}".format( instanceName ) if instanceName is not None and len(instanceName) else "", managers="<h3>Users who can change this applet's settings, " " including who can access your data: </h3>{}" "".format( managers if len(managers) else "<ul><li>None</li></ul>" ), reviewers="<h3>Users who can see your data for this " "applet: </h3>{}" "".format( reviewers if len(reviewers) else "<ul><li>None</li></ul>" ), role="an editor" if role=="editor" else "a {}".format(role) ).strip() return(body if not fullDoc else """ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Invitation to {appletName} on {instanceName}</title> </head> <body> {body} </body> </html> """.format( appletName=appletName, instanceName=instanceName, body=body ).strip())
def inviteUser(self, applet, role="user", email='', firstName='', lastName='', MRN=''): from girderformindlogger.models.invitation import Invitation from girderformindlogger.models.profile import Profile if not mail_utils.validateEmailAddress(email): raise ValidationException('invalid email', 'email') thisUser = self.getCurrentUser() encryptedEmail = UserModel().hash(email) invitedUser = UserModel().findOne({ 'email': encryptedEmail, 'email_encrypted': True }) if not invitedUser: invitedUser = UserModel().findOne({ 'email': email, 'email_encrypted': { '$ne': True } }) if not AppletModel().isCoordinator(applet['_id'], thisUser): raise AccessException( "Only coordinators and managers can invite users.") if role not in USER_ROLE_KEYS: raise ValidationException('Invalid role.', 'role') invitation = Invitation().createInvitationForSpecifiedUser( applet=applet, coordinator=thisUser, role=role, user=invitedUser, firstName=firstName, lastName=lastName, MRN=MRN, userEmail=encryptedEmail) url = 'web.mindlogger.org/#/invitation/%s' % (str(invitation['_id'], )) managers = mail_utils.htmlUserList(AppletModel().listUsers(applet, 'manager', force=True)) coordinators = mail_utils.htmlUserList(AppletModel().listUsers( applet, 'coordinator', force=True)) reviewers = mail_utils.htmlUserList(AppletModel().listUsers( applet, 'reviewer', force=True)) html = mail_utils.renderTemplate( 'userInvite.mako' if invitedUser else 'inviteUserWithoutAccount.mako', { 'url': url, 'userName': firstName, 'coordinatorName': thisUser['firstName'], 'appletName': applet['displayName'], 'MRN': MRN, 'managers': managers, 'coordinators': coordinators, 'reviewers': reviewers }) mail_utils.sendMail('invitation for an applet', html, [email]) return 'sent invitation mail to {}'.format(email)
def htmlInvitation(self, invitation, invitee=None, fullDoc=False, includeLink=True): """ Returns an HTML document rendering the invitation. :param invitation: Invitation to render :type invitation: dict :param invitee: Invited user :type invitee: dict or None :returns: html document """ from girderformindlogger.models.applet import Applet from girderformindlogger.models.profile import Profile from girderformindlogger.models.protocol import Protocol from girderformindlogger.models.token import Token from girderformindlogger.models.user import User from girderformindlogger.exceptions import GirderException from girderformindlogger.api.rest import getApiUrl from girderformindlogger.utility import context as contextUtil, \ mail_utils web_url = os.getenv('WEB_URI') or 'localhost:8082' accept = ("To accept or decline, visit <a href=\"{u}\">{u}</a>".format( u=f"https://{web_url}/#/invitation/{str(invitation['_id'])}") ) if includeLink else "" applet = Applet().load(ObjectId(invitation['appletId']), force=True) appletName = applet['meta']['applet'].get( 'displayName', applet.get('displayName', 'new applet')) try: skin = contextUtil.getSkin() except: skin = {} instanceName = skin.get("name", "MindLogger") role = invitation.get("role", "user") try: coordinator = Profile().coordinatorProfile(applet['_id'], invitation["invitedBy"]) except: coordinator = None displayProfile = Profile().displayProfileFields(invitation, invitee) description = applet.get('meta', {}).get('applet', {}).get( "schema:description", Protocol().load( applet['meta']['protocol']['_id'].split('protocol/')[-1], force=True).get('meta', {}).get('protocol') if 'protocol' in applet.get('meta', {}) else {}).get("schema:description", "") managers = mail_utils.htmlUserList(Applet().listUsers(applet, 'manager', force=True)) coordinators = mail_utils.htmlUserList(Applet().listUsers( applet, 'coordinator', force=True)) reviewers = mail_utils.htmlUserList(Applet().listUsers(applet, 'reviewer', force=True)) body = mail_utils.renderTemplate( f'welcome{"Owner" if role == "owner" else ""}.{invitation.get("lang", "en")}.mako', { 'accept': accept, 'appletName': appletName, 'byCoordinator': "by {} ({}) ".format( coordinator.get("displayName", "an anonymous entity"), "<a href=\"mailto:{email}\">{email}</a>".format( email=coordinator["email"]) if "email" in coordinator and coordinator["email"] is not None else "email not available") if isinstance( coordinator, dict) else "", 'coordinators': "{}".format(coordinators if len(coordinators) else "<ul><li>None</li></ul>"), 'instanceName': " on {}".format(instanceName) if instanceName is not None and len(instanceName) else "", 'managers': "{}".format( managers if len(managers) else "<ul><li>None</li></ul>"), 'reviewers': "{}".format( reviewers if len(reviewers) else "<ul><li>None</li></ul>"), 'role': "an editor" if role == "editor" else "a {}".format(role), 'url': f'https://{web_url}/#/invitation/{str(invitation["_id"])}' }) return (body if not fullDoc else """ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Invitation to {appletName} on {instanceName}</title> </head> <body> {body} </body> </html> """.format(appletName=appletName, instanceName=instanceName, body=body).strip())