Django: sending email x days later - django

In my Django project, users are allowed to register to a free trial, but if they do not complete a purchase within 15 days, their accounts are locked out until they do complete the purchase. After 13 days (ie within 48 hours or expiry) I wish to send an email the registered user reminding him/her to purchase.
Currently, I have a cron job set up to run daily and check all trial accounts if the registration date and current date are 2 days apart and if so, I send an email.
I was wondering if there is a more elegant solution to do this?

If you don't want to mess with your cron file you should check out Celery, an asynchronous task queue written in Python. It was originally created with Django in mind but has since been broken out into a separate package. What you want to do then is set up a Celerybeat schedule like this:
CELERYBEAT_SCHEDULE = {
"purchase-reminder": {
"task": "accounts.tasks.remind",
"schedule": timedelta(hours=24),
},
}
This will call the task (read: function) accounts.tasks.remind every 24 hours.

Related

Parallel Multi-instance is getting created user tasks with last employee details

I am developing a scenario on Camunda bpm Modeler 5 and bpm run 7.17.
Scenario:
using a service task to invoke an external rest-api using http-connector and that external rest api sends response as follows with multiple employees info in json format.
[
{"regId":"0XFY1FX00W","fname":"abc","lname":"def","email":"abc.def#gmail.com"},
{"regId":"0XFY1F000X","fname":"ghi","lname":"jklm","email":"ghi.jklm#ymail.com"},
{"regId":"0XFY1F000Y","fname":"nop","lname":"qrs","email":"nop.qrs#xmail.com"},
{"regId":"0XFY1F000Z","fname":"tuv","lname":"wxyz","email":"tuv.wxyz#zmail.com"}
]
I am trying to create 4 user tasks with those 4 employees and those user tasks should be populated with those employee first name and last names.
I have used parallel multi-instance and I succeeded with that 4 user tasks creation but all the user tasks contain last employee details i.e.
"fname":"tuv","lname":"wxyz".
Please let me know how to achieve that i.e. the user tasks should contain 4 different employee details?
Note: I have tested with sequence multi-task and it is working as expected and My requirement is to use user task and not any subprocess.

Amazon QuickSight embedded dashboard - how to cache user session in my webapp (billing and timing concern)

