def post_notifications(post_id):

    '''
    Sends out both Push and email.
    Called after the low quality of
    video is ready. Since this is a super high priority notification
    it is sent to all those users who upvoted, asked the question or follows the user'''
    result = db.session.execute(text('''Select
                                        aa.first_name, q.body,
                                         n.id, n.link, n.type
                                         from posts p
                                         left join questions q on q.id = p.question
                                         left join users aa on aa.id = p.answer_author
                                         left join notifications n on n.object_id = :post_id
                                         where p.id = :post_id
                                         and n.type in ('post-add-self_user','post-add-following_user')
                                         group by n.type
                                         limit 2 ;
                                         '''), params={'post_id': post_id})

    try:
        for row in result:

            answer_author_name = row[0]
            question_body = row[1]
            notification_id = row[2]
            link = row[3]
            notification_type = row[4]


            #Get a set of users who haven't been sent this gcm notification yet
            #This includes the question author
            results = db.session.execute(text('''Select
                                             un.user_id, u.first_name, u.email
                                             from user_notifications un
                                             left join user_push_notifications upn
                                             on upn.notification_id  = :notification_id and upn.user_id = un.user_id
                                             left join users u on u.id = un.user_id
                                             where
                                             u.monkness = -1 and
                                             un.notification_id = :notification_id
                                             and upn.user_id is null'''),
                                             params={'notification_id': notification_id})

            print 'Notification id: ', notification_id
            print 'Notification type:', notification_type

            for user in results:

                push.send(notification_id=notification_id, user_id=user[0])
                if notification_type == 'post-add-self_user':
                    print user.email
                    make_email.question_answered(receiver_email=user[2], receiver_name=user[1],
                                               celebrity_name=answer_author_name,
                                               user_id=row[0],
                                               question=question_body, web_link=link,
                                               post_id=post_id)
                    break
    except ObjectNotFoundException:
        pass
def question_upvote(user_id, question_id):
    try:

        should_be_pushed = notification_decision.decide_question_push(user_id=user_id, question_id=question_id)
        if should_be_pushed:

            n = Notification.query.filter(Notification.object_id == question_id,
                                          Notification.type == 'question-ask-self_user').first()
            if n is not None:
                pushed = UserPushNotification.query.filter(UserPushNotification.notification_id == n.id,
                                                           UserPushNotification.user_id == user_id).count()

                if not pushed:
                    push.send(notification_id=n.id, user_id=user_id)
    except ObjectNotFoundException:
        pass