diff --git a/InvenTree/InvenTree/metadata.py b/InvenTree/InvenTree/metadata.py index fa3dee4794..24e4dab7d7 100644 --- a/InvenTree/InvenTree/metadata.py +++ b/InvenTree/InvenTree/metadata.py @@ -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']: diff --git a/InvenTree/company/forms.py b/InvenTree/company/forms.py index 9da15a63e1..68e32628b4 100644 --- a/InvenTree/company/forms.py +++ b/InvenTree/company/forms.py @@ -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 diff --git a/InvenTree/company/models.py b/InvenTree/company/models.py index 1c2306fc6a..58cfbe7011 100644 --- a/InvenTree/company/models.py +++ b/InvenTree/company/models.py @@ -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'), diff --git a/InvenTree/company/serializers.py b/InvenTree/company/serializers.py index dd3175239b..9e64fbad0a 100644 --- a/InvenTree/company/serializers.py +++ b/InvenTree/company/serializers.py @@ -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 = [ diff --git a/InvenTree/company/test_api.py b/InvenTree/company/test_api.py index dd42b97801..345f4bf1a4 100644 --- a/InvenTree/company/test_api.py +++ b/InvenTree/company/test_api.py @@ -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): """