Example #1
0
def question_asked(question_id,
                   from_widget=False, mail_type="question_asked"):

    question = Question.query.filter(Question.id == question_id).first()
    users = User.query.filter(User.id.in_([question.question_author, question.question_to]))

    for user in users:
        if user.id == question.question_author:
            asker = user
        if user.id == question.question_to:
            asked = user

    cutoff_time = datetime.timedelta(days=3)

    ''' If asker source is widget
        and does not have any active mobile devices
        then send them an email notification
        confirming that the question has been asked
    '''
#and not len(notification_util.get_active_mobile_devices(asker.id))
    if from_widget :

        #is_first = True if Question.query.filter(Question.question_author == asker.id).count() == 1 else False
        mail_type += "_by"
        mail_dict['email_text'] = helper.dict[mail_type]['body'].format(asker.first_name, asked.first_name,
                                                                    asked.first_name.split(" ")[0])

        # if is_first:
        #     mail_dict['email_text'] = helper.dict[mail_type]['body_first_question']

        log_id = log_mail(asker.email, mail_type, question_id)

        mail_dict['salutation'] = "Hi %s" % asker.first_name
        mail_dict['pixel_image_url'] += "?id=" + log_id

        mail(email_id=asker.email, log_id=log_id, subject=helper.dict[mail_type]['subject'],
             body=header_template.render(mail_dict), mail_type=mail_type, object_id=question_id,
             cutoff_time=cutoff_time, mail_limit=1)
        mail_type = "question_asked"


    if not len(util.get_active_mobile_devices(asked.id)):

        mail_type += "_to"
        subject = helper.dict[mail_type]['subject'] % asker.first_name
        log_id = log_mail(asked.email, mail_type, question_id)

        print question.slug
        question_url = (config.WEB_URL + "/{0}/{1}").format(asked.username, question.slug)
        mail_dict['salutation'] = "Hi %s" % asked.first_name
        mail_dict['email_text'] = helper.dict[mail_type]['body'].format(asker.first_name, question_url, question.body)
        mail_dict['pixel_image_url'] += "?id=" + log_id

        mail(email_id=asked.email, log_id=log_id, subject=subject,
             body=header_template.render(mail_dict), mail_type=mail_type, object_id=question_id,
             cutoff_time=cutoff_time, mail_limit=1)
Example #2
0
def question_answered(receiver_email, receiver_name, celebrity_name, question, web_link,
                      post_id, user_id, mail_type='post_add'):

    if len(util.get_active_mobile_devices(user_id)):
        cutoff_time = datetime.timedelta(days=10000)

        log_id = log_mail(receiver_email, mail_type, post_id)
        mail_dict['salutation'] = "Hi %s" % receiver_name
        mail_dict['email_text'] = helper.dict[mail_type]['body'] % (celebrity_name, web_link, question)
        mail_dict['pixel_image_url'] += "?id=" + log_id

        mail(email_id=receiver_email, log_id=log_id, subject=helper.dict[mail_type]['subject'],
             body=header_template.render(mail_dict), mail_type=mail_type, object_id=post_id,
             cutoff_time=cutoff_time, mail_limit=1)
def send(notification_id, user_id, k=None, source='application', notification_limit=config.GLOBAL_PUSH_NOTIFICATION_DAY_LIMIT):

    devices = util.get_active_mobile_devices(user_id)

    should_push = util.count_of_push_notifications_sent(user_id=user_id) \
                                       <= notification_limit

    notification = Notification.query.get(notification_id)



    ''' Only if the user has valid devices to be pushed to and is
    under global limit for the same'''

    if len(devices) and should_push:
        print 'Still under limits for number of daily notifications'
        k = key[notification.type] if k is None else k

        group_id = '-'.join([str(notification.type), str(notification.object_id)])

        for device in devices:
            print 'Found valid devices for push notifications'


            user_push_notification = UserPushNotification(notification_id=notification_id,
                                                               user_id=user_id,
                                                              device_id=device.device_id,
                                                              push_id=device.push_id,
                                                              added_at=datetime.datetime.now(),
                                                              pushed_at=datetime.datetime.now(),
                                                              clicked_at=None,
                                                              source=source,
                                                              cancelled=False,
                                                              result=None,
                                                              id=get_item_id())
            db.session.add(user_push_notification)
            db.session.commit()
            payload = {
                             "user_to": user_id,
                             "type": 1,
                             "id": user_push_notification.id,
                             "notification_id": notification.id,
                             "heading": k['title'],
                             "text": notification.text.replace('<b>', '').replace('</b>', ''),
                             "styled_text": notification.text,
                             "icon_url": notification.icon,
                             "cover_image": None,
                             "group_id": group_id,
                             "link": notification.link,
                             "deeplink": notification.link,
                             "timestamp": int(time.mktime(user_push_notification.added_at.timetuple())),
                             "seen": False,
                             "label_one": k['label_one'],
                             "label_two": k['label_two']
                         }

            if get_device_type(device.device_id) == 'android':
                print 'pushing gcm for android'
                gcm_sender = GCM()
                gcm_sender.send_message([device.push_id], payload)

            if get_device_type(device.device_id) == 'ios':
                print 'pushing for iOS'
                print 'For Push id', device.push_id
                apns = APN()
                payload = get_payload_apns(payload=payload)
                print payload
                apns.send_message([device.push_id], payload)