def get_achievement_photo(achievement_id): try: from Tools import rescale from Models import Achievement import cloudstorage as gcs from search import nearby_search if achievement_id == 'nearby': print 'nearby' lon = request.query['lon'] lat = request.query['lat'] place_documents = nearby_search(lon, lat, 500000) print place_documents if place_documents is None: return json.dumps({'status_code': 204}) achievement_id = place_documents[0]['achievement_id'] print achievement_id achievement = Achievement.get_by_id(int(achievement_id)) pictures = list(achievement.pictures) picture_id = pictures[0] size = [int(request.query['x']), int(request.query['y'])] gcs_filename = '/thebucket/picture/' + picture_id + '/photo' gcs_file = gcs.open(gcs_filename) data = gcs_file.read() return rescale.rescale(data, *size) except BaseException, ex: logging.exception("Exception: %s" % ex) print(str(ex)) return json.dumps({"status_code": 400})
def get_picture(picture_id): print picture_id print request.query['x'] print request.query['y'] size = [int(request.query['x']), int(request.query['y'])] from Tools import rescale import cloudstorage as gcs gcs_filename = '/thebucket/picture/' + picture_id + '/photo' gcs_file = gcs.open(gcs_filename) data = gcs_file.read() return rescale.rescale(data, *size)
def add_place(): try: # Create a new achievement to associate with the new place new_achievement = Achievement(name=ach_name, description=ach_description, points=ach_points, requirements=ach_requirements) achievement_key = new_achievement.put() achievement = achievement_key.get() achievement_pictures = [] if add_photos: try: # Do a place search to get photos google_places = GooglePlaces(TEST_KEY) query_result = google_places.text_search(place_name) for place in query_result.places: print "Google place: " + str(place.name) thumbnail_size = (128, 128) first = True place.get_details() print "Google place, photos: " + str(len(place.photos)) for photo in place.photos: photo.get(maxheight=2000, maxwidth=2000) photo_data = photo.data mime_type = photo.mimetype thumbnail_data = rescale.rescale(photo_data, *thumbnail_size) thumbnail = images.Image(thumbnail_data) thumbnail.im_feeling_lucky() # TODO Generate BlobStore entity for get_serving_url(Blobstore # Create Picture Entity to get a unique ID picture = Picture(votes=0, mime_type=mime_type) picture_key = picture.put() picture = picture_key.get() picture_id = str(picture_key.id()) picture.created = datetime.now() # Go ahead and add a link from the Achievement Entity to the Picture Entity picture.put() # Write to GCS and create blob_key gcs_filename = '/thebucket/picture/' + picture_id + '/photo' try: with gcs.open(gcs_filename, 'w', content_type=mime_type) as f: f.write(photo.data) blob_key_picture = blobstore.create_gs_key('/gs' + gcs_filename) picture.picture_url = images.get_serving_url(blob_key_picture) gcs_filename = '/thebucket/picture/' + picture_id + '/thumbnail' with gcs.open(gcs_filename, 'w', content_type=mime_type) as f: f.write(thumbnail.execute_transforms()) blob_key_thumbnail = blobstore.create_gs_key('/gs' + gcs_filename) picture.thumbnail_url = images.get_serving_url(blob_key_thumbnail) except BaseException, TransformationError: print str(TransformationError) continue picture.put() # Save the first photo as the achievements thumbnail if first: achievement.thumbnail = picture.thumbnail_url first = False achievement_pictures.append(picture_id) achievement.pictures = achievement_pictures #end for, break because we only want the first place returned break #end for except GooglePlacesError as error_detail: # You've passed in parameter values that the Places API doesn't like.. print error_detail # end if add_photos achievement.put() # create place document and add it to index place_document = search.Document( doc_id=str(achievement_key.id()), fields=[ search.TextField(name='name', value=place_name), search.TextField(name='country', value=place_country), search.TextField(name='state', value=place_state), search.TextField(name='city', value=place_city), search.GeoField(name='location', value=search.GeoPoint(place_lat, place_lon)), search.NumberField(name='radius', value=place_accuracy), search.NumberField(name='altitude', value=place_altitude), search.TextField(name='achievement_id', value=str(achievement_key.id())), search.DateField(name='created', value=datetime.now()), ]) index = search.Index(name="places-index") index.put(place_document) return "Achievement:\n" + str(achievement.to_dict()) + "\nPlace:\n" + str(place_document) except BaseException, ex: logging.exception("Add place exception: %s " % ex) return str(ex)