'This is called by the typeclass system whenever an instance of
this class is saved for the first time. It is a generic hook
for calling the startup hooks for the various game entities.
When overloading you generally don\'t overload this but
overload the hooks called by this method.'

def at_first_save(self):

    self.basetype_setup()
    self.at_object_creation()
    if hasattr(self, '_createdict'):
        cdict = self._createdict
        updates = []
        if (not cdict.get('key')):
            if (not self.db_key):
                self.db_key = ('#%i' % self.dbid)
                updates.append('db_key')
        elif (self.key != cdict.get('key')):
            updates.append('db_key')
            self.db_key = cdict['key']
        if (cdict.get('location') and (self.location != cdict['location'])):
            self.db_location = cdict['location']
            updates.append('db_location')
        if (cdict.get('home') and (self.home != cdict['home'])):
            self.home = cdict['home']
            updates.append('db_home')
        if (cdict.get('destination') and (self.destination != cdict['destination'])):
            self.destination = cdict['destination']
            updates.append('db_destination')
        if updates:
            self.save(update_fields=updates)
        if cdict.get('permissions'):
            self.permissions.add(cdict['permissions'])
        if cdict.get('locks'):
            self.locks.add(cdict['locks'])
        if cdict.get('aliases'):
            self.aliases.add(cdict['aliases'])
        if cdict.get('location'):
            cdict['location'].at_object_receive(self, None)
            self.at_after_move(None)
        if cdict.get('tags'):
            self.tags.add(cdict['tags'])
        if cdict.get('attributes'):
            (keys, values) = (list(cdict['attributes']), listvalues(cdict['attributes']))
            self.attributes.batch_add(keys, values)
        if cdict.get('nattributes'):
            for (key, value) in cdict['nattributes'].items():
                self.nattributes.add(key, value)
        del self._createdict
    self.basetype_posthook_setup()
