def get(self): user = users.get_current_user() if user: nickname = user.nickname() login_url = users.create_logout_url('/') login_text = 'Sign out' else: self.redirect("/") return # retrieve request parameters stream_id = self.get_request_param(fh.stream_id_parm) # retrieve the stream from the ID try: stream = (ndb.Key('Stream', int(stream_id))).get() except: self.redirect('/') return if stream is None: self.redirect('/') return active_image = self.get_request_param(fh.active_image_parm) try: active_image = int(active_image) except (TypeError, ValueError): active_image = 0 #Increment view counter stream.viewList.append(datetime.now()) stream.numViews = stream.numViews + 1 stream.put() upload_url = blobstore.create_upload_url('/services/upload') # get the current image range ind1, ind2, status = fh.get_image_range_param(self) if ind1 is None or ind2 is None: ind1 = 1 ind2 = images_per_page # make call to viewimage service viewstream_service_url = fh.get_viewstream_service_url( stream_id, ind1, ind2) result = urllib2.urlopen(viewstream_service_url) response = json.loads("".join(result.readlines())) image_urls = response['urls'] tags = response[fh.tags_parm] tags = [{'name': tag, 'url': fh.get_viewtag_url(tag)} for tag in tags] #Values for GeoMap streamItemsLoc = response['streamItemsLoc'] # get total number of images and make links num_images = response[fh.num_images_parm] # get next 10 images link next_page_url = None if ind2 < num_images: next_page_url = fh.get_viewstream_url(stream_id, ind1 + images_per_page, ind2 + images_per_page) # get previous 10 images link prev_page_url = None if ind1 > 1: prev_page_url = fh.get_viewstream_url(stream_id, ind1 - images_per_page, ind2 - images_per_page) # see if user is subscribed to this stream # make call to subscribed service subscribed_service_url = fh.get_subscribed_service_url( user.user_id(), stream_id) result = urllib2.urlopen(subscribed_service_url) response = json.loads("".join(result.readlines())) is_subscribed = response['status'] redirect_url = urllib2.quote( fh.get_viewstream_url(stream_id, ind1, ind2)) if is_subscribed: sub_url = fh.get_unsubscribe_service_url(user.user_id(), stream_id, redirect_url) else: sub_url = fh.get_subscribe_service_url(user.user_id(), stream_id, redirect_url) item_data = [] items = stream.get_all_items() for i in range(len(items)): item = items[i] prev_ind = i - (i % 10) + 1 stream_url = fh.get_viewstream_url(stream.get_id(), prev_ind, prev_ind + 9, i - prev_ind + 1) if item.getLatLng() is not None: item_data.append({ "lat": item.latitude, "lng": item.longitude, "url": item.URL, "stream_name": stream.name, "stream_url": stream_url, "date_added": str(item.dateAdded) }) template_values = { 'html_template': 'MasterTemplate.html', 'stream': stream, 'stream_id': stream.stream_id(), 'upload_url': upload_url, 'image_urls': image_urls, 'user': user, 'login_url': login_url, 'login_text': login_text, 'is_subscribed': is_subscribed, 'sub_url': sub_url, 'tags': tags, 'tag_name_parm': fh.tag_name_parm, 'tag_url': fh.get_tagmod_url_noparm(), 'redirect_url': self.get_current_url(), 'stream_id_parm': fh.stream_id_parm, 'redirect_parm': fh.redirect_parm, 'url_parm': fh.url_parm, 'item_data': item_data, 'active_image': active_image } if next_page_url: template_values['next_page_url'] = next_page_url if prev_page_url: template_values['prev_page_url'] = prev_page_url path = os.path.join(os.path.dirname(__file__), '../../templates/ViewStream.html') self.response.write(template.render(path, template_values))
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))