Esempio n. 1
0
  def __property_config__(self, model_class, property_name):
    super(ReferenceListProperty, self).__property_config__(model_class,
                                                       property_name)

    if self.collection_name is None:
      self.collection_name = '%s_listref_set' % (model_class.__name__.lower())
    if hasattr(self.reference_class, self.collection_name):
      raise DuplicatePropertyError('Class %s already has property %s'
                                   % (self.reference_class.__name__,
                                      self.collection_name))
    setattr(self.reference_class,
            self.collection_name,
            _ReverseReferenceProperty(model_class, property_name))
    def __property_config__(self, model_class, property_name):
        """Loads all of the references that point to this model.

        We need to do this to create the ReverseReferenceProperty properties for
        this model and create the <reference>_set attributes on the referenced
        model, e.g.:

             class Story(db.Model):
                 title = db.StringProperty()
             class Comment(db.Model):
                 story = db.ReferenceProperty(Story)
             story = Story.get(id)
             print [c for c in story.comment_set]

        In this example, the comment_set property was created based on the reference
        from Comment to Story (which is inherently one to many).

        Args:
            model_class: Model class which will have its reference properties
                initialized.
            property_name: Name of property being configured.

        Raises:
            DuplicatePropertyError if referenced class already has the provided
                collection name as a property.
        """
        super(MemcacheReferenceProperty, self).__property_config__(model_class,
                                                                                                             property_name)

        if self.reference_class is _SELF_REFERENCE:
            self.reference_class = self.data_type = model_class

        if self.collection_name is None:
            self.collection_name = '%s_set' % (model_class.__name__.lower())
        existing_prop = getattr(self.reference_class, self.collection_name, None)
        if existing_prop is not None:
            if not (isinstance(existing_prop, db._ReverseReferenceProperty) and
                            existing_prop._prop_name == property_name and
                            existing_prop._model.__name__ == model_class.__name__ and
                            existing_prop._model.__module__ == model_class.__module__):
                raise DuplicatePropertyError('Class %s already has property %s '
                                                                     % (self.reference_class.__name__,
                                                                            self.collection_name))
        setattr(self.reference_class,
                        self.collection_name,
                        db._ReverseReferenceProperty(model_class, property_name))
    def __property_config__(self, model_class, property_name):
        '''
        Adds the normal and reverse collections to the model
        
        
        Args:
        model_class: Model class which will have its reference properties
        initialized.
        property_name: Name of property being configured.
        
        Raises:
        DuplicatePropertyError if referenced class already has the provided
        collection name as a property.
        '''

        super(ReferenceListProperty, self).__property_config__(model_class,
                                                               property_name)
                
        #set up (forward) collection
        if self.collection_name is None:
            self.collection_name = '%s_set' % (
                                    self.reference_class.__name__.lower())     
        
        existing_prop = getattr(self.model_class,
                                self.collection_name, None)
        
        if existing_prop is not None:
            
            if not(isinstance(existing_prop, db._ReverseReferenceProperty) and
                   existing_prop._prop_name == property_name and
                   existing_prop._model.__name__ == self.reference_class.__name__ and
                   existing_prop._model.__module__ == self.reference_class.__module__):
                raise db.DuplicatePropertyError( 
                    'Class %s already has property %s ' % (
                                                    self.model_class.__name__, 
                                                    self.collection_name))                
        #create and attach forward collection
        setattr(self.model_class, self.collection_name,
                _ForwardReferenceProperty(self.reference_class, property_name))        
        
        
        
        #Set up Reverse Collection
        if self.reverse_collection_name is None:
            self.reverse_collection_name = '%s_set' % (
                                            model_class.__name__.lower())
        
        
        
        #check for duplicate reverse_collection_name
        existing_prop = getattr(self.reference_class, 
                                self.reverse_collection_name, None)
        if existing_prop is not None:
            
            if not (isinstance(existing_prop, db._ReverseReferenceProperty) and
                    existing_prop._prop_name == property_name and
                    existing_prop._model.__name__ == model_class.__name__ and
                    existing_prop._model.__module__ == model_class.__module__):
                raise db.DuplicatePropertyError(
                    'Class %s already has property %s '
                    % (self.reference_class.__name__, 
                       self.reverse_collection_name))
                
        #create and attach reverse collection
        setattr(self.reference_class, self.reverse_collection_name,
                db._ReverseReferenceProperty(model_class, property_name))