Skip to content

Commit

Permalink
small cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
sfermigier committed Jul 9, 2018
1 parent 8fe88ad commit 35d978b
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 46 deletions.
12 changes: 4 additions & 8 deletions abilian/app.py
Expand Up @@ -704,7 +704,10 @@ def __init__(self, name=None, config=None, *args, **kwargs):
self.start_services()

if os.environ.get("FLASK_VALIDATE_HTML"):
self.after_request(self.validate_response)
# Workaround circular import
from abilian.testing.validation import validate_response

self.after_request(validate_response)

def configure(self, config):
if config:
Expand Down Expand Up @@ -1040,13 +1043,6 @@ def create_root_user(self):
db.session.commit()
return user

def validate_response(self, response):
# work around circular import
from abilian.testing.validation import assert_valid

assert_valid(response)
return response


def create_app(config=None):
return Application(config=config)
2 changes: 1 addition & 1 deletion abilian/services/auth/views.py
Expand Up @@ -99,7 +99,7 @@ def login_post():
if "error" in res:
code = res.pop("code")
flash(res["error"], "error")
return (render_template("login/login.html", **res), code)
return render_template("login/login.html", **res), code

return redirect_back(url=request.url_root)

Expand Down
2 changes: 1 addition & 1 deletion abilian/services/conversion/handlers.py
Expand Up @@ -234,7 +234,7 @@ def convert(self, blob, size=500):

converted_images = []
for fn in l:
converted = resize(open(fn, "rb").read(), size, size, mode=FIT)
converted = resize(open(fn, "rb").read(), size, size)
converted_images.append(converted)

return converted_images
Expand Down
4 changes: 2 additions & 2 deletions abilian/services/image/__init__.py
Expand Up @@ -70,7 +70,7 @@ def resize(orig, width, height, mode=FIT):

orig.seek(0)
image = open_image(orig)
format = image.format
image_format = image.format
x, y = image.size

if (x, y) == (width, height):
Expand All @@ -91,7 +91,7 @@ def resize(orig, width, height, mode=FIT):
assert image.size == (width, height)

output = BytesIO()
image.save(output, get_save_format(format))
image.save(output, get_save_format(image_format))
converted = output.getvalue()
cache[cache_key] = converted
return converted
Expand Down
4 changes: 2 additions & 2 deletions abilian/services/image/tests/test_image.py
Expand Up @@ -24,12 +24,12 @@ def test_get_save_format():


def test_fit(orig_image):
image = resize(orig_image, 500, 500, FIT)
image = resize(orig_image, 500, 500)
x, y = get_size(image)
assert (x, y) == (500, 357)

# image already fits in desired dimension
image = resize(orig_image, 1000, 1000, FIT)
image = resize(orig_image, 1000, 1000)
x, y = get_size(image)
assert x == 725 and y == 518

Expand Down
2 changes: 1 addition & 1 deletion abilian/services/indexing/schema.py
Expand Up @@ -33,7 +33,7 @@ class _DefaultSearchSchema(SchemaClass):
"""General search schema."""

object_key = ID(stored=True, unique=True)
id = NUMERIC(numtype=int, bits=64, signed=False, stored=True)
id = NUMERIC(bits=64, signed=False, stored=True)
object_type = ID(stored=True)
creator = ID(stored=True)
owner = ID(stored=True)
Expand Down
12 changes: 3 additions & 9 deletions abilian/services/indexing/tests/test_adapter.py
Expand Up @@ -31,9 +31,7 @@ class Indexable(IdMixin, CoreIndexable, db.Model):
index_to=(("related.name", ("name", "text")), ("related.description", "text"))
)

num = sa.Column(
sa.Integer, info=SEARCHABLE | dict(index_to=(("num", NUMERIC(numtype=int)),))
)
num = sa.Column(sa.Integer, info=SEARCHABLE | dict(index_to=(("num", NUMERIC()),)))


class SubclassEntityIndexable(Entity):
Expand Down Expand Up @@ -97,9 +95,7 @@ def test_build_attrs():
"tag_text",
}

schema = Schema(
id=NUMERIC(numtype=int, bits=64, signed=False, stored=True, unique=True)
)
schema = Schema(id=NUMERIC(bits=64, signed=False, stored=True, unique=True))
adapter = SAAdapter(Indexable, schema)
assert adapter.indexable
assert set(adapter.doc_attrs) == {"id", "text", "num", "name"}
Expand Down Expand Up @@ -135,9 +131,7 @@ def test_get_document(app, db):

def test_get_document_with_schema():
# test retrieve related attributes
schema = Schema(
id=NUMERIC(numtype=int, bits=64, signed=False, stored=True, unique=True)
)
schema = Schema(id=NUMERIC(bits=64, signed=False, stored=True, unique=True))
adapter = SAAdapter(Indexable, schema)
expected = dict(id=1, num=42)
obj = Indexable(**expected)
Expand Down
2 changes: 1 addition & 1 deletion abilian/services/repository/service.py
Expand Up @@ -122,7 +122,7 @@ def delete(self, uuid):

