The packages I used for this setup are:
First, lets create a simple postgresql database. Since I want the test database to be separate from the main database, I am going to create one called flaskexample and one called flaskexample_test. I will show you later on, how to specify the test database when running the nose tests.
1 2 3 4 5 |
|
Now, we can create our simple flask application. For the purposes of this blog, the flask application will only have a User table that we will go against. Copy the code below into your application.py file, then in the console start the python interpreter and run the following:
1 2 |
|
If this runs successfully, this should create your users table in your flaskexample database.
Now for the main Flask application. In this contrived example, we will have 2 routes to get to our user data. We will have the /users route, where we can either get a list of users or post a new user. We also have the /users/<string:id> route where we will be able to get a single user. Our tests will center around getting a list of users, adding a user, getting a specific user, trying to add a user when an email already exists and finally deleting that user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
For the nose tests, I create a tests directory, under my application root. For this example, we will have two files. A __init__.py file, where we will setup the flask test client and the SQLAlchemy database session. I also have a testusers.py file, which is where all the tests will go. See the code blocks below.
__init__.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
testusers.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
For the tests, I create a simple runtests.sh shell script, which is where I specify a different database to use when running tests. It has the following line:
1
|
|
When running the runtests.sh file, you should get some output like:
1 2 3 4 5 6 |
|
This means your tests have run successfully. If you go check your flaskexample_test database, you should have one user in the Users table. When you run the test again, that user will be removed via the init_db() call, and the tests will run against a blank database.
Obviously, this is a very simple example, but I think it is a good starting point for setting up Test Driven Development with Flask and SQLAlchemy.
Feel free to email me with any questions or comments. The git repo for this is located here
]]>Below is an explanation of the code I wrote, in order to accomplish this. I am assuming one would know how to setup and get the needed keys for a twitter application.
The code is assuming you will have a ‘settings.cfg’ file that houses your twitter_consumer_secret, twitter_consumer_key, access_token, and access_token_secret.
It looks like:
1 2 3 4 5 |
|
First off, lets look at the main part of the program. The first part of the program just sets up some configuration variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
Next, lets get a collect the 6 key/value pair parameters needed for the Authorization Header and for generating the signature. These 6 parameters will be used in generating the signature, and then that signature plus these 6 parameters will be used as the Authorization Header for the request.
I accomplish this in the get_oauth_parameters function.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The results of this function will look something like:
1 2 3 4 5 6 7 8 |
|
To get the nonce, I just do this:
1 2 3 4 5 |
|
After collecting the needed parameters, we can go ahead and generate the oauth_signature header value. The generate_signature method will need the following:
1 2 3 4 5 6 7 8 |
|
The method looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
The first part of this method combines the oauth_parameters, status and url_parameters into a hash, sorts the paramaters alphabetically using a OrderedDict and encodes into a single string.
According to the Twitter documentation, in order to generate the proper sting you need to:
I accomplish this in the stringify_parameters method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
After we have our stringified parameters, we can create our signature_base_string. In order to do this we need to:
We are now able to create our signing key and then calculate the signature to put in the Authorization Header.
To create the signing key, we simply need to combine the oauth_consumer_secret and the oauth_token_secret joined by a ‘&’ character.
1 2 3 4 5 6 7 8 |
|
Now that we have the signing_key, we can take that along with the signature_base_string and calculate our SHA1 signature.
1 2 3 4 5 6 7 |
|
We have our signature, now all that is left is to create our Authorization Header for the request and make our request. In order to properly create the header, we need to combine our 6 parameters from the get_oauth_parameters with the signature we just created, order them alphabetically and append them to a string beginning with “OAuth”. The key/value parameters will need to be separated by a ‘=’, they will both need to be percent encoded and the values enclosed in ‘”’.
1 2 3 4 5 6 7 8 |
|
We should now be able to make our request:
1 2 3 4 5 6 7 |
|
Feel free to email me with any questions or comments. The git repo for this is located here
]]>Make sure pip is installed by running pip in the terminal.
If it isn’t installed, install by doing
1
|
|
1
|
|
1
|
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Open a new terminal window. You should see virtualenvwrapper.sh being run and setting up your .virtualenvs directory.
Test creating a new virtualenv
1
|
|
1 2 3 4 5 6 7 8 |
|
That should put you into a new virtualenv called “testenv”
Lets install some packages and create a requirements.txt file
1 2 3 4 |
|
Your have just installed 3 python packages into your virtual environment, and created a .txt file that will have all the necessary information in order to reproduce your environment.
You can leave your virtualenv by doing
1
|
|
1 2 |
|
I doubt this is the only solution, or even the “proper” solution, but it seems to work.
Below are all the steps I took in order to serve pages out of directories other then those in the “Sites” directory. For these steps, I will assume there is an existing directory of “~/Code/MyWebSite” that has a simple “Hello World” index.html file in it.
Enable Web Sharing on the MAC by going to System Prefrences –> Sharing –> Check Enable Web Sharing
Edit your username.conf file located in /private/etc/apache2/users and add the “FollowSymLinks” directive
1 2 3 4 5 6 |
|
1
|
|
1 2 3 4 5 6 7 8 |
|
1
|
|
1
|
|
1
|
|
1
|
|
I am going to expand on that post a bit, and show you how to download all the images for a given photo using the flickr.photos.getSizes API call.
If you followed my previous post, you should have a “token” file in your directory that contains the “oauth_token” and “oauth_token_secret” in it. You should also have an “apikeys” file that contains your application “api_key” and “secret”. We will need some code to read these files to get the keys and secrets.
Here is what I came up with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
The next task at hand was coming up with a base class that can encapsulate all the work needed in order to make subsequent Flickr API calls once you have your api key and token. I named this class “FlickrApiMethod” and it sets up all the data needed to make Flickr API requests. It has a “makeCall” method that will make the REST call, load the returned JSON content and return True if the call was successful, or False if it was not. I plan on expanding on this a bit to support all the different response formats that Flickr supports, but for now it just supports JSON. This class also has an no implementation “getParameters” method that allows subclasses to fill out additional parameters that are needed for a specialized API call.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
Next, I will write a class, that given a unique “photo_id” it will call the flickr.photos.getSizes method and write out the files for all the sizes available.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Finally, a little bit of code to use these classes:
1 2 3 4 5 6 7 8 9 10 11 |
|
If all went well, you should now have files in your directory for all the different sizes of the photo you specified.
Feel free to email me with any questions or comments. The git repo for this is located here.
]]>I decided to write this app in python, a language which I have no professional experience in, and have only recently dabbled with. I figured it would be a good learning experience. We will see!
The first thing that needs to be accomplished, in order to make calls to the Flickr API, is authenticate a user. Flickr just recently added support for using OAuth to authenticate a user Using OAuth with Flickr and they will be removing their old authentication API in the future. The focus of this post will outline the steps I took in order to successfully use OAuth to authenticate against my application.
The code below has two prerequisites in order to work correctly:
To setup python-oauth2:
1 2 |
|
The first thing to understand about using OAuth with Flickr, is every request must be signed using the HMAC-SHA1 signature encryption. You do this by building a Request object via the python-oauth2 Request method, passing in the url you want to make the request to and the parameters you want to send. You then call the SignatureMethod_HMAC_SHA1().sign method and add the resulting signature to the Request.
The begin the authentication procedure, you first need to create a request that will get a request token. The request will be made to http://www.flickr.com/services/oauth/request_token with the following paramaters: * oauth_timestamp * oauth_signature_method * oauth_version * oauth_callback * oauth_nonce * oauth_consumer_key * oauth_signature
The code to perform this operation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
If all is successful, Flickr include a oauth_token and a oauth_token_secret in the Response. You can now store those tokens off and prompt a user to go authorize your application. They will need to go to http://www.flickr.com/services/oauth/authorize with the oauth_token appended as a querystring parameter along with the perms parameter indicating if your application will need read,write and delete privileges. Once the user authorizes your application, Flickr will redirect to the oauth_callback url specified above with a oauth_verifier querystring parameter that you can then use in the third and final step of oAuth authentication.
The code to perform this operation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
The third and final step in authenticating is exchanging the Request Token for an Access Token. The Access Token is something you will store off for that user and you will be able to make Flickr API calls using the Access Token.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
If all the requests succeeded you should have a token file in your directory with the oauth_token and the oauth_token_secret. You can use these tokens to make subsequent requests to the Flickr API. In my next post, I will illustrate how to do this.
Feel free to email me with any questions or comments. The git repo for this is located here
]]>