Merge pull request #668 from SchrodingersGat/part-creation-details

Part creation details
This commit is contained in:
Oliver 2020-03-18 22:23:35 +11:00 committed by GitHub
commit 1259cea2c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 2 deletions

View File

@ -126,6 +126,24 @@ class PartList(generics.ListCreateAPIView):
serializer_class = part_serializers.PartSerializer 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): def list(self, request, *args, **kwargs):
""" """
Instead of using the DRF serialiser to LIST, Instead of using the DRF serialiser to LIST,

View File

@ -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),
),
]

View File

@ -226,6 +226,9 @@ class Part(models.Model):
active: Is this part active? Parts are deactivated instead of being deleted active: Is this part active? Parts are deactivated instead of being deleted
virtual: Is this part "virtual"? e.g. a software product or similar virtual: Is this part "virtual"? e.g. a software product or similar
notes: Additional notes field for this part 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: class Meta:
@ -448,6 +451,12 @@ class Part(models.Model):
bom_checked_date = models.DateField(blank=True, null=True) 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): def format_barcode(self):
""" Return a JSON string for formatting a barcode for this Part object """ """ Return a JSON string for formatting a barcode for this Part object """

View File

@ -83,6 +83,22 @@
<td>{{ part.minimum_stock }}</td> <td>{{ part.minimum_stock }}</td>
</tr> </tr>
{% endif %} {% endif %}
<tr>
<td><b>{% trans "Creation Date" %}</b></td>
<td>{{ part.creation_date }}</td>
</tr>
{% if part.creation_user %}
<tr>
<td><b>{% trans "Created By" %}</b></td>
<td>{{ part.creation_user }}</td>
</tr>
{% endif %}
{% if part.responsible %}
<tr>
<td><b>{% trans "Responsible User" %}</b></td>
<td>{{ part.responsible }}</td>
</tr>
{% endif %}
</table> </table>
</div> </div>
<div class='col-sm-6'> <div class='col-sm-6'>

View File

@ -366,7 +366,10 @@ class PartDuplicate(AjaxCreateView):
if valid: if valid:
# Create the new Part # Create the new Part
part = form.save() part = form.save(commit=False)
part.creation_user = request.user
part.save()
data['pk'] = part.pk data['pk'] = part.pk
data['text'] = str(part) data['text'] = str(part)
@ -488,7 +491,12 @@ class PartCreate(AjaxCreateView):
if valid: if valid:
# Create the new Part # 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['pk'] = part.pk
data['text'] = str(part) data['text'] = str(part)