예제 #1
0
 def user_rent_item(sender, instance, *args, **kwargs):
     Rent = instance
     recv = Rent.item
     sender = Rent.hirer
     notify = Notification(sender=sender,
                           user=recv.owner,
                           notification_type=1)
     notify.save()
예제 #2
0
    def post(self):
        form_cls = model_form(Notification)
        notification = Notification()
        form = form_cls(request.form, csrf_enabled=False)
        if not form.validate():
            return json.dumps(form.errors, default=json_util.default), 400

        form.populate_obj(notification)
        notification.save()
        data = [dict(message=notification.message, id='%s' % notification.pk)]
        return json.dumps({'data': data}, default=json_util.default), 201
예제 #3
0
def ping_filter(message, users, sending_user, notify_text, notify_type, notify_id=None):
    for user in users:
        if username_in_message(message, user.username):
            # Create notification
            if user == sending_user:
                continue
            note = Notification(
                text="{} {}: {}".format(sending_user.username, notify_text, message),
                user=user,
                from_user=sending_user,
                type=notify_type,
                identifier=notify_id,
            )
            note.save()
            logger.info("Created notification for user {} from {}".format(note.user, note.from_user))
    return message
예제 #4
0
def ping_filter(message,
                users,
                sending_user,
                notify_text,
                notify_type,
                notify_id=None):
    for user in users:
        if username_in_message(message, user.username):
            # Create notification
            if user == sending_user:
                continue
            note = Notification(text='{} {}: {}'.format(
                sending_user.username, notify_text, message),
                                user=user,
                                from_user=sending_user,
                                type=notify_type,
                                identifier=notify_id)
            note.save()
            logger.info("Created notification for user {} from {}".format(
                note.user, note.from_user))
    return message
예제 #5
0
def notifier(sender, **kwargs):
    recipient = kwargs.pop('recipient', None)

    recipient_list = kwargs.pop('recipient_list', None)

    verb = kwargs.pop('verb', None)
    description = kwargs.pop('description', None)
    nf_type = kwargs.pop('nf_type', 'default')

    actor = kwargs.pop('actor', None)
    actor_text = kwargs.pop('actor_text', None)
    actor_url = kwargs.pop('actor_url', None)

    target = kwargs.pop('target', None)
    target_text = kwargs.pop('target_text', None)
    target_url = kwargs.pop('target_url', None)

    obj = kwargs.pop('obj', None)
    obj_text = kwargs.pop('obj_text', None)
    obj_url = kwargs.pop('obj_url', None)

    extra = kwargs.pop('extra', None)

    actor_text = truncate(
        actor_text,
        Notification._meta.get_field('actor_text').max_length)
    actor_url = truncate(
        actor_url,
        Notification._meta.get_field('actor_url_text').max_length)

    target_text = truncate(
        target_text,
        Notification._meta.get_field('target_text').max_length)
    target_url = truncate(
        target_url,
        Notification._meta.get_field('target_url_text').max_length)

    obj_text = truncate(obj_text,
                        Notification._meta.get_field('obj_text').max_length)
    obj_url = truncate(obj_url,
                       Notification._meta.get_field('obj_url_text').max_length)

    if recipient and recipient_list:
        raise TypeError(
            _("You must specify either a single recipient or a list"
              " of recipients, not both."))
    elif not recipient and not recipient_list:
        raise TypeError(
            _("You must specify the recipient of the notification."))

    if not actor and not actor_text:
        raise TypeError(_("Actor not specified."))

    if not verb:
        raise TypeError(_("Verb not specified."))

    if recipient_list and not isinstance(
            recipient_list, list) and not isinstance(recipient_list, set):
        raise TypeError(
            _("Supplied recipient is not an instance of list or set."))

    if recipient:
        notification = Notification(recipient=recipient,
                                    verb=verb,
                                    description=description,
                                    nf_type=nf_type,
                                    actor_content_object=actor,
                                    actor_text=actor_text,
                                    actor_url_text=actor_url,
                                    target_content_object=target,
                                    target_text=target_text,
                                    target_url_text=target_url,
                                    obj_content_object=obj,
                                    obj_text=obj_text,
                                    obj_url_text=obj_url,
                                    extra=extra)
        saved_notification = notification.save()
    else:
        notifications = []
        for recipient in recipient_list:
            notifications.append(
                Notification(recipient=recipient,
                             verb=verb,
                             description=description,
                             nf_type=nf_type,
                             actor_content_object=actor,
                             actor_text=actor_text,
                             actor_url_text=actor_url,
                             target_content_object=target,
                             target_text=target_text,
                             target_url_text=target_url,
                             obj_content_object=obj,
                             obj_text=obj_text,
                             obj_url_text=obj_url,
                             extra=extra))
        saved_notification = Notification.objects.bulk_create(notifications)
    return saved_notification
