permission

This commit is contained in:
Виктор Гладких 2019-12-10 18:36:18 +03:00
parent d441e69b81
commit 0a8db7e3ad
9 changed files with 116 additions and 58 deletions

View File

@ -0,0 +1,20 @@
# Generated by Django 2.2.7 on 2019-12-10 14:13
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('main', '0040_footer'),
('product', '0020_merge_20191209_0911'),
]
operations = [
migrations.AddField(
model_name='product',
name='site',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='main.SiteSettings'),
),
]

View File

@ -1,19 +0,0 @@
# Generated by Django 2.2.7 on 2019-12-10 07:40
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('main', '0039_sitefeature_old_id'),
('product', '0020_merge_20191209_0911'),
]
operations = [
migrations.AddField(
model_name='product',
name='sites',
field=models.ManyToManyField(to='main.SiteSettings'),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 2.2.7 on 2019-12-10 15:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('product', '0021_product_site'),
]
operations = [
migrations.AlterField(
model_name='producttype',
name='index_name',
field=models.CharField(choices=[('food', 'food'), ('wine', 'wine'), ('liquor', 'liquor'), ('souvenir', 'souvenir'), ('book', 'book')], db_index=True, max_length=50, unique=True, verbose_name='Index name'),
),
]

View File

@ -25,10 +25,17 @@ class ProductType(TranslatedFieldsMixin, ProjectBaseMixin):
SOUVENIR = 'souvenir'
BOOK = 'book'
INDEX_CHOICES = (
(FOOD, 'food'),
(WINE, 'wine'),
(LIQUOR, 'liquor'),
(SOUVENIR, 'souvenir'),
(BOOK, 'book')
)
name = TJSONField(blank=True, null=True, default=None,
verbose_name=_('Name'), help_text='{"en-GB":"some text"}')
index_name = models.CharField(max_length=50, unique=True, db_index=True,
verbose_name=_('Index name'))
verbose_name=_('Index name'), choices=INDEX_CHOICES)
use_subtypes = models.BooleanField(_('Use subtypes'), default=True)
tag_categories = models.ManyToManyField('tag.TagCategory',
related_name='product_types',
@ -222,7 +229,7 @@ class Product(GalleryModelMixin, TranslatedFieldsMixin, BaseAttributes,
default=None, null=True,
verbose_name=_('Serial number'))
sites = models.ManyToManyField(to='main.SiteSettings')
site = models.ForeignKey(to='main.SiteSettings', null=True, blank=True, on_delete=models.CASCADE)
objects = ProductManager.from_queryset(ProductQuerySet)()

View File

@ -8,7 +8,7 @@ from product.serializers import ProductDetailSerializer, ProductTypeBaseSerializ
ProductSubTypeBaseSerializer
from tag.models import TagCategory
from account.serializers.common import UserShortSerializer
from main.serializers import SiteSettingsSerializer
class ProductBackOfficeGallerySerializer(serializers.ModelSerializer):
"""Serializer class for model ProductGallery."""
@ -54,6 +54,7 @@ class ProductBackOfficeGallerySerializer(serializers.ModelSerializer):
class ProductBackOfficeDetailSerializer(ProductDetailSerializer):
"""Product back-office detail serializer."""
in_favorites = serializers.BooleanField(allow_null=True, read_only=True)
class Meta(ProductDetailSerializer.Meta):
"""Meta class."""
@ -67,9 +68,10 @@ class ProductBackOfficeDetailSerializer(ProductDetailSerializer):
# 'wine_sub_region',
'wine_village',
'state',
'site',
'product_type'
]
class ProductTypeBackOfficeDetailSerializer(ProductTypeBaseSerializer):
"""Product type back-office detail serializer."""

View File

@ -95,7 +95,7 @@ class ProductBaseSerializer(serializers.ModelSerializer):
wine_colors = TagBaseSerializer(many=True, read_only=True)
preview_image_url = serializers.URLField(allow_null=True,
read_only=True)
in_favorites = serializers.BooleanField(allow_null=True)
in_favorites = serializers.BooleanField(allow_null=True, read_only=True)
wine_origins = EstablishmentWineOriginBaseSerializer(many=True, read_only=True)
class Meta:

View File

