def list_user_communities(self, request): if request.id or request.email: user = get_user_from_request(user_id=request.id, user_email=request.email) else: user = auth_user(self.request_state.headers) user_community_list = user_community(user) if user else [] user_community_message_list = [] for userrole in user_community_list: community = userrole.get("community").get() if community: community_message = CommunityMessage(id=community.key.id(), name=community.name, description=community.description, welcome_msg=community.welcome_msg) user_community_message = UserCommunityMessage(community=community_message, role=userrole.get("role")) user_community_message_list.append(user_community_message) pending_invites_list = [] if request.include_invite and user: pending_invites = Invite.list_by_user(user.user_email) for pending_invite in pending_invites: community = pending_invite.community.get().to_response_message() pending_invites_list.append(UserInviteMessage(community=community, invite_hash=pending_invite.invite_hash, invite_msg=pending_invite.invite_msg)) return UserCommunityListMessage(community_list=user_community_message_list, invite_list=pending_invites_list)
def update_user(self, request): action_auth_user = auth_user(self.request_state.headers) if not (action_auth_user.user_email == request.user_email): if not is_auth_user_admin(action_auth_user=action_auth_user): return ResponseMessage(success=False) user = get_user_from_request(user_id=request.user_id, user_email=request.user_email, team_key=request.team_key) if user: user.display_name = request.user_display_name or user.display_name user.password = md5(request.user_password) if request.user_password else user.password user.image_url = request.user_image_url or user.image_url or "" user.put() community = Community.getCommunityFromTeamKey(request.team_key) if request.team_key else Community.get_by_id(request.community_id) resp = None if user and community: circle = 0 for circle_value, circle_name in community.circles.iteritems(): if circle_name == request.circle: circle = int(circle_value) resp = UserRole.edit(user, community, request.role, circle) return ResponseMessage(success=True if resp else False)
def insert_user(self, request): if not is_auth_user_admin(headers=self.request_state.headers): return ResponseMessage(success=False) action_user = auth_user(self.request_state.headers) user = get_user_from_request(user_id=request.user_id, user_email=request.user_email, team_key=request.team_key) if not user: user = User.insert_user(request.user_email, username=request.user_display_name, account_type=request.team_key, auth_source=AuthSourceType.PLUGIN, password=md5(request.user_password), image_url=request.user_image_url or "") community = Community.getCommunityFromTeamKey(request.team_key) if request.team_key else Community.get_by_id(request.community_id) role = request.role if request.role else UserRoleType.MEMBER resp = None if user and community: circle = 0 for circle_value, circle_name in community.circles.iteritems(): if circle_name == request.circle: circle = int(circle_value) resp = UserRole.insert(user, community, role, circle) send_added_user_email(community.name, user.display_name, "added", action_user.display_name, community.team_hash) return ResponseMessage(success=True if resp else False)
def edit_user_role(self, request): user = get_user_from_request(user_id=request.user_id, user_email=request.user_email) community = Community.get_by_id(request.community_id) resp = None if community: if user: resp = UserRole.edit(user, community, request.role) elif request.include_invite: resp = Invite.change_user_role(request.user_email, community, request.role) return ResponseMessage(success=True if resp else False)
def delete_user(self, request): user = get_user_from_request(user_id=request.user_id, user_email=request.user_email) community = Community.get_by_id(request.community_id) success = False if community: if user: UserRole.delete(user, community) success = True elif request.include_invite: Invite.delete(request.user_email, community) success = True return ResponseMessage(success=success)
def insert(cls, message, getCommunity=False): community, user = None, None try: from helper.utils import get_user_from_request if message.name is None: return "Community name is required" if not getCommunity else (community, user) if message.type: # community should be of type 'private' or 'public' if not message.type in [CommunityType.PRIVATE, CommunityType.PUBLIC]: return "Community should be of type 'private' or 'public'" if not getCommunity else (community, user) # only one public community is allowed elif message.type == CommunityType.PUBLIC: queryResultCount = Community.query(Community.type == message.type).count() if queryResultCount: return "Community not created. Can't create more than one public community." if not getCommunity else (community, user) else: message.type = CommunityType.PRIVATE community = cls.getCommunityFromTeamKey(team_key=message.team_key) if not community: from helper.utils import md5 team_hash = md5(message.team_key)[-8:] community = cls(name=message.name, description=message.description, welcome_msg=message.welcome_msg, type=message.type, team_key=message.team_key, team_secret=message.team_secret, team_hash=team_hash, plan=message.plan) community.circles = { CircleValue.CONTRIBUTOR : CircleType.CONTRIBUTOR, CircleValue.BETA_TESTER : CircleType.BETA_TESTER, CircleValue.ALPHA_TESTER : CircleType.ALPHA_TESTER, CircleValue.DEVELOPER : CircleType.DEVELOPER } community.put() respData = "Community created." user = get_user_from_request(user_id=message.user.id, user_email=message.user.user_email, team_key=message.team_key) userrole = None userrole_type = UserRoleType.ADMIN if message.team_key else UserRoleType.MANAGER if (not user) and message.team_key and message.user.user_email: from model.user import User from helper.utils import md5 user = User.insert_user(message.user.user_email, username=message.user.display_name, account_type=message.team_key, auth_source=AuthSourceType.PLUGIN, password=md5(message.user.password), image_url="") if user: from model.userrole import UserRole userrole = UserRole.insert(user, community, userrole_type, int(CircleValue.DEVELOPER)) if userrole is None: community.key.delete() respData = "Community is not created as user doesn't exist" except Exception as e: logging.exception("Exception while inserting community: %s" % e) respData = e return respData if not getCommunity else (community, user)
def list_favorite_apps(self, request): user = get_user_from_request(user_id=request.id, user_email=request.email) if not user: user = auth_user(self.request_state.headers) return UserFavoriteAppList(app_list=User.list_favorite_apps(user.key))