def post(self): response = {} # get current user try: user = StreamUser.get_current_user(self) except urllib2.HTTPError: fh.bad_request_error(self, response, 'HTTPERROR... authToken = {}'.format(self.get_request_param(fh.auth_token_parm))) return if user is None: fh.bad_request_error(self, response, 'Not logged in') return stream_id = self.get_request_param(fh.stream_id_parm) if stream_id is None or stream_id == '': fh.bad_request_error(self, response, 'No parameter {} found'.format(fh.stream_id_parm)) return # get the stream stream = Stream.get_by_id(stream_id) if stream is None: fh.bad_request_error(self, response, 'Invalid Stream ID') return n_ups = max(1, fh.get_num_uploads(self)) print('\nThere were {} uploads\n'.format(n_ups)) for i in range(n_ups): upload = fh.get_upload_from_filehandler(self, i) url = self.get_request_param(fh.url_parm) if upload is not None: image_url = fh.get_file_url(upload) name = upload.filename elif url not in [None, '']: try: urllib2.urlopen(url) image_url = url name = upload except: image_url = None else: image_url = None if image_url is not None: iscover = self.get_request_param('iscover') if iscover: stream.set_cover_image_url(image_url) else: lat = self.get_request_param('lat') print "Latitude = ", lat lng = self.get_request_param('lng') print "Longitude = ", lng # create StreamItem entity item = StreamItem.create( owner=user, file=upload, URL=image_url, name=name, stream=stream, latitude=lat, longitude=lng) # update stream list of images stream.add_item(item) # go back to viewstream page redirect = str(self.get_request_param(fh.redirect_parm)) if redirect: self.redirect(redirect) return else: self.set_content_text_plain() response = {'status': 'Upload successful', 'image_url': image_url } self.write_response(json.dumps(response)) return
def get(self): self.set_content_text_plain() response = {} stream_id = self.get_request_param(fh.stream_id_parm) if stream_id is None or stream_id == '': fh.bad_request_error( self, response, 'No parameter {} found'.format(fh.stream_id_parm)) return # get the stream stream = Stream.get_by_id(stream_id) if stream is None: fh.bad_request_error(self, response, 'Invalid Stream ID') return # write some stream info response[fh.stream_name_parm] = stream.name response[fh.owner_parm] = stream.get_owner_from_db().nickName response[fh.num_images_parm] = len(stream.items) # get the indices ind1, ind2, status = fh.get_image_range_param(self) if ind1 is None or ind2 is None: fh.bad_request_error(self, response, status) return # query for images items, in1, ind2 = stream.get_items(ind1, ind2) image_urls = [item.URL for item in items] if len(image_urls) == 0: response[fh.image_range_parm] = None else: response[fh.image_range_parm] = "{0}-{1}".format(ind1, ind2) # get the tags stream_tags = StreamTag.get_batch_by_stream(stream) response[fh.tags_parm] = [t.get_tag_name() for t in stream_tags] #get lat/lng data for GeoMap item_loc = [] for item in items: if item.getLatLng() is not None: oneItem = [] oneItem.append(item.stream.id()) oneItem.append(item.dateAdded.strftime("%Y-%m-%d %H:%M:%S")) if item.URL is None: imageURL = fh.get_file_url(item.blobKey) else: imageURL = item.URL oneItem.append(imageURL) oneItem.append(item.latitude) oneItem.append(item.longitude) item_loc.append(oneItem) response['streamItemsLoc'] = item_loc response['urls'] = image_urls self.write_response(json.dumps(response))