Add migrate for product models

This commit is contained in:
Виктор Гладких 2019-12-10 10:45:35 +03:00
parent 6a3a40be4f
commit 6b8fdf7eed
6 changed files with 121 additions and 1 deletions

View File

@ -36,6 +36,8 @@ class Role(ProjectBaseMixin):
SALES_MAN = 8
WINERY_REVIEWER = 9 # Establishments subtype "winery"
SELLER = 10
LIQUOR_REVIEWER = 11
ROLE_CHOICES = (
(STANDARD_USER, 'Standard user'),
@ -47,7 +49,8 @@ class Role(ProjectBaseMixin):
(RESTAURANT_REVIEWER, 'Restaurant reviewer'),
(SALES_MAN, 'Sales man'),
(WINERY_REVIEWER, 'Winery reviewer'),
(SELLER, 'Seller')
(SELLER, 'Seller'),
(LIQUOR_REVIEWER, 'Liquor reviewer')
)
role = models.PositiveIntegerField(verbose_name=_('Role'), choices=ROLE_CHOICES,
null=False, blank=False)

View File

@ -0,0 +1,19 @@
# 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

@ -222,6 +222,8 @@ class Product(GalleryModelMixin, TranslatedFieldsMixin, BaseAttributes,
default=None, null=True,
verbose_name=_('Serial number'))
sites = models.ManyToManyField(to='main.SiteSettings')
objects = ProductManager.from_queryset(ProductQuerySet)()
class Meta:

79
apps/product/tests.py Normal file
View File

@ -0,0 +1,79 @@
from rest_framework.test import APITestCase
from rest_framework import status
from account.models import User
from http.cookies import SimpleCookie
from django.urls import reverse
# Create your tests here.
from translation.models import Language
from account.models import Role, UserRole
from location.models import Country, Address, City, Region
from main.models import SiteSettings
class BaseTestCase(APITestCase):
def setUp(self):
self.username = 'sedragurda'
self.password = 'sedragurdaredips19'
self.email = 'sedragurda@desoz.com'
self.newsletter = True
self.user = User.objects.create_user(
username=self.username,
email=self.email,
password=self.password,
is_staff=True,
)
# get tokens
tokens = User.create_jwt_tokens(self.user)
self.client.cookies = SimpleCookie(
{'access_token': tokens.get('access_token'),
'refresh_token': tokens.get('refresh_token')})
self.lang = Language.objects.create(
title='Russia',
locale='ru-RU'
)
self.country_ru = Country.objects.create(
name={'en-GB': 'Russian'},
code='RU',
)
self.region = Region.objects.create(name='Moscow area', code='01',
country=self.country_ru)
self.region.save()
self.city = City.objects.create(
name='Mosocow', code='01',
region=self.region,
country=self.country_ru)
self.city.save()
self.address = Address.objects.create(
city=self.city, street_name_1='Krasnaya',
number=2, postal_code='010100')
self.address.save()
self.site = SiteSettings.objects.create(
subdomain='ru',
country=self.country_ru
)
self.site.save()
self.role = Role.objects.create(role=Role.LIQUOR_REVIEWER,
site=self.site)
self.role.save()
self.user_role = UserRole.objects.create(
user=self.user, role=self.role)
self.user_role.save()
class LiquorReviewerTests(BaseTestCase):
def test_get(self):
url = reverse("back:product:list-create")
response = self.client.get(url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)

View File

@ -7,6 +7,7 @@ from product import serializers, models
from product.views import ProductBaseView
from utils.serializers import ImageBaseSerializer
from utils.views import CreateDestroyGalleryViewMixin
from utils.permissions import IsLiquorReviewer
class ProductBackOfficeMixinView(ProductBaseView):
@ -97,6 +98,7 @@ class ProductListCreateBackOfficeView(BackOfficeListCreateMixin, ProductBackOffi
generics.ListCreateAPIView):
"""Product back-office list-create view."""
serializer_class = serializers.ProductBackOfficeDetailSerializer
permission_classes = [IsLiquorReviewer]
class ProductTypeListCreateBackOfficeView(BackOfficeListCreateMixin,

View File

@ -449,3 +449,18 @@ class IsWineryReviewer(IsStandardUser):
super().has_object_permission(request, view, obj)
]
return any(rules)
class IsLiquorReviewer(IsStandardUser):
# Через establishment получать страну
def has_permission(self, request, view):
rules = [
super().has_permission(request, view)
]
return any(rules)
def has_object_permission(self, request, view, obj):
rules = [
super().has_object_permission(request, view, obj)
]
return any(rules)