Beispiel #1
0
    def checkParameters(self):
        super(RandomReferenceProvider, self).checkParameters()
        model_name = self.param_values.get('model_name', None)

        model = seeder_models_logic.getModel(model_name)
        if not model:
            raise ParameterValueError('Model %s does not exist' % model_name)
Beispiel #2
0
  def checkParameters(self):
    super(RandomReferenceProvider, self).checkParameters()
    model_name = self.param_values.get('model_name', None)

    model = seeder_models_logic.getModel(model_name)
    if not model:
      raise ParameterValueError('Model %s does not exist' % model_name)
Beispiel #3
0
  def validateModel(self, model_data, check_number=True):
    """Validates the configuration sheet for a single model.
    """
    required_properties = ('name', 'properties')
    if check_number:
      required_properties += ('number',)
    for prop in required_properties:
      if prop not in model_data:
        raise JSONFormatError('Model doesn\'t include %s property' % prop)

    model_name = model_data['name']
    properties = model_data['properties']

    if check_number:
      try:
        _ = int(model_data['number'])
      except ValueError:
        raise JSONFormatError('Invalid number of models to seed for model %s'
                              % model_name)

    model_class = seeder_models_logic.getModel(model_name)

    if not model_class:
      raise ConfigurationValueError('Model %s does not exist' % model_name)

    for property_name, provider_data in properties.items():
      self.validateProperty(model_name, model_class,
                            property_name, provider_data)

    for prop in model_class.properties().values():
      # TODO(sttwister): Remove the special check for ReferenceProperty
      if (prop.required and prop.name not in properties and
          type(prop) != db.ReferenceProperty):
        raise ConfigurationValueError('Required property %s for model %s is'
                                      ' missing' % (prop.name, model_name))
Beispiel #4
0
  def getModel(self, model_data):
    """Returns a model seeded using the supplied data. The model is not saved
    to the datastore.
    """
    model_name = model_data['name']
    properties = model_data['properties']

    model_class = seeder_models_logic.getModel(model_name)

    # Get data providers for all the fields
    providers = {}
    for property_name, provider_data in properties.items():
      provider = self.getProvider(provider_data)
      if provider:
        providers[property_name] = provider

    values = {}

    for property_name, provider in providers.items():
      try:
        value = provider.getValue()

      except provider_error, inst:
        raise SeedingError('Error while seeding property %s for model %s: %s'%
                           (property_name, model_name, inst.message))

      values[str(property_name)] = value
Beispiel #5
0
    def getModel(self, model_data):
        """Returns a model seeded using the supplied data. The model is not saved
    to the datastore.
    """
        model_name = model_data['name']
        properties = model_data['properties']

        model_class = seeder_models_logic.getModel(model_name)

        # Get data providers for all the fields
        providers = {}
        for property_name, provider_data in properties.items():
            provider = self.getProvider(provider_data)
            if provider:
                providers[property_name] = provider

        values = {}

        for property_name, provider in providers.items():
            try:
                value = provider.getValue()

            except provider_error, inst:
                raise SeedingError(
                    'Error while seeding property %s for model %s: %s' %
                    (property_name, model_name, inst.message))

            values[str(property_name)] = value
Beispiel #6
0
  def getValue(self):
    self.checkParameters()

    model_name = self.param_values.get('model_name', None)
    model = seeder_models_logic.getModel(model_name)

    models = model.all()
    count = models.count()

    if not count:
      return None

    reference = models[random.randint(0, count-1)]
    return reference
Beispiel #7
0
    def getValue(self):
        self.checkParameters()

        model_name = self.param_values.get('model_name', None)
        model = seeder_models_logic.getModel(model_name)

        models = model.all()
        count = models.count()

        if not count:
            return None

        reference = models[random.randint(0, count - 1)]
        return reference
Beispiel #8
0
    def seedModel(self, model_data):
        """Returns a list of models seeded using the supplied data.
    """
        model_name = model_data['name']
        number = int(model_data.get('number', 1))
        properties = model_data['properties']

        model_class = seeder_models_logic.getModel(model_name)
        models = []

        self.processReferences(model_data)

        # Get data providers for all the fields
        providers = {}
        for property_name, provider_data in properties.items():
            provider = self.getProvider(provider_data)
            if provider:
                providers[property_name] = provider

        # Seed the models using the data providers
        for _ in range(number):
            model = self.getModel(model_data)

            model.put()

            # Check for all configured back_references
            for property_name, provider_data in properties.items():
                if provider_data['provider_name'] == 'RelatedModels':
                    related_models = self.seedModel(
                        provider_data['parameters'])
                    for related_model in related_models:
                        back_reference_property = getattr(
                            model_class, property_name)
                        # pylint: disable=W0212
                        setattr(related_model,
                                back_reference_property._prop_name, model)
                        # pylint: enable=W0212
                        related_model.put()

            models.append(model)

        return models
Beispiel #9
0
    def validateModel(self, model_data, check_number=True):
        """Validates the configuration sheet for a single model.
    """
        required_properties = ('name', 'properties')
        if check_number:
            required_properties += ('number', )
        for prop in required_properties:
            if prop not in model_data:
                raise JSONFormatError('Model doesn\'t include %s property' %
                                      prop)

        model_name = model_data['name']
        properties = model_data['properties']

        if check_number:
            try:
                _ = int(model_data['number'])
            except ValueError:
                raise JSONFormatError(
                    'Invalid number of models to seed for model %s' %
                    model_name)

        model_class = seeder_models_logic.getModel(model_name)

        if not model_class:
            raise ConfigurationValueError('Model %s does not exist' %
                                          model_name)

        for property_name, provider_data in properties.items():
            self.validateProperty(model_name, model_class, property_name,
                                  provider_data)

        for prop in model_class.properties().values():
            # TODO(sttwister): Remove the special check for ReferenceProperty
            if (prop.required and prop.name not in properties
                    and type(prop) != db.ReferenceProperty):
                raise ConfigurationValueError(
                    'Required property %s for model %s is'
                    ' missing' % (prop.name, model_name))
Beispiel #10
0
  def seedModel(self, model_data):
    """Returns a list of models seeded using the supplied data.
    """
    model_name = model_data['name']
    number = int(model_data.get('number', 1))
    properties = model_data['properties']

    model_class = seeder_models_logic.getModel(model_name)
    models = []

    self.processReferences(model_data)

    # Get data providers for all the fields
    providers = {}
    for property_name, provider_data in properties.items():
      provider = self.getProvider(provider_data)
      if provider:
        providers[property_name] = provider

    # Seed the models using the data providers
    for _ in range(number):
      model = self.getModel(model_data)

      model.put()

      # Check for all configured back_references
      for property_name, provider_data in properties.items():
        if provider_data['provider_name'] == 'RelatedModels':
          related_models = self.seedModel(provider_data['parameters'])
          for related_model in related_models:
            back_reference_property = getattr(model_class, property_name)
            # pylint: disable=W0212
            setattr(related_model, back_reference_property._prop_name, model)
            # pylint: enable=W0212
            related_model.put()

      models.append(model)

    return models
Beispiel #11
0
 def testGetReigsteredModel(self):
   """Tests retrieving a model that has been registered.
   """
   from soc.models.student import Student
   model = models_logic.getModel('soc.models.student.Student')
   self.assertEqual(model, Student)
Beispiel #12
0
 def testGetReigsteredModel(self):
     """Tests retrieving a model that has been registered.
 """
     from soc.models.student import Student
     model = models_logic.getModel('soc.models.student.Student')
     self.assertEqual(model, Student)