From d80948369b65ff7594ae5cf8d74aaad1f39cbb68 Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Sun, 27 Jun 2021 21:44:21 +1000
Subject: [PATCH] Include 'default' value in OPTIONS request for any fields
 with specified default values

---
 InvenTree/InvenTree/metadata.py               | 38 +++++++++++++++++--
 .../company/templates/company/index.html      | 25 ++++++++++--
 RELEASE.md                                    |  4 ++
 3 files changed, 61 insertions(+), 6 deletions(-)

diff --git a/InvenTree/InvenTree/metadata.py b/InvenTree/InvenTree/metadata.py
index 611b12101c..85815d2558 100644
--- a/InvenTree/InvenTree/metadata.py
+++ b/InvenTree/InvenTree/metadata.py
@@ -6,6 +6,7 @@ import logging
 
 from rest_framework import serializers
 from rest_framework.metadata import SimpleMetadata
+from rest_framework.utils import model_meta
 
 import users.models
 
@@ -41,11 +42,11 @@ class InvenTreeMetadata(SimpleMetadata):
 
         try:
             # Extract the model name associated with the view
-            model = view.serializer_class.Meta.model
+            self.model = view.serializer_class.Meta.model
 
             # Construct the 'table name' from the model
-            app_label = model._meta.app_label
-            tbl_label = model._meta.model_name
+            app_label = self.model._meta.app_label
+            tbl_label = self.model._meta.model_name
 
             table = f"{app_label}_{tbl_label}"
 
@@ -83,6 +84,37 @@ class InvenTreeMetadata(SimpleMetadata):
 
         return metadata
 
+    def get_serializer_info(self, serializer):
+        """
+        Override get_serializer_info so that we can add 'default' values
+        to any fields whose Meta.model specifies a default value
+        """
+
+        field_info = super().get_serializer_info(serializer)
+
+        try:
+            ModelClass = serializer.Meta.model
+
+            model_fields = model_meta.get_field_info(ModelClass)
+
+            for name, field in model_fields.fields.items():
+
+                if field.has_default() and name in field_info.keys():
+
+                    default = field.default
+
+                    if callable(default):
+                        try:
+                            default = default()
+                        except:
+                            continue
+
+                    field_info[name]['default'] = default
+        except AttributeError:
+            pass
+
+        return field_info
+
     def get_field_info(self, field):
         """
         Given an instance of a serializer field, return a dictionary
diff --git a/InvenTree/company/templates/company/index.html b/InvenTree/company/templates/company/index.html
index 3a3168f24e..2f7319fb74 100644
--- a/InvenTree/company/templates/company/index.html
+++ b/InvenTree/company/templates/company/index.html
@@ -15,10 +15,13 @@
 
 {% if pagetype == 'manufacturers' and roles.purchase_order.add or pagetype == 'suppliers' and roles.purchase_order.add or pagetype == 'customers' and roles.sales_order.add %}
 <div id='button-toolbar'>
+    <button type='button' class="btn btn-success" id='new-company'>
+        <span class='fas fa-plus-circle'></span> {{ button_text }} (Server Side)
+    </button>
+    <button type='button' class="btn btn-success" id='new-company-2'>
+        <span class='fas fa-plus-circle'></span> {{ button_text }} (Client Side)
+    </button>
     <div class='btn-group'>
-        <button type='button' class="btn btn-success" id='new-company'>
-            <span class='fas fa-plus-circle'></span> {{ button_text }}
-        </button>
     </div>
 </div>
 {% endif %}
@@ -35,6 +38,22 @@
         });
     });
 
+    $('#new-company-2').click(function() {
+        constructForm(
+            '{% url "api-company-list" %}',
+            {
+                method: 'POST',
+                fields: [
+                    'name',
+                    'description',
+                    'is_supplier',
+                    'is_manufacturer',
+                    'is_customer',
+                ]
+            }
+        );
+    });
+
     loadCompanyTable("#company-table", "{% url 'api-company-list' %}",
         {
             pagetype: '{{ pagetype }}',
diff --git a/RELEASE.md b/RELEASE.md
index 90fb027a62..7d6d0fba23 100644
--- a/RELEASE.md
+++ b/RELEASE.md
@@ -21,3 +21,7 @@ Create new release for the [inventree documentation](https://github.com/inventre
 ### Python Library Release
 
 Create new release for the [inventree python library](https://github.com/inventree/inventree-python)
+
+## App Release
+
+Create new versioned release for the InvenTree mobile app.