Beispiel #1
0
def create_token(url, user_id=None, notification=True):
    # Get the next counter to create the token
    counter = ShortLink.incr_counter()

    # Sanitize the url
    url = sanitize_url(url)

    # Get the title
    # Fix this!! test if the url exists or not!!
    try:
        title = extract_url_title(url)
    except:
        title = "No title"

    # Get the host
    host = extract_url_host(url)

    # Create the instance with the data
    sl = ShortLink()
    sl.counter = counter
    sl.url = url
    sl.title = title
    sl.host = host

    # Save
    sl.save()

    # If is a user link save it also
    if user_id:
        user_link = UserLink()
        user_link.user = User.objects.get(id=user_id)
        user_link.token = sl.token
        user_link.save()

        # Only need notification if we have a user
        if notification:
            # Send notifications
            notif = ShortLinkNotification(sl, user_id=user_id)
            #notif.send_push()  # Push realtime notification
            notif.save()  # save the notification for the dashboard

    # Fill the metrics
    SharedLinkMetrics().increment()

    logger.debug("{0} shorted url '{1}' to token '{2}'".format(user_id,
                                                               url,
                                                               sl.token))

    # Return the new token
    return sl.token
Beispiel #2
0
    def test_shortlink_notification_store(self):
        #with three we have enought to test
        sls = ShortLink.findall()[:3]
        user = User.objects.get(id=1)

        a_len = len(sls)

        for i in sls:
            notif = ShortLinkNotification(short_link=i, user=user)
            time.sleep(1)  # We need notification order
            notif.save()

        r = get_redis_connection()
        res = r.zrange(
            Notification.STORE_KEY_UNREAD_FORMAT.format(user.id), 0, -1)

        self.assertEquals(a_len, len(res))

        for i in range(len(res)):
            before = sls[i]
            after = json.loads(res[i])

            self.assertEquals(before.token, after['token'])