Help at counting comments of a post and restrict access to certain categories

16 views
Skip to first unread message

N. Fischer

unread,
Jan 16, 2017, 11:01:56 PM1/16/17
to web2py-users
Hi all,
I am currently working with a friend on a web2py project. This is our first time, we do anything with web2py and we managed to build the reddit clone and do little improvements, Massimo Di Pierro did in his video tutorials, thank you at this point for your great videos.
Now we want to add some other features and have some problems to implement 2 specific things.

1. We want to display how many comments a post has in the overview of the category. But we dont really know how we can access the comments of a post and count them.

in our database we have the both tables, post and comment. comment has a reference to the post it is connected to. 

2. We have a category called "Updates" and we want that only users in the group "admin" can create new posts. Other users shall only be able to comment on these posts.

@auth.requires_login()
def create_post():
        category = get_category()
        db.post.category.default = category.id
        form = SQLFORM(db.post).process(next='view_post/[id]');
        return locals()

@auth.requires_membership("admin")

We tried to use an if-statement that it requires an admin membership.

Hopefully, there is anyone out there who can help. Thank you for reading this.

Best regards
N. Fischer

Dave S

unread,
Jan 17, 2017, 3:27:16 AM1/17/17
to web2py-users


On Monday, January 16, 2017 at 8:01:56 PM UTC-8, N. Fischer wrote:
Hi all,
I am currently working with a friend on a web2py project. This is our first time, we do anything with web2py and we managed to build the reddit clone and do little improvements, Massimo Di Pierro did in his video tutorials, thank you at this point for your great videos.
Now we want to add some other features and have some problems to implement 2 specific things.

1. We want to display how many comments a post has in the overview of the category. But we dont really know how we can access the comments of a post and count them.

in our database we have the both tables, post and comment. comment has a reference to the post it is connected to. 

For any one post, you'd take its ID and use that in the select statement for the comments. 
Something like
post_id = something-previously-determined
query
= db(db.comment.post == post_id)



and then use the count() function to get the count.
com_count = query.count();




 If you want the counts for ALL posts, you may be doing a join to be able to use all post ids.



2. We have a category called "Updates" and we want that only users in the group "admin" can create new posts. Other users shall only be able to comment on these posts.

@auth.requires_login()
def create_post():
        category = get_category()
        db.post.category.default = category.id
        form = SQLFORM(db.post).process(next='view_post/[id]');
        return locals()

@auth.requires_membership("admin")

We tried to use an if-statement that it requires an admin membership.


I'd give a shot at moving the "requires_membership" line to right after the "requires_login" line,
so that both auth wrappers are decorating the create_post() procedure
 
Actually, looking at line 23 of
it appears you want to use one decorator (.requires) with both conditions in it (requires_login, requires_membership("admin").


Hopefully, there is anyone out there who can help. Thank you for reading this.

Best regards
N. Fischer

Good luck!

/dps
 
Reply all
Reply to author
Forward
0 new messages