From 2b08b0f2b9a3588be99f2a35b47f1fb2b8b158a0 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 Mar 2020 21:50:18 +1100 Subject: [PATCH 1/4] Add new fields for Part object - Creation date - Creation user - Responsible user --- .../migrations/0031_auto_20200318_1044.py | 31 +++++++++++++++++++ InvenTree/part/models.py | 9 ++++++ 2 files changed, 40 insertions(+) create mode 100644 InvenTree/part/migrations/0031_auto_20200318_1044.py diff --git a/InvenTree/part/migrations/0031_auto_20200318_1044.py b/InvenTree/part/migrations/0031_auto_20200318_1044.py new file mode 100644 index 0000000000..c43af364f2 --- /dev/null +++ b/InvenTree/part/migrations/0031_auto_20200318_1044.py @@ -0,0 +1,31 @@ +# Generated by Django 2.2.9 on 2020-03-18 10:44 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('part', '0030_auto_20200318_1027'), + ] + + operations = [ + migrations.AddField( + model_name='part', + name='creation_date', + field=models.DateField(auto_now_add=True, null=True), + ), + migrations.AddField( + model_name='part', + name='creation_user', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='parts_created', to=settings.AUTH_USER_MODEL), + ), + migrations.AddField( + model_name='part', + name='responsible', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='parts_responible', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 312cdcaa69..39c61079ea 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -226,6 +226,9 @@ class Part(models.Model): active: Is this part active? Parts are deactivated instead of being deleted virtual: Is this part "virtual"? e.g. a software product or similar notes: Additional notes field for this part + creation_date: Date that this part was added to the database + creation_user: User who added this part to the database + responsible: User who is responsible for this part (optional) """ class Meta: @@ -448,6 +451,12 @@ class Part(models.Model): bom_checked_date = models.DateField(blank=True, null=True) + creation_date = models.DateField(auto_now_add=True, editable=False, blank=True, null=True) + + creation_user = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True, related_name='parts_created') + + responsible = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True, related_name='parts_responible') + def format_barcode(self): """ Return a JSON string for formatting a barcode for this Part object """ From 8186e4bab00665429f2b72f950a56f84ccb9d473 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 Mar 2020 21:50:38 +1100 Subject: [PATCH 2/4] Display creation information in part detail page --- InvenTree/part/templates/part/detail.html | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/InvenTree/part/templates/part/detail.html b/InvenTree/part/templates/part/detail.html index a37ad5823d..5b69ccb193 100644 --- a/InvenTree/part/templates/part/detail.html +++ b/InvenTree/part/templates/part/detail.html @@ -83,6 +83,22 @@ {{ part.minimum_stock }} {% endif %} + + {% trans "Creation Date" %} + {{ part.creation_date }} + + {% if part.creation_user %} + + {% trans "Created By" %} + {{ part.creation_user }} + + {% endif %} + {% if part.responsible %} + + {% trans "Responsible User" %} + {{ part.responsible }} + + {% endif %}
From a147ce42843f4d830e4d64077362ec816c3fdb56 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 Mar 2020 21:53:02 +1100 Subject: [PATCH 3/4] Save the current user when creating a new part --- InvenTree/part/views.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/InvenTree/part/views.py b/InvenTree/part/views.py index 5bdb101398..c7a1b959fa 100644 --- a/InvenTree/part/views.py +++ b/InvenTree/part/views.py @@ -366,7 +366,10 @@ class PartDuplicate(AjaxCreateView): if valid: # Create the new Part - part = form.save() + part = form.save(commit=False) + + part.creation_user = request.user + part.save() data['pk'] = part.pk data['text'] = str(part) @@ -488,7 +491,12 @@ class PartCreate(AjaxCreateView): if valid: # Create the new Part - part = form.save() + part = form.save(commit=False) + + # Record the user who created this part + part.creation_user = request.user + + part.save() data['pk'] = part.pk data['text'] = str(part) From d51ac2f5c27e156364dd6e544010cda22c173225 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 Mar 2020 22:00:32 +1100 Subject: [PATCH 4/4] Save creation user when making a new part via the API --- InvenTree/part/api.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/InvenTree/part/api.py b/InvenTree/part/api.py index b55e027152..a121553409 100644 --- a/InvenTree/part/api.py +++ b/InvenTree/part/api.py @@ -126,6 +126,24 @@ class PartList(generics.ListCreateAPIView): serializer_class = part_serializers.PartSerializer + def create(self, request, *args, **kwargs): + """ Override the default 'create' behaviour: + We wish to save the user who created this part! + + Note: Implementation coped from DRF class CreateModelMixin + """ + + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + + # Record the user who created this Part object + part = serializer.save() + part.creation_user = request.user + part.save() + + headers = self.get_success_headers(serializer.data) + return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) + def list(self, request, *args, **kwargs): """ Instead of using the DRF serialiser to LIST,