def org(request): """Fixture to create an organization with proper clean up""" name = uuid.uuid4().hex print "Creating org '%s'." % name user = User(email='*****@*****.**' % name) user.save() org = Organization(name=name) org.add_member_to_team('Owners', user) org.save() def fin(): """Finalizer to clean up organization after tests""" while org.members: user = org.members[0] print "Deleting user '%s'." % user org.remove_member_from_members(user) user.delete() print "Deleting org '%s'." % name org.delete() request.addfinalizer(fin) return org
def run(self, resource, value, triggered, timestamp, incident_id, action=''): # Validate `resource` based on the rule's type. The `resource` must # be a me.Document subclass, if the corresponding rule is resource- # bound, otherwise None. if resource is not None: assert isinstance(resource, me.Document) assert resource.owner == self._instance.owner else: assert self._instance.is_arbitrary() emails = set(self.emails) user_ids = set(self.users) if not (self.users or self.teams): emails |= set(self._instance.owner.get_emails()) emails |= set(self._instance.owner.alerts_email) if user_ids: user_ids &= set([m.id for m in self._instance.owner.members]) for user in User.objects(id__in=user_ids): emails.add(user.email) for team_id in self.teams: try: team = self._instance.owner.teams.get(id=team_id) emails |= set([member.email for member in team.members]) except me.DoesNotExist: continue # FIXME Imported here due to circular dependency issues. from mist.api.notifications.methods import send_alert_email send_alert_email(self._instance, resource, incident_id, value, triggered, timestamp, emails, action=action, level=self.level, description=self.description)
def dismiss_scale_notifications(machine, feedback='NEUTRAL'): ''' Convenience function to dismiss scale notifications from a machine. Calls dismiss on each notification's channel. May update the feedback field on each notification. ''' recommendation = InAppRecommendation.objects(owner=machine.owner, model_id="autoscale_v1", rid=machine.id).first() # TODO Shouldn't we store which user executed the recommendations action? # Marking the recommendation as "dismissed by everyone" seems a bit wrong. # Perhaps recommendations' actions such as this one must be invoked by a # distinct API endpoint? recommendation.applied = feedback == "POSITIVE" user_ids = set(user.id for user in machine.owner.members) user_ids ^= set(recommendation.dismissed_by) recommendation.channel.dismiss( users=[user for user in User.objects(id__in=user_ids).only('id')])
def run(self, machine, value, triggered, timestamp, incident_id, action='', notification_level=0): # FIXME Imported here due to circular dependency issues. from mist.api.notifications.methods import send_alert_email # TODO Shouldn't be specific to machines. assert isinstance(machine, Machine) assert machine.owner == self._instance.owner emails = set(self.emails) user_ids = set(self.users) if not (self.users or self.teams): emails |= set(self._instance.owner.get_emails()) emails |= set(self._instance.owner.alerts_email) if user_ids: user_ids &= set([m.id for m in self._instance.owner.members]) for user in User.objects(id__in=user_ids): emails.add(user.email) for team_id in self.teams: try: team = self._instance.owner.teams.get(id=team_id) emails |= set([member.email for member in team.members]) except me.DoesNotExist: continue send_alert_email(machine.owner, self._instance.id, value, triggered, timestamp, incident_id, emails, cloud_id=machine.cloud.id, machine_id=machine.machine_id, action=action)
def register_user(email, first_name, last_name, registration_method, selected_plan=None, promo_code=None, token=None, status='pending', create_organization=True, request=None): # User does not exist so we have to add him/her to the database # First make sure that email is not banned # Then create the User objects and the Organization if email.split('@')[1] in config.BANNED_EMAIL_PROVIDERS: raise MethodNotAllowedError("Email provider is banned.") user = User() user.email = email user.first_name = first_name user.last_name = last_name user.registration_method = registration_method user.registration_date = time() user.status = status user.activation_key = get_secure_rand_token() user.can_create_org = True user.save() # For some users registering through sso it might not be necessary to # create an organization, hence the flag org = create_org_for_user(user, '', promo_code, token, selected_plan) \ if create_organization else None log_event_args = { 'owner_id': org and org.id or '', 'user_id': user.id, 'first_name': user.first_name, 'last_name': user.last_name, 'company': user.feedback.company_name, 'event_type': 'request', 'action': 'register', 'authentication_provider': registration_method } if request: log_event_args.update({ 'request_method': request.method, 'request_path': request.path, 'request_ip': ip_from_request(request), 'user_agent': request.user_agent, }) if org: log_event_args.update({'org_id': org.id, 'org_name': org.name}) # Create log for the registration of a user and if an org has been created # add the id and name of the org from mist.api.logs.methods import log_event log_event(**log_event_args) return user, org
def get_users_count(mongo_uri=None, confirmed=False): # return the number of all users, optionally confirmed only users if confirmed: return User.objects(status="confirmed").count() else: return User.objects().count()
def user(self): return User.objects(me.Q(id=self.user_id) | me.Q(email=self.email)).first()