I have embedded Amazon QuickSight dashboard in my web application by using amazon-quicksight-embedding-sdk (followed https://learnquicksight.workshop.aws/en/dashboard-embedding.html).
The user session seems to last many hours as mentioned in https://docs.aws.amazon.com/quicksight/latest/APIReference/API_GetDashboardEmbedUrl.html
When I requested the embed URL directly from my web browser, I could see that it was valid for many hours.
But my web app will request a new embed URL when user restarts it (by closing/reopening tab/browser). Does that mean a new user session was created and billed.
Is it possible to store the embed URL and to reuse it (as long as the user session lasts) for the case the same user closes the tab/browser and open the web app and the dashboard again (of course in the same browser)?
I tried to store the embedURL as a cookies named "embed_url". But calling amazon-quicksight-embedding-sdk.embedDashboard({url: embed_url}) resulted in
"Embedding failed because of invalid URL or authorization code. Both
of these must be valid and the authorization code must not be expired
for embedding to work."
I was sure the embed_url was still valid because requesting it by the browser directly worked.
Which "authorization code" is mentioned in the above error message? What did I miss or is it actually not possible?
Beside the billing concern, I've noticed that the call to get the embedURL took time (more than 5 seconds, eu-central-1) while the embedding took less (3 seconds). I thought I could improve the dashboard loading time by reusing the gotten embedURL. Any comments about the timing? Is it normal or did I do something wrong so that it was so slow? My test dashboard has only 1 diagram with unchanged dataset.
As per the Quicksight Pricing Page, if you're creating an embedded dashboard for a Quicksight "Reader", then you're paying $0.30/session per 30-minute logged-in-session for this Reader.
The validity of the session can be set in the SessionLifetimeInMinutes parameter of the GetDashboardEmbedUrl API, and has an upper bound of 600 minutes (10 hours).
As an example, suppose you set SessionLifetimeInMinutes to 600 mins for your Reader user. Also suppose that this user stayed logged in and uses the dashboard for 10 hours continuously, then that would equate to 20 sessions of usage (since the billing is in increments of 30-min chunks). At first glance it would seem that this would cause $0.30/session * 20 session-chunks = $6 to be billed.
However, as per the pricing page, there is an upper bound of $5.00 per month for every Reader. Which means that this Reader can never exceed $5 per month regardless of how many Quicksight sessions (of whatever duration) are created for them. So no matter how many times you call the GetDashboardEmbedUrl API for a given Reader, you're capped to $5/month for this user.
Also of use as to what constitutes a Reader session (from the pricing page):
When does a Reader Session start and end?
A Reader Session starts with user-initiated action (e.g., login, dashboard load, page refresh, drill-down or filtering) and runs for next 30-minutes.
Keeping Amazon QuickSight open in a background browser window/tab does not result in active sessions until the Reader initiates action on page.
But my web app will request a new embed URL when user restarts it (by closing/reopening tab/browser). Does that mean a new user session was created and billed.
I'm not 100% sure about this, but yes I believe a refresh (or open/close) of the tab results in a new session for the same user.
A Reader Session starts with user-initiated action (e.g., login, dashboard load, page refresh, drill-down or filtering) and runs for next 30-minutes.
The above excerpt is from the pricing page. So it does seem that page refresh (and thus another call to GetDashboardEmbedUrl) will trigger a new session for the user.
Which "authorization code" is mentioned in the above error message?
The GetEmbedDashboardUrl API response is a JSON object that looks like this:
{
"Status": 200,
"EmbedUrl": "https://us-east-1.quicksight.aws.amazon.com/embed/f4147cd0d4d_BLAH_BLAH_...",
"RequestId": "c15a7bad-629e-444a-b643-ff3142c9ae41"
}
If you look closer at the EmbedUrl, apart from the dashboard url itself, there are also these query-string parameters:
isauthcode
code
identityprovider
statePersistenceEnabled
potentially: other params too
The code parameter (embedded within the embedUrl) is the "authorization code" that you asked about.
Is it possible to store the embed URL and to reuse it (as long as the user session lasts) for the case the same user closes the tab/browser and open the web app and the dashboard again (of course in the same browser)?
No, that can't be done. As it says in the link you shared:
The following rules apply to the combination of URL and authorization code:
- They must be used together.
- They can be used one time only.
- They are valid for 5 minutes after you run this command.
So the embedURL and its associated auth code can only be used once together. Makes sense since this will prevent MITM replay attacks among other scenarios. Also I actually tried to cache the response and then re-use the embedUrl in case of a cache-hit, since this would improve the end-user experience. But this didn't work - a "replay" of the embedUrl is blocked by QuickSight, as mentioned in their doc.
Any comments about the timing?
This has been our experience also. The GetDashboardEmbedUrl REST API takes around 5-7 seconds (us-east-1) for our app and then the actual embedding takes another 3-5 seconds. Not great, but I don't see a way around this poor user experience as of now.

Django cron-job for user-specified times

I am wanting to send notifications based on a user-specified time. IE, in google calendar, I can receive a text message when my task time is hit.
Is the solution to this to run a cron job, have it execute every minute and scan which users have a time equaling the current time?
Since you tagged your question with celery, I assume you have celery running. You could use the eta kwarg to apply_async() to schedule a task to run at a specific time, see here:
http://docs.celeryproject.org/en/latest/userguide/calling.html#eta-and-countdown
If you need to use a cron job, I would not check if notification_time == current_time, but rather track unsent notifications with a boolean is_sent field on the model and check for notification_time <= current_time and not is_sent. This seems to be slightly less error prone. You could also add some form of check to prevent mass-sending notifications in case your system goes down for a few hours.

how do i update my db data(mongodb) after fixed interval of time using django?

My User collection contains data such as
{"user1":"zera",
"my_status":"active",
"date_creation" : ISODate("2013-10-01T10:15:52.055Z")
}
{"user2":"dfgf",
"my_status":"noactive",
"date_creation": ISODate("2013-10-01T08:55:41.212Z")
}
I need to find each user with my_status :"active" and update their my_status after 24 hours from each user's date_creation.
Can anyone suggest a method to do it using django?
Well, I'd write an async task to keep polling the database to check for users with active status. If the user is active, update their status.
For the asynchronous tasks, you can use python-rq but to make things easier there's a django module for python-rq, it's django-rq. Also, Celery is another popular and good option. There's also a module for Django, you can find it here.

can i get real time status updates using the Graph Real Time API

I have an app that currently that i have to cron to run that gets a bunch of user status updates using https://graph.facebook.com/the-selected-user/statuses?access_token
and this works fine. I run a cron once an hour. I would like to use the Real-time Updates, but not sure how to do that. I have the example subscription working, but can not make the connection on how to subscribe to what to get the users status updates
Thanks for any help
Since you have subscriptions working (the hard part), just add a subscription for topic: user and field: feed.
From the API documentation:
way you subscribe to a user's feed - the subscription topic should be
'user' and the subscription field should be 'feed'