I have a post model and a comment model that holds the comments that were made for a particular post.
class Post(models.Model):
body = models.TextField()
user = models.ForeignKey(User)
class Comment(models.Model):
post = models.ForeignKey(Post)
date = models.DateTimeField(auto_now_add=True)
comment = models.TextField()
comment_user = models.ForeignKey(User)
Now I want my Post resource to include URI to all the comments attached to a particular post.
I do know I can use fields.ForeignKey to represent which post my comments belong to, but I want the API to have the URI of all the comments that belong to the post in the post object. I hope that makes sense.
class PostResource(ModelResource):
comments = fields.ToManyField(CommentResource, 'comments')
I had been answered similar question before. Check this link
Related
I'm trying to use the ScanR API:
Technical documentation: https://scanr-api.enseignementsup-recherche.gouv.fr/api/swagger-ui.html
General presentation: https://api.gouv.fr/les-api/scanR
Online interface: https://scanr.enseignementsup-recherche.gouv.fr/
My goal is to give the API a structure/company name, and receive, among others, a structure/company id. Then I am able use the GET endpoint '/v2/structures/structure/{id}' to access the description.
I believe that to do this, I would use the POST endpoint '/v2/structures/search'.
However, I don't manage to structure the query in a way that works.
Can anyone give me an example?
The scanR team kindly provided an example that I share here:
url_structures = "https://scanr-api.enseignementsup-recherche.gouv.fr/api/v2/structures/search"
my_query = "carbon\ waters"
params = {
"pageSize": 12,
"query": my_query
}
scanr_output = requests.post(url_structures, json=params).json()
I have one to many relationship between a Post which has many comments of the domain Comment. In my gsp I´m showing a blog post with it´s comments below, at the end, there is form a user can fill out in order to create a new comment. So I´m passing the params filled in the form to a controller in order to save the new comment, but I´m not sure if I have to do it in the PostController (one side) or in the CommentController (many side). And second how exactly should I save the new comment, I used this, CommentController:
def save() {
def p = new Comment(params)
p.save()
redirect(action: 'blog', controller: 'Post', params: params)
}
Which at the end redirects to the PostController where I render the post view with the all the content including the new comment, PostController
def blog()
{
def post = Post.get(params.id)
def entra = Post.findById(params.id)
[post: post, articulos: entra]
}
" but I´m not sure if I have to do it in the PostController (one side)
or in the CommentController (many side)."
Controllers are not domain objects and are not involved in the relationship between Post and Comment. You can create a controller called PoopyCakaController and do the logic there. It is really irrelevant. Given that info, in my opinion, I guess it depends on whether multiple objects can have comments. If post owns those comments and no other objects have comments, do it in the post controller, otherwise do it in the comment controller.
"And second how exactly should I save the new comment, I used this,
CommentController:"
Is that working for you? If so, that's fine. If it is not working for you, post what the problem is including any errors you are getting.
I have a mongodb document 'Post' which has EmbeddedListField of 'comments'. I'm using tastypie to build the API layer and I want the comments to be listed with their body fields only when a single post is requested. When a list of posts is requested I do not want to show the full comment body as this would kill my app's performance. This is what I have in my resource file:
comments = tastypie_mongoengine_fields.EmbeddedListField(of='api_core.resources.EmbeddedCommentResource', attribute='comments', full=True, null=True)
What can I do about this? I do not want to create two entry points for 'post_entry' and 'post_list' as this would be bad design for the consumer of the APIs.
I did a simple check in the dehydrate method:
def dehydrate(self, bundle):
if self.get_resource_uri(bundle) != bundle.request.path:
bundle.data['comments_count'] = len(bundle.data['comments'])
del bundle.data['comments']
bundle.data['user_id'] = bundle.data['user'].data['id']
bundle.data['user_name'] = bundle.data['user'].data['first_name']
bundle.data['user_uri'] = bundle.data['user'].data['resource_uri']
del bundle.data['user']
return bundle
With the new "Reply" to "Comments" feature on Facebook, I've noticed that replies to comments are treated the same as comments. But I was wondering if there is anyway to distinguish the two?
You first have to enable July Breaking Changes from your app advanced settings
Then use the fields parameter with the comments graph API and include the parent.field(id) column with the and also the filter parameter with the stream value. the final result :
{POST_ID}/comments?filter=stream&fields=parent.fields(id),message,from,likes
this should return both comments and replies with the parent element which has the comment id that the reply belongs to
-- update
and for better array arrangement for replies you can use the following to merge replies with the actual comment array you can include comments.summary(true) in the fields parameter
{POST_ID}/comments?limit=0&filter=toplevel&fields=comments.summary(true),message,from,likes
filter parameter is optional
for more info about the fields : http://developers.facebook.com/docs/reference/api/Comment/
and in case you want to do it in FQL, check this post's comments http://developers.facebook.com/blog/post/2013/04/03/new-apis-for-comment-replies/
Yes. You can query each comment object in the Graph API for the value of its parent field. If the comment in question is a reply, then the value of the parent field will be a reference to the parent comment. Otherwise, no value is returned.
Reference here: https://developers.facebook.com/docs/reference/api/Comment/
You can get comment replies in this way.
/{{POST_ID}}/?fields=comments{comments}&access_token={{ACCESS_TOKEN}}
You can get any sub info(from,id) of comment replies by just nesting fields inside comments like this:
/{{POST_ID}}/?fields=comments{comments,from,id}&access_token={{ACCESS_TOKEN}}
Similar post over here :
https://stackoverflow.com/a/37743410/6001533
If you're listening for comments on the 'feed' webhook, you should check if:
entry[0][changes][0][value][post_id] === entry[0][changes][0][value][parent_id]
This will be true for top-level (new) comments, and false for replies to comments.
To piggy back off #sujit's answer I took his answer and in one call from the feed you can get the entire feed, comments and replies to comments as well as associated images to those comments and replies in one shot.
Here is the code
https://graph.facebook.com/$get_facebook/feed?access_token=$facebook_accesstoken&client_id=$facebook_appid&client_secret=$facebook_appsecret&metadata=1&fields=id,status_type,created_time,from,message,comments{comments{attachment,from,id,message},from,id,message,attachment},picture,link,icon
On Facebook pages, many HTML elements include a 'data-ft' JSON object that is of the form:
data-ft='{
"src":10,
"sty":263,
"actrs":"117307284966434",
"targets":"117307284966434",
"pub_time":1317143005,
"fbid":"153538678072594",
"qid":"5657092603540274768",
"s_obj":5,
"s_edge":1,
"s_prnt":28,
"ft_prefix":"feed_story.top_news",
"ft_story_name":"StreamStoryCreateGeneric_ShareStreamContent_External_Other",
"mf_story_key":"10150331666719785",
"object_id":"153538678072594",
"mf_objid":"153538678072594",
"viewstate_id":"3201743663063655712",
"sub_level":"mid",
"sbj_type":"page",
"is_boulder":"1",
"authentic":1,
"pageid":"117307284966434",
"filter":"h",
"pos":14
}'
What do these keys represent? Some of them are straightforward, such as 'sty' as style, 'actrs' is the Facebook-ID of the original poster, and 'pub_time' is the UNIX epoch representation of the post date & time.
In particular, I am interested in understanding what the 'authentic' key represents, as well as the 'fbid' and 'qid' values.
Thanks for your insight, SO.
This is the root of how BFB (Better Facebook user script) can allow filtering, tabbing, etc.
In the HTML source, there is an attribute on each post that looks like this:
data-ft:{
"src":10, "sty":46, "actrs":"14385334364",
"pub_time":1289830690, "fbid":"1485431831867", "s_obj":11, "s_edge":1,
"s_prnt":11, "pos":1, "sec":"new", "filter":"lf",
"app_id":"201278444497"
}
This is the data we need!
BFB parses this when processing each post and extracts the data.
sty = Story type. Each type of story, like wall posts, status updates, pictures, links, etc has a unique story type with its own number. Unfortunately, these are not documented anywhere! I have to figure out the types by observation and trial and error. It's painful. But knowing this type numbers allows BFB to do filtering based on what kind of story it is.
actrs = The unique Facebook id's of the person (or people) that made the post. Again, good for filtering.
pub_time = The time the post was made. This is useful later...
fbid = The unique Facebook ID of the post. Every post has its own ID. At least, it should. See the explanations below for why this is not as reliable as it sounds
app_id = The unique ID of the Facebook application that made this post