-1

I want to try setup a private website, where users can purchase bookings but only their own bookings can be viewed within the entire shop when logged in. Saleor seems to be the most complete ecommerce package for Python/Django.

Is there a way i can block access to using categories? As in, i can create a category 'Johnson Family' and only select certain users to have access to 'Johnson Family' categories, if i approve their email to have access. Then those users would then see 'products' or 'bookings' specifically for them within the shop.

Edit: Apologies, i should of ask 'how' and not 'if' of course it can be done in Django, but i wasn't sure 'how?'

4

1 回答 1

1

As you have asked a generalized question, here is the generalized solution:

Is there a way i can block access to using categories?

Yes, you can. Django auth module has a concept of group where you can create a group and add users to this group. Then in your view you can check whether user belongs to the particular group or not. You can do something like:

from django.contrib.auth.models import User, Group

#create the group
group = Group(name='Johnson Family')
group.save()

# Add user to the group
user = User.objects.get(email='some@email.id')
user.groups.add(group)

# Call this method from your view to check if user belongs to a group
def is_member(user, group_name):
    return user.groups.filter(name=group_name).exists()

Then those users would then see 'products' or 'bookings' specifically for them within the shop.

For this you can always filter the queryset to return the objects that belong to a specific user. e.g. (assuming Product and Booking model has a foreign key to user):

Product.objects.filter(user=some_user)
Booking.objects.filter(user=some_user)
于 2017-05-08T08:56:18.230 回答