Code Monkey home page Code Monkey logo

django-favit's Introduction

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
INSTALLED_APPS = [
  ...
  "favit",
  ...
]
  • Add favit urls to your project's urls.py file:
urlpatterns = patterns('',
  ...
  (r'^favit/', include('favit.urls')),
  ...
)
  • Sync your database:
python manage.py migrate
  • 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:
{% 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:
<p>Favorite Count {{ obj|favorites_count }}</p>
  • Get Favorite instance for an object (obj) and a user (user)
{% with obj|get_favorite_for:user as fav_object %}
    ...
{% endwith %}
  • Favorite Button for an object my_obj (optional param specified if show count, default True):
{% favorite_button my_obj count=False %}

Favorites Manager

  • Create a Favorite instance for a user and object:
>>> 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:
>>> fav = Favorite.objects.create(user, 1, Song)
or:
>>> fav = Favorite.objects.create(user, 1, "music.Song")
  • Get the objects favorited by a given user:
>>> 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:
>>> 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:
>>> 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:
>>> 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
>>> from music.models import Song
>>> song = Song.objects.get(pk=1)
>>> fav = Favorite.objects.for_object(song)

django-favit's People

Contributors

boulderdave avatar coordt avatar geerk avatar kleingeist avatar lomatus avatar martinblech avatar massada avatar matagus avatar mfeldman avatar mjroson avatar moemen avatar mpsdocs avatar mrezk avatar polmuz avatar recklessromeo avatar spencercooley avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.