{ "info": { "author": "Tommaso Barbugli", "author_email": "tbarbugli@gmail.com", "bugtrack_url": null, "classifiers": [ "Environment :: Web Environment", "Framework :: Django", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Topic :: Internet :: WWW/HTTP", "Topic :: Internet :: WWW/HTTP :: Dynamic Content" ], "description": "## Stream Django\n[](https://travis-ci.org/GetStream/stream-django) [](https://coveralls.io/github/GetStream/stream-django?branch=master) [](http://badge.fury.io/py/stream-django)\n\n[stream-django](https://github.com/GetStream/stream-django) is a Django client for [Stream](https://getstream.io/), it supports Django from 1.5 up to and including 2.2 using Python 2.7 and 3.4, 3.5, 3.6 and 3.7.\n\nYou can sign up for a Stream account at https://getstream.io/get_started.\n\nNote there is also a lower level [Python - Stream integration](https://github.com/getstream/stream-python) library which is suitable for all Python applications.\n\n### Build activity streams & news feeds\n\n
\n
\n
\n
\n\nBelow we show an example of how you can read the notification feed.\n```python\nnotification_feed = feed_manager.get_notification_feed(user_id)\n\n```\nBy default the notification feed will be empty. You can specify which users to notify when your model gets created. In the case of a retweet you probably want to notify the user of the parent tweet.\n\n```python\nclass Tweet(models.Model, Activity):\n\n @property\n def activity_notify(self):\n if self.is_retweet and self.parent_tweet.author != self.author:\n target_feed = feed_manager.get_notification_feed(self.parent_tweet.author_id)\n return [target_feed]\n\n```\n\nAnother example would be following a user. You would commonly want to notify the user which is being followed.\n\n```python\nclass Follow(models.Model, Activity):\n\n @property\n def activity_notify(self):\n return [feed_manager.get_notification_feed(self.target_user.id)]\n\n```\n\n\n#### Follow a feed\nTo create the newsfeeds you need to notify the system about follow relationships. The manager comes with APIs to let a user's news feeds follow another user's feed. This code lets the current user's timeline and timeline_aggregated feeds follow the target_user's personal feed.\n\n```python\nfeed_manager.follow_user(request.user.id, target_user)\n\n```\n\n### Showing the newsfeed\n\n#### Activity enrichment\n\nWhen you read data from feeds, a like activity will look like this:\n\n```python\n{'actor': 'core.User:1', 'verb': 'like', 'object': 'core.Like:42'}\n```\n\nThis is far from ready for usage in your template. We call the process of loading the references from the database enrichment. An example is shown below:\n\n```python\nfrom stream_django.enrich import Enrich\n\nenricher = Enrich()\nfeed = feed_manager.get_feed('timeline', request.user.id)\nactivities = feed.get(limit=25)['results']\nenriched_activities = enricher.enrich_activities(activities)\n```\n\n\n\n#### Templating\n\nNow that you've enriched the activities you can render the template.\nFor convenience we include the render activity template tag:\n\n```\n{% load activity_tags %}\n\n{% for activity in activities %}\n {% render_activity activity %}\n{% endfor %}\n\n```\n\nThe render_activity template tag will render the template activity/[aggregated]/[verb].html with the activity as context.\n\nFor example activity/tweet.html will be used to render an normal activity with verb tweet\n\n```\n{{ activity.actor.username }} said \"{{ activity.object.body }} {{ activity.created_at|timesince }} ago\"\n```\n\nand activity/aggregated/like.html for an aggregated activity with verb like\n\n```\n{{ activity.actor_count }} user{{ activity.actor_count|pluralize }} liked {% render_activity activity.activities.0 %}\n```\n\nIf you need to support different kind of templates for the same activity, you can send a third parameter to change the template selection.\n\nThe example below will use the template activity/[aggregated]/homepage_%(verb)s.html\n```\n{% render_activity activity 'homepage' %}\n```\n\n\n### Settings\n\n**STREAM_API_KEY**\nYour stream site api key. Default ```''```\n\n**STREAM_API_SECRET**\nYour stream site api key secret. Default ```''```\n\n**STREAM_LOCATION**\nThe location API endpoint the client will connect to. Eg: ```STREAM_LOCATION='us-east'```\n\n**STREAM_TIMEOUT**\nThe connection timeout (in seconds) for the API client. Default ```6.0```\n\n**STREAM_FEED_MANAGER_CLASS**\nThe path to the feed manager class. Default ```'stream_django.managers.FeedManager'```\n\n**STREAM_USER_FEED**\nThe name of the feed (as it is configured in your GetStream.io Dasboard) where activities are stored. Default ```'user'```\n\n**STREAM_NEWS_FEEDS**\nThe name of the news feed (as they are configured in your GetStream.io Dasboard) where activities from followed feeds are stored. Default ```{'timeline':'timeline', 'timeline_aggregated':'timeline_aggregated'}```\n\n**STREAM_NOTIFICATION_FEED**\nThe name of the feed (as it is configured in your GetStream.io Dasboard) where activity notifications are stored. Default ```'notification'```\n\n**STREAM_DISABLE_MODEL_TRACKING**\nDisable automatic tracking of Activity models. Default ```False```\n\n### Temporarily disabling the signals\n\nModel syncronization is disabled during schema/data migrations runs, syncdb and fixture loading (and during django test runs).\nYou can completely disable feed publishing via the ```STREAM_DISABLE_MODEL_TRACKING``` django setting.\n\n\n### Customizing enrichment\n\nSometimes you'll want to customize how enrichment works. The documentation will show you several common options.\n\n#### Enrich extra fields\n\nIf you store references to model instances in the activity extra_data you can use the Enrich class to take care of it for you\n\n```python\nfrom stream_django.activity import create_model_reference\n\nclass Tweet(models.Model, Activity):\n\n @property\n def extra_activity_data(self):\n ref = create_model_reference(self.parent_tweet)\n return {'parent_tweet': ref }\n\n\n# instruct the enricher to enrich actor, object and parent_tweet fields\nenricher = Enrich(fields=['actor', 'object', 'parent_tweet'])\nfeed = feed_manager.get_feed('timeline', request.user.id)\nactivities = feed.get(limit=25)['results']\nenriched_activities = enricher.enrich_activities(activities)\n```\n\n#### Change how models are retrieved\n\nThe enrich class that comes with the packages tries to minimise the amount of database queries. The models are grouped by their class and then retrieved with a pk__in query. You can implement a different approach to retrieve the instances of a model subclassing the ```stream_django.enrich.Enrich``` class.\n\nTo change the retrieval for every model you should override the ```fetch_model_instances``` method; in alternative you can change how certain models' are retrieved by implementing the hook function ```fetch_