tag.py 3.15 KB
Newer Older
Ryan Herbert's avatar
Ryan Herbert committed
1 2
import defs
import re
3
import json
Ryan Herbert's avatar
Ryan Herbert committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

class TagManager(object):

    def __init__(self, prefix):
        self.prefix = prefix

    def expression(self):
        return r'%s(\w+)' % self.prefix

class TagExtractor(TagManager):

    def __init__(self, prefix, db):
        super(TagExtractor, self).__init__(prefix)
        self.db = db

    def create(self, name):
        db = self.db
        try:
            tid = db.tag.insert(name=name)
            db.commit()
        except:
            tid = db(db.tag.name == name).select(db.tag.id).first()
        return tid
27 28 29 30 31 32 33 34 35 36 37

    def link_to_group(self, tag_id, group_id):
        db = self.db
        assocs = db((db.group_tag.tag_id == tag_id) &
                    (db.group_tag.group_id == group_id)
                ).select()
        if len(assocs) == 0:
            db.group_tag.insert(group_id=group_id, tag_id=tag_id)
            db.commit()

    def link_to_record(self, tag_id, table, record_id):
Ryan Herbert's avatar
Ryan Herbert committed
38
        db = self.db
39
        db.tag_ref.insert(tag_id=tag_id,
Ryan Herbert's avatar
Ryan Herbert committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53
                          table_name=table,
                          record_id=record_id)
        db.commit()

    def remove_tags(self, table, record_id):
        db = self.db
        db((db.tag_ref.table_name == table) &
           (db.tag_ref.record_id == record_id)
          ).delete()
        db.commit()

    def parse_text(self, text):
        return re.findall(self.expression(), text)

54
    def execute(self, table, record_id, text, group_id, reset=False):
Ryan Herbert's avatar
Ryan Herbert committed
55 56 57 58
        if (reset):
            self.remove_tags(table, record_id)
        tags = self.parse_text(text)
        for tag in tags:
59 60 61
            tag_id = self.create(tag)
            self.link_to_group(tag_id, group_id)
            self.link_to_record(tag_id, table, record_id)
Ryan Herbert's avatar
Ryan Herbert committed
62 63 64 65 66 67 68
        return tags

class TagDecorator(TagManager):

    def __init__(self, prefix):
        super(TagDecorator, self).__init__(prefix)

69 70 71
    def decoration(self, ltype, stype, target):
        this = "$(this)" # hack to solve some character escaping issues
        return r'<a onclick="event.preventDefault();event.stopPropagation();db.callLinkable(%s)" href="%s" class="%s-link" data-sample-type="%s" data-linkable-type="%s" data-linkable-name="\1">%s\1</a>' % (this, target, ltype, stype, ltype, self.prefix)
Ryan Herbert's avatar
Ryan Herbert committed
72

73
    def decorate_text(self, text, ltype, stype, target):
Ryan Herbert's avatar
Ryan Herbert committed
74 75
        if (text is None):
            return None
76
        return re.sub(self.expression(), self.decoration(ltype, stype, target), text)
Ryan Herbert's avatar
Ryan Herbert committed
77 78 79 80 81 82 83 84

def get_tag_prefix():
    try:
        tag_prefix = defs.TAG_PREFIX
    except:
        tag_prefix = '#'
    return tag_prefix

85
def register_tags(db, table, record_id, text, group_id, reset=False):
Ryan Herbert's avatar
Ryan Herbert committed
86 87
    tag_prefix = get_tag_prefix()
    tag_extractor = TagExtractor(tag_prefix, db)
88
    tags = tag_extractor.execute(table, record_id, text, group_id, reset)
89

90
def get_tags(db, group_id):
91
    return db((db.tag.id == db.group_tag.tag_id) &
92
              (db.group_tag.group_id == group_id)
93 94 95 96
            ).select(db.tag.ALL)

def tags_to_json(tags):
    tag_list = []
97
    prefix = get_tag_prefix()
98 99 100
    for tag in tags:
        tag_dict = {}
        tag_dict['id'] = tag.id
101
        tag_dict['name'] = tag.name
102 103
        tag_list.append(tag_dict)

104
    return json.dumps(tag_list)