from framework import StoredObject, fields


class Guid(StoredObject):

    _id = fields.StringField()
    referent = fields.AbstractForeignField()

    _meta = {
        'optimistic': True,
    }


class GuidStoredObject(StoredObject):

    # Redirect to content using URL redirect by default
    redirect_mode = 'redirect'

    def _ensure_guid(self):
        """Create GUID record if current record doesn't already have one, then
        point GUID to self.

        """
        # Create GUID with specified ID if ID provided
        if self._primary_key:

            # Done if GUID already exists
            guid = Guid.load(self._primary_key)
            if guid is not None:
                return

            # Create GUID
            guid = Guid(
                _id=self._primary_key,
                referent=self
            )
            guid.save()

        # Else create GUID optimistically
        else:

            # Create GUID
            guid = Guid()
            guid.save()
            guid.referent = (guid._primary_key, self._name)
            guid.save()

            # Set primary key to GUID key
            self._primary_key = guid._primary_key

    def save(self, *args, **kwargs):
        """ Ensure GUID on save initialization. """
        rv = super(GuidStoredObject, self).save(*args, **kwargs)
        self._ensure_guid()
        return rv

    @property
    def annotations(self):
        """ Get meta-data annotations associated with object. """
        return self.metadata__annotated

from framework import StoredObject, fields


class Guid(StoredObject):

    _id = fields.StringField()
    referent = fields.AbstractForeignField()

    _meta = {
        'optimistic': True,
    }


class GuidStoredObject(StoredObject):

    # Redirect to content using URL redirect by default
    redirect_mode = 'redirect'

    def _ensure_guid(self):
        """Create GUID record if current record doesn't already have one, then
        point GUID to self.

        """
        # Create GUID with specified ID if ID provided
        if self._primary_key:

            # Done if GUID already exists
            guid = Guid.load(self._primary_key)
            if guid is not None:
                return

            # Create GUID
            guid = Guid(
                _id=self._primary_key,
                referent=self
            )
            guid.save()

        # Else create GUID optimistically
        else:

            # Create GUID
            guid = Guid()
            guid.save()
            guid.referent = (guid._primary_key, self._name)
            guid.save()

            # Set primary key to GUID key
            self._primary_key = guid._primary_key

    def save(self, *args, **kwargs):
        """ Ensure GUID on save initialization. """
        self._ensure_guid()
        return super(GuidStoredObject, self).save(*args, **kwargs)

    @property
    def annotations(self):
        """ Get meta-data annotations associated with object. """
        return self.metadata__annotated

Fix last commit: Must ensure GUID before saving so that PK is defined
Fix last commit: Must ensure GUID before saving so that PK is defined

  from framework import StoredObject, fields
  
  
  class Guid(StoredObject):
  
      _id = fields.StringField()
      referent = fields.AbstractForeignField()
  
      _meta = {
          'optimistic': True,
      }
  
  
  class GuidStoredObject(StoredObject):
  
      # Redirect to content using URL redirect by default
      redirect_mode = 'redirect'
  
      def _ensure_guid(self):
          """Create GUID record if current record doesn't already have one, then
          point GUID to self.
  
          """
          # Create GUID with specified ID if ID provided
          if self._primary_key:
  
              # Done if GUID already exists
              guid = Guid.load(self._primary_key)
              if guid is not None:
                  return
  
              # Create GUID
              guid = Guid(
                  _id=self._primary_key,
                  referent=self
              )
              guid.save()
  
          # Else create GUID optimistically
          else:
  
              # Create GUID
              guid = Guid()
              guid.save()
              guid.referent = (guid._primary_key, self._name)
              guid.save()
  
              # Set primary key to GUID key
              self._primary_key = guid._primary_key
  
      def save(self, *args, **kwargs):
          """ Ensure GUID on save initialization. """
-         rv = super(GuidStoredObject, self).save(*args, **kwargs)
          self._ensure_guid()
-         return rv
+         return super(GuidStoredObject, self).save(*args, **kwargs)
  
      @property
      def annotations(self):
          """ Get meta-data annotations associated with object. """
          return self.metadata__annotated
  
Fix last commit: Must ensure GUID before saving so that PK is defined
## Code Before:
from framework import StoredObject, fields


class Guid(StoredObject):

    _id = fields.StringField()
    referent = fields.AbstractForeignField()

    _meta = {
        'optimistic': True,
    }


class GuidStoredObject(StoredObject):

    # Redirect to content using URL redirect by default
    redirect_mode = 'redirect'

    def _ensure_guid(self):
        """Create GUID record if current record doesn't already have one, then
        point GUID to self.

        """
        # Create GUID with specified ID if ID provided
        if self._primary_key:

            # Done if GUID already exists
            guid = Guid.load(self._primary_key)
            if guid is not None:
                return

            # Create GUID
            guid = Guid(
                _id=self._primary_key,
                referent=self
            )
            guid.save()

        # Else create GUID optimistically
        else:

            # Create GUID
            guid = Guid()
            guid.save()
            guid.referent = (guid._primary_key, self._name)
            guid.save()

            # Set primary key to GUID key
            self._primary_key = guid._primary_key

    def save(self, *args, **kwargs):
        """ Ensure GUID on save initialization. """
        rv = super(GuidStoredObject, self).save(*args, **kwargs)
        self._ensure_guid()
        return rv

    @property
    def annotations(self):
        """ Get meta-data annotations associated with object. """
        return self.metadata__annotated

## Instruction:
Fix last commit: Must ensure GUID before saving so that PK is defined
## Code After:
from framework import StoredObject, fields


class Guid(StoredObject):

    _id = fields.StringField()
    referent = fields.AbstractForeignField()

    _meta = {
        'optimistic': True,
    }


class GuidStoredObject(StoredObject):

    # Redirect to content using URL redirect by default
    redirect_mode = 'redirect'

    def _ensure_guid(self):
        """Create GUID record if current record doesn't already have one, then
        point GUID to self.

        """
        # Create GUID with specified ID if ID provided
        if self._primary_key:

            # Done if GUID already exists
            guid = Guid.load(self._primary_key)
            if guid is not None:
                return

            # Create GUID
            guid = Guid(
                _id=self._primary_key,
                referent=self
            )
            guid.save()

        # Else create GUID optimistically
        else:

            # Create GUID
            guid = Guid()
            guid.save()
            guid.referent = (guid._primary_key, self._name)
            guid.save()

            # Set primary key to GUID key
            self._primary_key = guid._primary_key

    def save(self, *args, **kwargs):
        """ Ensure GUID on save initialization. """
        self._ensure_guid()
        return super(GuidStoredObject, self).save(*args, **kwargs)

    @property
    def annotations(self):
        """ Get meta-data annotations associated with object. """
        return self.metadata__annotated
