Re: [web2py] how to create a counter button...

2013-01-17 Thread sasogeek
what is an ajax callback and how do i use it? what is it for?

On Thursday, 17 January 2013 03:16:38 UTC, rochacbruno wrote:

 models/foo.py

 # the thing table
 db.define_table(thing, Field(name))

 # you may want to store likes on another table so you never allow a user 
 to like the same thing twice
 db.define_table(thing_likes,
 Field(user, reference auth_user, notnull=True),
 Field(thing, reference thing, notnull=True),
 Field(unikey, unique=True, notnull=True)
 )

 # this ensure that user cannot like a thing twice
 db.thing_likes.unikey.compute = lambda row: %(user)s-%(thing)s % row


 controllers/default.py

 def show_thing():
  thing_id = request.args(0)
  thing = db.thing[thing_id]
  thing_likes = db(db.thing_likes.id == thing.id).count()
  return dict(thing=thing. thing_likes=thing_likes)

 def like_thing():
 thing_id = request.args(0)
 user_id = auth.user_id
 try:
 db.thing_likes.validate_and_insert(
 thing=int(thing_id),
 user=int(user_id)
 )
 redirect(URL('show_thing', args=thing_id))
 except:
 return thing cannot be liked twice
 

 views/default/show_thing.html

 h1{{=thing.name}}/h1
 a href={{=URL('like_thing', args=thing.id)}} Like this thing /a


 

 To a better implementation you should use an ajax callback to the like 
 button, and also you should check if user already likes to show a different 
 button, maybe unlike.


-- 





Re: [web2py] how to create a counter button...

2013-01-17 Thread Bruno Rocha
Did you read the book?  I recommend chapters 3, 4 and 6

Plus the Ajax one: http://web2py.com/books/default/chapter/29/11

-- 





Re: [web2py] how to create a counter button...

2013-01-17 Thread sasogeek
I did read the book, but only the chapters I needed information from to get 
my app started :) thanks anyway
please look up my app here though if you'd be interested in using it 
http://newupp.tk
what  it's about is briefed here 
https://sasogeek.pythonanywhere.com/newup/default/about (temporary url 
though)

thanks again for your help :)

On Thursday, 17 January 2013 14:42:06 UTC, rochacbruno wrote:


 Did you read the book?  I recommend chapters 3, 4 and 6

 Plus the Ajax one: http://web2py.com/books/default/chapter/29/11


-- 





Re: [web2py] how to create a counter button...

2013-01-16 Thread Bruno Rocha
models/foo.py

# the thing table
db.define_table(thing, Field(name))

# you may want to store likes on another table so you never allow a user to
like the same thing twice
db.define_table(thing_likes,
Field(user, reference auth_user, notnull=True),
Field(thing, reference thing, notnull=True),
Field(unikey, unique=True, notnull=True)
)

# this ensure that user cannot like a thing twice
db.thing_likes.unikey.compute = lambda row: %(user)s-%(thing)s % row


controllers/default.py

def show_thing():
 thing_id = request.args(0)
 thing = db.thing[thing_id]
 thing_likes = db(db.thing_likes.id == thing.id).count()
 return dict(thing=thing. thing_likes=thing_likes)

def like_thing():
thing_id = request.args(0)
user_id = auth.user_id
try:
db.thing_likes.validate_and_insert(
thing=int(thing_id),
user=int(user_id)
)
redirect(URL('show_thing', args=thing_id))
except:
return thing cannot be liked twice


views/default/show_thing.html

h1{{=thing.name}}/h1
a href={{=URL('like_thing', args=thing.id)}} Like this thing /a




To a better implementation you should use an ajax callback to the like
button, and also you should check if user already likes to show a different
button, maybe unlike.

--