A simple reusable app for django that makes it easy to deal with faving
Project description
# django-favit
A simple reusable app for django that makes it easy to deal with faving
and unfaving any object from any application.
## Installation
* Install django-favit in your vilrtual env:
```
pip install django-favit
```
* Add the app to your settings.py
```python
INSTALLED_APPS = [
...
"favit",
...
]
```
* Add favit urls to your project's `urls.py` file:
```python
urlpatterns = patterns('',
...
(r'^favit/', include('favit.urls')),
...
)
```
* Sync your database:
```
python manage.py syncdb
```
* Make sure you have jQuery ajax CSRF configuration right
See https://docs.djangoproject.com/en/1.7/ref/contrib/csrf/#ajax
## Usage:
### Template tags:
* Get the favorited objects for a given user:
```python
{% with user_favorites <user> "app_label.model" as favorite_list %}
{% for fav_obj in favorite_list %}
{# do something with fav_obj #}
{% endfor %}
{% endwith %}
```
* Given an object `obj` you may show it fav count like this:
```python
<p>Favorite Count {{ obj|favorites_count }}</p>
```
* Get Favorite instance for an object (obj) and a user (user)
```python
{% with obj|get_favorite_for:user as fav_object %}
...
{% endwith %}
```
* Favorite Button for an object `my_obj`:
```python
{% favorite_button my_obj %}
```
### Favorites Manager
* Create a Favorite instance for a user and object:
```python
>>> from django.contrib.auth.models import User
>>> from music.models import Song
>>> user = User.objects.get(username='jdoe')
>>> song = Song.objects.get(pk=1)
>>> fav = Favorite.objects.create(user, song)
```
or:
```python
>>> fav = Favorite.objects.create(user, 1, Song)
```
or:
```python
>>> fav = Favorite.objects.create(user, 1, "music.Song")
```
* Get the objects favorited by a given user:
```python
>>> from django.contrib.auth.models import User
>>> user = User.objects.get(username='jdoe')
>>> Favorite.objects.for_user(user)
>>> [<Favorite: Favorite object 1>, <Favorite: Favorite object 2>, <Favorite: Favorite object 3>]
```
* Now, get user favorited objects belonging to a given model:
```python
>>> from django.contrib.auth.models import User
>>> from music.models import Song
>>> user = User.objects.get(username='jdoe')
>>> Favorite.objects.for_user(user, model=Song)
>>> [<Favorite: Favorite object 1>, <Favorite: Favorite object 2>, <Favorite: Favorite object 3>]
```
* Get the favorited object instances of a given model favorited by any user:
```python
>>> from music.models import Song
>>> Favorite.objects.for_model(Song)
>>> [<Favorite: Favorite object 1>, <Favorite: Favorite object 2>, <Favorite: Favorite object 3>]
```
* Get a Favorite instance for a given object and user:
```python
>>> from django.contrib.auth.models import User
>>> from music.models import Song
>>> user = User.objects.get(username='jdoe')
>>> song = Song.objects.get(pk=1)
>>> fav = Favorite.objects.get_favorite(user, song)
```
* Get all Favorite instances for a given object
```python
>>> from music.models import Song
>>> song = Song.objects.get(pk=1)
>>> fav = Favorite.objects.for_object(song)
```
A simple reusable app for django that makes it easy to deal with faving
and unfaving any object from any application.
## Installation
* Install django-favit in your vilrtual env:
```
pip install django-favit
```
* Add the app to your settings.py
```python
INSTALLED_APPS = [
...
"favit",
...
]
```
* Add favit urls to your project's `urls.py` file:
```python
urlpatterns = patterns('',
...
(r'^favit/', include('favit.urls')),
...
)
```
* Sync your database:
```
python manage.py syncdb
```
* Make sure you have jQuery ajax CSRF configuration right
See https://docs.djangoproject.com/en/1.7/ref/contrib/csrf/#ajax
## Usage:
### Template tags:
* Get the favorited objects for a given user:
```python
{% with user_favorites <user> "app_label.model" as favorite_list %}
{% for fav_obj in favorite_list %}
{# do something with fav_obj #}
{% endfor %}
{% endwith %}
```
* Given an object `obj` you may show it fav count like this:
```python
<p>Favorite Count {{ obj|favorites_count }}</p>
```
* Get Favorite instance for an object (obj) and a user (user)
```python
{% with obj|get_favorite_for:user as fav_object %}
...
{% endwith %}
```
* Favorite Button for an object `my_obj`:
```python
{% favorite_button my_obj %}
```
### Favorites Manager
* Create a Favorite instance for a user and object:
```python
>>> from django.contrib.auth.models import User
>>> from music.models import Song
>>> user = User.objects.get(username='jdoe')
>>> song = Song.objects.get(pk=1)
>>> fav = Favorite.objects.create(user, song)
```
or:
```python
>>> fav = Favorite.objects.create(user, 1, Song)
```
or:
```python
>>> fav = Favorite.objects.create(user, 1, "music.Song")
```
* Get the objects favorited by a given user:
```python
>>> from django.contrib.auth.models import User
>>> user = User.objects.get(username='jdoe')
>>> Favorite.objects.for_user(user)
>>> [<Favorite: Favorite object 1>, <Favorite: Favorite object 2>, <Favorite: Favorite object 3>]
```
* Now, get user favorited objects belonging to a given model:
```python
>>> from django.contrib.auth.models import User
>>> from music.models import Song
>>> user = User.objects.get(username='jdoe')
>>> Favorite.objects.for_user(user, model=Song)
>>> [<Favorite: Favorite object 1>, <Favorite: Favorite object 2>, <Favorite: Favorite object 3>]
```
* Get the favorited object instances of a given model favorited by any user:
```python
>>> from music.models import Song
>>> Favorite.objects.for_model(Song)
>>> [<Favorite: Favorite object 1>, <Favorite: Favorite object 2>, <Favorite: Favorite object 3>]
```
* Get a Favorite instance for a given object and user:
```python
>>> from django.contrib.auth.models import User
>>> from music.models import Song
>>> user = User.objects.get(username='jdoe')
>>> song = Song.objects.get(pk=1)
>>> fav = Favorite.objects.get_favorite(user, song)
```
* Get all Favorite instances for a given object
```python
>>> from music.models import Song
>>> song = Song.objects.get(pk=1)
>>> fav = Favorite.objects.for_object(song)
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-favs-sc-0.2.5.tar.gz
(7.6 kB
view details)
Built Distribution
File details
Details for the file django-favs-sc-0.2.5.tar.gz
.
File metadata
- Download URL: django-favs-sc-0.2.5.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1335814636f521173c360b0af5bb43bd22378f78a764c51f3c2f1edd1d47fad5 |
|
MD5 | 5055c12d6fb08b2020e05619fad6746c |
|
BLAKE2b-256 | 14fb446b52e9d618e8329d98479edda19299f2142a2bd13deeba3d2d38a0c0c6 |
File details
Details for the file django_favs_sc-0.2.5-py2-none-any.whl
.
File metadata
- Download URL: django_favs_sc-0.2.5-py2-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | df3ff75a3c9ab21603a97f22637063079331916f39e8aa930218b86240ff5bc6 |
|
MD5 | 477683e69b8856fc3ec20978aa835b41 |
|
BLAKE2b-256 | 9b0629a257e3bb364d92dcc2e695037cef9195912cb6501bb3bf455c4706538c |