Commit 7de554c6 authored by Jean-samuel TETTEKPOE's avatar Jean-samuel TETTEKPOE
Browse files

Fix DRF 3.8 breaking change default+read_only

parent 0166f5bf
Pipeline #9146 passed with stage
in 41 seconds
# -*- coding: utf-8 -*-
import requests
import os
from django.db import models
from django.conf import settings
from rest_framework import serializers, viewsets
from bars_django.utils import VirtualField, permission_logic, CurrentBarCreateOnlyDefault, CurrentUserCreateOnlyDefault
from bars_django.utils import VirtualField, permission_logic, CurrentBarDefault
from bars_core.perms import PerBarPermissionsOrAnonReadOnly, BarRolePermissionLogic
from bars_core.models.bar import Bar
from bars_core.models.user import User
......@@ -33,10 +32,14 @@ class BugReportSerializer(serializers.ModelSerializer):
fields = '__all__'
_type = VirtualField("BugReport")
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentBarCreateOnlyDefault())
author = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentUserCreateOnlyDefault())
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(CurrentBarDefault()))
author = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(serializers.CurrentUserDefault()))
def create(self, data):
if 'author' not in data:
data['author'] = self.context['request'].user
if 'bar' not in data:
data['bar'] = self.context['request'].bar
b = super(BugReportSerializer, self).create(data)
if settings.SLACK_HOOK:
proxies = settings.PROXIES
......
......@@ -6,7 +6,7 @@ from rest_framework import serializers, viewsets
from rest_framework.response import Response
from rest_framework.decorators import action
from bars_django.utils import VirtualField, permission_logic, CurrentBarCreateOnlyDefault
from bars_django.utils import VirtualField, permission_logic, CurrentBarDefault
from bars_core.models.bar import Bar
from bars_core.models.user import User, get_default_user
from bars_core.models.role import Role
......@@ -43,7 +43,12 @@ class AccountSerializer(serializers.ModelSerializer):
read_only_fields = ('bar', 'money', 'overdrawn_since', 'last_modified')
_type = VirtualField("Account")
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentBarCreateOnlyDefault())
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(CurrentBarDefault()))
def create(self, data):
if 'bar' not in data:
data['bar'] = self.context['request'].bar
return super(AccountSerializer, self).create(data)
class AccountViewSet(viewsets.ModelViewSet):
......
from django.db import models
from django.db.models import fields
from bars_django.utils import VirtualField, permission_logic, get_root_bar, CurrentBarCreateOnlyDefault
from bars_django.utils import VirtualField, permission_logic, get_root_bar, CurrentBarDefault
from rest_framework import viewsets, serializers
from rest_framework.response import Response
......@@ -44,9 +44,14 @@ class RoleSerializer(serializers.ModelSerializer):
model = Role
fields = '__all__'
_type = VirtualField("Role")
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentBarCreateOnlyDefault())
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(CurrentBarDefault()))
perms = serializers.ListField(child=serializers.CharField(max_length=127), read_only=True, source='get_permissions')
def create(self, data):
if 'bar' not in data:
data['bar'] = self.context['request'].bar
return super(RoleSerializer, self).create(data)
class RoleViewSet(viewsets.ModelViewSet):
queryset = Role.objects.all()
......
......@@ -64,32 +64,11 @@ class BarMiddleware(MiddlewareMixin):
return None
class CurrentBarDefault:
requires_context = True
class CurrentUserCreateOnlyDefault:
is_update = None
user = None
def set_context(self, serializer_field):
self.is_update = serializer_field.parent.instance is not None
self.user = serializer_field.context['request'].user
def __call__(self):
if self.is_update:
raise fields.SkipField()
return self.user
class CurrentBarCreateOnlyDefault:
is_update = None
bar = None
def set_context(self, serializer_field):
self.is_update = serializer_field.parent.instance is not None
self.bar = serializer_field.context['request'].bar
def __call__(self):
if self.is_update:
raise fields.SkipField()
return self.bar
def __call__(self, serializer_field):
return serializer_field.context['request'].bar
def get_client_ip(request):
"""
......
......@@ -6,7 +6,7 @@ from rest_framework.response import Response
from rest_framework.serializers import ValidationError
from rest_framework.fields import CreateOnlyDefault
from bars_django.utils import VirtualField, permission_logic, CurrentBarCreateOnlyDefault
from bars_django.utils import VirtualField, permission_logic, CurrentBarDefault
from bars_core.perms import PerBarPermissionsOrAnonReadOnly, BarRolePermissionLogic, RootBarRolePermissionLogic, RootBarPermissionsOrAnonReadOnly
from bars_core.models.bar import Bar
from bars_items.models.itemdetails import ItemDetails
......@@ -104,7 +104,7 @@ class BuyItemPriceSerializer(serializers.ModelSerializer):
read_only_fields = ("bar",)
_type = VirtualField("BuyItemPrice")
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentBarCreateOnlyDefault())
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(CurrentBarDefault()))
barcode = serializers.CharField(required=False, write_only=True)
buyitem = serializers.PrimaryKeyRelatedField(required=False, default=None, queryset=BuyItem.objects.all())
price = serializers.FloatField(required=False)
......
......@@ -7,7 +7,7 @@ from rest_framework import viewsets, serializers, exceptions
from rest_framework.response import Response
from rest_framework.decorators import action
from bars_django.utils import VirtualField, permission_logic, CurrentBarCreateOnlyDefault
from bars_django.utils import VirtualField, permission_logic, CurrentBarDefault
from bars_core.perms import PerBarPermissionsOrAnonReadOnly, BarRolePermissionLogic
from bars_core.models.bar import Bar
from bars_items.models.stockitem import StockItem
......@@ -100,13 +100,18 @@ class SellItemSerializer(serializers.ModelSerializer):
'unit_factor': {'required': False}}
_type = VirtualField("SellItem")
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentBarCreateOnlyDefault())
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(CurrentBarDefault()))
stockitems = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
fuzzy_qty = serializers.FloatField(read_only=True, source='calc_qty')
fuzzy_price = serializers.FloatField(read_only=True, source='calc_price')
unit_factor = serializers.FloatField(write_only=True, default=1)
oldest_inventory = serializers.DateTimeField(read_only=True, source='calc_oldest_inventory')
def create(self, data):
if 'bar' not in data:
data['bar'] = self.context['request'].bar
return super(SellItemSerializer, self).create(data)
class MergeSellItemSerializer(serializers.Serializer):
sellitem = serializers.PrimaryKeyRelatedField(queryset=SellItem.objects.all())
......
......@@ -5,7 +5,7 @@ from rest_framework import viewsets, serializers
from rest_framework.response import Response
from rest_framework.decorators import action
from bars_django.utils import VirtualField, permission_logic, CurrentBarCreateOnlyDefault
from bars_django.utils import VirtualField, permission_logic, CurrentBarDefault
from bars_core.perms import PerBarPermissionsOrAnonReadOnly, BarRolePermissionLogic
from bars_core.models.bar import Bar
# from bars_items.models.itemdetails import ItemDetails
......@@ -89,12 +89,17 @@ class StockItemSerializer(serializers.ModelSerializer):
exclude = ('unit_factor',)
_type = VirtualField("StockItem")
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentBarCreateOnlyDefault())
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(CurrentBarDefault()))
qty = serializers.FloatField(source='sell_qty', read_only=True)
price = serializers.FloatField(source='display_price')
sell_to_buy = serializers.FloatField()
last_inventory = serializers.DateTimeField(read_only=True)
def create(self, data):
if 'bar' not in data:
data['bar'] = self.context['request'].bar
return super(StockItemSerializer, self).create(data)
def validate_sell_to_buy(self, value):
if value <= 0:
raise ValidationError("'sell_to_buy' field has to be nonnegative")
......
......@@ -5,7 +5,7 @@ from rest_framework import serializers, viewsets, status
from rest_framework.response import Response
from rest_framework.decorators import action
from bars_django.utils import VirtualField, permission_logic, CurrentBarCreateOnlyDefault, CurrentUserCreateOnlyDefault
from bars_django.utils import VirtualField, permission_logic, CurrentBarDefault
from bars_core.models.bar import Bar
from bars_core.models.user import User
from bars_core.perms import PerBarPermissionsOrAnonReadOnly, BarRolePermissionLogic
......@@ -31,10 +31,12 @@ class SuggestedItemSerializer(serializers.ModelSerializer):
fields = '__all__'
_type = VirtualField("SuggestedItem")
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentBarCreateOnlyDefault())
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(CurrentBarDefault()))
voters_list = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
def create(self, validated_data):
if 'bar' not in validated_data:
validated_data['bar'] = self.context['request'].bar
si = SuggestedItem.objects.create(**validated_data)
si.voters_list.add(self.context.get('request').user)
return si
......
from django.db import models
from django.http import Http404
from rest_framework import serializers, viewsets
from rest_framework.response import Response
from django.core.exceptions import ValidationError
from bars_django.utils import VirtualField, permission_logic, CurrentBarCreateOnlyDefault, CurrentUserCreateOnlyDefault
from bars_django.utils import VirtualField, permission_logic, CurrentBarDefault
from bars_core.perms import BarRolePermissionLogic, PerBarPermissionsOrObjectPermissionsOrAnonReadOnly
from bars_core.models.bar import Bar
from bars_core.models.user import User
......@@ -72,8 +71,8 @@ class MenuSellItemSerializer(serializers.ModelSerializer):
class MenuSerializer(serializers.ModelSerializer):
_type = VirtualField("Menu")
items = MenuSellItemSerializer(many=True)
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentBarCreateOnlyDefault())
user = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentUserCreateOnlyDefault())
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(CurrentBarDefault()))
user = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(serializers.CurrentUserDefault()))
class Meta:
model = Menu
......@@ -81,6 +80,11 @@ class MenuSerializer(serializers.ModelSerializer):
read_only_fields = ('bar', 'user', )
def create(self, data):
if 'bar' not in data:
data['bar'] = self.context['request'].bar
if 'user' not in data:
data['user'] = self.context['request'].user
items = data.pop('items')
menu = super(MenuSerializer, self).create(data)
......
......@@ -2,7 +2,7 @@ from django.db import models
from rest_framework import serializers, viewsets, filters
from django_filters.rest_framework import DjangoFilterBackend
from bars_django.utils import VirtualField, permission_logic, CurrentBarCreateOnlyDefault, CurrentUserCreateOnlyDefault
from bars_django.utils import VirtualField, permission_logic, CurrentBarDefault
from bars_core.models.bar import Bar
from bars_core.models.user import User
from bars_core.perms import PerBarPermissionsOrAnonReadOnly, BarRolePermissionLogic
......@@ -32,8 +32,15 @@ class NewsSerializer(serializers.ModelSerializer):
fields = '__all__'
_type = VirtualField("News")
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentBarCreateOnlyDefault())
author = serializers.PrimaryKeyRelatedField(read_only=True, default=CurrentUserCreateOnlyDefault())
bar = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(CurrentBarDefault()))
author = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CreateOnlyDefault(serializers.CurrentUserDefault()))
def create(self, data):
if 'author' not in data:
data['author'] = self.context['request'].user
if 'bar' not in data:
data['bar'] = self.context['request'].bar
return super(NewsSerializer, self).create(data)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment