Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #42 from postatum/93707062_constr_on_other_field
Browse files Browse the repository at this point in the history
Use new field processors API. Log ES mapping errors
  • Loading branch information
jstoiko committed May 25, 2015
2 parents 914534f + ec4502b commit 9135c3b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
14 changes: 7 additions & 7 deletions nefertari/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ def get_authuser_by_name(cls, request):
return cls.get_resource(username=username)


def lower_strip(value):
return (value or '').lower().strip()
def lower_strip(instance, new_value):
return (new_value or '').lower().strip()


def encrypt_password(password):
""" Crypt :password: if it's not crypted yet. """
if password and not crypt.match(password):
password = unicode(crypt.encode(password))
return password
def encrypt_password(instance, new_value):
""" Crypt :new_value: if it's not crypted yet. """
if new_value and not crypt.match(new_value):
new_value = unicode(crypt.encode(new_value))
return new_value


class AuthUser(AuthModelDefaultMixin, engine.BaseDocument):
Expand Down
12 changes: 8 additions & 4 deletions nefertari/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def perform_request(self, *args, **kw):

return super(ESHttpConnection, self).perform_request(*args, **kw)
except Exception as e:
log.error(e.error)
status_code = e.status_code
if status_code == 404:
raise IndexNotFoundException()
Expand Down Expand Up @@ -172,10 +173,13 @@ def create_index(cls, index_name=None):
@classmethod
def setup_mappings(cls):
models = engine.get_document_classes()
for model_name, model_cls in models.items():
if getattr(model_cls, '_index_enabled', False):
es = ES(model_cls.__name__)
es.put_mapping(body=model_cls.get_es_mapping())
try:
for model_name, model_cls in models.items():
if getattr(model_cls, '_index_enabled', False):
es = ES(model_cls.__name__)
es.put_mapping(body=model_cls.get_es_mapping())
except JHTTPBadRequest as ex:
raise Exception(ex.json['extra']['data'])

def __init__(self, source='', index_name=None, chunk_size=100):
self.doc_type = self.src2type(source)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_authentication/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class TestModelHelpers(object):

def test_lower_strip(self, engine_mock):
from nefertari.authentication import models
assert models.lower_strip('Foo ') == 'foo'
assert models.lower_strip(None) == ''
assert models.lower_strip(None, 'Foo ') == 'foo'
assert models.lower_strip(None, None) == ''

def test_encrypt_password(self, engine_mock):
from nefertari.authentication import models
encrypted = models.encrypt_password('foo')
encrypted = models.encrypt_password(None, 'foo')
assert models.crypt.match(encrypted)
assert encrypted != 'foo'
assert encrypted == models.encrypt_password(encrypted)
assert encrypted == models.encrypt_password(None, encrypted)

@patch('nefertari.authentication.models.uuid.uuid4')
def test_create_apikey_token(self, mock_uuid, engine_mock):
Expand Down

0 comments on commit 9135c3b

Please sign in to comment.