2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-04 20:51:00 +00:00

[Feature] Company Addresses (#4732)

* Add initial model structure

* Initial Address model defined

* Add migration and unit tests

* Initial migration for Address model generated

* Unit tests for Address model added

* Move address field to new model

* Added migration to move address field to Address model

* Implement address feature to backend

* API endpoints for list and detail implemented

* Serializer class for Address implemented

* Final migration to delete old address field from company added

* Tests for API and migrations added

* Amend migration file names

* Fix migration names in test

* Add address property to company model

* Iinital view and JS code

* Fix indents

* Fix different things

* Pre-emptive change before merge

* Post-merge fixes

* dotdotdot...

* ...

* iDots

* .

* .

* .

* Add form functionality and model checks

* Forms require a confirmation slider to be checked to submit
  if address is selected as primary

* Backend resets primary address before saving if new address
  is designated as primary

* Fix pre-save logic to enforce primary uniqueness

* Fix typos

* Sort out migrations

* Forgot one

* Add admin entry and small fixes

* Fix migration file name and dependency

* Update InvenTree/company/models.py

Co-authored-by: Matthias Mair <code@mjmair.com>

* Update InvenTree/company/models.py

Co-authored-by: Matthias Mair <code@mjmair.com>

* Correct final issues

* .

---------

Co-authored-by: Matthias Mair <code@mjmair.com>
This commit is contained in:
Lavissa
2023-06-17 13:55:25 +02:00
committed by GitHub
parent 61d2f452b2
commit bf707766b6
28 changed files with 1185 additions and 28 deletions

View File

@ -0,0 +1,37 @@
# Generated by Django 3.2.18 on 2023-05-02 19:56
import InvenTree.fields
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('company', '0062_contact_metadata'),
]
operations = [
migrations.CreateModel(
name='Address',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(help_text='Title describing the address entry', max_length=100, verbose_name='Address title')),
('primary', models.BooleanField(default=False, help_text='Set as primary address', verbose_name='Primary address')),
('line1', models.CharField(blank=True, help_text='Address line 1', max_length=50, verbose_name='Line 1')),
('line2', models.CharField(blank=True, help_text='Address line 2', max_length=50, verbose_name='Line 2')),
('postal_code', models.CharField(blank=True, help_text='Postal code', max_length=10, verbose_name='Postal code')),
('postal_city', models.CharField(blank=True, help_text='Postal code city', max_length=50, verbose_name='City')),
('province', models.CharField(blank=True, help_text='State or province', max_length=50, verbose_name='State/Province')),
('country', models.CharField(blank=True, help_text='Address country', max_length=50, verbose_name='Country')),
('shipping_notes', models.CharField(blank=True, help_text='Notes for shipping courier', max_length=100, verbose_name='Courier shipping notes')),
('internal_shipping_notes', models.CharField(blank=True, help_text='Shipping notes for internal use', max_length=100, verbose_name='Internal shipping notes')),
('link', InvenTree.fields.InvenTreeURLField(blank=True, help_text='Link to address information (external)', verbose_name='Link')),
('company', models.ForeignKey(help_text='Select company', on_delete=django.db.models.deletion.CASCADE, related_name='addresses', to='company.company', verbose_name='Company')),
],
),
migrations.AddConstraint(
model_name='address',
constraint=models.UniqueConstraint(condition=models.Q(('primary', True)), fields=('company',), name='one_primary_per_company'),
),
]

View File

@ -0,0 +1,37 @@
# Generated by Django 3.2.18 on 2023-05-02 20:41
from django.db import migrations
def move_address_to_new_model(apps, schema_editor):
Company = apps.get_model('company', 'Company')
Address = apps.get_model('company', 'Address')
for company in Company.objects.all():
if company.address != '':
# Address field might exceed length of new model fields
l1 = company.address[:50]
l2 = company.address[50:100]
Address.objects.create(company=company,
title="Primary",
primary=True,
line1=l1,
line2=l2)
company.address = ''
company.save()
def revert_address_move(apps, schema_editor):
Address = apps.get_model('company', 'Address')
for address in Address.objects.all():
address.company.address = f'{address.line1}{address.line2}'
address.company.save()
address.delete()
class Migration(migrations.Migration):
dependencies = [
('company', '0063_auto_20230502_1956'),
]
operations = [
migrations.RunPython(move_address_to_new_model, reverse_code=revert_address_move)
]

View File

@ -0,0 +1,17 @@
# Generated by Django 3.2.18 on 2023-05-13 14:53
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('company', '0064_move_address_field_to_address_model'),
]
operations = [
migrations.RemoveField(
model_name='company',
name='address',
),
]

View File

@ -0,0 +1,22 @@
# Generated by Django 3.2.19 on 2023-06-16 20:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('company', '0065_remove_company_address'),
]
operations = [
migrations.AlterModelOptions(
name='address',
options={'verbose_name_plural': 'Addresses'},
),
migrations.AlterField(
model_name='address',
name='postal_city',
field=models.CharField(blank=True, help_text='Postal code city/region', max_length=50, verbose_name='City/Region'),
),
]