Re: [web2py] Re: Best Method to implement a like button

2013-03-05 Thread Jaime Sempere
A little correction for any future visitor:

compute= lambda row: %(username)s-%(songname)s-%(playlist_name)s),
should be:
compute= lambda row: %(username)s-%(songname)s-%(playlist_name)s %row),

Anyway the solution is great. Thanks a lot.

El miércoles, 12 de septiembre de 2012 00:54:45 UTC+2, rochacbruno escribió:

 db.define_table('likes',
 Field('username', 'reference auth_user'),

 Field('songname', 'reference songs'),
 Field('playlist_name', 'reference playlist'),
 Field('unique_key', unique=True, notnull=True,  compute= lambda row: 
 %(username)s-%(songname)s-%(playlist_name)s),
  
 )

 Now on any attempt to insert duplicates DAL will have an exception, 
 because unique_key is unique and the computation will try to add same 
 values.

 try:
 db.likes.insert(..)
 except:
 #YOU ALREADY LIKE THIS


 *Bruno Cezar Rocha** - @rochacbruno*
 rocha...@gmail.com javascript: | Mobile: +55 (11) 99210-8821
 www.CursoDePython.com.br | www.rochacbruno.com.br
 Blog: Using Python to get all the external links from a 
 webpagehttp://rochacbruno.com.br/using-python-to-get-all-the-external-links-from-a-webpage/
   Get a signature like this. 
 http://r1.wisestamp.com/r/landing?promo=18dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_18
  Click 
 here.http://r1.wisestamp.com/r/landing?promo=18dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_18






-- 

--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [web2py] Re: Best Method to implement a like button

2012-09-18 Thread Mark Li
Thanks for the response guys! I'm going with rochabruno's method as it is 
the most straightfoward, I hadn't really looked into the compute 
capabilities.

On Tuesday, September 11, 2012 3:55:21 PM UTC-7, rochacbruno wrote:

 The computation should be

 compute= lambda row: %(username)s-%(songname)s-%(playlist_name)s % row


-- 





[web2py] Re: Best Method to implement a like button

2012-09-11 Thread villas
How about:  in 'likes' make a unique composite index of both fields 
(created at DB level).  Try to insert a record.  If it fails (because of 
the index) then delete instead.


On Monday, September 10, 2012 11:58:03 PM UTC+1, Mark Li wrote:

 I have 3 datatables as follows:


 db.define_table('songs',
 Field('songname'),
 format='%(songname)s')

 db.define_table('likes',
 Field('username', 'reference auth_user'),
 Field('songname', 'reference songs'))

 with the 3rd table being db.auth_user

 I would like to implement a like button, where clicking it adds an entry 
 to the intermediate table 'likes'. For example, if User1 likes Song1, it 
 would perform the following:
 db.likes.insert(username=auth.user.id, songname=1)



 Right now I have the like button as the following in the view: 
 {{=A('like me', callback=URL('add_like'))}}

 and the function as:
 def add_like():
 db.likes.insert(username=auth.user.id, songname=1)

 The problem I'm having is that a user should only be allowed to 'like' a 
 song once; if the user has already liked the song, clicking on 'like' again 
 should remove that like. Basically a toggle functionality for the 'like' 
 button.

 I'm not sure how to implement the last part, as it seems checking the 
 entire database for the existence of that record on every 'like' click is 
 overkill. Is there an eloquent way (minimal database calls) to go about 
 implementing this toggle functionality for a 'like' button?







-- 





[web2py] Re: Best Method to implement a like button

2012-09-11 Thread Mark Li
I went with the unique composite field index method (unique across 3 
fields, not just 2), with my database as follows:

db.define_table('likes',
Field('username', 'reference auth_user'),
Field('songname', 'reference songs'),
Field('playlist_name', 'reference playlist'))

if auth.is_logged_in():
already_liked = db((db.likes.username==auth.user.id)  (db.likes.
playlist_name==request.vars.playlist_name)) 

db.likes.songname.requires=IS_NOT_IN_DB(already_liked, 'likes.songname')


I also changed my add_like() controller function to the following:


def add_like():


ret = db.likes.validate_and_insert(username=auth.user.id, playlist_name 
= request.vars.playlist_name, songname = request.vars.songname)

if ret.errors:

already_liked = (db.likes.username==auth.user.id)  (db.likes.
playlist_name==request.vars.playlist_name)  (db.likes.songname==request.
vars.songname)
db(already_liked).delete()

return like removed

else:
return like added

I used the web2py ajax function to call the add_like action. The whole 
process seems somewhat sluggish, are there any unnecessary database calls 
I'm making?

On Tuesday, September 11, 2012 4:53:44 AM UTC-7, villas wrote:

 How about:  in 'likes' make a unique composite index of both fields 
 (created at DB level).  Try to insert a record.  If it fails (because of 
 the index) then delete instead.


 On Monday, September 10, 2012 11:58:03 PM UTC+1, Mark Li wrote:

 I have 3 datatables as follows:


 db.define_table('songs',
 Field('songname'),
 format='%(songname)s')

 db.define_table('likes',
 Field('username', 'reference auth_user'),
 Field('songname', 'reference songs'))

 with the 3rd table being db.auth_user

 I would like to implement a like button, where clicking it adds an 
 entry to the intermediate table 'likes'. For example, if User1 likes Song1, 
 it would perform the following:
 db.likes.insert(username=auth.user.id, songname=1)



 Right now I have the like button as the following in the view: 
 {{=A('like me', callback=URL('add_like'))}}

 and the function as:
 def add_like():
 db.likes.insert(username=auth.user.id, songname=1)

 The problem I'm having is that a user should only be allowed to 'like' a 
 song once; if the user has already liked the song, clicking on 'like' again 
 should remove that like. Basically a toggle functionality for the 'like' 
 button.

 I'm not sure how to implement the last part, as it seems checking the 
 entire database for the existence of that record on every 'like' click is 
 overkill. Is there an eloquent way (minimal database calls) to go about 
 implementing this toggle functionality for a 'like' button?