@ -9,7 +9,7 @@ from translation.models import Language
from account.models import Role, UserRole
from location.models import Country, Address, City, Region
from main.models import SiteSettings
from product.models import Product
from product.models import Product, ProductType
class BaseTestCase(APITestCase):
def setUp(self):
@ -71,12 +71,19 @@ class BaseTestCase(APITestCase):
self.user_role.save()
self.product = Product.objects.create()
self.product_type = ProductType.objects.create(index_name=ProductType.LIQUOR)
self.product_type.save()
self.product = Product.objects.create(name='Product')
self.product.save()
class LiquorReviewerTests(BaseTestCase):
def test_get(self):
self.product.product_type = self.product_type
self.product.save()
url = reverse("back:product:list-create")
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
@ -86,8 +93,6 @@ class LiquorReviewerTests(BaseTestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_post_patch_put_delete(self):
# POST
data_post = {
"slug": None,
"public_mark": None,
@ -97,13 +102,13 @@ class LiquorReviewerTests(BaseTestCase):
"available": False,
"establishment": None,
"wine_village": None,
"in_favorites": 'false',
"state": Product.PUBLISHED
"state": Product.PUBLISHED,
"site_id": self.site.id,
"product_type_id": self.product_type.id
}
url = reverse("back:product:list-create")
response = self.client.post(url, data=data_post, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
data_patch = {
'name': 'Test product'
@ -112,3 +117,5 @@ class LiquorReviewerTests(BaseTestCase):
response = self.client.patch(url, data=data_patch, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)

View File

@ -102,7 +102,6 @@ class ProductListCreateBackOfficeView(BackOfficeListCreateMixin, ProductBackOffi
permission_classes = [IsLiquorReviewer]
class ProductTypeListCreateBackOfficeView(BackOfficeListCreateMixin,
ProductTypeBackOfficeMixinView,
generics.ListCreateAPIView):

View File

@ -9,7 +9,7 @@ from authorization.models import JWTRefreshToken
from utils.tokens import GMRefreshToken
from establishment.models import EstablishmentSubType
from location.models import Address
from product.models import Product
from product.models import Product, ProductType
class IsAuthenticatedAndTokenIsValid(permissions.BasePermission):
@ -448,38 +448,62 @@ class IsLiquorReviewer(IsStandardUser):
]
pk_object = None
product = None
roles = None
permission = False
if 'site_id' in request.data and 'product_type_id' in request.data:
if request.data['site_id'] is not None \
and request.data['product_type_id'] is not None:
product_types = ProductType.objects. \
filter(index_name=ProductType.LIQUOR,
id=request.data['product_type_id'])
if product_types.exists():
roles = Role.objects.filter(role=Role.LIQUOR_REVIEWER,
site_id=request.data['site_id'])
if 'pk' in view.kwargs:
pk_object = view.kwargs['pk']
if pk_object is not None:
product = Product.objects.get(pk=pk_object)
if product.site_id is not None \
and product.product_type_id is not None:
if hasattr(product, 'sites') and product.sites.exists():
role = Role.objects.filter(role=Role.LIQUOR_REVIEWER, site__in=[site for site in product.sites])
permission = UserRole.objects.filter(user=request.user, role=role).exists()
product_types = ProductType.objects. \
filter(index_name=ProductType.LIQUOR,
id=product.product_type_id)
if product_types.exists():
roles = Role.objects.filter(role=Role.LIQUOR_REVIEWER,
site_id=product.site_id)
if roles is not None:
permission = UserRole.objects.filter(user=request.user, role__in=[role for role in roles])\
.exists()
rules.append(permission)
return any(rules)
def has_object_permission(self, request, view, obj):
rules = [
super().has_object_permission(request, view, obj)
]
pk_object = None
product = None
permission = False
if 'pk' in view.kwargs:
pk_object = view.kwargs['pk']
if pk_object is not None:
product = Product.objects.get(pk=pk_object)
if product.sites.exists():
role = Role.objects.filter(role=Role.LIQUOR_REVIEWER, site__in=[site for site in product.sites])
permission = UserRole.objects.filter(user=request.user, role=role).exists()
rules.append(permission)
return any(rules)
#
# def has_object_permission(self, request, view, obj):
# rules = [
# super().has_object_permission(request, view, obj)
# ]
# # pk_object = None
# # product = None
# # permission = False
# #
# # if 'pk' in view.kwargs:
# # pk_object = view.kwargs['pk']
# #
# # if pk_object is not None:
# # product = Product.objects.get(pk=pk_object)
# #
# # if product.sites.exists():
# # role = Role.objects.filter(role=Role.LIQUOR_REVIEWER, site__in=[site for site in product.sites])
# # permission = UserRole.objects.filter(user=request.user, role=role).exists()
# #
# # rules.append(permission)
# return any(rules)