SessionAttr.exists - Python API

Returns True if a given attribute exists or False otherwise.

There are two versions of the call - .exists is used with a single attribute and .exists_many (rather than .exist) is used with multiple attributes. Performance-wise, it is more efficient to use .exists_many if more than one attribute should be checked.

exists

session.attr.exists(self, name)

  • name: Name of the attribute to check
  • Returns: The boolean flag described above
  # -*- coding: utf-8 -*-

  # Zato
  from zato.server.service import Service

  class SessionAttrExists(Service):
      def handle(self):

          # Current user's data
          username = 'admin1'
          password = 'abxqDJpXMVXYEO8NOGx9nVZvv4xSew9'
          current_app = 'CRM'
          remote_addr = '127.0.0.1'
          user_agent = 'Firefox 139.0'

          # Log in current user
          session = self.sso.user.login(self.cid, username, password,
              current_app, remote_addr, user_agent)

          # Get current UST
          ust = session.ust

          # Get session object
          session = self.sso.user.session.get(self.cid, ust, ust, current_app, remote_addr)

          # Information about the attribute to be created
          name = 'my-attribute'
          value = 'my-value'

          # Create a new attribute
          session.attr.create(name, value)

          # Check if the attribute exists
          exists = session.attr.exists(name)

          self.logger.info('Exists -> %s', exists)
  INFO - True

exists_many

session.attr.exists_many(self, data)

Checks if attributes from the input list exist. A dictionary of boolean flags is returned for each input attribute name.

  • data: A list of names of attributes to check
  • Returns: A dictionary of attribute names pointing to boolean flags that indicate if a particular attribute exists

  # -*- coding: utf-8 -*-

  # Zato
  from zato.server.service import Service

  class SessionAttrExistsMany(Service):
      def handle(self):

          # Current user's data
          username = 'admin1'
          password = 'abxqDJpXMVXYEO8NOGx9nVZvv4xSew9'
          current_app = 'CRM'
          remote_addr = '127.0.0.1'
          user_agent = 'Firefox 139.0'

          # Log in current user
          session = self.sso.user.login(
              self.cid, username, password, current_app, remote_addr, user_agent)

          # Get current UST
          ust = session.ust

          # Get session object
          session = self.sso.user.session.get(
              self.cid, ust, ust, current_app, remote_addr)

          # Prepare a list of dictionaries with attributes to create
          data = [
              {'name':'my-attr1-zxc-11', 'value':'11'},
              {'name':'my-attr2-zxc-22', 'value':'22'},
              {'name':'my-attr3-zxc-33', 'value':'33'},
          ]

          # Create new attributes
          session.attr.create_many(data)

          # Check if all the attributes just created actually exist
          result = session.attr.exists_many(['my-attr1-zxc-11', 'my-attr2-zxc-22', 'my-attr3-zxc-33'])

          self.logger.info('Result -> %s', result)
INFO - Result -> {'my-attr2-zxc-22': True, 'my-attr3-zxc-33': True, 'my-attr1-zxc-11': True} ```