Merge remote-tracking branch 'inventree/master'

This commit is contained in:
Oliver Walters 2021-01-03 09:03:06 +11:00
commit a0642899b2
5 changed files with 125 additions and 12 deletions

View File

@ -5,10 +5,13 @@ from __future__ import unicode_literals
from .validators import allowable_url_schemes from .validators import allowable_url_schemes
from django.utils.translation import ugettext as _
from django.forms.fields import URLField as FormURLField from django.forms.fields import URLField as FormURLField
from django.db import models as models from django.db import models as models
from django.core import validators from django.core import validators
from django import forms from django import forms
from decimal import Decimal from decimal import Decimal
import InvenTree.helpers import InvenTree.helpers
@ -31,6 +34,32 @@ class InvenTreeURLField(models.URLField):
}) })
class DatePickerFormField(forms.DateField):
"""
Custom date-picker field
"""
def __init__(self, **kwargs):
help_text = kwargs.get('help_text', _('Enter date'))
required = kwargs.get('required', False)
initial = kwargs.get('initial', None)
widget = forms.DateInput(
attrs={
'type': 'date',
}
)
forms.DateField.__init__(
self,
required=required,
initial=initial,
help_text=help_text,
widget=widget
)
def round_decimal(value, places): def round_decimal(value, places):
""" """
Round value to the specified number of places. Round value to the specified number of places.

View File

@ -748,3 +748,6 @@ input[type="submit"] {
to { transform: scale(1) rotate(360deg);} to { transform: scale(1) rotate(360deg);}
} }
input[type="date"].form-control, input[type="time"].form-control, input[type="datetime-local"].form-control, input[type="month"].form-control {
line-height: unset;
}

View File

@ -10,6 +10,7 @@ from django import forms
from InvenTree.forms import HelperForm from InvenTree.forms import HelperForm
from InvenTree.fields import RoundingDecimalFormField from InvenTree.fields import RoundingDecimalFormField
from InvenTree.fields import DatePickerFormField
from .models import Build, BuildItem, BuildOrderAttachment from .models import Build, BuildItem, BuildOrderAttachment
@ -26,18 +27,16 @@ class EditBuildForm(HelperForm):
'batch': 'fa-layer-group', 'batch': 'fa-layer-group',
'serial-numbers': 'fa-hashtag', 'serial-numbers': 'fa-hashtag',
'location': 'fa-map-marker-alt', 'location': 'fa-map-marker-alt',
'target_date': 'fa-calendar-alt',
} }
field_placeholder = { field_placeholder = {
'reference': _('Build Order reference') 'reference': _('Build Order reference'),
'target_date': _('Order target date'),
} }
# TODO: Make this a more "presentable" date picker target_date = DatePickerFormField(
# TODO: Currently does not render super nicely with crispy forms help_text=_('Target date for build completion. Build will be overdue after this date.')
target_date = forms.DateField(
widget=forms.DateInput(
attrs={'type': 'date'}
)
) )
class Meta: class Meta:

View File

@ -12,6 +12,7 @@ from mptt.fields import TreeNodeChoiceField
from InvenTree.forms import HelperForm from InvenTree.forms import HelperForm
from InvenTree.fields import RoundingDecimalFormField from InvenTree.fields import RoundingDecimalFormField
from InvenTree.fields import DatePickerFormField
from stock.models import StockLocation from stock.models import StockLocation
from .models import PurchaseOrder, PurchaseOrderLineItem, PurchaseOrderAttachment from .models import PurchaseOrder, PurchaseOrderLineItem, PurchaseOrderAttachment
@ -120,6 +121,7 @@ class EditSalesOrderForm(HelperForm):
self.field_prefix = { self.field_prefix = {
'reference': 'SO', 'reference': 'SO',
'link': 'fa-link', 'link': 'fa-link',
'target_date': 'fa-calendar-alt',
} }
self.field_placeholder = { self.field_placeholder = {
@ -128,11 +130,8 @@ class EditSalesOrderForm(HelperForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# TODO: Improve this using a better date picker target_date = DatePickerFormField(
target_date = forms.DateField( help_text=_('Target date for order completion. Order will be overdue after this date.'),
widget=forms.DateInput(
attrs={'type': 'date'},
)
) )
class Meta: class Meta:

View File

@ -11,6 +11,7 @@ from django.contrib.auth.models import Group
from InvenTree.status_codes import PurchaseOrderStatus from InvenTree.status_codes import PurchaseOrderStatus
from .models import PurchaseOrder, PurchaseOrderLineItem from .models import PurchaseOrder, PurchaseOrderLineItem
from .models import SalesOrder
import json import json
@ -59,6 +60,88 @@ class OrderListTest(OrderViewTestCase):
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
class SalesOrderCreate(OrderViewTestCase):
"""
Create a SalesOrder using the form view
"""
URL = reverse('so-create')
def test_create_view(self):
"""
Retrieve the view for creating a sales order'
"""
response = self.client.get(self.URL, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.status_code, 200)
def post(self, data, **kwargs):
return self.client.post(self.URL, data, HTTP_X_REQUESTED_WITH='XMLHttpRequest', **kwargs)
def test_post_error(self):
"""
POST with errors
"""
n = SalesOrder.objects.count()
data = {
'reference': '12345678',
}
response = self.post(data)
data = json.loads(response.content)
self.assertIn('form_valid', data.keys())
# Customer is not specified - should return False
self.assertFalse(data['form_valid'])
errors = json.loads(data['form_errors'])
self.assertIn('customer', errors.keys())
self.assertIn('description', errors.keys())
# No new SalesOrder objects should have been created
self.assertEqual(SalesOrder.objects.count(), n)
def test_post_valid(self):
"""
POST a valid SalesOrder
"""
n = SalesOrder.objects.count()
data = {
'reference': '12345678',
'customer': 4,
'description': 'A description',
}
response = self.post(data)
json_data = json.loads(response.content)
self.assertTrue(json_data['form_valid'])
# Create another SalesOrder, this time with a target date
data = {
'reference': '12345679',
'customer': 4,
'description': 'Another order, this one with a target date!',
'target_date': '2020-12-25',
}
response = self.post(data)
json_data = json.loads(response.content)
self.assertEqual(SalesOrder.objects.count(), n + 2)
class POTests(OrderViewTestCase): class POTests(OrderViewTestCase):
""" Tests for PurchaseOrder views """ """ Tests for PurchaseOrder views """