Exemplo n.º 1
0
    def register_model(self, model_instance):

        type_name = model_instance.__get_elastic_type_name__()
        if type_name not in self.model_registry:
            self.model_registry[type_name] = model_instance.__class__
            self.instance_registry[type_name] = model_instance.__class__()
            properties = {}
            for field_name, field_instance in model_instance.__fields__.items():
                from elasticosm.models.fields import StringField, ReferenceField
                if isinstance(field_instance, StringField):
                    properties[field_name] = __string_field_mapping__
                if isinstance(field_instance, ReferenceField):
                    properties[field_name] = __string_field_mapping__
                if field_instance.is_encrypted:
                    properties[field_name] = __encrypted_field_mapping__
            if len(properties.keys()) > 0:
                mapping_def = {type_name: {'properties': properties }}
                mapping_json = json.dumps(mapping_def)
                db = ElasticOSMConnection.get_db()
                server = ElasticOSMConnection.get_server()
                http_server = server.replace(":9500", ":9200")
                uri = "http://%s/%s/%s/_mapping" % (http_server, db, type_name)
                r = requests.put(uri, data=mapping_json)
                outcome = json.loads(r.text)
                if 'ok' in outcome:
                    if outcome['ok']:
                        print "Registered model %s on database " + \
                                "%s" % (type_name, db)
                else:
                    print "Failed to register model %s on database " + \
                            "%s with error: %s" % (type_name,
                                                   db,
                                                   outcome.get('error', None)
                                                   )
Exemplo n.º 2
0
 def save(self):
     for v in self.__fields__.values():
         v.on_save(self)
     if self.id is None:
         self.id = str(uuid.uuid4())
     self.pre_save()
     ElasticOSMConnection.save(self)
Exemplo n.º 3
0
 def on_save(self, obj):
     if obj.__fields_values__[self.name] is None:
         obj.__fields_values__[self.name] = self.default
     if obj.id is None:
         if self.unique:
             unique_field_key = "%s__%s" % (
                                 obj.__get_elastic_type_name__(),
                                 self.name
                                 )
             unique_data = {'_id': obj.__fields_values__[self.name],
                            '_type': unique_field_key}
             try:
                 ElasticOSMConnection.get_connection().index(
                                     unique_data,
                                     ElasticOSMConnection.get_uniques_db(),
                                     unique_data['_id'],
                                     unique_data['_type'],
                                     op_type='create'
                                     )
             except AlreadyExistsException:
                 raise UniqueFieldExistsException('Value %s exists for ' + \
                                                  'field %s' % (
                                                     unique_data['_id'],
                                                     self.name)
                                                  )
Exemplo n.º 4
0
 def get_version(self):
     if self.id is None:
         return 0
     r = ElasticOSMConnection.get_by_id(self.type_name, self.id)
     d = json.loads(r.text)
     version = d['_version']
     return version
Exemplo n.º 5
0
 def get_value(self, obj):
     value_id = obj.__fields_values__[self.name]
     if value_id is None or value_id == '':
         return None
     if value_id not in obj.__reference_cache__:
         from elasticosm.core.connection import ElasticOSMConnection
         referenced_object = ElasticOSMConnection.get_by_id(value_id)
         obj.__reference_cache__[value_id] = referenced_object
     return obj.__reference_cache__[value_id]
Exemplo n.º 6
0
 def get(cls, *args, **kwargs):
     type_name = cls.__get_elastic_type_name__()
     from elasticosm.models import ElasticModel
     base_model_type = ElasticModel.__get_elastic_type_name__()
     if type_name == base_model_type:
         type_name = None
     r = ElasticOSMConnection.get(type_name, kwargs)
     if r.count() < 1:
         return None
     if r.count() > 1:
         raise MultipleObjectsReturned(
                 'Multiple objects returned for query %s' % kwargs
                 )
     return r[0]
Exemplo n.º 7
0
 def delete(self):
     return ElasticOSMConnection.delete(self)
Exemplo n.º 8
0
 def count(cls):
     return ElasticOSMConnection.get_count(cls.__get_elastic_type_name__())