Text

My Current Zsh Theme

So, recently I began using the Zsh for the first time and surprisingly the switch from Bash to the Zsh has been pretty smooth. I discovered an awesome community driven framework, oh-my-zsh, that helps in managing your zsh configurations. I was playing with one particular theme called pygmalion and it looked pretty neat. It utilized the git features when you add git as one of the plugins.

db_screenshot

If the git project, is dirty, it adds a lightning icon to notify you.

db_screenshot2

Nowadays, for projects, i am forced to use svn, so thought it would be cool to add something similar for svn.

Apparently, if you add svn as one of the plugins in your .zshrc, you can then add the following to your theme file and get something similar for svn projects.

BATTERY PERCENT IN PROMPT:

For people who mostly work in terminals, and on battery power, I thought it would be useful to show the battery percentage in your prompt. So, i added some code in the theme that would display the battery percentage in the prompt, only if the laptop is running on battery power. Also, made it so that battery percentage changes from green to yellow to red depending on the battery left.

Battery Percentage shows green when 60% <battery life < 100%

Battery Percentage shows yellow when 15 % <battery life < 60% 

Battery Percentage shows red when battery life < 15 %.

The battery status code is show below. Basically I am just getting the battery percent and battery status. Battery status can be, Charging, Discharging etc. Depending on the percent i specify the color for batt:${BATTPRCNT}. And i only show the battery percent if the battery is discharging. I did not see the use of showing the battery percent in the prompt if the laptop was charged, but you can change it according to your needs.

If you want to try using this theme, just fork https://github.com/milin/oh-my-zsh. and copy the theme milind.zsh-theme into your ~/.oh-my-zsh theme folder.

Link

instagram-engineering:

One of the questions we always get asked at meet-ups and conversations with other engineers is, “what’s your stack?” We thought it would be fun to give a sense of all the systems that power Instagram, at a high-level; you can look forward to more in-depth descriptions of some of these systems in…

Text

This

Stand alone, make your way

Through these dead carcasses.

There’s a fog that’s coming to get

You and you fall awake. 

You’re back into this..face your demons now

Mara laughs at your futile tries

You give in to your other side.

Craving this and wanting that

you quiver like fish out of water

and you take a step back into this 

Face your demons now 

You’re back into this..face your demons now 

Tags: rants poem song
Text

Postgres ident error

Today I had this particular error. psql: FATAL:  Ident authentication failed for user “postgres”.

When i tried to run django’s sqlcreatedb | psql -U postgres.

To solve this i had to go to the pg_hba.conf file located in postgres directory and put trust instead of ident in the file.

local   all         postgres                          trust

local   all         postgres                          trust

Text

Resizing a column in Postgres.

I was using hakanw’s django-email-usernames, to enable users to login with their email addresses as well. It was working great but it had only one issue. Django sets the character limit to 30 characters or less, for usernames. django-email-usernames does give the users the option to extend the username column when doing a fresh syncdb. But their sql command was not working for me for some reason. I think its because i was using postgres. 

So i had to manually log into postgres and resize the column. Here are some of my notes on how i did it.

If you issue the following query,

SELECT atttypmod FROM pg_attribute

WHERE attrelid = ‘auth_user’::regclass

AND attname = ‘username’;

It will give you the size of the column username. When i issued the command it gave me the output of 34, which means the size of the column is 30 since 4 is added for legacy reasons. To resize the username to 75, we use the following command.

UPDATE pg_attribute SET atttypmod = 75+4

WHERE attrelid = ‘auth_user’::regclass

AND attname = ‘username’;

 Another method as mentioned by my friend, @too_many_bryans, which also works just as fine is.

ALTER TABLE auth_user ALTER COLUMN username TYPE varchar(75)




Text

PostGIS Error when saving coordinates in django-admin

I was having this error “An Error while transforming the geometry to the SRID of geometry form field”, everytime i saved a Location’s coordinates using the django-admin. I was using PostGIS and postgres and I had already followed django’s awesome installations instruction on installing geodjango. I also had another instance of the server running with the same configurations and it was not having this particular issue. 

Initially I thought i had not properly installed the PROJ library, since geodjango was having difficulties reprojecting my geometry. After a couple of searches, I found that the issue was that the PROJ version compatible with ubuntu 10.04, the one i am using, is 4.6.0, and that particular  package does not come with the datum shifting files installed. The django docs say “that this particular version will cause problems with the geographic admin because null datum grid is not available for transforming geomotries to the spherical mercator projection.” The recommended solution was to download the datum shifting files yourself ,create the grid file and install it yourself.

