Skip to content

Easily manage blacklist and wishlist in your Django project.

Notifications You must be signed in to change notification settings

AzikDeveloper/django-wishlist-blacklist

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 

Repository files navigation

django-wishlist-blacklist

Easily manage blacklist and wishlist in your Django project.

Installation

pip install django-wishlist-blacklist (soon in pip)

Add django_wishlist_blacklist to your INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'django_wishlist_blacklist',
    ...
]

Usage for wishlisting:

Inherit from django_wishlist_blacklist.models.WishlistAuthorModel for your User model and django_wishlist_blacklist.models.WishlistTargetModel for your Product model:

from django_wishlist_blacklist.models import WishlistAuthorModel, WishlistTargetModel


class User(WishlistAuthorModel, AbstractUser)
    pass


class Product(WishlistTargetModel)
    pass

And we are done!

Usage Example

>>> product = Product.objects.first()
>>> user.is_wishlisted(product)
False
>>> user.add_to_wishlist(product)
<ModelBind: Casey Gates -> Ball(7)>
>>> user.is_wishlisted(product)
True
>>> user.get_wishlists(Product)
<QuerySet [<Ball: Ball(7)>]>
>>> user.remove_from_wishlist(product)
>>> user.get_wishlists(Product)
<QuerySet []>

Ready API for blacklisting and wishlisting

Add django_wishlist_blacklist.urls to your URL patterns:

urlpatterns = [
    ...
    url(r'^wb/', include('django_wishlist_blacklist.urls')),
    ...
]

Example to add product to wishlist:

POST /wb/wishlist/add/

{
  "target_ct": "product.Product",
  "target_object_id": 9
}

Example to remove product from wishlist:

POST /wb/wishlist/remove/

{
  "target_ct": "product.Product",
  "target_object_id": 9
}

Base serializer for appending is_wishlisted and is_blacklisted fields to serializer

from django_wishlist_blacklist.serializers import WishlistStateSerializerMixin


class ProductSerializer(WishlistStateSerializerMixin, serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = ('id', 'name', 'brand', 'created_at')
>>> serializer = ProductSerializer(product, context={'request': request})
>>> serializer.data
[
    {
        "id": 6,
        "name": "Ball",
        "brand": "Ball",
        "created_at": "2020-01-01T00:00:00Z",
        "is_wishlisted": True,
    },
    {
        "id": 7,
        "name": "Ball",
        "brand": "Ball",
        "created_at": "2020-01-01T00:00:00Z",
        "is_wishlisted": False,
    }
]

Blacklisting works the same way as wishlisting

About

Easily manage blacklist and wishlist in your Django project.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages