1

I have created a Django app with the usual components: applications, models views, templates, etc.

The architecture of a Django app is such that it basically just sits there and does nothing until you call one of the views by hitting a REST endpoint. Then it serves a page (or in my case some JSON) and waits for the next REST request.

I would like to insert into this app some automated tweeting. For this purpose I will be using the python-twitter library. My tweets will contain a URL. Twitter's website says that any URLs inserted into tweets get shortened to 23 characters by Twitter itself. So the remaining characters are available for the non-URL portion of the tweet. But the 23-character size may change. So Twitter recommends checking the current size of shortened URLs when the application is loaded but no more than once daily. This is how I can check the current shortened-URL size using python-twitter:

>>> import twitter

>>> twitter_keys = {
    "CONSUMER_KEY": "BlahBlahBlah1",
    "CONSUMER_SECRET": "BlahBlahBlah2",
    "ACCESS_TOKEN_KEY": "BlahBlahBlah3",
    "ACCESS_TOKEN_SECRET": "BlahBlahBlah4",
}

>>> api = twitter.Api(
    consumer_key=twitter_keys['CONSUMER_KEY'],
    consumer_secret=twitter_keys['CONSUMER_SECRET'],
    access_token_key=twitter_keys['ACCESS_TOKEN_KEY'],
    access_token_secret=twitter_keys['ACCESS_TOKEN_SECRET'],
)

>>> api.GetShortUrlLength()
23

Where and how should I save this value 23 such that it is retrieved from Twitter only once at the start of the application, but available to my Django models all the time during the execution of my application? Should I put it in the settings.py file? Or somewhere else? Please include a code sample if necessary to make it absolutely clear and explicit.

Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
  • One way to address this would be to have a dedicated cache key to store this value, and access this key when necessary. You can have a cron job /management command that updates this value once a day. – karthikr Jun 07 '16 at 04:10
  • Hello Saqib, was going through my old answers when I came across this. What did you end up doing here? – e4c5 Jul 31 '16 at 14:47

1 Answers1

1

Lot's of different ways and it's primarily a matter of opinion. The simplest of course would be to keep that data in the source file for the module that connects to twitter. Which looks like your existing system. Which works fine as long as this is not an app that's being commited to a public VCS repository.

If the code get's into a public repository you have two choices. Use an 'app_settings' file or save it in the db. Both approaches are described here: https://stackoverflow.com/a/37266007/267540

Community
  • 1
  • 1
e4c5
  • 52,766
  • 11
  • 101
  • 134