Reputation: 35
My User Model
SELLER = "SELLER"
CUSTOMER = "CUSTOMER"
USER_TYPE_CHOICES = (
('SELLER' , 'Seller'),
('CUSTOMER' , 'Customer'),
)
class User(AbstractBaseUser,PermissionsMixin):
email = models.EmailField( max_length=255, unique=True)
user_type = models.CharField(max_length=200,choices=USER_TYPE_CHOICES,null=True)
last_login = models.DateTimeField( blank=True, null=True)
is_staff = models.BooleanField( default=False)
is_superuser = models.BooleanField( default=False)
is_active = models.BooleanField( default=True)
date_joined = models.DateTimeField(default=timezone.now)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class Meta:
ordering = ['email']
def clean(self):
super().clean()
self.email = self.__class__.objects.normalize_email(self.email)
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
I have two user_types one seller and one customer how i can use user_passes_test decorator to see if the user.user_type is seller so he can see the seller view only seller can see the seller view
def seller_home(request,pk):
try:
seller = Seller.objects.get(id=pk)
except User.DoesNotExist:
raise Http404("Seller Does Not Exisit")
sellerproducts = Product.objects.filter(seller=seller)
totalsellerproducts = sellerproducts.count()
context = {'seller':seller,'sellerproducts':sellerproducts,"totalsellerproducts":totalsellerproducts }
return render(request,"seller_home.html",context)
Upvotes: 0
Views: 176
Reputation: 2004
I think that you can't, because user_passes_test
is for authenticated users only, so the seller or customer cannot be sent by URL to the view, because it will be insecure, anyone will be able to test any pk, You must use the authenticated user in request.user
to do this, then the decorator will be easy. Something like this:
def seller_check(user):
return user.user_type == User.SELLER
@user_passes_test(seller_check)
def seller_home(request):
pass
Upvotes: 1