예제 #6
0
def generate_notifications():
    """looks through all student requests, older than 20 secs, find offline tutors and send notification if allowed and interval is OKAY

    """
    # filter relevant student requests
    student_rs = StudentRequest.objects.filter(
        is_active=True,
        meeting__isnull=True,
        match__isnull=True,
        created__lte=timezone.now()-timedelta(seconds=5)
    )

    # TODO: first step is to filter student requests that have no notifications sent yet?
    # next would be, filter only the ones that havent sent notifications in X seconds
    student_rs = student_rs.filter(Q(notifications__isnull=True) |
                                   Q(notifications__date__lte=timezone.now() - timedelta(seconds=10))).prefetch_related('user', 'subject', 'notifications').distinct()
    # TODO: prioritize those with longer waiting times
    # student_rs = student_rs.order_by(
    # fetch possible tutors beforehand, reducing the query complexity each step

    # Find matching tutors
    # this may need to be optimized further --> Selecting whole user table!
    all_tutors = CustomUser.objects.prefetch_related('tutordata').filter(
        tutordata__isnull=False, email_verified=True, tutordata__verified=True,
        notificationsettings__isnull=False)

    all_tutors = all_tutors.exclude(meeting__ended=False)

    # find those who have notifications turned on and interval is okay
    all_tutors = all_tutors.filter(
        Q(notificationsettings__enable_push=True) | Q(
            notificationsettings__enable_mail=True)).exclude(
        notification__date__gte=timezone.now() - F('notificationsettings__notify_interval')).distinct()

    time_range_query = Q(notificationsettings__ranges__days__contains=timezone.datetime.today().weekday(),
                         notificationsettings__ranges__start_time__lte=timezone.now().time(),
                         notificationsettings__ranges__end_time__gte=timezone.now().time())

    # now to find those whose time is in range
    all_tutors = all_tutors.filter((Q(notificationsettings__ranges_mode=NotificationSettings.RANGE_ALLOW) & time_range_query) | Q(
        Q(notificationsettings__ranges_mode=NotificationSettings.RANGE_BLOCK) & ~time_range_query)).distinct()

    # don't do anything if there arent any student requests
    if not student_rs:
        return
    # all_tutors = list(all_tutors)
    for request in student_rs:

        # filter again for those who didn't already get a notification for the same request
        tutors = all_tutors.filter(tutordata__subjects=request.subject).exclude(
            notification__in=request.notifications.all())
        # .exclude(notification__date__gte=timezone.now() - F('notificationsettings__notify_interval')).distinct()

        # now to find the 10 best!
        sorted_tutors = sorted(tutors, key=lambda tutor: calculate_user_matching_score(
            request, tutor))[::-1][:10]
        for t in sorted_tutors:
            notification = Notification()
            notification.user = t
            notification.type = Notification.STUDENT_REQUEST
            notification.title = f"{request.user.first_name} braucht deine Hilfe!"
            notification.body = f"Fach: {request.subject.name} in {str(request.user.studentdata.school_data)}"
            notification.content_object = request
            notification.save()
            notification.send()
예제 #7
0
def notifier(sender, **kwargs):
    recipient = kwargs.pop('recipient', None)

    recipient_list = kwargs.pop('recipient_list', None)

    verb = kwargs.pop('verb', None)
    description = kwargs.pop('description', None)
    nf_type = kwargs.pop('nf_type', 'default')

    actor = kwargs.pop('actor', None)
    actor_text = kwargs.pop('actor_text', None)
    actor_url = kwargs.pop('actor_url', None)

    target = kwargs.pop('target', None)
    target_text = kwargs.pop('target_text', None)
    target_url = kwargs.pop('target_url', None)

    obj = kwargs.pop('obj', None)
    obj_text = kwargs.pop('obj_text', None)
    obj_url = kwargs.pop('obj_url', None)

    extra = kwargs.pop('extra', None)

    if recipient and recipient_list:
        raise TypeError(_("You must specify either a single recipient or a list"
                        " of recipients, not both."))
    elif not recipient and not recipient_list:
        raise TypeError(_("You must specify the recipient of the notification."))

    if not actor and not actor_text:
        raise TypeError(_("Actor not specified."))

    if not verb:
        raise TypeError(_("Verb not specified."))

    if recipient_list and not isinstance(recipient_list, list):
        raise TypeError(_("Supplied recipient is not an instance of list."))

    if recipient:
        notification = Notification(
            recipient=recipient,
            verb=verb, description=description, nf_type=nf_type,
            actor_content_object=actor, actor_text=actor_text,
            actor_url_text=actor_url,

            target_content_object=target, target_text=target_text,
            target_url_text=target_url,

            obj_content_object=obj, obj_text=obj_text, obj_url_text=obj_url,
            extra=extra
        )
        saved_notification = notification.save()
    else:
        notifications = []
        for recipient in recipient_list:
            notifications.append(Notification(
                recipient=recipient,
                verb=verb, description=description, nf_type=nf_type,
                actor_content_object=actor, actor_text=actor_text,
                actor_url_text=actor_url,

                target_content_object=target, target_text=target_text,
                target_url_text=target_url,

                obj_content_object=obj, obj_text=obj_text,
                obj_url_text=obj_url,

                extra=extra
            ))
        saved_notification = Notification.objects.bulk_create(notifications)
    return saved_notification