Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved wishlist based on @dfalk's work #105

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Extract middleware wishlist loading into WishlistManager and improve …
…notification sending
  • Loading branch information
uranusjr committed Apr 27, 2013
commit 1f927d8d442c46327bc1d910873ac5d2aff491f2
12 changes: 12 additions & 0 deletions cartridge/shop/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,15 @@
editable=False,
default=True,
)

register_setting(
name="SHOP_WISHLIST_NOTIFICATIONS",
label=_(
"Sends email notifications if an item in user's wishlist is in stock"
),
description=_(
"Sends email notifications if an item in user's wishlist is in stock"
),
editable=True,
default=True,
)
21 changes: 21 additions & 0 deletions cartridge/shop/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,24 @@ def get_valid(self, code, cart):
if products.filter(variations__sku__in=cart.skus()).count() == 0:
raise self.model.DoesNotExist
return discount


class WishlistManager(Manager):

def from_request(self, request):
"""
Gets current user's wishlist. Authenticated users' wishlists are stored
in the database, while unauthenticated users' are stored in a cookie.
Note that this method returns a `list` of SKUs in both cases, not a
`QuerySet` of `ProductVariation` objects.
"""
if request.user.is_authenticated():
wishlist = []
skus = self.filter(user=request.user).values_list("sku", flat=True)
for sku in skus:
wishlist.append(sku)
else:
wishlist = request.COOKIES.get("wishlist", "").split(",")
if not wishlist[0]:
wishlist = []
return wishlist
11 changes: 1 addition & 10 deletions cartridge/shop/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,4 @@ class ShopMiddleware(SSLRedirect):
"""
def process_request(self, request):
request.cart = Cart.objects.from_request(request)
if not request.user.is_authenticated():
wishlist = request.COOKIES.get("wishlist", "").split(",")
if not wishlist[0]:
wishlist = []
else:
wishlist_items = Wishlist.objects.filter(user=request.user)
wishlist = []
for item in wishlist_items:
wishlist.append(item.sku)
request.wishlist = wishlist
request.wishlist = Wishlist.objects.from_request(request)
7 changes: 6 additions & 1 deletion cartridge/shop/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,11 @@ def update_stock(self, quantity):
self.product.save()


@receiver(pre_save, sender=ProductVariation)
@receiver(pre_save, sender=ProductVariation,
dispatch_uid="wishlist_notifications_on_save")
def wishlist_notifications_on_save(sender, instance, **kwargs):
if settings.SHOP_WISHLIST_NOTIFICATIONS is not True:
return
if instance.id:
productvariation = ProductVariation.objects.get(pk=instance.id)
old_stock = productvariation.num_in_stock
Expand Down Expand Up @@ -873,6 +876,8 @@ class Wishlist(models.Model):
user = models.ForeignKey(User)
sku = fields.SKUField()

objects = managers.WishlistManager()

class Meta:
unique_together = ('user', 'sku')
verbose_name = _("Wishlist item")
Expand Down