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();



<URL:http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#count--isempty--delete--update>

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

<URL:http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#One-to-many-relation>


> 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
<URL:http://web2py.com/books/default/chapter/29/09/access-control#Decorators>
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
 

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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/d/optout.

Reply via email to