After following these steps, I was successful in resolving the error.

Text

Django pop-up Multiple Checkbox form using colorbox

I recently had the chance to play with colorbox, which is a lightweight customizable lightbox plugin for jQuery. I needed my django form to be a pop up and I did not want to change a lot of stuff I had already written. Here are some of my notes on how i got it to work .

My form was working perfectly, all i needed to do now was to make it a pop up and handle all the errors and success page in the pop up itself. Thankfully colorbox can be extended with callbacks and event-hooks without altering the source files. Also, its appearance is controlled through CSS so users can restyle the box.

All I needed to do was write some very basic html for the form.

 In the form’s view you do not need to pass another instance of the form to catch the errors as well. All you need to do is write some custom code for when the form is saved successfully and when it has errors. Here I am using my custom JsonResponse to render into string an html file that contains all the results when the form is succesfully saved. After the form is saved it will send a html/text with the custom email contents to the pop up form. Similary when the form has errors, I am using  the output from JsonResponse  to catch the errors and am sending to the pop-up form in the same way.

In your template, you basically do callback and saying that if the form saved succesfully, send the success response as dictated by the view.

For some reason, colorbox did not like that I was using the CheckboxSelectMultiple widget, and when i tried to render the form in the template as {{ form }}, it would not output anything. To go around this issue, I tried to solve it manually. I looped through the field id and field label and setup the input type to what i wanted, and it was all good. {% for username_id, username_label in iform.username.field.choices %}   {{username_label}} {% endfor %}

Text

South Migration Issue. Relation already exists Error.

I was having this issue today where I was trying to migrate my database changes but it kept giving me this error DatabaseError: column ‘Enter Column Name Here ‘of relation ‘Enter Relation Name here’  already exists. Everytime I did ./manage.py migrate app it would give me the same error. I tried doing ./manage.py  migrate —list to see the list of migrations and it showed me that i had two migrations both starting with 0005.

(*) 0005_auto__add_field_listing_search_address

(*) 0005_search_address.

I figured that i must have forgotten to check in my migrations when i commited my changes and as a result when i did svn up,  one more migrations was added to my migrations directory. So I had two 0005_*  migrations but with different names. It would have been good if south intelligently knew that there was a conflict and tried to resolve it. Anyway, thus in order to get my migrations working again I had to manually go to postgres and delete the corresponding south migration history.

I used sudo -u postgres psql postgres postgres to log into psql  as root. 

\c: ‘database name’ # connect to the corresponding database

\d: See which table you need.

select * from south_migration_history ;# to check the id of the migration causing the conflict

delete from south_migration_history where id=5;

Now when I did ./manage.py migrate — list it only showed me

(*) 0005_search_address and i was successfully able to use ./manage.py migrate app again.

Text

Uploading Images and files in django forms.

If you want to upload an image in a django model form, all you need is a ImageField in your django model. ImageField inherits all attributes and methods from FileField but also validates that the uploaded object is a valid image.

Next thing you need is to specify where the image is supposed to be uploaded. This is done via the upload_to option to tell django which subdirectory of MEDIA_ROOT should the image be uploaded to. Here I have defined a function issue_screenshot_path which returns the filepath of the image that is stored. Django AutoSlug is used to name the image, and it populates from another field called app. I used the slugfield so that names of the images that are uploaded have relationship to the field app. Also, the slugfield helps in preserving the uniqueness of the the image name.

The code for the model form is pretty much generic except for the fact that the template for the corresponding form, must have the attribute enctype = ‘multipart/form-data’

The file data is received via requeset.FILES, a dictionary contatining a key for each FileField or ImageField in the form, by the view handling the form. It should be noted that request.FILES will only contain data if the request method was POST and the <form> that posted the request has the attribute enctype = ‘multiplart/form-data’


Text

PostgreSql database dropping issue

I was involved in a case today where I had to drop a Postgresql database using the command dropdb -U database_user database but it was giving me this error, 

dropdb: database removal failed: ERROR:  database “Enter Database Name Here” is being accessed by other users.

Intially, I thought it was because i was using ipython to access the database by making some django querries, but even when stopped ipython, the particular error was still persisting.

After a bit of googling, I found out that using select * from pg_stat_activity, i can get the process ids of all the ongoing connections with the database. This was a local database, so the case of other network users accessing the database was out of question.

Next step was to kill -9 all the process ids (procpid) associated with the select * from pg_stat_activity command.

That solved the problem and I could successfully drop the database.

