Example #1
0
def groupLeave(userID, matchID):
    try:
        group = Match.objects.get(pk=matchID)
        leavingMember = Profile.objects.get(pk=userID)
        group.group_members.remove(leavingMember)
        groupList = group.group_members
        if groupList.exists():
            group.save()
            for member in group.group_members.all():
                #notify all group members they have a message
                print(member.id)
                userDevice = FCMDevice()
                userDevice.registration_id = member.deviceID
                userDevice.type = "Android"
                notificationMsg = leavingMember.name + " has left the Group " + group.group_name
                if member.notification:
                    userDevice.send_message(title="Group Notification",
                                            body=notificationMsg)
                userDevice.send_message(data={
                    "title": "Group Notification",
                    "body": notificationMsg
                })
        else:
            group.delete()
    except ObjectDoesNotExist:
        pass
Example #2
0
    def handle(self, *args, **options):
        registration_id = options['registration_id']
        title = options['title']
        body = options['body']

        device = FCMDevice(registration_id=registration_id, type='android')

        device.send_message(title=title, body=body)
Example #3
0
def linked_accounts_analyzer():
    for linked_account in LinkedAccount.objects.all():
        users = linked_account.users.all()
        if users:
            try:
                user = api.get_user(linked_account.twitter_screen_name)
                tw_screen_name = user.screen_name
                tw_name = user.name
                tweets = api.user_timeline(
                    screen_name=linked_account.twitter_screen_name, count=20)
                results = []
                for tweet in tweets:
                    results.append(tweet.text)
                if linked_account.last_tweet in results:
                    index = results.index(linked_account.last_tweet)
                    results = results[:index]
                if results:
                    linked_account.last_tweet = results[0]
                    linked_account.save()
                    logger.info("-- Text considered dangerous --")
                    logger.info("--------------------------------------")
                    for result in results:
                        text_to_analize = TextBlob(str(result))
                        if text_to_analize.detect_language() == 'es':
                            try:
                                analized_text = text_to_analize.translate(
                                    to='en')
                            except:
                                logger.info("-- No translated --")
                        if analized_text.sentiment.polarity < 0:
                            for u in users:
                                if u.device_set is None:
                                    logger.info("not device registered for " +
                                                u.username)
                                else:
                                    user_result = Result(
                                        tweet=str(result),
                                        owner_screen=tw_screen_name,
                                        owner_name=tw_name,
                                        user=u)
                                    user_result.save()
                                    device = FCMDevice(registration_id=u.
                                                       device_set.first().key,
                                                       type="android")
                                    device.send_message(
                                        title="Alerta!",
                                        body="Tweet peligroso!",
                                        data={"tweet": str(result)})
                            logger.info(result + " ---- polarity: " +
                                        str(analized_text.sentiment.polarity))
                    logger.info("--------------------------------------")
            except tweepy.TweepError as e:
                logger.info("Internal problems with the linked accounts")
    name = "linked_accounts_analyzer"
Example #4
0
def send_notification(body_signal,
                      msg="O paciente {0} teve um sinal alterado"):
    patient = body_signal.owner
    monitor_tokens = patient.get_monitor_tokens()
    message_title = "Paciente {} Alerta".format(patient)
    message_body = msg.format(patient)
    print(message_body, monitor_tokens, patient)

    for token in monitor_tokens:
        if token is not None:
            device = FCMDevice(registration_id=token, type='android')
            data_message = {
                "Type": "ALERTA",
                "Title": message_title,
                "Body": message_body
            }
            device.send_message(title=message_title,
                                body=message_body,
                                data=data_message)
Example #5
0
def groupAdd(userMatchID, matchID, profile_id):
    group = Match.objects.get(pk=matchID)
    userMatch = Match.objects.get(pk=userMatchID)
    currUser = Profile.objects.get(pk=profile_id)
    newMember = userMatch.returnOtherMatch(currUser)
    if not group.group_members.all().filter(id=newMember.id).count():
        group.group_members.add(newMember)
        userMatch.save()

        #notify group member
        userDevice = FCMDevice()
        userDevice.registration_id = newMember.deviceID
        userDevice.type = "Android"
        notificationMsg = currUser.name + " has added you into Group: " + group.group_name
        if newMember.notification:
            userDevice.send_message(title="Group Invite", body=notificationMsg)
        userDevice.send_message(data={
            "title": "Group Invite",
            "body": notificationMsg
        })
