Added arbitrary files and links to boxes
This commit is contained in:
@@ -6,10 +6,12 @@ from django.shortcuts import get_object_or_404, redirect, render
|
||||
from .forms import (
|
||||
BoxForm,
|
||||
BoxTypeForm,
|
||||
ThingFileForm,
|
||||
ThingFormSet,
|
||||
ThingLinkForm,
|
||||
ThingPictureForm,
|
||||
)
|
||||
from .models import Box, BoxType, Thing, ThingType
|
||||
from .models import Box, BoxType, Thing, ThingFile, ThingLink, ThingType
|
||||
|
||||
|
||||
@login_required
|
||||
@@ -46,12 +48,14 @@ def box_detail(request, box_id):
|
||||
def thing_detail(request, thing_id):
|
||||
"""Display details of a thing."""
|
||||
thing = get_object_or_404(
|
||||
Thing.objects.select_related('thing_type', 'box', 'box__box_type'),
|
||||
Thing.objects.select_related('thing_type', 'box', 'box__box_type').prefetch_related('files', 'links'),
|
||||
pk=thing_id
|
||||
)
|
||||
|
||||
boxes = Box.objects.select_related('box_type').all().order_by('id')
|
||||
picture_form = ThingPictureForm(instance=thing)
|
||||
file_form = ThingFileForm()
|
||||
link_form = ThingLinkForm()
|
||||
|
||||
if request.method == 'POST':
|
||||
action = request.POST.get('action')
|
||||
@@ -77,10 +81,49 @@ def thing_detail(request, thing_id):
|
||||
thing.save()
|
||||
return redirect('thing_detail', thing_id=thing.id)
|
||||
|
||||
elif action == 'add_file':
|
||||
file_form = ThingFileForm(request.POST, request.FILES)
|
||||
if file_form.is_valid():
|
||||
thing_file = file_form.save(commit=False)
|
||||
thing_file.thing = thing
|
||||
thing_file.save()
|
||||
return redirect('thing_detail', thing_id=thing.id)
|
||||
|
||||
elif action == 'add_link':
|
||||
link_form = ThingLinkForm(request.POST)
|
||||
if link_form.is_valid():
|
||||
thing_link = link_form.save(commit=False)
|
||||
thing_link.thing = thing
|
||||
thing_link.save()
|
||||
return redirect('thing_detail', thing_id=thing.id)
|
||||
|
||||
elif action == 'delete_file':
|
||||
file_id = request.POST.get('file_id')
|
||||
if file_id:
|
||||
try:
|
||||
thing_file = ThingFile.objects.get(pk=file_id, thing=thing)
|
||||
thing_file.file.delete()
|
||||
thing_file.delete()
|
||||
except ThingFile.DoesNotExist:
|
||||
pass
|
||||
return redirect('thing_detail', thing_id=thing.id)
|
||||
|
||||
elif action == 'delete_link':
|
||||
link_id = request.POST.get('link_id')
|
||||
if link_id:
|
||||
try:
|
||||
thing_link = ThingLink.objects.get(pk=link_id, thing=thing)
|
||||
thing_link.delete()
|
||||
except ThingLink.DoesNotExist:
|
||||
pass
|
||||
return redirect('thing_detail', thing_id=thing.id)
|
||||
|
||||
return render(request, 'boxes/thing_detail.html', {
|
||||
'thing': thing,
|
||||
'boxes': boxes,
|
||||
'picture_form': picture_form,
|
||||
'file_form': file_form,
|
||||
'link_form': link_form,
|
||||
})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user