Quote
"In a controversy the instant we feel anger we have already ceased striving for the truth, and have begun striving for ourselves."

Buddha

http://www.brainyquote.com/quotes/authors/b/buddha.html

Tags: quotes
Text

The need for Parallel Programming

This is the technical report I wrote in my Technical writing class in 2009 at Louisiana Tech. I was going through my old blogs and found this so, thought i would migrate it here.

INTRODUCTION

In this report I have examined how Multi-core computers are emerging as a new wave of technology and how implementation of parallel running applications can better utilize the multi-core resources. I have researched that traditional ways of programming and generating software have failed to fully utilize the enormous resources of today’s multi-core computers. 

The Multi-core Architecture:

Multi-core processors are formed as a result of combining two or more solo cores into a unit comprising a single integrated circuit called a die. Multi-core processors came into being since improving performance by increasing the clock speed had reached its physical limitations. Also, the cost of producing a single powerful core proved to be really expensive. As Marc Tremblay, chief architect of Sun Microsystems’ Scalable Systems Group points out, “We could build a slightly faster chip, but it would cost twice the die area while gaining only a 20 percent speed increase”( Geer 11).

Current transistor technology prohibits any more increase in the clock speed. This problem is highlighted when Geer states, “as a transistor gets smaller, the gate, which switches the electricity on and off, gets thinner and less able to block the flow of electrons. Thus, small transistors tend to use electricity all the time, even when they aren’t switching. This wastes power”. Consequently, manufacturers have started buildings chips with multiple cores each having dedicated coolers which are energy efficient.

 

The following diagram is based on Intel tests and it shows that the multi-core chips perform better than single core ones. The benchmarks achieved by the multi-core computers rise exponentially whereas those of the single core follow a more linear graph.


(Adapted from: “ Chip Makers Turn to Multi-core Processors)


Utilizing the Multi-core architectures

The traditional way of programming will not efficiently take advantage of multi-core systems.

In order to fully exploit these multi-core machines, organizations need to redesign applications so that the processors can treat them as multiple threads of execution. Programmers need to hunt for optimum spots in their codes to insert the parallel code, divide the work approximately into equal parts that can be run simultaneously and associate the precise times for the communication of the threads. Redesigning applications to implement recognition of the core speed of one core by another core in the die must also be taken into grave consideration. As Jones points out, “While that next-generation chip will have more CPUs, each individual CPU will be no faster than the previous year’s model. If we want our programs to run faster, we must learn to write parallel programs” (Beautiful Concurrency). Therefore,software developers must take steps to modify the traditional way of writing programs to make way for the implementation of concurrency.


The Traditional Approach

The performance of traditional software depends on three main areas of the system :

  • Clock Speed

  • Optimization of the Execution Flow

  • Cache

Clock Speed:

Clock speed basically means the speed of the computer. However, the practice of increasing the clock speeds of computers to increase their overall efficiency is slowly becoming an antiquated concept. Clock speeds have hit a wall in recent years and the highest clock speed one may encounter nowadays in a computer is about 3.73 GHz. ncreasing the clock speed beyond 3.73 GHz has serious consequences such as “increased heat production, high power consumption and current leakage problems” (Sutter).

 

The following graph showcases Intel Company’s Trend in increasing the clock speed of its computers through time. Towards the year 2003, a sharp bend is observed in the rising of clock speeds.


(Adapted from “ The Free Lunch is Over: A Fundamental Turn Towards Concurrency”)

 

As Geer points out, “Chip performance increased 60 percent per year in the 1990s but slowed to 40 percent per year from 2000 to 2004, when performance increased by only 20 percent”.At the moment 3.73 GHz is the highest clock speed in a computer. It is not feasible to go higher as it gives rise to several physical issues already mentioned. Consequently, processor producing companies like Intel and AMD have swayed from the traditional path of increasing clock speeds and adopted a much better alternative – Multi-core computer architectures.


Optimization of the Execution Flow:

Execution Flow basically relates to the amount of work that is done in a particular CPU cycle. Therefore, it is directly proportional to the clock speed of a computer. Therefore, in order to get more out of each CPU cycle, many optimizations are conducted, “ including pipelining, branch prediction,

executing multiple instructions in the same clock cycle, and even reordering the instruction stream for out-of-order execution. These techniques are all designed to make the instructions flow better and/or execute faster, and to squeeze the most work out of each clock cycle by reducing latency and maximizing the work accomplished per clock cycle”. (Sutter)

