def create(cls, content, author, topic, avatar): new_comment = Comment(content=content, author_email=author, topic_title=topic.title, topic_id=topic.key.id(), author_avatar=avatar) new_comment.put() taskqueue.add(url="/task/email-new-comment", params={ "topic_author_email": topic.author_email, "topic_title": topic.title, "comment_content": new_comment.content }) list_of_subscribers = Subscription.query( Subscription.topic_id == topic.key.id(), Subscription.deleted == False).fetch() if list_of_subscribers: for subscriber in list_of_subscribers: taskqueue.add(url="/task/email-sub-new-comment", params={ "subscriber_email": subscriber.subscriber_email, "topic_title": topic.title, "comment_content": new_comment.content }) return new_comment
def get(self, match_key): self._require_registration() match = Match.get_by_id(match_key) if not match: self.abort(404) user = self.user_bundle.account.key favorite = Favorite.query(Favorite.model_key == match_key, Favorite.model_type == ModelType.MATCH, ancestor=user).get() subscription = Subscription.query( Favorite.model_key == match_key, Favorite.model_type == ModelType.MATCH, ancestor=user).get() if not favorite and not subscription: # New entry; default to being a favorite is_favorite = True else: is_favorite = favorite is not None enabled_notifications = [ (en, NotificationType.render_names[en]) for en in NotificationType.enabled_match_notifications ] self.template_values['match'] = match self.template_values['is_favorite'] = is_favorite self.template_values['subscription'] = subscription self.template_values['enabled_notifications'] = enabled_notifications self.response.out.write( jinja2_engine.render('mytba_match.html', self.template_values))
def get(self, team_number): self._require_login() self._require_registration() team_key = 'frc{}'.format(team_number) team = Team.get_by_id(team_key) if not team: self.abort(404) user = self.user_bundle.account.key favorite = Favorite.query(Favorite.model_key==team_key, Favorite.model_type==ModelType.TEAM, ancestor=user).get() subscription = Subscription.query(Favorite.model_key==team_key, Favorite.model_type==ModelType.TEAM, ancestor=user).get() if not favorite and not subscription: # New entry; default to being a favorite is_favorite = True else: is_favorite = favorite is not None enabled_notifications = [(en, NotificationType.render_names[en]) for en in NotificationType.enabled_team_notifications] self.template_values['team'] = team self.template_values['is_favorite'] = is_favorite self.template_values['subscription'] = subscription self.template_values['enabled_notifications'] = enabled_notifications self.response.out.write(jinja2_engine.render('mytba_team.html', self.template_values))
def get(self, event_key=None): self._require_login() self._require_registration() if event_key is None: events = EventHelper.getEventsWithinADay() EventHelper.sort_events(events) self.template_values['events'] = events self.response.out.write(jinja2_engine.render('mytba_add_hot_matches_base.html', self.template_values)) return event = Event.get_by_id(event_key) if not event: self.abort(404) subscriptions_future = Subscription.query( Subscription.model_type==ModelType.MATCH, Subscription.notification_types==NotificationType.UPCOMING_MATCH, ancestor=self.user_bundle.account.key).fetch_async(projection=[Subscription.model_key]) matches = [] if event.matchstats and 'match_predictions' in event.matchstats: match_predictions = event.matchstats['match_predictions'] max_hotness = 0 min_hotness = float('inf') for match in event.matches: if not match.has_been_played and match.key.id() in match_predictions: prediction = match_predictions[match.key.id()] red_score = prediction['red']['score'] blue_score = prediction['blue']['score'] if red_score > blue_score: winner_score = red_score loser_score = blue_score else: winner_score = blue_score loser_score = red_score hotness = winner_score + 2.0*loser_score # Favor close high scoring matches max_hotness = max(max_hotness, hotness) min_hotness = min(min_hotness, hotness) match.hotness = hotness matches.append(match) existing_subscriptions = set() for sub in subscriptions_future.get_result(): existing_subscriptions.add(sub.model_key) hot_matches = [] for match in matches: match.hotness = 100 * (match.hotness - min_hotness) / (max_hotness - min_hotness) match.already_subscribed = match.key.id() in existing_subscriptions hot_matches.append(match) hot_matches = sorted(hot_matches, key=lambda match: -match.hotness) matches_dict = {'qm': hot_matches[:25]} self.template_values['event'] = event self.template_values['matches'] = matches_dict self.response.out.write(jinja2_engine.render('mytba_add_hot_matches.html', self.template_values))
def add_subscription(cls, sub, device_key=""): current = Subscription.query(Subscription.model_key == sub.model_key, Subscription.model_type == sub.model_type, ancestor=ndb.Key(Account, sub.user_id)).get() if current is None: # Subscription doesn't exist, add it sub.put() # Send updates to user's other devices NotificationHelper.send_subscription_update( sub.user_id, device_key) return 200 else: if len( set(current.notification_types).symmetric_difference( set(sub.notification_types))) == 0: # Subscription already exists. Don't add it again return 304 else: # We're updating the settings current.notification_types = sub.notification_types current.put() # Send updates to user's other devices NotificationHelper.send_subscription_update( sub.user_id, device_key) return 200
def add_subscription(self, request): current_user = endpoints.get_current_user() if current_user is None: return BaseResponse(code=401, message="Unauthorized to add subscription") userId = PushHelper.user_email_to_id(current_user.email()) modelKey = request.model_key sub = Subscription.query( Subscription.user_id == userId, Subscription.model_key == modelKey).get() if sub is None: # Subscription doesn't exist, add it Subscription( user_id = userId, model_key = modelKey, notification_types = PushHelper.notification_enums_from_string(request.notifications)).put() if request.device_key: # Send updates to user's other devices GCMMessageHelper.send_subscription_update(userId, request.device_key) return BaseResponse(code=200, message="Subscription added") else: if sub.notification_types == PushHelper.notification_enums_from_string(request.notifications): # Subscription already exists. Don't add it again return BaseResponse(code=304, message="Subscription already exists") else: # We're updating the settings sub.notification_types = PushHelper.notification_enums_from_string(request.notifications) sub.put() if request.device_key: # Send updates to user's other devices GCMMessageHelper.send_subscription_update(userId, request.device_key) return BaseResponse(code=200, message="Subscription updated")
def get(self, match_key): self._require_login() self._require_registration() match = Match.get_by_id(match_key) if not match: self.abort(404) user = self.user_bundle.account.key favorite = Favorite.query(Favorite.model_key==match_key, Favorite.model_type==ModelType.MATCH, ancestor=user).get() subscription = Subscription.query(Favorite.model_key==match_key, Favorite.model_type==ModelType.MATCH, ancestor=user).get() if not favorite and not subscription: # New entry; default to being a favorite is_favorite = True else: is_favorite = favorite is not None enabled_notifications = [(en, NotificationType.render_names[en]) for en in NotificationType.enabled_match_notifications] self.template_values['match'] = match self.template_values['is_favorite'] = is_favorite self.template_values['subscription'] = subscription self.template_values['enabled_notifications'] = enabled_notifications self.response.out.write(jinja2_engine.render('mytba_match.html', self.template_values))
def get(self, topic_id): topic = Topic.get_by_id(int(topic_id)) user = users.get_current_user() comments = Comment.query(Comment.topic_id == topic.key.id(), Comment.deleted == False).order( Comment.created).fetch() comments_sum = len(comments) if user: subscriber = Subscription.query( Subscription.topic_id == topic.key.id(), Subscription.deleted == False, Subscription.subscriber_email == user.email()).get() else: subscriber = "" params = { "topic": topic, "comments": comments, "comments_sum": comments_sum, "user": user, "subscriber": subscriber } return self.render_template("topic.html", params=params)
def get(self, team_number): self._require_registration() team_key = 'frc{}'.format(team_number) team = Team.get_by_id(team_key) if not team: self.abort(404) user = self.user_bundle.account.key favorite = Favorite.query(Favorite.model_key == team_key, Favorite.model_type == ModelType.TEAM, ancestor=user).get() subscription = Subscription.query( Favorite.model_key == team_key, Favorite.model_type == ModelType.TEAM, ancestor=user).get() if not favorite and not subscription: # New entry; default to being a favorite is_favorite = True else: is_favorite = favorite is not None enabled_notifications = [ (en, NotificationType.render_names[en]) for en in NotificationType.enabled_team_notifications ] self.template_values['team'] = team self.template_values['is_favorite'] = is_favorite self.template_values['subscription'] = subscription self.template_values['enabled_notifications'] = enabled_notifications self.response.out.write( jinja2_engine.render('mytba_team.html', self.template_values))
def get(self): # Get all forum subscription. Topic id of 1 means general subscriptions to latest topics. subscriptions = Subscription.query(Subscription.topic_id == 1).fetch() # Build an email list email_list = [] for subscription in subscriptions: email_list.append(subscription.user_email) # Get topics that were active in the past day recent_topics = Topic.query(Topic.updated_at > datetime.now() - timedelta(days=1)) # Send email to each address in the list for email in email_list: body = u"Here's a list of recent hot topics: " + "\n" # List all active topics in each email for topic in recent_topics: body += topic.title + u" - " + u"http://ninja-tech.appspot.com/topic/view/" + str(topic.key.id()) + "\n " params = { "email" : email, "subject" : "Latest news", "body" : body } taskqueue.add(url="/task/send-subscription-email", params=params)
def get_users_subscribed(cls, model_key): user_list = Subscription.query( Subscription.model_key == model_key).fetch() output = [] for user in user_list: output.append(user.user_id) return output
def get(self, event_key=None): self._require_registration() if event_key is None: events = EventHelper.getEventsWithinADay() EventHelper.sort_events(events) self.template_values['events'] = events self.response.out.write(jinja2_engine.render('mytba_add_hot_matches_base.html', self.template_values)) return event = Event.get_by_id(event_key) if not event: self.abort(404) subscriptions_future = Subscription.query( Subscription.model_type==ModelType.MATCH, Subscription.notification_types==NotificationType.UPCOMING_MATCH, ancestor=self.user_bundle.account.key).fetch_async(projection=[Subscription.model_key]) matches = [] if event.matchstats and 'match_predictions' in event.matchstats: match_predictions = event.matchstats['match_predictions'] max_hotness = 0 min_hotness = float('inf') for match in event.matches: if not match.has_been_played and match.key.id() in match_predictions: prediction = match_predictions[match.key.id()] red_score = prediction['red']['score'] blue_score = prediction['blue']['score'] if red_score > blue_score: winner_score = red_score loser_score = blue_score else: winner_score = blue_score loser_score = red_score hotness = winner_score + 2.0*loser_score # Favor close high scoring matches max_hotness = max(max_hotness, hotness) min_hotness = min(min_hotness, hotness) match.hotness = hotness matches.append(match) existing_subscriptions = set() for sub in subscriptions_future.get_result(): existing_subscriptions.add(sub.model_key) hot_matches = [] for match in matches: match.hotness = 100 * (match.hotness - min_hotness) / (max_hotness - min_hotness) match.already_subscribed = match.key.id() in existing_subscriptions hot_matches.append(match) hot_matches = sorted(hot_matches, key=lambda match: -match.hotness) matches_dict = {'qm': hot_matches[:25]} self.template_values['event'] = event self.template_values['matches'] = matches_dict self.response.out.write(jinja2_engine.render('mytba_add_hot_matches.html', self.template_values))
def is_subscribed(self, topic_id): query = Subscription.query(Subscription.topic_id == int(topic_id), Subscription.user_id == self.id).fetch() if query: return True else: return False
def get_users_subscribed_to_event(cls, event, notification): keys = [] keys.append(event.key_name) keys.append("{}*".format(event.year)) users = Subscription.query(Subscription.model_key.IN(keys), Subscription.notification_types == notification).fetch() output = [] for user in users: output.append(user.user_id) return output
def get_users_subscribed_for_alliances(cls, event, notification): keys = [] for team in event.alliance_teams: keys.append(team) keys.append("{}_{}".format(event.key_name, team)) # team@event key keys.append("{}*".format(event.year)) # key for all events in year keys.append(event.key_name) users = Subscription.query(Subscription.model_key.IN(keys), Subscription.notification_types == notification).fetch() output = [user.user_id for user in users] return output
def remove_subscription(cls, userId, modelKey, device_key=""): to_delete = Subscription.query(Subscription.model_key == modelKey, ancestor=ndb.Key(Account, userId)).fetch(keys_only=True) if len(to_delete) > 0: ndb.delete_multi(to_delete) # Send updates to user's other devices NotificationHelper.send_subscription_update(userId, device_key) return 200 else: # Subscription doesn't exist. Can't delete it return 404
def get(self): redirect = self.request.get('redirect') if redirect: self._require_login(redirect) else: self._require_login('/account') # Redirects to registration page if account not registered self._require_registration('/account/register') push_sitevar = Sitevar.get_by_id('notifications.enable') if push_sitevar is None or not push_sitevar.values_json == "true": ping_enabled = "disabled" else: ping_enabled = "" # Compute myTBA statistics user = self.user_bundle.account.key num_favorites = Favorite.query(ancestor=user).count() num_subscriptions = Subscription.query(ancestor=user).count() # Compute suggestion statistics submissions_pending = Suggestion.query( Suggestion.review_state == Suggestion.REVIEW_PENDING, Suggestion.author == user).count() submissions_accepted = Suggestion.query( Suggestion.review_state == Suggestion.REVIEW_ACCEPTED, Suggestion.author == user).count() # Suggestion review statistics review_permissions = False num_reviewed = 0 total_pending = 0 if AccountPermissions.MUTATE_DATA in self.user_bundle.account.permissions: review_permissions = True num_reviewed = Suggestion.query( Suggestion.reviewer == user).count() total_pending = Suggestion.query( Suggestion.review_state == Suggestion.REVIEW_PENDING).count() self.template_values['status'] = self.request.get('status') self.template_values[ 'webhook_verification_success'] = self.request.get( 'webhook_verification_success') self.template_values['ping_enabled'] = ping_enabled self.template_values['num_favorites'] = num_favorites self.template_values['num_subscriptions'] = num_subscriptions self.template_values['submissions_pending'] = submissions_pending self.template_values['submissions_accepted'] = submissions_accepted self.template_values['review_permissions'] = review_permissions self.template_values['num_reviewed'] = num_reviewed self.template_values['total_pending'] = total_pending self.response.out.write( jinja2_engine.render('account_overview.html', self.template_values))
def list_subscriptions(self, request): current_user = endpoints.get_current_user() if current_user is None: return SubscriptionCollection(subscriptions = []) userId = PushHelper.user_email_to_id(current_user.email()) subscriptions = Subscription.query(ancestor=ndb.Key(Account, userId)).fetch() output = [] for subscription in subscriptions: output.append(SubscriptionMessage(model_key = subscription.model_key, notifications = PushHelper.notification_string_from_enums(subscription.notification_types), model_type = subscription.model_type)) return SubscriptionCollection(subscriptions = output)
def list_subscriptions(self, request): current_user = endpoints.get_current_user() if current_user is None: return SubscriptionCollection(subscriptions = []) userId = PushHelper.user_email_to_id(current_user.email()) subscriptions = Subscription.query( Subscription.user_id == userId ).fetch() output = [] for subscription in subscriptions: output.append(SubscriptionMessage(model_key = subscription.model_key, notifications = PushHelper.notification_string_from_enums(subscription.notification_types))) return SubscriptionCollection(subscriptions = output)
def remove_subscription(cls, userId, modelKey, device_key=""): to_delete = Subscription.query(Subscription.model_key == modelKey, ancestor=ndb.Key(Account, userId)).fetch(keys_only=True) if len(to_delete) > 0: ndb.delete_multi(to_delete) if device_key: # Send updates to user's other devices NotificationHelper.send_subscription_update(userId, device_key) return 200 else: # Subscription doesn't exist. Can't delete it return 404
def get(self): self._require_registration() push_sitevar = Sitevar.get_by_id('notifications.enable') if push_sitevar is None or not push_sitevar.values_json == "true": ping_enabled = "disabled" else: ping_enabled = "" # Compute myTBA statistics user = self.user_bundle.account.key num_favorites = Favorite.query(ancestor=user).count() num_subscriptions = Subscription.query(ancestor=user).count() # Compute suggestion statistics submissions_pending = Suggestion.query( Suggestion.review_state == Suggestion.REVIEW_PENDING, Suggestion.author == user).count() submissions_accepted = Suggestion.query( Suggestion.review_state == Suggestion.REVIEW_ACCEPTED, Suggestion.author == user).count() # Suggestion review statistics review_permissions = False num_reviewed = 0 total_pending = 0 if AccountPermissions.REVIEW_MEDIA in self.user_bundle.account.permissions: review_permissions = True num_reviewed = Suggestion.query( Suggestion.reviewer == user).count() total_pending = Suggestion.query( Suggestion.review_state == Suggestion.REVIEW_PENDING).count() # Fetch trusted API keys trusted_keys = ApiAuthAccess.query(ApiAuthAccess.owner == user).fetch() self.template_values['status'] = self.request.get('status') self.template_values[ 'webhook_verification_success'] = self.request.get( 'webhook_verification_success') self.template_values['ping_enabled'] = ping_enabled self.template_values['num_favorites'] = num_favorites self.template_values['num_subscriptions'] = num_subscriptions self.template_values['submissions_pending'] = submissions_pending self.template_values['submissions_accepted'] = submissions_accepted self.template_values['review_permissions'] = review_permissions self.template_values['num_reviewed'] = num_reviewed self.template_values['total_pending'] = total_pending self.template_values['trusted_keys'] = trusted_keys self.template_values['auth_type_names'] = AuthType.type_names self.response.out.write( jinja2_engine.render('account_overview.html', self.template_values))
def post(self): """ new subscription for latest topics """ mail = self.request.get('email') subscriber = Subscription.query(Subscription.email == mail) if not subscriber: Subscription.create(email=mail) return self.render_template("error.html", params={"message": SUBSCRIBTION_NEW}) else: return self.render_template("error.html", params={"message": SUBSCRIBTION_EXIST})
def list_subscriptions(self, request): user_id = get_current_user_id(self.headers) if user_id is None: return SubscriptionCollection(subscriptions=[]) subscriptions = Subscription.query(ancestor=ndb.Key(Account, user_id)).fetch() output = [] for subscription in subscriptions: output.append(SubscriptionMessage( model_key=subscription.model_key, notifications=PushHelper.notification_string_from_enums(subscription.notification_types), model_type=subscription.model_type)) return SubscriptionCollection(subscriptions=output)
def get_users_subscribed_to_match(cls, match, notification): keys = [] for team in match.team_key_names: keys.append(team) keys.append("{}_{}".format(match.event.id(), team)) keys.append(match.key_name) keys.append(match.event.id()) logging.info("Getting subscriptions for keys: "+str(keys)) users = Subscription.query(Subscription.model_key.IN(keys), Subscription.notification_types == notification).fetch() output = [] for user in users: output.append(user.user_id) return output
def get(self, event_key): self._require_login('/account/register') self._require_registration('/account/register') # Handle wildcard for all events in a year event = None is_wildcard = False if event_key.endswith('*'): try: year = int(event_key[:-1]) except: year = None if year and year >= 1992 and year <= tba_config.MAX_YEAR: event = Event( # fake event for rendering name="ALL {} EVENTS".format(year), year=year, ) is_wildcard = True else: event = Event.get_by_id(event_key) if not event: self.abort(404) user = self.user_bundle.account.key favorite = Favorite.query(Favorite.model_key == event_key, Favorite.model_type == ModelType.EVENT, ancestor=user).get() subscription = Subscription.query( Favorite.model_key == event_key, Favorite.model_type == ModelType.EVENT, ancestor=user).get() if not favorite and not subscription: # New entry; default to being a favorite is_favorite = True else: is_favorite = favorite is not None enabled_notifications = [ (en, NotificationType.render_names[en]) for en in NotificationType.enabled_event_notifications ] self.template_values['event'] = event self.template_values['is_wildcard'] = is_wildcard self.template_values['is_favorite'] = is_favorite self.template_values['subscription'] = subscription self.template_values['enabled_notifications'] = enabled_notifications self.response.out.write( jinja2_engine.render('mytba_event.html', self.template_values))
def get_users_subscribed_to_match(cls, match, notification): keys = [] for team in match.team_key_names: keys.append(team) keys.append("{}_{}".format(match.event_key_name, team)) keys.append("{}*".format(match.year)) # key for all events in year keys.append(match.key_name) keys.append(match.event_key_name) users = Subscription.query(Subscription.model_key.IN(keys), Subscription.notification_types == notification).fetch() output = [] for user in users: output.append(user.user_id) return output
def get(self): self._require_registration() notifications_enabled = NotificationsEnable.notifications_enabled() if not notifications_enabled: ping_enabled = "disabled" else: ping_enabled = "" # Compute myTBA statistics user = self.user_bundle.account.key num_favorites = Favorite.query(ancestor=user).count() num_subscriptions = Subscription.query(ancestor=user).count() # Compute suggestion statistics submissions_pending = Suggestion.query(Suggestion.review_state==Suggestion.REVIEW_PENDING, Suggestion.author==user).count() submissions_accepted = Suggestion.query(Suggestion.review_state==Suggestion.REVIEW_ACCEPTED, Suggestion.author==user).count() # Suggestion review statistics review_permissions = False num_reviewed = 0 total_pending = 0 if self.user_bundle.account.permissions: review_permissions = True num_reviewed = Suggestion.query(Suggestion.reviewer==user).count() total_pending = Suggestion.query(Suggestion.review_state==Suggestion.REVIEW_PENDING).count() # Fetch trusted API keys api_keys = ApiAuthAccess.query(ApiAuthAccess.owner == user).fetch() write_keys = filter(lambda key: key.is_write_key, api_keys) write_keys.sort(key=lambda key: key.event_list[0]) read_keys = filter(lambda key: key.is_read_key, api_keys) self.template_values['status'] = self.request.get('status') self.template_values['webhook_verification_success'] = self.request.get('webhook_verification_success') self.template_values['ping_sent'] = self.request.get('ping_sent') self.template_values['ping_enabled'] = ping_enabled self.template_values['num_favorites'] = num_favorites self.template_values['num_subscriptions'] = num_subscriptions self.template_values['submissions_pending'] = submissions_pending self.template_values['submissions_accepted'] = submissions_accepted self.template_values['review_permissions'] = review_permissions self.template_values['num_reviewed'] = num_reviewed self.template_values['total_pending'] = total_pending self.template_values['read_keys'] = read_keys self.template_values['write_keys'] = write_keys self.template_values['auth_write_type_names'] = AuthType.write_type_names self.response.out.write(jinja2_engine.render('account_overview.html', self.template_values))
def get(self): self._require_registration() push_sitevar = Sitevar.get_by_id("notifications.enable") if push_sitevar is None or not push_sitevar.values_json == "true": ping_enabled = "disabled" else: ping_enabled = "" # Compute myTBA statistics user = self.user_bundle.account.key num_favorites = Favorite.query(ancestor=user).count() num_subscriptions = Subscription.query(ancestor=user).count() # Compute suggestion statistics submissions_pending = Suggestion.query( Suggestion.review_state == Suggestion.REVIEW_PENDING, Suggestion.author == user ).count() submissions_accepted = Suggestion.query( Suggestion.review_state == Suggestion.REVIEW_ACCEPTED, Suggestion.author == user ).count() # Suggestion review statistics review_permissions = False num_reviewed = 0 total_pending = 0 if self.user_bundle.account.permissions: review_permissions = True num_reviewed = Suggestion.query(Suggestion.reviewer == user).count() total_pending = Suggestion.query(Suggestion.review_state == Suggestion.REVIEW_PENDING).count() # Fetch trusted API keys trusted_keys = ApiAuthAccess.query(ApiAuthAccess.owner == user).fetch() self.template_values["status"] = self.request.get("status") self.template_values["webhook_verification_success"] = self.request.get("webhook_verification_success") self.template_values["ping_enabled"] = ping_enabled self.template_values["num_favorites"] = num_favorites self.template_values["num_subscriptions"] = num_subscriptions self.template_values["submissions_pending"] = submissions_pending self.template_values["submissions_accepted"] = submissions_accepted self.template_values["review_permissions"] = review_permissions self.template_values["num_reviewed"] = num_reviewed self.template_values["total_pending"] = total_pending self.template_values["trusted_keys"] = trusted_keys self.template_values["auth_type_names"] = AuthType.type_names self.response.out.write(jinja2_engine.render("account_overview.html", self.template_values))
def create_comment(user, topic_id, comment_content): topic = Topic.get_by_id(topic_id) new_comment = Comment(user_email=user.email(), user_id=user.id, content=comment_content, topic_id=topic_id, topic_title=topic.title) new_comment.put() # Update topic updated_at property topic.put() # Build a list of emails to send email_list = [] # Check subscriptions query = Subscription.query(Subscription.topic_id == topic_id).fetch() for entry in query: if entry.user_email == user.email( ): # Don't send the email to the comment author continue else: email_list.append(entry.user_email) # If email list is empty do nothing if not email_list: return 0 # Else send the emails for email in email_list: params = { "email": email, "subject": "New reply", "body": u"A topic you follow http://ninja-tech.appspot.com/topic/view/{1} - '{0}' has a new comment." .format(topic.title, topic_id) } taskqueue.add(url="/task/email-topic-author", params=params)
def get(self): self._require_registration() push_sitevar = Sitevar.get_by_id('notifications.enable') if push_sitevar is None or not push_sitevar.values_json == "true": ping_enabled = "disabled" else: ping_enabled = "" # Compute myTBA statistics user = self.user_bundle.account.key num_favorites = Favorite.query(ancestor=user).count() num_subscriptions = Subscription.query(ancestor=user).count() # Compute suggestion statistics submissions_pending = Suggestion.query(Suggestion.review_state==Suggestion.REVIEW_PENDING, Suggestion.author==user).count() submissions_accepted = Suggestion.query(Suggestion.review_state==Suggestion.REVIEW_ACCEPTED, Suggestion.author==user).count() # Suggestion review statistics review_permissions = False num_reviewed = 0 total_pending = 0 if self.user_bundle.account.permissions: review_permissions = True num_reviewed = Suggestion.query(Suggestion.reviewer==user).count() total_pending = Suggestion.query(Suggestion.review_state==Suggestion.REVIEW_PENDING).count() # Fetch trusted API keys api_keys = ApiAuthAccess.query(ApiAuthAccess.owner == user).fetch() write_keys = filter(lambda key: key.is_write_key, api_keys) read_keys = filter(lambda key: key.is_read_key, api_keys) self.template_values['status'] = self.request.get('status') self.template_values['webhook_verification_success'] = self.request.get('webhook_verification_success') self.template_values['ping_sent'] = self.request.get('ping_sent') self.template_values['ping_enabled'] = ping_enabled self.template_values['num_favorites'] = num_favorites self.template_values['num_subscriptions'] = num_subscriptions self.template_values['submissions_pending'] = submissions_pending self.template_values['submissions_accepted'] = submissions_accepted self.template_values['review_permissions'] = review_permissions self.template_values['num_reviewed'] = num_reviewed self.template_values['total_pending'] = total_pending self.template_values['read_keys'] = read_keys self.template_values['write_keys'] = write_keys self.template_values['auth_write_type_names'] = AuthType.write_type_names self.response.out.write(jinja2_engine.render('account_overview.html', self.template_values))
def list_subscriptions(self, request): user_id = get_current_user_id(self.headers) if user_id is None: return SubscriptionCollection(subscriptions=[]) subscriptions = Subscription.query( ancestor=ndb.Key(Account, user_id)).fetch() output = [] for subscription in subscriptions: output.append( SubscriptionMessage( model_key=subscription.model_key, notifications=PushHelper.notification_string_from_enums( subscription.notification_types), model_type=subscription.model_type)) return SubscriptionCollection(subscriptions=output)
def get(self): self._require_login('/account/register') self._require_registration('/account/register') user = self.user_bundle.account.key favorites = Favorite.query(ancestor=user).fetch() subscriptions = Subscription.query(ancestor=user).fetch() favorites_by_type = defaultdict(list) for fav in favorites: favorites_by_type[ModelType.type_names[fav.model_type]].append(fav) subscriptions_by_type = defaultdict(list) for sub in subscriptions: subscriptions_by_type[ModelType.type_names[sub.model_type]].append( sub) now = datetime.datetime.now() self.template_values['favorites_by_type'] = dict(favorites_by_type) self.template_values['subscriptions_by_type'] = dict( subscriptions_by_type) self.template_values[ 'enabled_notifications'] = NotificationType.enabled_notifications self.template_values['this_year'] = now.year error = self.request.get('error') if error: if error == 'invalid_model': error_message = "Invalid model key" elif error == "no_sub_types": error_message = "No notification types selected" elif error == "invalid_account": error_message = "Invalid account" elif error == "invalid_year": error_message = "You can only subscribe to the current year" elif error == "sub_not_found": error_message = "Subscription not found" elif error == "fav_not_found": error_message = "Favorite not found" else: error_message = "An unknown error occurred" self.template_values['error_message'] = error_message path = os.path.join(os.path.dirname(__file__), '../templates/mytba.html') self.response.out.write(template.render(path, self.template_values))
def remove_subscription(self, request): current_user = endpoints.get_current_user() if current_user is None: return BaseResponse(code=401, message="Unauthorized to remove subscription") userId = PushHelper.user_email_to_id(current_user.email()) modelKey = request.model_key to_delete = Subscription.query( Subscription.user_id == userId, Subscription.model_key == modelKey).fetch(keys_only=True) if len(to_delete) > 0: ndb.delete_multi(to_delete) if request.device_key: # Send updates to user's other devices GCMMessageHelper.send_subscription_update(userId, request.device_key) return BaseResponse(code=200, message="Subscriptions deleted") else: # Subscription doesn't exist. Can't delete it return BaseResponse(code=404, message="Subscription not found")
def get(self): redirect = self.request.get('redirect') if redirect: self._require_login(redirect) else: self._require_login('/account') # Redirects to registration page if account not registered self._require_registration('/account/register') push_sitevar = Sitevar.get_by_id('notifications.enable') if push_sitevar is None or not push_sitevar.values_json == "true": ping_enabled = "disabled" else: ping_enabled = "" # Compute myTBA statistics user = self.user_bundle.account.key num_favorites = Favorite.query(ancestor=user).count() num_subscriptions = Subscription.query(ancestor=user).count() # Compute suggestion statistics submissions_pending = Suggestion.query(Suggestion.review_state==Suggestion.REVIEW_PENDING, Suggestion.author==user).count() submissions_accepted = Suggestion.query(Suggestion.review_state==Suggestion.REVIEW_ACCEPTED, Suggestion.author==user).count() # Suggestion review statistics review_permissions = False num_reviewed = 0 total_pending = 0 if AccountPermissions.MUTATE_DATA in self.user_bundle.account.permissions: review_permissions = True num_reviewed = Suggestion.query(Suggestion.reviewer==user).count() total_pending = Suggestion.query(Suggestion.review_state==Suggestion.REVIEW_PENDING).count() self.template_values['status'] = self.request.get('status') self.template_values['webhook_verification_success'] = self.request.get('webhook_verification_success') self.template_values['ping_enabled'] = ping_enabled self.template_values['num_favorites'] = num_favorites self.template_values['num_subscriptions'] = num_subscriptions self.template_values['submissions_pending'] = submissions_pending self.template_values['submissions_accepted'] = submissions_accepted self.template_values['review_permissions'] = review_permissions self.template_values['num_reviewed'] = num_reviewed self.template_values['total_pending'] = total_pending self.response.out.write(jinja2_engine.render('account_overview.html', self.template_values))
def get(self, event_key): self._require_registration() # Handle wildcard for all events in a year event = None is_wildcard = False if event_key.endswith("*"): try: year = int(event_key[:-1]) except: year = None if year and year >= 1992 and year <= tba_config.MAX_YEAR: event = Event(name="ALL {} EVENTS".format(year), year=year) # fake event for rendering is_wildcard = True else: event = Event.get_by_id(event_key) if not event: self.abort(404) user = self.user_bundle.account.key favorite = Favorite.query( Favorite.model_key == event_key, Favorite.model_type == ModelType.EVENT, ancestor=user ).get() subscription = Subscription.query( Favorite.model_key == event_key, Favorite.model_type == ModelType.EVENT, ancestor=user ).get() if not favorite and not subscription: # New entry; default to being a favorite is_favorite = True else: is_favorite = favorite is not None enabled_notifications = [ (en, NotificationType.render_names[en]) for en in NotificationType.enabled_event_notifications ] self.template_values["event"] = event self.template_values["is_wildcard"] = is_wildcard self.template_values["is_favorite"] = is_favorite self.template_values["subscription"] = subscription self.template_values["enabled_notifications"] = enabled_notifications self.response.out.write(jinja2_engine.render("mytba_event.html", self.template_values))
def add_subscription(cls, sub, device_key=""): current = Subscription.query(Subscription.model_key == sub.model_key, ancestor=ndb.Key(Account, sub.user_id)).get() if current is None: # Subscription doesn't exist, add it sub.put() # Send updates to user's other devices NotificationHelper.send_subscription_update(sub.user_id, device_key) return 200 else: if current.notification_types == sub.notification_types: # Subscription already exists. Don't add it again return 304 else: # We're updating the settings current.notification_types = sub.notification_types current.put() # Send updates to user's other devices NotificationHelper.send_subscription_update(sub.user_id, device_key) return 200
def get(self): self._require_login('/account/register') self._require_registration('/account/register') user = self.user_bundle.account.key favorites = Favorite.query(ancestor=user).fetch() subscriptions = Subscription.query(ancestor=user).fetch() favorites_by_type = defaultdict(list) for fav in favorites: favorites_by_type[ModelType.type_names[fav.model_type]].append(fav) subscriptions_by_type = defaultdict(list) for sub in subscriptions: subscriptions_by_type[ModelType.type_names[sub.model_type]].append(sub) now = datetime.datetime.now() self.template_values['favorites_by_type'] = dict(favorites_by_type) self.template_values['subscriptions_by_type'] = dict(subscriptions_by_type) self.template_values['enabled_notifications'] = NotificationType.enabled_notifications self.template_values['this_year'] = now.year error = self.request.get('error') if error: if error == 'invalid_model': error_message = "Invalid model key" elif error == "no_sub_types": error_message = "No notification types selected" elif error == "invalid_account": error_message = "Invalid account" elif error == "invalid_year": error_message = "You can only subscribe to the current year" elif error == "sub_not_found": error_message = "Subscription not found" elif error == "fav_not_found": error_message = "Favorite not found" else: error_message = "An unknown error occurred" self.template_values['error_message'] = error_message path = os.path.join(os.path.dirname(__file__), '../templates/mytba.html') self.response.out.write(template.render(path, self.template_values))
def get(self): year = date.today().year - 1 # Compile key regex # Matches event (2014ctgro), team@event (frc2014_2014ctgro), firehose (2014*) ps = "^{}[a-z]+|_{}[a-z]+|{}\*$".format(year, year, year) logging.info("Pattern: {}".format(ps)) p = re.compile(ps) subs = Subscription.query().fetch() to_delete = [] for sub in subs: if p.match(sub.model_key): to_delete.append(sub.key) count = len(to_delete) if to_delete: ndb.delete_multi(to_delete) logging.info("Removed {} old subscriptions".format(count)) template_values = {'count': count} path = os.path.join(os.path.dirname(__file__), '../../templates/admin/subs_clear_do.html') self.response.out.write(template.render(path, template_values))
def get(self): user = CustomUser.get_current_user(self) if not user: return self.redirect("/") existing_subscription = Subscription.query( Subscription.user_id == user.id, Subscription.topic_id == 1).fetch() if existing_subscription: existing_subscription[0].key.delete() return self.write("You have been unsubscribed.") new_subscription = Subscription(user_id=user.id, topic_id=1, user_email=user.email()) new_subscription.put() return self.write("You have successfuly subscribed.")
def get(self): """ list of topics that were updated in last 24 hours """ # get the topics from datastore time_24_hours_old = datetime.datetime.now() - datetime.timedelta(hours=24) newest_topics = Topic.query(Topic.updated_at < time_24_hours_old, Topic.deleted == False).fetch() # list of topic idents for links topics_idents = [topic.key.id() for topic in newest_topics] # get all subscribers from datastore subscribers = Subscription.query(Subscription.subscribed == True).fetch() # list of emails emails = [subscriber.email for subscriber in subscribers] urls = "" # building topics links and sending mails if newest_topics: for topic_id in topics_idents: urls += "<li>http://emerald-eon-159115.appspot.com/topic/{}\n</li>".format(topic_id) for email in emails: newest_topics_email(email, urls)
def post(self): author_email = self.request.get("author_email") topic_title = self.request.get("topic_title") topic_id = self.request.get("topic_id") topic = Topic.get_by_id(int(topic_id)) mail.send_mail(sender="*****@*****.**", to=author_email, subject="New comment", body="""Tvoja tema {0} ima nov komentar. <a href="http://ninjatechforum.appspot.com/topic/{1}">{0}</a>""".format( topic_title, topic_id)) subscriptions = Subscription.query( Subscription.topic_id == topic.key.id()).fetch() for subscription in subscriptions: mail.send_mail(sender="*****@*****.**", to=subscription.email, subject="New comment", body="""Tema {0} ima nov komentar. <a href="http://ninjatechforum.appspot.com/topic/{1}">{0}</a>""".format( topic_title, topic_id))
def delete_subscription(self, topic_id): query = Subscription.query(Subscription.topic_id == int(topic_id), Subscription.user_id == self.id).fetch() for entry in query: entry.key.delete()
def get(self): self._require_login('/account/register') self._require_registration('/account/register') user = self.user_bundle.account.key favorites = Favorite.query(ancestor=user).fetch() subscriptions = Subscription.query(ancestor=user).fetch() team_keys = set() team_fav = {} team_subs = {} event_keys = set() event_fav = {} event_subs = {} events = [] for item in favorites + subscriptions: if item.model_type == ModelType.TEAM: team_keys.add(ndb.Key(Team, item.model_key)) if type(item) == Favorite: team_fav[item.model_key] = item elif type(item) == Subscription: team_subs[item.model_key] = item elif item.model_type == ModelType.EVENT: if item.model_key.endswith('*'): # All year events wildcard event_year = int(item.model_key[:-1]) events.append(Event( # add fake event for rendering id=item.model_key, short_name='ALL EVENTS', event_short=item.model_key, year=event_year, start_date=datetime.datetime(event_year, 1, 1) )) else: event_keys.add(ndb.Key(Event, item.model_key)) if type(item) == Favorite: event_fav[item.model_key] = item elif type(item) == Subscription: event_subs[item.model_key] = item team_futures = ndb.get_multi_async(team_keys) event_futures = ndb.get_multi_async(event_keys) teams = sorted([team_future.get_result() for team_future in team_futures], key=lambda x: x.team_number) team_fav_subs = [] for team in teams: fav = team_fav.get(team.key.id(), None) subs = team_subs.get(team.key.id(), None) team_fav_subs.append((team, fav, subs)) events += [event_future.get_result() for event_future in event_futures] events = sorted(events, key=lambda x: x.start_date) event_fav_subs = [] for event in events: fav = event_fav.get(event.key.id(), None) subs = event_subs.get(event.key.id(), None) event_fav_subs.append((event, fav, subs)) self.template_values['team_fav_subs'] = team_fav_subs self.template_values['event_fav_subs'] = event_fav_subs self.template_values['status'] = self.request.get('status') self.template_values['year'] = datetime.datetime.now().year self.response.out.write(jinja2_engine.render('mytba.html', self.template_values))
def get(self): self._require_login() self._require_registration() user = self.user_bundle.account.key favorites = Favorite.query(ancestor=user).fetch() subscriptions = Subscription.query(ancestor=user).fetch() team_keys = set() team_fav = {} team_subs = {} event_keys = set() event_fav = {} event_subs = {} events = [] match_keys = set() match_event_keys = set() match_fav = {} match_subs = {} for item in favorites + subscriptions: if item.model_type == ModelType.TEAM: team_keys.add(ndb.Key(Team, item.model_key)) if type(item) == Favorite: team_fav[item.model_key] = item elif type(item) == Subscription: team_subs[item.model_key] = item elif item.model_type == ModelType.MATCH: match_keys.add(ndb.Key(Match, item.model_key)) match_event_keys.add(ndb.Key(Event, item.model_key.split('_')[0])) if type(item) == Favorite: match_fav[item.model_key] = item elif type(item) == Subscription: match_subs[item.model_key] = item elif item.model_type == ModelType.EVENT: if item.model_key.endswith('*'): # All year events wildcard event_year = int(item.model_key[:-1]) events.append(Event( # add fake event for rendering id=item.model_key, short_name='ALL EVENTS', event_short=item.model_key, year=event_year, start_date=datetime.datetime(event_year, 1, 1), end_date=datetime.datetime(event_year, 1, 1) )) else: event_keys.add(ndb.Key(Event, item.model_key)) if type(item) == Favorite: event_fav[item.model_key] = item elif type(item) == Subscription: event_subs[item.model_key] = item team_futures = ndb.get_multi_async(team_keys) event_futures = ndb.get_multi_async(event_keys) match_futures = ndb.get_multi_async(match_keys) match_event_futures = ndb.get_multi_async(match_event_keys) teams = sorted([team_future.get_result() for team_future in team_futures], key=lambda x: x.team_number) team_fav_subs = [] for team in teams: fav = team_fav.get(team.key.id(), None) subs = team_subs.get(team.key.id(), None) team_fav_subs.append((team, fav, subs)) events += [event_future.get_result() for event_future in event_futures] EventHelper.sort_events(events) event_fav_subs = [] for event in events: fav = event_fav.get(event.key.id(), None) subs = event_subs.get(event.key.id(), None) event_fav_subs.append((event, fav, subs)) matches = [match_future.get_result() for match_future in match_futures] match_events = [match_event_future.get_result() for match_event_future in match_event_futures] MatchHelper.natural_sort_matches(matches) match_fav_subs_by_event = {} for event in match_events: match_fav_subs_by_event[event.key.id()] = (event, []) for match in matches: event_key = match.key.id().split('_')[0] fav = match_fav.get(match.key.id(), None) subs = match_subs.get(match.key.id(), None) match_fav_subs_by_event[event_key][1].append((match, fav, subs)) event_match_fav_subs = sorted(match_fav_subs_by_event.values(), key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) event_match_fav_subs = sorted(event_match_fav_subs, key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) self.template_values['team_fav_subs'] = team_fav_subs self.template_values['event_fav_subs'] = event_fav_subs self.template_values['event_match_fav_subs'] = event_match_fav_subs self.template_values['status'] = self.request.get('status') self.template_values['year'] = datetime.datetime.now().year self.response.out.write(jinja2_engine.render('mytba.html', self.template_values))
def get_subscription(client): subscription = Subscription.query(Subscription.client == client.key, Subscription.status == STATUS_AVAILABLE).get() return subscription
def get_users_subscribed(cls, model_key): user_list = Subscription.query(Subscription.model_key == model_key).fetch() output = [] for user in user_list: output.append(user.user_id) return output
def get(self): self._require_registration() user = self.user_bundle.account.key favorites = Favorite.query(ancestor=user).fetch() subscriptions = Subscription.query(ancestor=user).fetch() team_keys = set() team_fav = {} team_subs = {} event_keys = set() event_fav = {} event_subs = {} events = [] match_keys = set() match_event_keys = set() match_fav = {} match_subs = {} for item in favorites + subscriptions: if item.model_type == ModelType.TEAM: team_keys.add(ndb.Key(Team, item.model_key)) if type(item) == Favorite: team_fav[item.model_key] = item elif type(item) == Subscription: team_subs[item.model_key] = item elif item.model_type == ModelType.MATCH: match_keys.add(ndb.Key(Match, item.model_key)) match_event_keys.add( ndb.Key(Event, item.model_key.split('_')[0])) if type(item) == Favorite: match_fav[item.model_key] = item elif type(item) == Subscription: match_subs[item.model_key] = item elif item.model_type == ModelType.EVENT: if item.model_key.endswith('*'): # All year events wildcard event_year = int(item.model_key[:-1]) events.append( Event( # add fake event for rendering id=item.model_key, short_name='ALL EVENTS', event_short=item.model_key, year=event_year, start_date=datetime.datetime(event_year, 1, 1), end_date=datetime.datetime(event_year, 1, 1))) else: event_keys.add(ndb.Key(Event, item.model_key)) if type(item) == Favorite: event_fav[item.model_key] = item elif type(item) == Subscription: event_subs[item.model_key] = item team_futures = ndb.get_multi_async(team_keys) event_futures = ndb.get_multi_async(event_keys) match_futures = ndb.get_multi_async(match_keys) match_event_futures = ndb.get_multi_async(match_event_keys) teams = sorted( [team_future.get_result() for team_future in team_futures], key=lambda x: x.team_number) team_fav_subs = [] for team in teams: fav = team_fav.get(team.key.id(), None) subs = team_subs.get(team.key.id(), None) team_fav_subs.append((team, fav, subs)) events += [event_future.get_result() for event_future in event_futures] EventHelper.sort_events(events) event_fav_subs = [] for event in events: fav = event_fav.get(event.key.id(), None) subs = event_subs.get(event.key.id(), None) event_fav_subs.append((event, fav, subs)) matches = [match_future.get_result() for match_future in match_futures] match_events = [ match_event_future.get_result() for match_event_future in match_event_futures ] MatchHelper.natural_sort_matches(matches) match_fav_subs_by_event = {} for event in match_events: match_fav_subs_by_event[event.key.id()] = (event, []) for match in matches: event_key = match.key.id().split('_')[0] fav = match_fav.get(match.key.id(), None) subs = match_subs.get(match.key.id(), None) match_fav_subs_by_event[event_key][1].append((match, fav, subs)) event_match_fav_subs = sorted( match_fav_subs_by_event.values(), key=lambda x: EventHelper.distantFutureIfNoStartDate(x[0])) event_match_fav_subs = sorted( event_match_fav_subs, key=lambda x: EventHelper.distantFutureIfNoEndDate(x[0])) self.template_values['team_fav_subs'] = team_fav_subs self.template_values['event_fav_subs'] = event_fav_subs self.template_values['event_match_fav_subs'] = event_match_fav_subs self.template_values['status'] = self.request.get('status') self.template_values['year'] = datetime.datetime.now().year self.response.out.write( jinja2_engine.render('mytba.html', self.template_values))
def get(self): self._require_login('/account/register') self._require_registration('/account/register') user = self.user_bundle.account.key favorites = Favorite.query(ancestor=user).fetch() subscriptions = Subscription.query(ancestor=user).fetch() team_keys = set() team_fav = {} team_subs = {} event_keys = set() event_fav = {} event_subs = {} events = [] for item in favorites + subscriptions: if item.model_type == ModelType.TEAM: team_keys.add(ndb.Key(Team, item.model_key)) if type(item) == Favorite: team_fav[item.model_key] = item elif type(item) == Subscription: team_subs[item.model_key] = item elif item.model_type == ModelType.EVENT: if item.model_key.endswith('*'): # All year events wildcard event_year = int(item.model_key[:-1]) events.append( Event( # add fake event for rendering id=item.model_key, short_name='ALL EVENTS', event_short=item.model_key, year=event_year, start_date=datetime.datetime(event_year, 1, 1), end_date=datetime.datetime(event_year, 1, 1))) else: event_keys.add(ndb.Key(Event, item.model_key)) if type(item) == Favorite: event_fav[item.model_key] = item elif type(item) == Subscription: event_subs[item.model_key] = item team_futures = ndb.get_multi_async(team_keys) event_futures = ndb.get_multi_async(event_keys) teams = sorted( [team_future.get_result() for team_future in team_futures], key=lambda x: x.team_number) team_fav_subs = [] for team in teams: fav = team_fav.get(team.key.id(), None) subs = team_subs.get(team.key.id(), None) team_fav_subs.append((team, fav, subs)) events += [event_future.get_result() for event_future in event_futures] EventHelper.sort_events(events) event_fav_subs = [] for event in events: fav = event_fav.get(event.key.id(), None) subs = event_subs.get(event.key.id(), None) event_fav_subs.append((event, fav, subs)) self.template_values['team_fav_subs'] = team_fav_subs self.template_values['event_fav_subs'] = event_fav_subs self.template_values['status'] = self.request.get('status') self.template_values['year'] = datetime.datetime.now().year self.response.out.write( jinja2_engine.render('mytba.html', self.template_values))