The Stream Blog

Example App: Python/Django on Heroku

We are excited to announce the first of our GetStream example applications. We hope that with this series of examples people will see how easy it is to get started with Stream and how an application can implement news feeds in less than one day of work.

Our first example is written in Python and uses the Django web framework (and of course Stream’s python client). It is hosted on Github and can be found here.

To make things even easier we have integrated Stream with Heroku and, thanks to the awesome deploy button, deploying the demo application only takes 3 clicks and less than 2 minutes! It’s free so go ahead and start the Deploy to Heroku while you continue reading.

Deploy

The example app

The example app we have built is a very simple clone of Pinterest; it ships with 3 different kinds of feeds:

  1. user feed, which shows current user’s pins
  2. flat feed, which shows the pins from the users you follow
  3. aggregated feed, which shows the pins from the users you follow and aggregates them

Now, since you are probably going to be the only user (at least, when you try it out) we automatically log you in as admin. To show how flat and aggregated feeds work we let admin follow himself (what a narcissistic user!), so that when you pin something you will see it appearing in every feed.

If you want to play bit more with the app we suggest that you login to the admin (/admin) and create more users.

Note: the application is not made for production use; the Stream implementation is a very good starting point, and we suggest looking at it to get started (especially the Manager class and the enrichment functions); Django settings are not suited to be re-used as they are in a production site!

Looking at the code

If you are familiar with Django you will not see anything special in the way this project is laid out; in fact, we have tried to keep things very simple: most of the integration with Stream APIs lives in the core.feed_managers module. There you have the Manager class that takes care of querying Stream APIs via the python client and perform basic operations like add/remove activities and follow/unfollow feeds.
For example, reading the last 25 activities from current user’s flat feed looks like this:

feed = manager.get_feeds(request.user.id)[‘flat’]
activities = feed.get(limit=25)[‘results’]

The manager is then used in core.views to retrieve the data to present to the user (eg. flat feed and aggregated feed); by forms to insert or delete activities and to follow or unfollow users.

Another interesting piece of code is in models:Pin.create_activity; where a Pin instance converted to it’s Activity data.

The same activity is then “enriched” by the enrich_activities function in the view. This enrichment process is a very common and allows you to store objects’ references in feeds and retrieve them when you read them back from feeds. You can read more about this in the documentation.

We hope that you will enjoy playing with the example app and that it gives some helpful insight on how you can integrate your application with Stream.