However, in order to combat the saturation of the clock speed, today’s chip designers are aggressively pursuing optimizations to get more work done in each CPU cycle. Unfortunately, these optimizations have the potential to change the overall semantics of the source code of one’s software. Moreover, with the dawn of multi-core processors, chip designers have started to engineer these optimizations in favor of parallel computing and concurrent programming. Thus, the traditional approach to software development is on the verge of a revolution. Today’s Systems will prove very beneficial if it adopts concurrent programming paradigm from the very start.


Cache:

Cache Memory is the memory designed to instantiate faster access of data which are frequently used. These frequently accessed data are stored elsewhere and are expensive to read. So, their duplicates are stored in the Cache Memory where the cost of reading is comparatively low. Traditional software enjoyed faster performance when the Cache was increased. It is the only performance enhancing procedure that will continue in the near future.

An important thing to witness is that all of the aforementioned performance enhancing techniques are agnostic of concurrency. As computers all around the world start to adopt the multi-core architecture, concurrency will be definitely be the norm of computing.

 

In order to ensure top notch performance of applications on these new processors, many applications will have to be written or rewritten following the rules and regulations of parallel programming. Development of parallel applications can be arduous and it expects fresh programming

skills in potential developers. Organizations all over are facing a challenge trying to meet the new demands of the multi-core software transition. Concurrent programming, specially Open Multi-Processing for multi-core processors, will prove to be a solution to these new and upcoming challenges (Leiserson IV).


Concurrent Programming

Concurrent Programming utilizes two or more processors by making them cooperative in performing a task. It is not a new way of programming. However, majority of programmers today hesitate to use it as it implicates changes in the way programmers develop their software. Also,it is a bit harder to understand. As Hasselbring puts it, “Concurrent programming is conceptually harder to undertake and to understand than sequential programming, because a programmer has to manage the coexistence and coordination of multiple concurrent activities”.The very semantics of one’s code has to be compromised when concurrent programming is involved. Nevertheless, it has limitless advantages in parallel computing and nowadays when the demand for multi-core computers are rapidly growing in the market, concurrent programming is definitely on the highway to popularity. If a software adopts the strategies of concurrent programming, it will be able to take advantage of the benefits of today’s modern multi-core computers. It true when Sutter says, “ Applications will increasingly need to be concurrent if they want to fully exploit continuing exponential CPU throughput gains”.

Most of today’s softwares have long been using sequential programming concepts which uses a single thread of program execution in developing its applications. Sequential programming proved beneficial when computers had single core architectures but now when multi-core computers have hit the arena it proves to be inefficient. Concurrent programming fully exploits today’s multi-cores by increasing the software’s throughput, the number of tasks completed in a certain time. If we adopt concurrent programming paradigms such as OpenMP, it will prove beneficial as its consumers will experience smoothers and faster execution of their software without having to sacrifice a huge portion of their valuable memory.


Open Specifications for Multi Processing (OpenMP)

OpenMP is an open-source application programming interface that supports multi-platform shared memory multi-processing through multi-threaded programming using Fortran and C/C++ language compiler directives. Lawrence Livermore National Laboratory defines OpenMP as, “an Application Program Interface (API), jointly defined by a group of major computer hardware and software vendors. OpenMP provides a portable, scalable model for developers of shared memory parallel applications”(Barney). Compilers, programs that transform the source code written in a programming language to a language that the machine understands,for OpenMP are provided by several companies such as Hewlett-Packard, Portland Group,Microsoft, Intel,IBM and Sun Microsystems. It is also, supported by the GNU gcc compiler. The programmer identifies the parallel sections of the code by inserting pragmas, compiler directives , into the code (Leiserson and Mirman).


How OpenMP will help Developers

Applications nowadays need to take advantage of the multi-cores on the processor chips in today’s modern computer. If they do otherwise, the application will not be able to operate any faster.

This is where OpenMP takes the center stage.OpenMP assists developers to design multithreaded, applications quickly. Multithreaded applications are those applications that exploit the multi-core computers to its full capabilities. The threads in a multithreaded applications are somewhat like copies of their parent process, a running program, and they share resources and global variables. OpenMP

has many advantages such as:

  • It lets the programmer decide which part of the code he wants to run concurrently.

  • It creates and manages the threads for the programmer.

  • Its compiler are available from a lager number of companies such as Intel and IBM to name a few.

  • It provides a set of pragmas, runtime routines, and environment variables that programmers can use to specify shared-memory parallelism in Fortran, C, and C++ programs.” (Copty)

  • OpenMP can run the code as a serial code.

  • OpenMP is easier to program than other parallel programming languages such as MPI(Message Passing Interface)

