← Back to the web2py-plugin list
Rating Widget
A rating widget using jquery.rating.js
Demo
| form | : |
Usage
-
controllers/plugin_rating_widget.py
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.from plugin_rating_widget import RatingWidget
db = DAL('sqlite:memory:')
db.define_table('product',
Field('rating', 'integer',
requires=IS_IN_SET(range(1, 6)), # "requires" is necessary for the rating widget
))
################################ The core ######################################
# Inject the horizontal radio widget
db.product.rating.widget = RatingWidget()
################################################################################
def index():
form = SQLFORM(db.product)
if form.accepts(request.vars, session):
session.flash = 'submitted %s' % form.vars
redirect(URL('index'))
return dict(form=form)
Source code
-
modules/plugin_rating_widget.py
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.from gluon import *
from gluon.storage import Storage
# For referencing static and views from other application
import os
APP = os.path.basename(os.path.dirname(os.path.dirname(__file__)))
def _set_files(files):
if current.request.ajax:
current.response.js = (current.response.js or '') + """;(function ($) {
var srcs = $('script').map(function(){return $(this).attr('src');}),
hrefs = $('link').map(function(){return $(this).attr('href');});
$.each(%s, function() {
if ((this.slice(-3) == '.js') && ($.inArray(this.toString(), srcs) == -1)) {
var el = document.createElement('script'); el.type = 'text/javascript'; el.src = this;
document.body.appendChild(el);
} else if ((this.slice(-4) == '.css') && ($.inArray(this.toString(), hrefs) == -1)) {
$('<link rel="stylesheet" type="text/css" href="' + this + '" />').prependTo('head');
if (/* for IE */ document.createStyleSheet){document.createStyleSheet(this);}
}});})(jQuery);""" % ('[%s]' % ','.join(["'%s'" % f.lower().split('?')[0] for f in files]))
else:
current.response.files[:0] = [f for f in files if f not in current.response.files]
class RatingWidget(object):
def __init__(self):
settings = self.settings = Storage()
settings.files = None
def __call__(self, field, value, **attributes):
if self.settings.files is None:
_files = [URL(APP, 'static', 'plugin_rating_widget/rating_widget.css'),
URL(APP, 'static', 'plugin_rating_widget/jquery.rating.pack.js')]
else:
_files = self.settings.files
_set_files(_files)
_id = '%s_%s' % (field._tablename, field.name)
attr = dict(_id=_id, _name=field.name, requires=field.requires, _class='text')
opts = [INPUT(_type='radio', _name=field.name, _value=k, value=value, _class='star')
for k, v in field.requires.options() if str(v)]
script = SCRIPT("""
jQuery(function() { var t = 10; (function run() {if ((function() {
var el = jQuery('#%(id)s .star');
if (el.rating == undefined) { return true; }
el.rating();
})()) {setTimeout(run, t); t = 2*t;}})();});
""" % dict(id=_id))
return SPAN(script, SPAN(*opts, **attr), **attributes)
