I have a simple Django Rest Framework app running. I want to auto post to app's Facebook page whenever new item is saved in the db. I was wondering if Django management commands is the best solution. The other option is google cloud functions to make a request at specified time and post to Facebook if there are new items (could be expensive and unnecessarily complex).
Any suggestions would be appreciated.
You can listen to your item model's post_save signal and connect it to a handler function which is responsible for posting your saved item data to a Facebook page. Refer to this Django Docs page on how to connect your handler to a signal.
For better performance, posting to a Facebook page can be done asynchronously by leveraging task queue like Celery so it won't affect your REST API response time.
Related
I'm developing an API with Django Rest Framework for handling some subscriptions
I wanted do make the registration in 2 steps.
First, i would register user in my database, after that i want to register user to other service (email marketing, for example).
I have to do it in two steps because the registering of the user in external services may take some time (or the service can be unavailable at the time of request) and i don't want users to wait everything so they can go to other pages.
My idea is: After registering user into my database, i would return http 200_ok to the front-end so user can go to other pages, and in my backend i would use that data to try to register the user in external services as many times as needed (e.g: waiting if other services are unavailable).
But rest_framework only allows me to return Response() (i'm using class-based views), so i only can return http 200_ok, but if i return the function ends and my other functions (that will take more time to run) will end aswell.
Is there a way to do this?
Have a look at https://django-background-tasks.readthedocs.io/en/latest/
This could help solve your background tasks
I am building Django 2.2 +Node + Angular 8 app. Django is used to run a couple of simple scrapers when user clicks on Search btn. I want to make user notified that the scraping process started successfully (if it is the fact) and that the scraping is finished, or that some errors occurred. Some intermediate statuses are also desirable, but not mandatory. I thought about using django.contrib.messages, but not sure how to make my Angular app receive them.
Can anybody advise me with that problem?
P.S.: not sure if it is important - I want to use Angular's snackbar to make user notified about scraping statuses.
there is only one common way to push messages from Django to angular (from server -> to client), is to create a web-socket.
Check out this tutorial to create a web-socket that initiate a connexion between Angular and Django, and then pushing random notificatons from Django to Angular and finally closing the websocket.
In your case :
Open Web Socket when starting the task
Sending notifications about the task (DJANGO -> NG)
Sending Final Notification and Closing Web Socket
For other ideas checkout this medium: Do you really need websockets ?
I have just integrated Stripe Checkout with my Django app. However, from their code:
session = stripe.checkout.Session.create(
customer_email=customer.email,
payment_method_types=['card'],
line_items=line_items,
mode='payment',
success_url='http://127.0.0.1:8000/SUCCESS/?session_id={CHECKOUT_SESSION_ID}',
cancel_url='http://127.0.0.1:8000/cart'
)
It redirects to a Success_url. I would like to display their order info and send an email from the success page but currently, everyone can visit(would cause random emails etc). Is there a way i can limit this for the person that just checked out? Thank you!
The success page should really just be a page to let your users know that their payment went through. I wouldn't personally recommend triggering any app-specific logic when your users land on it. As you've pointed out this could cause accidents with users randomly stumbling on that page and triggering emails. It also offers an opportunity for bad actors to exploit your application.
Instead, I would create a webhook endpoint and listen for the checkout.session.completed event, and trigger your emails from there. Building a webhook endpoint should be no more difficult than building any other endpoint on your server, with there only being some small extra logic to verify incoming requests.
Another option would be to use a tool like Zapier to listen for these events from Stripe and trigger various flows from them: https://stripe.com/partners/zapier
I have an application that uses Python appengine, there is a service that updates the status of users, if an admin person has a page open, I would need it to update in real time. I know that appengine has CRON and task queues, what would be the correct way to handle this? Should I set an update flag in the models that that triggers jscript?
The Channel API can be used to send real-time(ish) data to clients, without the need of clients polling the server.
I have created Django's registration process using django.contrib.auth and have also set up user profiles. Now I want to add an option to fetch the profile's information from third-party sites like Facebook, Twitter, and LinkedIn. I have found django-socialregistration, but I don't know how to override the default setup view. What is the best and easy solution for the task?
Here's the basic idea of the setup that I use, although it's only for Facebook:
Integrate the Facebook JavaScript SDK
Bind a JavaScript function which calls FB.login to a "login with Facebook" button, request all the permissions that you need for that user
In the function(response) section of the FB.login call, handle the returned response, then do an Ajax POST to a Django view (I use jQuery for this) which takes the response, parses it to retrieve the access token for the user and store it in the user profile object for that user.
You can then use this access_token to make Facebook Graph API requests on behalf of that user server-side, or if you want, you can actually just use the JavaScript SDK to make all your calls entirely on the client-side with lots of Ajax to interact with the Django backend.
I find this to be a much easier solution than trying to use one of the Django packages, because most of them are out of date.