add form for locations
This commit is contained in:
parent
5ee0c7caa7
commit
37a304219f
|
@ -2,10 +2,11 @@ from django import forms
|
|||
from django.urls import reverse_lazy
|
||||
|
||||
from crispy_forms.helper import FormHelper
|
||||
from crispy_forms.layout import Layout, Submit, Button, Field
|
||||
from crispy_forms.layout import Submit, Button
|
||||
from crispy_forms.bootstrap import FormActions
|
||||
|
||||
from .models import Customer
|
||||
from core import utils
|
||||
from .models import Customer, Location
|
||||
|
||||
|
||||
class CustomerForm(forms.ModelForm):
|
||||
|
@ -30,3 +31,34 @@ class CustomerForm(forms.ModelForm):
|
|||
Button('cancel', 'Cancel', css_class="btn btn-secondary",
|
||||
onclick="closeModal()")
|
||||
))
|
||||
|
||||
|
||||
class LocationForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Location
|
||||
fields = (
|
||||
'name',
|
||||
'customer'
|
||||
)
|
||||
|
||||
def __init__(self, user=None, *args, **kwargs):
|
||||
super(LocationForm, self).__init__(*args, **kwargs)
|
||||
"""
|
||||
If the user is not a superuser it's always assigned to a customer which
|
||||
we can use to assign to the field.
|
||||
"""
|
||||
self.fields['customer'].queryset = (
|
||||
utils.objects_for_allowed_customers(
|
||||
Customer, user=user))
|
||||
|
||||
self.helper = FormHelper(self)
|
||||
self.helper.attrs = {
|
||||
'hx-post': reverse_lazy('device_category_create'),
|
||||
'id': 'device-category-form',
|
||||
}
|
||||
self.helper.layout.append(
|
||||
FormActions(
|
||||
Submit('save_category', 'Save'),
|
||||
Button('cancel', 'Cancel', css_class="btn btn-secondary",
|
||||
onclick="closeModal()")
|
||||
))
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
import pytest
|
||||
from mixer.backend.django import mixer
|
||||
|
||||
from customers import forms
|
||||
|
||||
pytestmark = pytest.mark.django_db
|
||||
|
||||
|
||||
def test_location_form(create_admin_user):
|
||||
fixture = create_admin_user()
|
||||
form = forms.LocationForm(user=fixture['admin'], data={})
|
||||
assert form.is_valid() is False, (
|
||||
"Should be false because no data was given")
|
||||
|
||||
data = {"name": "Main Office",
|
||||
"customer": 3}
|
||||
form = forms.LocationForm(user=fixture['admin'], data=data)
|
||||
assert form.is_valid() is False, (
|
||||
"Should be false because the customer doesn't exist.")
|
||||
|
||||
data = {"name": "Main Office",
|
||||
"customer": fixture['customer'].id}
|
||||
form = forms.LocationForm(user=fixture['admin'], data=data)
|
||||
assert form.is_valid() is True, ("Should be valid with the given data.")
|
Loading…
Reference in New Issue