Manage note colours from database

This commit is contained in:
Keith Edmunds 2022-02-05 21:32:41 +00:00
parent 1de7cefe72
commit 53899b3a24

View File

@ -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