def post(self): photo = Photo(parent=db_key(DB_NAME)) photo.approved = False photo.caption = self.request.get('caption') file_upload = self.request.get('file-upload') photo.put() bucket_name = os.environ.get( 'BUCKET_NAME', app_identity.get_default_gcs_bucket_name()) photo_filename = "/%s/%s" % (bucket_name, photo.key.urlsafe()) write_retry_params = gcs.RetryParams(backoff_factor=1.1) gcs_file = gcs.open(photo_filename, 'w', content_type='image/gif', retry_params=write_retry_params) gcs_file.write(file_upload) gcs_file.close() self.redirect('')
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))