def albumoverview(request, albumid): user = Helpers.getUser(request) album = get_object_or_404(Album, id=albumid, owner=user); context = {"album": album} # Fetch pages. pages = AlbumPage.objects.filter(album=albumid) if request.method == 'POST': # Add new page? if request.POST["action"] == 'addpage': if "layout" in request.POST: layoutno = request.POST["layout"]; if not pages: pageno = 1 else: pageno = pages.count() + 1 albumpage = AlbumPage(album=album, layout=layoutno, pageno=pageno) albumpage.save() # a sluggish way of handling false submission. Should be implemented with Django Forms else: return redirect('/albums/%s?edit=true' % albumid) pages = AlbumPage.objects.filter(album=albumid) # construct 'photos' dictionaries for each page, like the albumpage view does for albumpage in pages: albumpage.photodict = Helpers.createPhotoDictionary(albumpage) albumpage.layouttemplate = 'albumpage-layout-%d.html' % albumpage.layout context["pages"] = pages # for convenience, editmode is passed to the template as a string that can # be appended to URLs if ("edit" in request.GET): context["editmode"] = "?edit=true" return render_to_response('albumoverview.html', context, context_instance=RequestContext(request))
def albumpage(request, albumid, pageno): pageno = int(pageno) user = Helpers.getUser(request) album = get_object_or_404(Album, id=albumid, owner=user) context = {"album": album} # Fetch information about the requested page context["pageno"] = pageno albumpage = get_object_or_404(AlbumPage, album=albumid, pageno=pageno) context["albumpage"] = albumpage if pageno > 1: context["prevpageno"] = int(pageno) - 1 if pageno < album.pages.count(): context["nextpageno"] = int(pageno) + 1 photosdict = Helpers.createPhotoDictionary(albumpage) context["photos"] = photosdict # If we returned from the search/basket page, there's a caption in the URL. Extract that # There is a maximum of 4 photos per page in any layout if request.method == 'GET': if "search-result-url" in request.GET: photono = request.GET["photono"] if "photono" in request.GET else None if photono in photosdict: photo = photosdict[photono] else: photo = Photo(albumpage_id=albumpage.id, photono=photono) photosdict[photono] = photo photo.url = request.GET["search-result-url"] photo.unsaved = True context["unsaved"] = True # If this was an edit, update the objects with the new values if request.method == 'POST': # There is a maximum of 4 photos per page in any layout for photono in range(1, 5): newcaption = request.POST.get("photo-%d-caption" % photono, None) newurl = request.POST.get("photo-%d-url" % photono, None) if newcaption or newurl: if photono in photosdict: photo = photosdict[photono] else: photo = Photo(albumpage_id=albumpage.id, photono=photono) photosdict[photono] = photo photo.caption = newcaption photo.url = newurl photo.save() album.cover = photo album.save() # Render response. # First choose the template to use for this page's layout. context["layouttemplate"] = 'albumpage-layout-%d.html' % albumpage.layout # for convenience, editmode is passed to the template as a string that can # be appended to URLs if "edit" in request.GET: context["editmode"] = "?edit=true" return render_to_response('albumpage.html', context, context_instance=RequestContext(request))