def generateMissing(self, applet): """ Helper function to generate profiles for users that predate this class. To be threaded unless no users with profiles exist. :param applet: Applet to get users for. :type applet: dict :returns: list of dicts """ from girderformindlogger.models.applet import Applet from girderformindlogger.models.user import User as UserModel # get groups for applet appletGroups = Applet().getAppletGroups(applet) userList = { role: { groupId: { 'active': list(UserModel().find( query={"groups": { "$in": [ObjectId(groupId)] }}, fields=['_id'])) } for groupId in appletGroups[role].keys() } for role in appletGroups.keys() } # restructure dictionary & return userList = { str(ulu["user"]["_id"]): { k: v for k, v in { "displayName": ulu["user"].get("coordinatorDefined", {}).get( "displayName", ulu["user"].get("userDefined", {}).get( "displayName", ulu["user"].get("displayName"))), "groups": ulu.get("groups") }.items() if v is not None } for ulu in [{ "user": self.createProfile( applet, UserModel().load(user, AccessType.READ, force=True)), "groups": [{ "_id": groupId, "name": appletGroups[role][groupId], "status": status, "role": role } for role in userList for groupId in userList[role] for status in userList[role][groupId]] } for user in set([ ui.get('_id') for u in (userList[role][groupId][status] for role in userList for groupId in userList[role] for status in userList[role][groupId]) for ui in u ])] } return (userList)
def createProfile(self, applet, user, role="user"): """ Create a new profile to store information specific to a given (applet ∩ user) :param applet: The applet for which this profile exists :type applet: dict :param user: The user for which this profile exists :type user: dict :returns: The profile document that was created. """ from girderformindlogger.models.applet import Applet from girderformindlogger.models.group import Group if not isinstance(applet, dict): applet = Applet().load(applet, force=True) user = self._canonicalUser(applet["_id"], user) returnFields = ["_id", "appletId", "coordinatorDefined", "userDefined"] existing = self.findOne( { 'appletId': applet['_id'], 'userId': user['_id'], 'profile': True }, fields=returnFields) if existing: return existing if applet['_id'] not in [ a.get('_id') for a in Applet().getAppletsForUser(role, user) ]: groups = Applet().getAppletGroups(applet).get(role) if bool(groups): group = Group().load(ObjectId(list(groups.keys())[0]), force=True) Group().inviteUser(group, user, level=AccessType.READ) Group().joinGroup(group, user) else: raise ValidationException( "User does not have role \"{}\" in this \"{}\" applet " "({})".format(role, Applet().preferredName(applet), str(applet['_id']))) now = datetime.datetime.utcnow() profile = { k: v for k, v in { 'appletId': ObjectId(applet['_id']), 'userId': ObjectId(user['_id']), 'profile': True, 'created': now, 'updated': now, 'size': 0, 'coordinatorDefined': {}, 'userDefined': { 'displayName': user.get('displayName', user.get( 'firstName')), 'email': user.get('email') } }.items() if v is not None } self.setPublic(profile, False, save=False) # Save the profile. self.save(profile, validate=False) return ({k: v for k, v in profile.items() if k in returnFields})