diff --git a/app/model.py b/app/model.py index fe0e82d..ef6a4f8 100644 --- a/app/model.py +++ b/app/model.py @@ -1,6 +1,8 @@ #!/usr/bin/python3 import os.path +import re + import sqlalchemy from datetime import datetime @@ -44,9 +46,10 @@ class NoteColours(Base): id = Column(Integer, primary_key=True, autoincrement=True) substring = Column(String(256), index=False) hexcolour = Column(String(6), index=False) - enabled = Column(Boolean, default=True, index=False) + enabled = Column(Boolean, default=True, index=True) is_regex = Column(Boolean, default=False, index=False) is_casesensitive = Column(Boolean, default=False, index=False) + order = Column(Integer, index=True) def __repr__(self): return ( @@ -61,9 +64,26 @@ class NoteColours(Base): Currently ignore is_regex and is_casesensitive """ - for rec in session.query(NoteColours).all(): - if rec.substring in text: - return '#' + rec.hexcolour + for rec in ( + session.query(NoteColours) + .filter(NoteColours.enabled == True) + .order_by(NoteColours.order) + .all() + ): + if rec.is_regex: + if rec.is_casesensitive: + p = re.compile(rec.substring) + else: + p = re.compile(rec.substring, re.IGNORECASE) + if p.match(text): + return '#' + rec.hexcolour + else: + if rec.is_casesensitive: + if rec.substring in text: + return '#' + rec.hexcolour + else: + if rec.substring.lower() in text.lower(): + return '#' + rec.hexcolour return None