Friday, January 13, 2012

web2py: plugin rating widget

I was looking for a plugin for rating with starts and found this one but it doesn't give me much information how to do it so I gave up. (It's sad, I couldn't plug the plugin.)

Plugin Rating
http://www.web2py.com/plugins/default/rating

Now, kenji (s-cubism) just introduced the new plugin and it works like a charm ! It's easy to use and I strongly recommend if you're looking for the plugin.

Rating Widget
http://dev.s-cubism.com/plugin_rating_widget 

Here's how I did.

1. Create new app called "rating"

2. Create models/rating.py
# coding: utf8
from plugin_rating_widget import RatingWidget
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()
################################################################################

3.  Edit controllers/default.py for def index

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)
4. Edit views/default.html

{{left_sidebar_enabled,right_sidebar_enabled=False,True}}
{{extend 'layout.html'}}
{{=form}}
5. Result

Of course, you can create another table and store the submitted value to it, calculate the average, whatever you like. This plugin is good enough for me to build some kind of user review app !




8 comments:

  1. Thank you chibasan! Subscribed to your blog ;)

    ReplyDelete
    Replies
    1. 안녕하세요. Thank you for the comment. Are you Korean? My wife is Korean so I know how to speak!

      Delete
    2. Hello. I just read your reply today. Yes, I am a Korean. hehe
      Are you in Chicago? I just moved to Chicago from Los Angeles. Do you have a web2py meet up here? Anyways, nice to meet you. My name is Kenny.

      p.s
      Chicago is beautiful!

      Delete
  2. Nice to meet you, kench. I live in chicago's north west suburbs. We don't have scheduled monthly meet up but we did web2py meet up in downtown with Massimo last Xmas.

    ReplyDelete
  3. Hello, I am using this plugin but the stars are not appearing ... anyone knows what may br the problem?

    ReplyDelete
    Replies
    1. Same problem here, did you find a solution?

      Delete
  4. Not sure I tested wrong time ago. Maybe something happens during the web2py version up like some CSS styles or Javascript.

    ReplyDelete
  5. Hello.the download link of "http://dev.s-cubism.com/plugin_rating_widget " is broken for me. Could you give a zipped file of the star-rating plugin file,thank you. liuyibing45@126.com

    ReplyDelete