mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add "default" from serializer field (if present)
- Overrides model default - Set choices for currency serializer field - Adds some unit testing
This commit is contained in:
parent
8b3a497554
commit
33ec91acc7
@ -7,6 +7,7 @@ import logging
|
||||
from rest_framework import serializers
|
||||
from rest_framework.metadata import SimpleMetadata
|
||||
from rest_framework.utils import model_meta
|
||||
from rest_framework.fields import empty
|
||||
|
||||
import users.models
|
||||
|
||||
@ -146,6 +147,10 @@ class InvenTreeMetadata(SimpleMetadata):
|
||||
# Run super method first
|
||||
field_info = super().get_field_info(field)
|
||||
|
||||
# If a default value is specified for the serializer field, add it!
|
||||
if 'default' not in field_info and not field.default == empty:
|
||||
field_info['default'] = field.get_default()
|
||||
|
||||
# Introspect writable related fields
|
||||
if field_info['type'] == 'field' and not field_info['read_only']:
|
||||
|
||||
|
@ -11,7 +11,6 @@ from InvenTree.fields import RoundingDecimalFormField
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
import django.forms
|
||||
|
||||
import djmoney.settings
|
||||
from djmoney.forms.fields import MoneyField
|
||||
|
||||
from common.settings import currency_code_default
|
||||
|
@ -105,7 +105,11 @@ class Company(models.Model):
|
||||
blank=True,
|
||||
)
|
||||
|
||||
website = models.URLField(blank=True, verbose_name=_('Website'), help_text=_('Company website URL'))
|
||||
website = models.URLField(
|
||||
blank=True,
|
||||
verbose_name=_('Website'),
|
||||
help_text=_('Company website URL')
|
||||
)
|
||||
|
||||
address = models.CharField(max_length=200,
|
||||
verbose_name=_('Address'),
|
||||
|
@ -17,6 +17,8 @@ from .models import Company
|
||||
from .models import ManufacturerPart, ManufacturerPartParameter
|
||||
from .models import SupplierPart, SupplierPriceBreak
|
||||
|
||||
from common.settings import currency_code_default
|
||||
|
||||
|
||||
class CompanyBriefSerializer(InvenTreeModelSerializer):
|
||||
""" Serializer for Company object (limited detail) """
|
||||
@ -60,6 +62,11 @@ class CompanySerializer(InvenTreeModelSerializer):
|
||||
parts_supplied = serializers.IntegerField(read_only=True)
|
||||
parts_manufactured = serializers.IntegerField(read_only=True)
|
||||
|
||||
currency = serializers.ChoiceField(
|
||||
choices=djmoney.settings.CURRENCY_CHOICES,
|
||||
default=currency_code_default,
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Company
|
||||
fields = [
|
||||
|
@ -62,12 +62,79 @@ class CompanyTest(InvenTreeAPITestCase):
|
||||
self.assertEqual(response.data['name'], 'ACMOO')
|
||||
|
||||
def test_company_search(self):
|
||||
# Test search functionality in company list
|
||||
"""
|
||||
Test search functionality in company list
|
||||
"""
|
||||
|
||||
url = reverse('api-company-list')
|
||||
data = {'search': 'cup'}
|
||||
response = self.get(url, data)
|
||||
self.assertEqual(len(response.data), 2)
|
||||
|
||||
def test_company_create(self):
|
||||
"""
|
||||
Test that we can create a company via the API!
|
||||
"""
|
||||
|
||||
url = reverse('api-company-list')
|
||||
|
||||
# Name is required
|
||||
response = self.post(
|
||||
url,
|
||||
{
|
||||
'description': 'A description!',
|
||||
},
|
||||
expected_code=400
|
||||
)
|
||||
|
||||
# Minimal example, checking default values
|
||||
response = self.post(
|
||||
url,
|
||||
{
|
||||
'name': 'My API Company',
|
||||
'description': 'A company created via the API',
|
||||
},
|
||||
expected_code=201
|
||||
)
|
||||
|
||||
self.assertTrue(response.data['is_supplier'])
|
||||
self.assertFalse(response.data['is_customer'])
|
||||
self.assertFalse(response.data['is_manufacturer'])
|
||||
|
||||
self.assertEqual(response.data['currency'], 'USD')
|
||||
|
||||
# Maximal example, specify values
|
||||
response = self.post(
|
||||
url,
|
||||
{
|
||||
'name': "Another Company",
|
||||
'description': "Also created via the API!",
|
||||
'currency': 'AUD',
|
||||
'is_supplier': False,
|
||||
'is_manufacturer': True,
|
||||
'is_customer': True,
|
||||
},
|
||||
expected_code=201
|
||||
)
|
||||
|
||||
self.assertEqual(response.data['currency'], 'AUD')
|
||||
self.assertFalse(response.data['is_supplier'])
|
||||
self.assertTrue(response.data['is_customer'])
|
||||
self.assertTrue(response.data['is_manufacturer'])
|
||||
|
||||
# Attempt to create with invalid currency
|
||||
response = self.post(
|
||||
url,
|
||||
{
|
||||
'name': "A name",
|
||||
'description': 'A description',
|
||||
'currency': 'POQD',
|
||||
},
|
||||
expected_code=400
|
||||
)
|
||||
|
||||
self.assertTrue('currency' in response.data)
|
||||
|
||||
|
||||
class ManufacturerTest(InvenTreeAPITestCase):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user