Example #6
0
def send_app_notification(all_devices, title, body, data=None):
    return
    from notification.models import MobileDevice
    all_devices = MobileDevice.objects.filter(id__in=all_devices)
    if not all_devices:
        print('No Devices found for notification')
        return
    if settings.ENABLE_NOTIFICATION:
        device = FCMDevice()
        for each_user in all_devices:
            # print(each_user.device_id)
            # print(each_user.token)
            device.device_id = each_user.device_id
            device.registration_id = each_user.token
            device.save()
            device.send_message(data=data)
    else:
        print('Notification sent to Users:')
        for each_user in all_devices:
            msg = "Device Id: {} Token: {} Title: {} Body: {} ".format(
                each_user.device_id, each_user.token, title, body)
            print(msg)
Example #7
0
def matchAccept(userid, match, acceptance):
    userProfile = Profile.objects.get(pk=userid)
    otherUser = match.returnOtherMatch(userProfile)

    if match.user1 == userProfile:
        match.user1accepted = acceptance
        match.user1HasMatched = True

        if match.user2HasMatched:
            if match.user2accepted:
                match.hasMatched = True
                # Notify User2 of a match;
                user2device = FCMDevice()
                user2device.registration_id = otherUser.deviceID
                user2device.type = "Android"
                notificationMsg = "A new match has been found for you!"
                if otherUser.notification:
                    user2device.send_message(title="New Match!",
                                             body=notificationMsg)
                user2device.send_message(data={
                    "title": "New Match!",
                    "body": notificationMsg
                })

    else:
        match.user2accepted = acceptance
        match.user2HasMatched = True

        if match.user1HasMatched:
            if match.user1accepted:
                match.hasMatched = True
                # Notify user1 of a match;
                user1device = FCMDevice()
                user1device.registration_id = otherUser.deviceID
                user1device.type = "Android"
                notificationMsg = "A new match has been found for you!"
                if otherUser.notification:
                    user1device.send_message(title="New Match!",
                                             body=notificationMsg)
                user1device.send_message(data={
                    "title": "New Match!",
                    "body": notificationMsg
                })

    match.save()
Example #8
0
def createMessage(senderid, matchid, message, isGroup):
    retval = {}
    try:
        sender = Profile.objects.get(pk=senderid)
        match = Match.objects.get(pk=matchid)
        msg = Message(sender=sender, matchID=match, message=message)
        msg.save()
        if isGroup:
            group = groupMembers(matchid)
            for member in group:
                #notify all group members they have a message
                print(member.id)
                userDevice = FCMDevice()
                userDevice.registration_id = member.deviceID
                userDevice.type = "Android"
                notificationMsg = match.group_name + " has recieved a new message"
                if member.notification:
                    userDevice.send_message(title="New Message",
                                            body=notificationMsg)
                userDevice.send_message(data={
                    "title": "New Message",
                    "body": notificationMsg
                })
        else:
            #notify user they have a message
            userDevice = FCMDevice()
            userDevice.registration_id = match.returnOtherMatch(
                sender).deviceID
            userDevice.type = "Android"
            notificationMsg = sender.name + " has sent you a message"
            if match.returnOtherMatch(sender).notification:
                userDevice.send_message(title="New Message",
                                        body=notificationMsg)
            userDevice.send_message(data={
                "title": "New Message",
                "body": notificationMsg
            })
        retval["id"] = msg.id
    except ObjectDoesNotExist:
        retval["id"] = -1
    except MultipleObjectsReturned:
        retval["id"] = -2
    return retval
Example #9
0
def question_answer(request, id):
    question = Form_question.objects.get(id=id)
    answer = AnswerForm()
    if request.method == 'POST':
        answer = AnswerForm(request.POST)
        ans = answer.save(commit=False)
        ans.question = question
        ans.save()
        print(ans)
        device = FCMDevice()
        device.registration_id = question.user.registration_id
        device.save()
        print(device.send_message(title="Abilis Question Answered", body="Your Question has been answered",
                                  data={"question_id": id}))
        ans.save()
        return redirect('dashboard')
    else:
        return render(request, 'forum/question_answer.html', context={'question': question, 'answer_forum': answer})
Example #10
0
def logout_notify(token):
    device = FCMDevice(registration_id=token, type='android')
    data_message = {"Type": "LOGOUT"}
    device.send_message(data=data_message)