-- 





[web2py] Re: Best Method to implement a like button

2012-09-11 Thread villas
Depends how you are displaying your list to do the liking and unliking.
A few thoughts
Make sure your composite index has the field which filters out the most 
records first.
In your controller you may be able to cut out most queries by considering 
something like this:

   - Hit the DB once and make a dict of all the listed songs with a flag 
   whether liked or not.
   - Refer to the list when displaying the 'like' 'unlike' labels for each 
   song.
   - Dispense with things like this db.likes.songname.requires=IS_NOT_IN_DB 
   etc
   

On Tuesday, September 11, 2012 10:06:54 PM UTC+1, Mark Li wrote:

 I went with the unique composite field index method (unique across 3 
 fields, not just 2), with my database as follows:

 db.define_table('likes',
 Field('username', 'reference auth_user'),
 Field('songname', 'reference songs'),
 Field('playlist_name', 'reference playlist'))

 if auth.is_logged_in():
 already_liked = db((db.likes.username==auth.user.id)  (db.likes.
 playlist_name==request.vars.playlist_name)) 

 db.likes.songname.requires=IS_NOT_IN_DB(already_liked, 
 'likes.songname')


 I also changed my add_like() controller function to the following:


 def add_like():


 ret = db.likes.validate_and_insert(username=auth.user.id,playlist_name 
 = request.vars.playlist_name, songname = request.vars.songname)
 
 if ret.errors:

 already_liked = (db.likes.username==auth.user.id)  (db.likes.
 playlist_name==request.vars.playlist_name)  (db.likes.songname==request.
 vars.songname)
 db(already_liked).delete()

 return like removed

 else:
 return like added

 I used the web2py ajax function to call the add_like action. The whole 
 process seems somewhat sluggish, are there any unnecessary database calls 
 I'm making?

 On Tuesday, September 11, 2012 4:53:44 AM UTC-7, villas wrote:

 How about:  in 'likes' make a unique composite index of both fields 
 (created at DB level).  Try to insert a record.  If it fails (because of 
 the index) then delete instead.


 On Monday, September 10, 2012 11:58:03 PM UTC+1, Mark Li wrote:

 I have 3 datatables as follows:


 db.define_table('songs',
 Field('songname'),
 format='%(songname)s')

 db.define_table('likes',
 Field('username', 'reference auth_user'),
 Field('songname', 'reference songs'))

 with the 3rd table being db.auth_user

 I would like to implement a like button, where clicking it adds an 
 entry to the intermediate table 'likes'. For example, if User1 likes Song1, 
 it would perform the following:
 db.likes.insert(username=auth.user.id, songname=1)



 Right now I have the like button as the following in the view: 
 {{=A('like me', callback=URL('add_like'))}}

 and the function as:
 def add_like():
 db.likes.insert(username=auth.user.id, songname=1)

 The problem I'm having is that a user should only be allowed to 'like' a 
 song once; if the user has already liked the song, clicking on 'like' again 
 should remove that like. Basically a toggle functionality for the 'like' 
 button.

 I'm not sure how to implement the last part, as it seems checking the 
 entire database for the existence of that record on every 'like' click is 
 overkill. Is there an eloquent way (minimal database calls) to go about 
 implementing this toggle functionality for a 'like' button?







-- 





Re: [web2py] Re: Best Method to implement a like button

2012-09-11 Thread Bruno Rocha
db.define_table('likes',
Field('username', 'reference auth_user'),

Field('songname', 'reference songs'),
Field('playlist_name', 'reference playlist'),
Field('unique_key', unique=True, notnull=True,  compute= lambda row:
%(username)s-%(songname)s-%(playlist_name)s),

)

Now on any attempt to insert duplicates DAL will have an exception, because
unique_key is unique and the computation will try to add same values.

try:
db.likes.insert(..)
except:
#YOU ALREADY LIKE THIS


*Bruno Cezar Rocha** - @rochacbruno*
rochacbr...@gmail.com | Mobile: +55 (11) 99210-8821
www.CursoDePython.com.br | www.rochacbruno.com.br
Blog: Using Python to get all the external links from a
webpagehttp://rochacbruno.com.br/using-python-to-get-all-the-external-links-from-a-webpage/
  Get a signature like this.
http://r1.wisestamp.com/r/landing?promo=18dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_18
Click
here.http://r1.wisestamp.com/r/landing?promo=18dest=http%3A%2F%2Fwww.wisestamp.com%2Femail-install%3Futm_source%3Dextension%26utm_medium%3Demail%26utm_campaign%3Dpromo_18

-- 





Re: [web2py] Re: Best Method to implement a like button

2012-09-11 Thread Bruno Rocha
The computation should be

compute= lambda row: %(username)s-%(songname)s-%(playlist_name)s % row

--