Skip to content

Commit

Permalink
Support glob patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
seanh committed Oct 5, 2015
1 parent fbf9a28 commit 6c86576
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions h/badge/models.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# -*- coding: utf-8 -*-
import fnmatch

import sqlalchemy as sa
from sqlalchemy.orm import exc

from h.db import Base
from h import hashids
from h.i18n import TranslationString as _


class BadgeBlocklist(Base):

"""A list of URIs for which the badge API will always return 0.
This means that the Chrome extension will never show a number of
annotations on its badge for these URIs.
"""

__tablename__ = 'badge_blocklist'

id = sa.Column(sa.Integer, autoincrement=True, primary_key=True)
Expand All @@ -24,16 +27,26 @@ def __repr__(self):

@sa.orm.validates('uri')
def validate_uri(self, key, uri):
if self.query.filter(BadgeBlocklist.uri==uri).all():
if self.get_by_uri(uri):
raise ValueError(_("{uri} is already blocked.").format(uri=uri))
else:
return uri

@classmethod
def get_by_uri(cls, uri):
return cls.query.filter(BadgeBlocklist.uri==uri).one()
try:
return cls.query.filter(BadgeBlocklist.uri == uri).one()
except exc.NoResultFound:
return None

@classmethod
def all(cls):
return cls.query.all()

@classmethod
def is_blocked(cls, uri):
"""Return True if the given URI is blocked."""
for pattern in cls.all():
if fnmatch.fnmatch(uri, unicode(pattern)):
return True
return False
2 changes: 1 addition & 1 deletion h/badge/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def badge(request):
if not uri:
raise httpexceptions.HTTPBadRequest()

if uri in [unicode(u) for u in models.BadgeBlocklist.all()]:
if models.BadgeBlocklist.is_blocked(uri):
return {'total': 0}

return {
Expand Down

0 comments on commit 6c86576

Please sign in to comment.