Example #1
0
def track_tag_callback(request, username, tag_id):    
    """PuSH will send pings to this URL when a tag's feed is updated
    """
    import hashlib
    from django.conf import settings
    
    tag = get_object_or_404(Tag, pk=tag_id)
    user = User.objects.get(username=username)
    if 'hub.mode' in request.GET and request.GET['hub.mode'] == 'subscribe':
        # Confirm subscription
        h = hashlib.sha1(('%s tracking tag %d at %s' % (user.username, 
                                                        tag.id, 
                                                        settings.SECRET_KEY)))
        if request.GET['hub.topic'] == request.build_absolute_uri(reverse('tag_feed', kwargs={'id': tag.id})) \
                and request.GET['hub.verify_token'] == h.hexdigest():
            # TODO: Consider using the hub.secret mechanism for authentication
            # All OK
            return HttpResponse(request.GET['hub.challenge'])
        else:
            #if request.GET['hub.topic'] != request.build_absolute_uri(reverse('tag_feed', kwargs={'id': tag.id})):
            #    return HttpResponse(
            #        'Error: Topic "%s" does not match feed URL "%s"' % (
            #            request.GET['hub.topic'], 
            #            request.build_absolute_uri(
            #                reverse('tag_feed', kwargs={'id': tag.id}))))
            #if request.GET['hub.verify_token'] != h.hexdigest():
            #    return HttpResponse('Error: Tokens do not match "%s tracking tag %d"' % (user.username, 
            #                                            tag.id))
            return HttpResponse('Error confirming topic and token')
            
    # Create notification
    n = Notification.objects.create(
        user=user,
        subject=tag.description if len(tag.description) <= 30 else '%s...' % (tag.description[:27], ),
        subject_url=reverse(view_tag, kwargs={'id': tag.id}),
        verb='was flagged')
        # TODO: Fetch and parse feed item
        #object = models.CharField(max_length=30)
        #object_url = models.URLField()
    # Notify user if necessary
    if user.get_profile().notify_by_email:
        t = loader.get_template('web/email_notification.txt')
        c = Context({'n': n})
        body_text = t.render(c)
        message = EmailMultiAlternatives('Update from trc.me', 
                                         body_text, 
                                         settings.DEFAULT_FROM_EMAIL, 
                                         [user.email])
        if hasattr(settings, 'EMAIL_ADDRESS_BOUNCE'):
            message.headers = {'Return-Path': settings.EMAIL_ADDRESS_BOUNCE}
        t = loader.get_template('web/email_notification.html')
        body_html = t.render(c)
        message.attach_alternative(body_html, 'text/html')
        message.send()
    return HttpResponse('OK')
Example #2
0
def follow_user_callback(request, username, tracked_username):    
    """PuSH will send pings to this URL when a user's feed is updated
    """
    import hashlib
    from django.conf import settings
    
    tracked_user = get_object_or_404(Tag, username=tracked_username)
    user = User.objects.get(username=username)
    
    if 'hub.mode' in request.GET and request.GET['hub.mode'] == 'subscribe':
        # Confirm subscription
        h = hashlib.sha1(('%s tracking user %s at %s' % (request.user.username, 
                                                        tracked_user.username, 
                                                        settings.SECRET_KEY)))
        if request.GET['hub.topic'] == reverse('user_feed', kwargs={'username': tracked_username}) \
                and request.GET['hub.verify_token'] == h.hexdigest():
            # TODO: Consider using the hub.secret mechanism for authentication
            # All OK
            return HttpResponse(request.GET['hub.challenge'])
        else:
            return HttpResponse('Error confirming topic and token')
        
    # Create notification
    n = Notification.objects.create(
        user=user,
        subject=tracked_user.username,
        subject_url=reverse(view_user, kwargs={'username': tracked_user.username}))
        # TODO: Fetch and parse feed item
    # Notify user if necessary
    if user.get_profile().notify_by_email:
        t = loader.get_template('web/email_notification.txt')
        c = Context({'n': n})
        body_text = t.render(c)
        message = EmailMultiAlternatives('Update from trc.me', 
                                         body_text, 
                                         settings.DEFAULT_FROM_EMAIL, 
                                         [user.email])
        if hasattr(settings, 'EMAIL_ADDRESS_BOUNCE'):
            message.headers = {'Return-Path': settings.EMAIL_ADDRESS_BOUNCE}
        t = loader.get_template('web/email_notification.html')
        body_html = t.render(c)
        message.attach_alternative(body_html, 'text/html')
        message.send()
    return HttpResponse('OK')