def __getitem__(self, uuid):
# type: (UUID) -> Any
value = self.get(uuid, default=None)
value = self.get(uuid)
if value is None:
raise KeyError("No file can be found for this uuid", uuid)
return value
Expand Down
6 changes: 3 additions & 3 deletions abilian/services/tagging/service.py
Expand Up @@ -16,13 +16,13 @@ class Taggable(object):
class TagService(object):
"""The tag service."""

def tag(self, object, term, user=None):
def tag(self, obj, term, user=None):
"""Apply a tag on a taggable object.
If user is None, uses the current logged in user.
"""

def untag(self, object, term, user=None):
def untag(self, obj, term, user=None):
"""Remove the given tag from the given object.
See tag().
Expand All @@ -31,5 +31,5 @@ def untag(self, object, term, user=None):
def get_objects_tagged_with(self, term):
"""Returns a list of objects tagged with a given term."""

def get_tags_applied_on(self, object):
def get_tags_applied_on(self, obj):
"""Returns a list of tags applied on a given document."""
2 changes: 1 addition & 1 deletion abilian/services/vocabularies/service.py
Expand Up @@ -42,7 +42,7 @@ class VocabularyService(Service):

def init_app(self, app):
Service.init_app(self, app)
app.register_jinja_loaders(jinja2.PackageLoader(__name__, "templates"))
app.register_jinja_loaders(jinja2.PackageLoader(__name__))

@property
def vocabularies(self):
Expand Down
33 changes: 19 additions & 14 deletions abilian/testing/validation.py
Expand Up @@ -8,17 +8,23 @@

import requests
from flask import Response, current_app, request
from hyperlink import URL

SKIPPED_URLS = [
SKIPPED_PATHS = [
# FIXME: later
"http://localhost/admin/settings"
"/admin/settings"
]


class ValidationError(AssertionError):
pass


def validate_response(response):
assert_valid(response)
return response


def assert_valid(response):
# type: (Response) -> None
if response.direct_passthrough:
Expand All @@ -31,27 +37,27 @@ def assert_valid(response):
if response.status_code == 302:
return

if request.url in SKIPPED_URLS:
if URL.from_text(request.url).path in SKIPPED_PATHS:
return

if response.mimetype == "text/html":
validate_html(response)
assert_html_valid(response)

elif response.mimetype == "application/json":
validate_json(response)
assert_json_valid(response)

else:
raise AssertionError("Unknown mime type: " + response.mimetype)

return


def validate_html(response):
validate_html_using_htmlhint(response)
validate_html_using_external_service(response)
def assert_html_valid(response):
assert_html_valid_using_htmlhint(response)
assert_html_valid_using_external_service(response)


def validate_html_using_htmlhint(response):
def assert_html_valid_using_htmlhint(response):
with NamedTemporaryFile() as tmpfile:
tmpfile.write(response.data)
tmpfile.flush()
Expand All @@ -64,10 +70,9 @@ def validate_html_using_htmlhint(response):
raise ValidationError(msg)


def validate_html_using_external_service(response):
validator_url = current_app.config.get("VALIDATOR_URL") or os.environ.get(
"VALIDATOR_URL"
)
def assert_html_valid_using_external_service(response):
config = current_app.config
validator_url = config.get("VALIDATOR_URL") or os.environ.get("VALIDATOR_URL")

if not validator_url:
return
Expand All @@ -89,7 +94,7 @@ def validate_html_using_external_service(response):
raise ValidationError(msg)


def validate_json(response):
def assert_json_valid(response):
try:
json.loads(response.data)
except BaseException:
Expand Down
1 change: 0 additions & 1 deletion abilian/web/attachments/views.py
Expand Up @@ -96,7 +96,6 @@ def get(self):
mimetype=content_type,
cache_timeout=0,
add_etags=False,
conditional=False,
)


Expand Down
2 changes: 1 addition & 1 deletion abilian/web/search/criterion.py
Expand Up @@ -183,7 +183,7 @@ def get_rel_attr(self, attr_name, model):
rel_model = rel_attr.property.mapper.class_
attr = getattr(rel_model, attr_name, None)

return (rel_model, attr)
return rel_model, attr

def is_excluded(self, attr_name, request):
"""To be overriden by subclasses that want to filter searched
Expand Down
1 change: 0 additions & 1 deletion abilian/web/uploads/views.py
Expand Up @@ -87,7 +87,6 @@ def get(self, handle, *args, **kwargs):
mimetype=content_type,
cache_timeout=0,
add_etags=False,
conditional=False,
)

def delete(self, handle, *args, **kwargs):
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Expand Up @@ -54,6 +54,7 @@ bcrypt
# low-level tools
python-deprecated
typing
hyperlink

# Used by DeferredJS (TODO: remove)
lxml
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Expand Up @@ -30,6 +30,8 @@ flask-testing==0.7.1
flask-wtf==0.12
flask==1.0.2
html5lib==1.0.1 # via bleach
hyperlink==18.0.0
idna==2.7 # via hyperlink
infinity==1.4 # via intervals
intervals==0.8.1 # via wtforms-components
itsdangerous==0.24 # via flask, flask-debugtoolbar
Expand Down

0 comments on commit 35d978b

Please sign in to comment.