def post(self): user = StreamUser.get_current_user(self) if user is None: self.redirect("/") return # make call to unsubscribe service button = self.get_request_param('button') if button == "Cancel": self.redirect('/manage?{0}=Unsubscribe cancelled'.format( fh.message_parm)) return reg = re.compile(r'^unsubscribe_(.*)') matches = [ reg.match(key) for key in self.get_request_parameter_dictionary().keys() ] stream_ids = [ str(match.groups()[0]) for match in matches if match is not None ] message = "" for id in stream_ids: base_url = "http://{0}/services/unsubscribe".format( os.environ['HTTP_HOST']) delete_stream_url = '{0}?{1}={2};{3}={4}'.format( base_url, fh.stream_id_parm, id, fh.user_id_parm, StreamUser.get_current_user(self).user_id()) try: result = urllib2.urlopen(delete_stream_url) message = "{0}, {1}".format(message, result) except urllib2.HTTPError: self.redirect('/error?{0}={1}'.format( fh.message_parm, 'Error unsubscribing from stream')) #try to wait until streams are really gone... for id in stream_ids: while StreamSubscriber.get_by_stream_id_and_user_id( id, user.user_id()) is not None: pass self.redirect('http://{0}/manage?{1}={2}'.format( os.environ['HTTP_HOST'], fh.message_parm, 'Unsubscribed from streams'))
def post(self): user = StreamUser.get_current_user(self) if user: login_url = fh.get_logout_url(self, '/') login_text = 'Sign out' else: self.redirect("/") return stream_ids = json.loads(self.get_request_param(fh.stream_id_parm)) stream_names = json.loads(self.get_request_param(fh.stream_name_parm)) template_values = { 'html_template': 'MasterTemplate.html', 'user': user.nickName, 'login_url': login_url, 'login_text': login_text, 'unsub_streams': zip(stream_ids, stream_names), 'stream_id_parm': fh.stream_id_parm } self.set_content_text_html() path = os.path.join(os.path.dirname(__file__), '../../templates/Unsubscribe.html') self.response.write(fh.render_html_template(path, template_values))
def get(self): user = StreamUser.get_current_user(self) if user: login_url = fh.get_logout_url(self, '/') login_text = 'Sign out' else: self.redirect("/") return template_values = { 'html_template': 'MasterTemplate.html', 'user': user.nickName, 'email': user.email, 'login_url': login_url, 'login_text': login_text, 'stream_name_parm': fh.stream_name_parm, 'tags_parm': fh.tags_parm, 'cover_url_parm': fh.cover_url_parm, 'subs_parm': fh.subscribers_parm } self.set_content_text_html() path = os.path.join(os.path.dirname(__file__), '../../templates/Create.html') self.response.write(fh.render_html_template(path, template_values))
def get(self): self.set_content_text_plain() response = {} # get current user user_id = self.get_request_param(fh.user_id_parm) if user_id in ['', None]: fh.bad_request_error( self, response, 'No parameter {} found'.format(fh.user_id_parm)) return user = StreamUser.get_by_id(user_id) if user is None: fh.bad_request_error(self, response, 'Invalid user') return # query for all streams owned by user my_streams = Stream.get_ids_by_owner(user) # query for all streams subscribed to by user sub_streams = StreamSubscriber.get_by_user(user) # write some stream info response['owned_streams'] = [s for s in my_streams] response['subscribed_streams'] = [s.stream.id() for s in sub_streams] self.write_response(json.dumps(response))
def get(self): self.set_content_text_plain() response = {} # get auth token, if present auth_token = urllib.quote( str(self.get_request_param(fh.auth_token_parm))) user_data_str = urllib2.urlopen( 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=' + auth_token).read() user_data = json.loads(user_data_str) # get user from auth token user = StreamUser.get_by_email(user_data['email']) if user is None: fh.bad_request_error(self, response, 'Invalid user') return # query for all streams subscribed to by user sub_streams = StreamSubscriber.get_by_user(user) # write some stream info response = [ s.stream.get().get_meta_dict_with_most_recent_image_url() for s in sub_streams ] self.write_response(json.dumps(response))
def get(self): self.set_content_text_plain() response = {} # get current user owner_id = self.get_request_param(fh.user_id_parm) owner = StreamUser.get_by_id(owner_id) if owner is None: fh.bad_request_error(self, response, 'Not logged in') return # get stream name stream_name = self.get_request_param(fh.stream_name_parm) response[fh.stream_name_parm] = stream_name if stream_name is None or stream_name == "": fh.bad_request_error( self, response, 'No parameter {} found'.format(fh.stream_name_parm)) return # get cover image URL cover_url = self.get_request_param(fh.cover_url_parm) response[fh.cover_url_parm] = cover_url # create new stream stream = Stream.create(name=stream_name, owner=owner, cover_url=cover_url) if stream is None: fh.bad_request_error( self, response, 'Stream {0} already exists for user {1}'.format( stream_name, owner.nickName)) return response[fh.stream_id_parm] = stream.key.id() # add stream to document index for searching fh.searchablize_stream(stream, response) # process subscribers list subs = self.get_request_param(fh.subscribers_parm) response[fh.subscribers_parm] = subs num_added = StreamSubscriber.add_subscribers_by_emails( stream, subs.split(';')) response['num_new_subscribers'] = num_added # process tags list #TODO: tags list in create tags = self.get_request_param(fh.tags_parm) StreamTag.add_tags_to_stream_by_name(stream, tags.split(';')) response[fh.tags_parm] = tags response['status'] = "Created new stream: {}".format(stream_name) self.write_response(json.dumps(response))
def post(self): # make call to createStream service parm_dict = self.get_request_parameter_dictionary() create_stream_url = 'http://{0}/services/createstream?{1}={2};{3}'.format(os.environ['HTTP_HOST'], fh.user_id_parm, StreamUser.get_current_user(self).user_id(), urllib.urlencode(parm_dict)) try: result = urllib2.urlopen(create_stream_url) response = json.loads("".join(result.readlines())) redirect_url = fh.get_viewstream_url(response[fh.stream_id_parm], 1, 10) self.redirect(redirect_url) except urllib2.HTTPError: self.redirect('/error?{0}={1}'.format(fh.message_parm, 'Error creating stream'))
def get(self): user = StreamUser.get_current_user(self) if user: login_url = fh.get_logout_url(self, '/') login_text = 'Sign out' else: self.redirect("/") return template_values = { 'html_template': 'MasterTemplate.html', 'user': user.nickName, 'login_url': login_url, 'login_text': login_text, 'stream_name_parm': fh.stream_name_parm, 'tags_parm': fh.tags_parm, 'cover_url_parm': fh.cover_url_parm, 'subs_parm': fh.subscribers_parm } search_string = self.get_request_param(fh.search_string_parm) if search_string is not None: template_values['search_string'] = search_string tags = self.get_request_param(fh.tags_parm) if tags not in [None, '']: s = urllib.unquote(tags).decode('utf8') tags = eval(s) template_values['search_tags'] = [{ 'name': tag, 'url': fh.get_viewtag_url(tag) } for tag in tags] stream_ids = self.get_request_param(fh.stream_id_parm) if stream_ids not in [None, '']: s = urllib.unquote(stream_ids).decode('utf8') ids = eval(s) template_values['search_streams'] = Stream.get_batch_by_ids(ids) path = os.path.join(os.path.dirname(__file__), '../../templates/StreamSearch.html') self.set_content_text_html() self.write_response(fh.render_html_template(path, template_values))
def get(self): user = StreamUser.get_current_user(self) if user: login_url = fh.get_logout_url(self, '/') login_text = 'Sign out' else: self.redirect("/") return tag_name = self.get_request_param(fh.tag_name_parm) # service call tag_service_url = fh.get_tagged_streams_url(tag_name) result = urllib2.urlopen(tag_service_url) stream_dict = json.loads("".join(result.readlines())) stream_ids = stream_dict[fh.stream_id_parm] stream_names = stream_dict[fh.stream_name_parm] cover_urls = stream_dict[fh.cover_url_parm] stream_urls = [fh.get_viewstream_default_url(s) for s in stream_ids] streams = [] for i in range(len(stream_ids)): streams.append({ 'name': stream_names[i], 'coverImageURL': cover_urls[i], 'url': stream_urls[i] }) template_values = { 'html_template': 'MasterTemplate.html', 'user': user.nickName, 'email': user.email, 'login_url': login_url, 'login_text': login_text, 'tag_name': tag_name, 'streams': streams } self.set_content_text_html() path = os.path.join(os.path.dirname(__file__), '../../templates/ViewTag.html') self.response.write(fh.render_html_template(path, template_values))
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