If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

Example of using the TAGS:

Bitcoin BETS

 

============ how to do =============

in model (db.py) insert table:

db.define_table('tags',
    Field('name', 'string', length=30),
    Field('uses', 'integer', default=1),
    )
# some table:
db.define_table('items',
    Field('tags', 'string', length=150),
    ...

 

For take tags for input in form:

    tags = db(db.tags).select(orderby=db.tags.name)
    h_tags = CAT(SCRIPT('function at(n) { $("#no_table_tags").val($("#no_table_tags").val() + n + ",");}'))
    for tag in tags:
        h_tags += SPAN(tag.name, _class='tag', _onclick='at("%s");' % tag.name)
        f = SQLFORM.factory(
            Field('name', 'string',
                  label=T('Name')),
            Field('tags', 'string', default = sess.tags or '', label=T('Tags')),
            )
    # add list of tags in form
    f[0].insert(1, TR(TD(), TD(h_tags)))

 

When db.items inserted update tags table:

    if tags:
        for tag in tags.split(','):
            tag = tag.strip().lower()
            if not tag: continue
            rec = db(db.tags.name.lower() == tag).select().first()
            if rec:
                rec.update_record( uses = rec.uses + 1)
            else:
                db.tags.insert(name = tag)

 

When tags is setted for filter records:

    tags = type(tags) == type('') and tags.split(',')
    for wr in db(
                 & (not tags or db.items.tags.contains(tags,  all=True, case_sensitive=False))
                 ).select(orderby=~db.items.id):
        pass ...

 

for set tags in any page:

def filter(sess, req):
    h = CAT()
    
    hide_list_load = '$("#list_load").slideUp(300);'
    h += A(XML('<i class="fa fa-search-minus"></i>'), _class='tag',
           _onclick=hide_list_load + "ajax('%s')" % URL('hand','filter_clear'), _style='background-color:#da4f49;')
    
    h += A(XML('<i class="fa fa-search"></i>'), _class='tag',
           _onclick=hide_list_load + "ajax('%s', ['tags']);" % URL('hand','filter_tags'), _style='background-color:#da4f49;' )
    wlist_flt = session.wlist_flt
    wlist_flt = type(wlist_flt) == type('') and wlist_flt or ''
    h += INPUT(_name='tags', _value=wlist_flt, _id='tags')
    h += SPAN(XML('<i class="fa fa-eraser"></i>'), _class='tag', _onclick='$("#tags").val("");')
    h += CAT(SCRIPT('function at(n) { $("#tags").val($("#tags").val() + n + ",");}'))
    tags = db(db.tags).select(orderby=~db.tags.uses, limitby=(0,20)).sort(lambda row: row.name.lower())
    for tag in tags:
        uses = tag.uses
        size = uses> 300 and 24 or uses> 100 and 20 or uses> 10 and 16 or 14
        
        h += DIV(tag.name, _class='tag', _onclick='at("%s");' % tag.name,
                  _style='font-size:%spx;' % size)
    return DIV(h, _class='row')

 

def index():

    return dict(h = filter(session, request))

Related slices

Comments (0)


Hosting graciously provided by:
Python Anywhere