various bug fixes

This commit is contained in:
Edward Betts 2017-02-20 11:10:48 +00:00
parent 06eb73cffa
commit 1e0891623a

View file

@ -19,6 +19,8 @@ doc_hashids = Hashids(min_length=8)
Base = declarative_base() Base = declarative_base()
Base.query = session.query_property() Base.query = session.query_property()
re_server_url = re.compile('^http://perma.pub/\d+/([^/]+)/([^/]+)$')
# list of disallowed usernames - maybe this should be in the database # list of disallowed usernames - maybe this should be in the database
reserved_name = ['root', 'admin', 'administrator', 'support', 'info', reserved_name = ['root', 'admin', 'administrator', 'support', 'info',
'test', 'tech', 'online', 'old', 'new', 'jobs', 'login', 'job', 'ipad' 'test', 'tech', 'online', 'old', 'new', 'jobs', 'login', 'job', 'ipad'
@ -138,7 +140,11 @@ class Item(TimeStampedModel):
@classmethod @classmethod
def get_by_hashid(cls, hashid): def get_by_hashid(cls, hashid):
return cls.query.get(doc_hashids.decode(hashid)) try:
item_id = doc_hashids.decode(hashid)[0]
except IndexError:
return
return cls.query.get(item_id)
@property @property
def url(self): def url(self):
@ -163,11 +169,12 @@ class Item(TimeStampedModel):
@classmethod @classmethod
def from_external(cls, url): def from_external(cls, url):
home = url_for('.home', _external=True) home = url_for('.home', _external=True)
if not url.startswith(home): m = re_server_url.match(url)
if not m:
return return
username, _, hashid = url[len(home):].partition('/') username, hashid = m.groups()
q = cls.query.filter(User.username == username, item_id = doc_hashids.decode(hashid)[0]
cls.id == doc_hashids.decode(hashid)[0]) q = cls.query.filter(User.username == username, cls.id == item_id)
return q.one_or_none() return q.one_or_none()
class XanaDoc(Item): class XanaDoc(Item):