How to automatically save the headline for every new article provided my 3rd party js script is embed on a website
Asked Answered
A

1

10

I am working on a something similar to Disqus, and I created a third-party javascript snippet which the user will embed in the website and have a rating widget for each article. Users can rate the article using the widget. Everything is working, the server is making the request, but I am making the article object instance explicitly.

I need to automate this, like for a new article on the website, checking the request is coming from the authenticated website and create a new rating widget in the Database in Django and Django-rest-framework.

I am using Python 2.7.

Question: How do I automatically save the headline of the new article, if its new and authenticated in the database?

I know that I need to use a model to implement this, but I am unsure how to do the actual implementation.

EDIT:

Let's say this is the query

https://example.com/embed/comments/?base=default&version=edb619270a92c3035c453faa7c9444d1&f=example&t_i=article_2431522&t_u=http%3A%2F%2Fwww.firstpost.com%2Fbollywood%2Flatest-trailer-of-spectre-is-out-james-bond-is-back-all-guns-and-cars-blazing-2431522.html%09&t_e=Latest%20trailer%20of%20%27Spectre%27%20is%20out%3A%20James%20Bond%20is%20back%20all%20guns%20and%20cars%20blazing&t_d=Latest%20trailer%20of%20%27Spectre%27%20is%20out%3A%20James%20Bond%20is%20back%20all%20guns%20and%20cars%20blazing&t_t=Latest%20trailer%20of%20%27Spectre%27%20is%20out%3A%20James%20Bond%20is%20back%20all%20guns%20and%20cars%20blazing&s_o=default

In my model I need save the following, like f to forum (where forum=models.CharField("short name", max_length=30, unique=True)

I know I need to parse the url for every &, but don't know how. I checked the documentation of rest-framework, but I didn't get the gist of it.

    `f ---->forum,
    t_i----> identifier,
    t_u----> url 
    t_s----> slug,
    t_e----> title,
    t_d----> documentTitle,
    t_t----> title || documentTitle,
    t_c ---->category,
    s_o----> sortOrder,
    l----> language`

What's the best practice to save? Hope this helps

Alcaeus answered 13/9, 2015 at 11:2 Comment(3)
this is a pretty open-ended question. any way you can elaborate and make it more specific?Incrust
You'll need to look into CSRF protection if you're embedding it in a site of a different domain. As far as creating an instance of the model, you do an AJAX POST ( assuming you're using Django Rest Framework) to your server from the javascript widget whenever appropriate. This will create a new instance of whatever model you're needing to create.Allonym
@Incrust I've added specificsAlcaeus
J
4

I'm going to just answer the question you stated at the end: "How do I automatically save the headline of the new article"

You're right, you'll need to create an Article model that mirrors the 3rd party site's articles.

It'll need to have a field for the title/headline (probably CharField), make sure you make it big enough and/or deal with cases where the title is bigger.

You'll also need a unique ID for each article. Ideally, rather than using Django's default, you'll use whatever the 3rd party site is using as the unique ID as a One to One mapping.

Then whenever a request comes in you can use the get_or_create method to ensure the article exists in your DB.

Jaclyn answered 14/9, 2015 at 7:30 Comment(3)
I already figured everything except your last sentence. That's exactly is my question.Could you please elaborate on the last sentence?I don't want code. Do I need to update serializers.py? Thank you in advance, KiwiAlcaeus
Its part of the querysets API docs.djangoproject.com/en/1.8/ref/models/querysets/…Jaclyn
It's not called by default by the viewset nor by the api. In this case I would say that you need to overwrite the get_object method of the view/viewset and here to perform retrieve or create.Hammack

© 2022 - 2024 — McMap. All rights reserved.