How does OpenMP work?


Before OpenMP can be incorporated into one’s source code, a simple extension has to be added to the Fortran/C/C++ compiler. After the addition of the extension is complete OpenMP helps an ordinary program achieve parallelism just by addition of a few specifics codes that entails the boundary of the parallel sections of the program. OpenMP utilizes a list of compiler directions called # pragmas which dictates how the program will work. These pragmas are implemented in a way such that even if it the code is run in a compiler oblivious to OpenMP, it will still compile and run but without any parallelism.

Thus, by giving the developers an easy start, OpenMP manages to make learning parallel computing and concurrency easier for naïve programmers who are oblivious to the programming paradigms of concurrency.


Conclusion

 Software writing is at the brink of concurrency revolution. More and more CPU producing companies have started switching from the traditional single core processors to the new multi-core processors to better enhance performance of their systems. As pointed out in this report, traditional software applications need to go through semantic changes if it is to fully reap the advantages of the multi-core architectures of the new era. New software programs must be written following the guidelines of concurrency compatible interfaces and older programs must also be re-written to enhance performance. One such interface which provides an easy start up for programmers to learn how to write parallel programs is OpenMP. If more people adopt the usage of OpenMP or other parallel programming concepts, they could benefit from improved performance of the software in multi-core processors which are already very popular in the market. This could without a doubt increase the sale of our softwares substantially, paving the way for a mega profit margin.


 

Sources

Blaise, Barney. “OpenMP.” https://computing.llnl.gov. January 2009. Lawrence Livermore National

Laboratory. 18 Feb 2009 <https://computing.llnl.gov/tutorials/openMP/#Abstract>.

Copty, Nawal. “Introducing OpenMP: A Portable, Parallel Programming API for Shared Memory

Multiprocessors.” www.developers.sun.com. January 2007. Sun Microsystems. 18 Feb 2009

<http://developers.sun.com/solaris/articles/omp-intro.html>.

Geer, David. “Chip Makers Turn to Multicore Processors.” http://ieeexplore.ieee.org. May 2005. 18

Feb 2009 <http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=01430623>.

Hasselbring, Wilhelm. “Programming Languages and Systems for Prototyping Concurrent

Applications.” http://se.informatik.uni. 2004. 18 Feb 2009 <http://se.informatik.uni-

oldenburg.de:30000/158/1/ACMCS2000.pdf>.

Jones, Simon Peyton. “Beautiful COncurreny.” www-personal.umich.edu. May 2007. Microsoft

Research. 18 Feb 2009 <http://www-personal.umich.edu/~jkglenn/beautiful.pdf>.

Leiserson, Charles E.. “How to Survive the Multicore Software Revolution.” www.cilk.com. May

2008. Cilk. 18 Feb 2009 <www.cilk.com/multicore-e-book/>.

Sutter, Herb. “The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software.” http://

 www.gotw.ca. March 2005. 18 Feb 2009

Text

Setting up external monitor as the primary monitor in Ubuntu 10.10 having Ati radeon graphics card

I had recently purchased a Lenovo y560p laptop which had an ati graphics card. Anyone who has used ati graphics card along with ubuntu knows that third party drivers for the ati, does not work very well out of the box for Ubuntu. So, at first I tried to search for my third party Ati drivers in the additional drivers section in Ubuntu. I am ,as of now ,using Ubuntu 10.10, which only has drivers for Ati radeon HD 5000 series and below. My graphics card was Ati Radeon HD 6750. I tried running HD 5000, it was definitely compatible but there was already a driver for Ati radeon HD 6750 available from the ati website, so I used that instead.

After taking care of that, I wanted my external monitor to be my primary display and my laptop to be my secondary display so, I could use dual monitor, instead of mirroring, but Ati radeon’s catalyst control center, does not really have a way to switch your default monitor from your laptop to your external monitor and vice versa in ubuntu, though it has very good support for Windows and Mac OSX.

Finally, I figured out a way to fix this problem. Turns out xrandr - -output DFP1 - -primary does the trick. Replace DFP1 to the name of the monitor you want to make your primary monitor. You can run xrandr - -prop to get the proper names of the monitors currently connected.

If you run xrandr - -output DFP1 - -primary, it does switch the primary monitors, but once you restart your laptop, the laptop becomes the primary display once again.

So, I thought I would write a simple script, that runs at start up, which checks, if there is an external monitor connected, and if there is, then run the command to make the external monitor the primary display.

