def get(self): auth = authenticate(self) if auth[0]: report = TrendReport.query(TrendReport.name == TREND_REPORT).get() if report is None: message = "No report currently generated" template_values = { 'user': auth[0], 'url': auth[1], 'url_linktext': auth[2], 'html_text': message, 'report_sending': User.query(User.username == auth[0]._User__email).get().report_sending } template = JINJA_ENVIRONMENT.get_template( '/Pages/TrendingStreams.html') html_text = template.render(template_values) else: html_text = report.html self.response.write(html_text)
def authenticate(page): user = users.get_current_user() if user: url = users.create_logout_url(page.request.uri) url_linktext = 'Logout' # If user does not exist on site, enroll user # TODO: Make this a separate page username = user.email.im_self._User__email site_users = User.query(User.username == username) site_users = site_users.get() if not site_users: new_user = User(username=username, email=username, streams_owned=[], streams_subscribed=[], report_sending="EveryDay") new_user.put() else: url = users.create_login_url(page.request.uri) url_linktext = 'Login' template_values = { 'url': url, 'url_linktext': url_linktext, } template = JINJA_ENVIRONMENT.get_template('/Pages/autherror.html') page.response.write(template.render(template_values)) return (None, url, url_linktext) return (user, url, url_linktext)
def get(self): auth = authenticate(self) if auth[0]: current_user = User.query( User.username == auth[0]._User__email).get() form_data = cgi.FieldStorage() requests = form_data.getlist("chkDeleteStream") index = search.Index(INDEX_NAME) for key_str in requests: key = ndb.Key(urlsafe=key_str) stream = key.get() for pic_key in stream.pictures: picture = pic_key.get() picture_name = picture.name filename = '/{}/Pictures'.format( BUCKET_NAME) + "/" + picture_name cloudstorage.delete(filename) blob_key = picture.image images.delete_serving_url(blob_key) blobstore.delete(blob_key) pic_key.delete() key.delete() index.delete(key_str) current_user.streams_owned.remove(key) current_user.put() time.sleep(.1) self.redirect('/ManageStream')
def get(self): user_email = self.request.get('user') current_user = User.query(User.email == user_email).get() streams_subscribed = [key.get() for key in current_user.streams_subscribed] items = [] for stream in streams_subscribed: for picture in stream.pictures: item = [] item.append(stream.name) item.append(stream.url) picture_temp = picture.get() item.append(images.get_serving_url(picture_temp.image, secure_url=False)) item.append(picture_temp.date_uploaded) items.append(item) items = sorted(items, key=lambda x: str(x[3])) self.response.headers['Content-Type'] = 'application/json' new_json = [] i = 0 for item in items: if i == 16: break item_dict = {"name": item[0], "url": item[2], "path": item[1], "datetime": item[3]} new_json.append(item_dict) i += 1 self.response.out.write(json.dumps(new_json, default=json_serial))
def get(self): auth = authenticate(self) if auth[0]: template_values = { 'user': auth[0], 'url': auth[1], 'url_linktext': auth[2], } current_user = User.query(User.username == auth[0]._User__email).get() template_values = dict(template_values.items() + self.build_template(current_user, self.request).items()) template = JINJA_ENVIRONMENT.get_template('/Pages/ViewAllStream.html') self.response.write(template.render(template_values))
def post(self): auth = authenticate(self) if auth[0]: current_user = User.query( User.username == auth[0]._User__email).get() template_values = dict( self.build_post_template(current_user, self.request).items()) stream_name = template_values.get("name") status = template_values.get("status") page = template_values.get("page") self.redirect('/ViewSingleStream?stream_name=' + stream_name + ";status=" + status + ";page=" + str(page)) # [END ViewStream]
def post(self): auth = authenticate(self) if auth[0]: current_user = User.query( User.username == auth[0]._User__email).get() email_sending = self.request.get('onetype') current_user.report_sending = email_sending current_user.put() time.sleep(.6) self.redirect('/TrendingStreams')
def get(self): auth = authenticate(self) if auth[0]: current_user = User.query( User.username == auth[0]._User__email).get() form_data = cgi.FieldStorage() requests = form_data.getlist("chkUnsubStream") for key_str in requests: key = ndb.Key(urlsafe=key_str) current_user.streams_subscribed.remove(key) current_user.put() time.sleep(.1) self.redirect('/ManageStream')
def get(self): auth = authenticate(self) if auth[0]: current_user = User.query( User.username == auth[0]._User__email).get() form_data = cgi.FieldStorage() requests = form_data.getlist("subscribe") stream_name = self.request.get('stream_name') status = self.request.get('stream_name') for key_str in requests: key = ndb.Key(urlsafe=key_str) stream = key.get if not key in current_user.streams_subscribed: current_user.streams_subscribed.append(key) current_user.put() time.sleep(.1) self.redirect('/ViewSingleStream?stream_name=' + stream_name + ";status=" + status)
def post(self): auth = authenticate(self) if auth[0]: stream_name = self.request.get('streamName') tag_stream = str(self.request.get('tagStream')) cover_image_url = str(self.request.get('coverImageUrl')) tag_stream_list = [] if tag_stream: tag_stream_list = re.split(',\s*', tag_stream) # check if tags are valid for tag in tag_stream_list: if not re.match("^#\w+$", tag) or " " in tag: self.redirect('/Error') return # check if stream name is valid old_stream = Stream.query(Stream.name == stream_name).get() if old_stream: self.redirect('/Error') return #Make the Stream new_stream = Stream(name=stream_name, times_viewed=0, view_times=[], tags=tag_stream_list, picture_count=0, url="/ViewSingleStream?stream_name=" + stream_name, creation_time=datetime.datetime.now()) if cover_image_url: new_stream.cover_page_url = cover_image_url new_stream.put() current_user = User.query( User.username == auth[0]._User__email).get() current_user.streams_owned.append(new_stream._entity_key) current_user.put() #We actually have to wait for the transaction to take place... smh #Actual solution can be found https://stackoverflow.com/questions/15460926/google-app-engine-ndb-put-and-then-query-there-is-always-one-less-item # TODO: lets fix this later time.sleep(.1) subscribers = str(self.request.get('subscriber')) subscribers = subscribers.split(",") subscriberMessage = self.request.get('subscriberMessage') if subscriberMessage: subscriberMessage = str(subscriberMessage) else: subscriberMessage = "Welcome to the stream" sg = sendgrid.SendGridAPIClient( apikey= "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" ) from_email = Email("*****@*****.**") for subscriber in subscribers: user = User.query(User.username == subscriber).get() if not user is None: user.streams_subscribed.append(new_stream._entity_key) user.put() # Send an email to_email = Email(user.email) subject = "You've been subscribed!" #content = Content("text/plain", str(self.request.get('subscriberMessage'))) content = Content("text/plain", subscriberMessage) mail = Mail(from_email, subject, to_email, content) response = sg.client.mail.send.post( request_body=mail.get()) # need to also make a search api document tag_words = tag_stream tag_words.replace("#", "") full_tag_stream = tag_stream + tag_words d = search.Document( doc_id=new_stream.key.urlsafe(), fields=[ search.TextField(name="stream_name", value=stream_name), search.TextField(name="cover_image", value=cover_image_url), search.TextField(name="url", value=new_stream.url), search.TextField(name="tag", value=full_tag_stream.replace(",", " ")) ], language="en") try: search.Index(name=INDEX_NAME).put(d) except search.Error: self.redirect('/Error') self.redirect('/ManageStream')
def get(self): auth = authenticate(self) #if auth[0]: time = datetime.datetime.time(datetime.datetime.now()) hour = time.hour minute = time.minute if (hour > 0): hour_limit = hour - 1 else: hour_limit = 23 min_limit = minute streams = Stream.query() for stream in streams: view_times = stream.view_times for time in view_times: myhour = time.hour if time.hour == 0: myhour = 24 if myhour < hour_limit or (time.minute <= min_limit and myhour == hour_limit): view_times.remove(time) stream.view_times = view_times stream.put() streams = Stream.query() streams = list(filter(lambda x: len(x.view_times) > 0, streams)) sorted_streams = sorted(streams, key=lambda k: len(k.view_times), reverse=True) num = len(sorted_streams) title = "Top 3 Trending Streams" if num > 3: sorted_streams = sorted_streams[:3] elif num == 2: title = "Top 2 Trending Streams" elif num == 1: title = "Top Trending Stream" elif num == 0: title = "No Streams Trending Currently" counts = list(map(lambda x: len(list(x.view_times)), sorted_streams)) for stream in sorted_streams: if not stream.cover_page_url: if stream.pictures: stream.cover_page_url = images.get_serving_url( stream.pictures[0].get().image, secure_url=False) stream.put() template_values = { 'user': auth[0], 'url': auth[1], 'url_linktext': "", #'url_linktext': auth[2], # removing login/logout link from trending stream page # because report is generated by cron job, user is never "logged in" and text shows incorrect value 'title': title, 'streams': sorted_streams, 'counts': counts } template = JINJA_ENVIRONMENT.get_template( '/Pages/TrendingStreams.html') render = template.render(template_values) report = TrendReport.query(TrendReport.name == TREND_REPORT).get() if report: report.html = render else: report = TrendReport(name=TREND_REPORT, html=render) report.put() sg = sendgrid.SendGridAPIClient( apikey= "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" ) from_email = Email("*****@*****.**") # send emails users = User.query() for user in users: send = False if user.report_sending == "Every5Minutes": send = True elif user.report_sending == "Every1Hour" and minute == 0: send = True elif user.report_sending == "EveryDay" and hour == 0 and minute == 0: send = True if send: to_email = Email(user.email) subject = "Trending Streams Report" content = Content("text/html", render) mail = Mail(from_email, subject, to_email, content) response = sg.client.mail.send.post(request_body=mail.get())