Add 'completed_by' field to Build

- On Complete() or Cancel() user field is filled in
This commit is contained in:
Oliver Walters 2019-05-03 07:58:46 +10:00
parent a70ec0b179
commit 019d5aa4ba
4 changed files with 37 additions and 4 deletions

View File

@ -0,0 +1,21 @@
# Generated by Django 2.2 on 2019-05-02 21:26
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),
('build', '0008_auto_20190501_2344'),
]
operations = [
migrations.AddField(
model_name='build',
name='completed_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='builds_completed', to=settings.AUTH_USER_MODEL),
),
]

View File

@ -7,6 +7,7 @@ from __future__ import unicode_literals
from datetime import datetime
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from django.core.exceptions import ValidationError
@ -116,13 +117,20 @@ class Build(models.Model):
completion_date = models.DateField(null=True, blank=True)
completed_by = models.ForeignKey(User,
on_delete=models.SET_NULL,
blank=True, null=True,
related_name='builds_completed'
)
URL = models.URLField(blank=True, help_text='Link to external URL')
notes = models.TextField(blank=True)
""" Notes attached to each build output """
@transaction.atomic
def cancelBuild(self):
def cancelBuild(self, user):
""" Mark the Build as CANCELLED
- Delete any pending BuildItem objects (but do not remove items from stock)
@ -133,8 +141,10 @@ class Build(models.Model):
for item in self.allocated_stock.all():
item.delete()
# Date of 'completion' is the date the build was cancelled
self.completion_date = datetime.now().date()
self.completed_by = user
self.status = self.CANCELLED
self.save()
@ -166,6 +176,8 @@ class Build(models.Model):
# Mark the date of completion
self.completion_date = datetime.now().date()
self.completed_by = user
# Add stock of the newly created item
item = StockItem.objects.create(
part=self.part,

View File

@ -66,9 +66,9 @@
</td>
</tr>
{% endif %}
{% if batch.completion_date %}
{% if build.completion_date %}
<tr>
<td>Completed</td><td>{{ build.creation_date }}</td>
<td>Completed</td><td>{{ build.completion_date }}{% if build.completed_by %}<span class='badge'>{{ build.completed_by }}</span>{% endif %}</td>
</tr>
{% endif %}
{% if build.notes %}

View File

@ -56,7 +56,7 @@ class BuildCancel(AjaxView):
build = get_object_or_404(Build, pk=self.kwargs['pk'])
build.cancelBuild()
build.cancelBuild(request.user)
return self.renderJsonResponse(request, None)