It uses a very simple approach. It tracks the number of times the word ‘connected’ appears in the xrandr -d :0 -q command, and only if the output is 2, does it switches the primary monitor.

Text

Overriding Twitter’s Default Callback Url using Oauth

Unlike Facebook, Twitter does not let you specifiy your callback url when you are initiating Twitter’s Oauth procedure. The default Callback Url is the one specified when you registered your application here https://dev.twitter.com/apps/new . If you wanted to change your callback url, you would have to manually log in to your app settings in https://dev.twitter.com/apps and edit them. This is annoying for various reasons.Suppose you have a single twitter application, that you want to use on multiple sites. For example, let us say Groupon, has two sites. One for its customers and one for its clients,customer.groupon.com and clients.groupon.com. When a customer clicks on ‘Connect to Twitter’ in the customer website, let’s say it uses the default callback url in the twitter website, customer.groupon.com. But when a client clicks the ‘Connect to Twitter’ to link the clients account with twitter, so it can tweet new deals everytime it makes a deal on groupon, the default callback needs to be overriden so it becomes clients.groupon.com instead of customer.groupon.com.

Turns out the default callback url can be overriden very easily, though not straighforward at first.

 If you wanted to use the default callback url, you just send a HTTP GET request to ’http://twitter.com/oauth/request_token’ and ‘http://twitter.com/oauth/access_token’.

If you wanted to override the default callback URL, you would send a HTTP POST request to the request token url and access token url with  

body= urllib.urlencode(‘oauth_callback’:’site-you-want-to-redirect-to’).

Now, you can override, twitter’s default callback url, depending on which site you are using your ‘Connect to Twitter’ feature.

Text

Twitter Connect in django using Oauth

I was recently involved in a django project where I had the opportunity to learn Oauth2.0. Almost all new social networking sites have started using Oauth2.0 in favour of basic authentication which was the prevalent authentication system sometime ago. Twitter has not yet migrated to using Oauth2.0, it is using Oauth1.0a as of now.

I am specifically going to write about how I integrated twitter authentication into my django-app. I am assuming the reader is familiar with python and django, or the MVC framework in general.First you are going to need your twitter consumer key and your twitter consumer secret, which you will receive once you have registered your new application with twitter here https://dev.twitter.com/apps/new . Unlike facebook, in twitter you have to explicitly specify your Oauth Callback Url, while registering your app. This limits you to use the same callback url that you have specified in your twitter settings page.So, if your domain name has changed, which can occur multiple times in your development environment for various reasons, you would want to override the twitter oauth callback url . This is a bit tricky at first but it is easy once you know how to do it. I will describe the steps to override twitter oauth callback url in my next post.

I wanted my twitter-connect view in my django app, to store the user’s twitter access_token, access_token_secret , their user_id and their twitter username. So, I set up my model accordingly.

You will need python-oauth2 library to communicate with twitter using the oauth protocol. You can get the latest version of python-oauth2 from https://github.com/simplegeo/python-oauth2.

Once you have the python-oauth2 library installed, you are ready to write the initial piece of code for your twitter connect view.

It is best to store all your twitter consumer key and your twitter consumer secret in the settings.py file in your django application. Here I am checking for a ‘redirect’ in my request.session, if you have already stored your twitter credentials in the database then it will redirect you to the dashboard, else you will follow the regular twitter authentication protocol.

It is important to store the session variable because, authentication via Ouath2.0 is a three stage process. First, using our twitter consumer key and consumer secret, we tell to twitter that we are a valid application, twitter upon verifying this, will send us a request_token and request_token_secret. We store these two tokens in our session variables. Then we set up the new authorize url as shown below and redirect to the new url without our request token.

Then, twitter upon receiving the correct request token, sends us a oauth verifer. We get the oauth verifier using request.GET and we set the token using the request token, request token secret and the newly received oauth verifier.

In the content thus recieved, we will have a json object or the access_token, access_token_secret,user_id, and screenname. We have to store this access_token in our database, if we want to post to our user’s twitter account using the access_token.

Once you have the user’s access token, tweeting on behalf of the user is super simple.

You just submit a Post request to ”https://api.twitter.com/1/statuses/update.json” using the required oauth credentials we have stored in our database. Here, data is a dict, consisting of our tweet. Twitter, calls it ‘status’ so, we update ‘status’ to our new tweet and send a post request to our request_uri. 

If you followed the steps correctly, you should be able to use oauth to successfully tweet to a user’s twitter feed using your django view.