def like_product(request, product_id): product=get_object_or_404(Product, id=product_id) if request.user in product.likes.all(): product.likes.remove(request.user) else: product.likes.add(request.user) print('article liked') notify(user=request.user, verb='a aime votre article', target=product.owner) print('notification sent') return redirect('products:view_product', id=product_id)
def follow_store(request, store_id): store = get_object_or_404(Store, id=store_id) if not request.user in store.followers.all(): store.followers.add(request.user) notify(user=request.user, verb='admire votre store', target=store.owner) else: store.followers.remove(request.user) return redirect('stores:stores')
def MakePost(request, store_id): store = get_object_or_404(Store, id=store_id) if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.author = store post.save() followers = store.followers.all() for follower in followers: notify(user=request.user, verb='a fait une annonce', target=follower) return redirect('products:home') else: form = PostForm() return render(request, 'stores/make_post.html', {'form': form})
def like_product_by_api(request, *args, **kwargs): serializer=ProductSerializer(data=request.POST) if serializer.is_valid(raise_exception=True): data=serializer.validated_data product_id=data.get("id") product=get_object_or_404(Product, id=product_id) if request.user in product.likes.all(): product.likes.remove(request.user) serializer=ProductSerializer(product) print('arcticle disliked') return Response({serializer.data}, status=200) else: product.likes.add(request.user) print('article liked') serializer=ProductSerializer(product) notify(user=request.user, verb='a aime votre article', target=product.owner) return Response({serializer.data}, status=200) return Response({}, status=200)
def notify_followers(followers=[]): if len(followers)==True: for follower in followers: notify(user=request.user, verb='a fait une annonce', target=follower)