restucture

This commit is contained in:
Matthias 2021-05-18 10:19:35 +02:00
parent cb0ef30eff
commit ad4902ea44
2 changed files with 65 additions and 56 deletions

View File

@ -10,8 +10,6 @@ from decimal import Decimal, InvalidOperation
from django import forms from django import forms
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from djmoney.forms.fields import MoneyField
from InvenTree.forms import HelperForm from InvenTree.forms import HelperForm
from .files import FileManager from .files import FileManager
@ -119,21 +117,6 @@ class MatchItem(forms.Form):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def clean(number):
""" Clean-up decimal value """
# Check if empty
if not number:
return number
# Check if decimal type
try:
clean_number = Decimal(number)
except InvalidOperation:
clean_number = number
return clean_number.quantize(Decimal(1)) if clean_number == clean_number.to_integral() else clean_number.normalize()
# Setup FileManager # Setup FileManager
file_manager.setup() file_manager.setup()
@ -148,25 +131,13 @@ class MatchItem(forms.Form):
# Set field name # Set field name
field_name = col_guess.lower() + '-' + str(row['index']) field_name = col_guess.lower() + '-' + str(row['index'])
# check if field def was overriden
overriden_field = self.get_special_field(col_guess, row, file_manager)
if overriden_field:
self.fields[field_name] = overriden_field
# Create input for required headers # Create input for required headers
if col_guess in file_manager.REQUIRED_HEADERS: elif col_guess in file_manager.REQUIRED_HEADERS:
# Set field input box
# TODO maybe not here but in an own function?
if 'quantity' in col_guess.lower():
self.fields[field_name] = forms.CharField(
required=False,
widget=forms.NumberInput(attrs={
'name': 'quantity' + str(row['index']),
'class': 'numberinput', # form-control',
'type': 'number',
'min': '0',
'step': 'any',
'value': clean(row.get('quantity', '')),
})
)
else:
# Get value # Get value
value = row.get(col_guess.lower(), '') value = row.get(col_guess.lower(), '')
# Set field input box # Set field input box
@ -204,16 +175,6 @@ class MatchItem(forms.Form):
# Get value # Get value
value = row.get(col_guess.lower(), '') value = row.get(col_guess.lower(), '')
# Set field input box # Set field input box
if 'price' in col_guess.lower():
self.fields[field_name] = MoneyField(
label=_(col_guess),
default_currency=InvenTreeSetting.get_setting('INVENTREE_DEFAULT_CURRENCY'),
decimal_places=5,
max_digits=19,
required=False,
default_amount=clean(value),
)
else:
self.fields[field_name] = forms.CharField( self.fields[field_name] = forms.CharField(
required=False, required=False,
initial=value, initial=value,
@ -237,3 +198,22 @@ class MatchItem(forms.Form):
if item_match: if item_match:
# Update initial value # Update initial value
self.fields[field_name].initial = item_match.id self.fields[field_name].initial = item_match.id
def clean_nbr(self, number):
""" Clean-up decimal value """
# Check if empty
if not number:
return number
# Check if decimal type
try:
clean_number = Decimal(number)
except InvalidOperation:
clean_number = number
return clean_number.quantize(Decimal(1)) if clean_number == clean_number.to_integral() else clean_number.normalize()
def get_special_field(self, col_guess, row, file_manager):
""" function to be overriden in inherited forms to add specific form settings """
pass

View File

@ -15,7 +15,7 @@ from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.views.generic import DetailView, ListView, UpdateView from django.views.generic import DetailView, ListView, UpdateView
from django.views.generic.edit import FormMixin from django.views.generic.edit import FormMixin
from django.forms import HiddenInput, IntegerField from django.forms import HiddenInput, IntegerField, CharField, NumberInput
import logging import logging
from decimal import Decimal, InvalidOperation from decimal import Decimal, InvalidOperation
@ -44,6 +44,8 @@ from InvenTree.views import InvenTreeRoleMixin
from InvenTree.status_codes import PurchaseOrderStatus, SalesOrderStatus, StockStatus from InvenTree.status_codes import PurchaseOrderStatus, SalesOrderStatus, StockStatus
from djmoney.forms.fields import MoneyField
logger = logging.getLogger("inventree") logger = logging.getLogger("inventree")
@ -580,6 +582,33 @@ class PurchaseOrderUpload(FileManagementFormView):
""" set special field """ """ set special field """
# run default # run default
super().get_special_field(col_guess, row, file_manager) super().get_special_field(col_guess, row, file_manager)
# set quantity field
if 'quantity' in col_guess.lower():
return CharField(
required=False,
widget=NumberInput(attrs={
'name': 'quantity' + str(row['index']),
'class': 'numberinput',
'type': 'number',
'min': '0',
'step': 'any',
'value': self.clean_nbr(row.get('quantity', '')),
})
)
# set price field
elif 'price' in col_guess.lower():
return MoneyField(
label=_(col_guess),
default_currency=InvenTreeSetting.get_setting('INVENTREE_DEFAULT_CURRENCY'),
decimal_places=5,
max_digits=19,
required=False,
default_amount=self.clean_nbr(row.get('price', '')),
)
name = 'order' name = 'order'
form_list_override = [ form_list_override = [
('items', MyMatch), ('items', MyMatch),