Skip to content

Commit

Permalink
#4 parts view updated
Browse files Browse the repository at this point in the history
  • Loading branch information
chs8691 committed Dec 1, 2019
1 parent 7ce1a94 commit 097765f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Binary file modified db.sqlite3
Binary file not shown.
35 changes: 26 additions & 9 deletions myequis/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import humanize
from django.db.models import Q, OuterRef, Exists
from django.db.models import Q, OuterRef, Exists, Subquery
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.template import loader
Expand Down Expand Up @@ -319,8 +319,8 @@ def get(self, request, *args, **kwargs):
'km': record.km,
'id': record.id})

mounted = len(Material.objects.filter(mount_record_id=record.id))
dismounted = len(Material.objects.filter(dismount_record_id=record.id))
mounted = len(Mounting.objects.filter(mount_record_id=record.id))
dismounted = len(Mounting.objects.filter(dismount_record_id=record.id))

template = loader.get_template('myequis/edit_record.html')
return HttpResponse(
Expand Down Expand Up @@ -475,12 +475,18 @@ def list_bicycle_parts(request, bicycle_id):
bicycle = Bicycle.objects.get(pk=bicycle_id)
records = Record.objects.filter(bicycle__id=bicycle_id).order_by('-date')

# Actual mounted
materials = Material.objects.filter(mount_record__bicycle_id=bicycle.id).filter(dismount_record=None)
# All actual Mountings for this bicycle
mountings = Mounting.objects.annotate(
bicycle_found=Exists(
Record.objects.filter(bicycle_id=bicycle.id)
)
).filter(bicycle_found=True, dismount_record=None)

logger.warning("mountings={}".format(str(mountings)))

component_data_list = []

for component in Component.objects.all():
for component in Component.objects.filter(species_id=bicycle.species.id):
component_data = dict()

component_data['component_name'] = component.name
Expand All @@ -490,15 +496,26 @@ def list_bicycle_parts(request, bicycle_id):
part_data_list = []

for part in parts:
part_material = next((m for m in materials if m.part.id == part.id), None)
mounting = next((m for m in mountings if m.part.id == part.id), None)
if mounting is not None:
part_material = mounting.material
else:
part_material = None

distance = ""
if part_material is not None:
distance = 0
# logger.warning("part={}".format(str(part)))
# logger.warning("partMaterial={}".format(str(part_material)))

if len(records) > 0:
distance = (records[0].km - part_material.mount_record.km)
# Material can be mounted multiple times and in different bicycles
for mounting in Mounting.objects.filter(material_id=part_material.id):
if mounting.dismount_record is None:
# actual mounted
distance += records[0].km - mounting.mount_record.km
else:
# Materials history
distance += mounting.dismount_record.km - mounting.mount_record.km

# logger.warning("part={}".format(str(part)))
part_data_list.append(dict(part=part, material=part_material, distance=distance, ))
Expand Down

0 comments on commit 097765f

Please sign in to comment.