Beispiel #1
0
def favorite(request):
    if request.method != 'POST':
        return HttpResponse('POST required at this url', status=400, content_type='text/plain')
    if not request.user.is_authenticated():
        return HttpResponse('silly rabbit, only signed-in folks can favorite', status=400, content_type='text/plain')

    action = request.POST.get('action', 'favorite')
    asset_id = request.POST.get('asset_id', '')
    try:
        (asset_id,) = re.findall('6a\w+', asset_id)
    except TypeError:
        raise Http404

    if action != 'favorite':
        return HttpResponse('Unsupported action %r' % action, status=400, content_type='text/plain')

    with typepad.client.batch_request():
        asset = Asset.get_by_url_id(asset_id)
    fav = Favorite()
    fav.in_reply_to = asset.asset_ref
    request.user.favorites.post(fav)
    typepadapp.signals.favorite_created.send(sender=fav, instance=fav, parent=asset,
        group=request.group)

    return HttpResponse('OK', content_type='text/plain')
Beispiel #2
0
def asset_meta(request, fresh=False):
    if not request.user.is_authenticated():
        return HttpResponse('silly rabbit, asset_meta is for authenticated users',
            status=400, content_type='text/plain')

    user_id = request.user.xid
    cache_key = 'favorites:%s' % user_id
    favs = None if fresh else cache.get(cache_key)

    if favs is None:
        log.debug("Oops, going to server for %s's asset_meta", request.user.xid)

        fav_objs = {}
        html_ids = request.POST.getlist('asset_id')
        with typepad.client.batch_request():
            for html_id in html_ids:
                assert html_id.startswith('asset-')
                xid = html_id[6:]
                fav_objs[html_id] = Favorite.head_by_user_asset(user_id, xid)

        favs = list(html_id for html_id, fav_obj in fav_objs.items()
            if fav_obj.found())
        if not fresh:
            cache.set(cache_key, favs, ONE_DAY)
    else:
        log.debug('Yay, returning asset_meta for %s from cache', request.user.xid)

    favs = dict((html_id, {"favorite": True}) for html_id in favs)
    return HttpResponse(json.dumps(favs), content_type='application/json')
Beispiel #3
0
def favorite(request):
    if request.method != 'POST':
        return HttpResponse('POST required at this url', status=400, content_type='text/plain')

    action = request.POST.get('action', 'favorite')
    asset_id = request.POST.get('asset_id', '')
    try:
        (asset_id,) = re.findall('6a\w+', asset_id)
    except TypeError:
        raise Http404

    if action == 'favorite':
        with typepad.client.batch_request():
            asset = Asset.get_by_url_id(asset_id)
        fav = Favorite()
        fav.in_reply_to = asset.asset_ref
        request.user.favorites.post(fav)
        typepadapp.signals.favorite_created.send(sender=fav, instance=fav, parent=asset,
            group=request.group)

        # Save the user's last face when favorited.
        try:
            last = Lastface.objects.get(owner=request.user.xid)
        except Lastface.DoesNotExist:
            pass
        else:
            Favoriteface(favoriter=request.user.xid, favorited=asset_id,
                lastface=last.face).save()
    else:
        # Getting the xid will do a batch, so don't do it inside our other batch.
        xid = request.user.xid
        with typepad.client.batch_request():
            asset = Asset.get_by_url_id(asset_id)
            fav = Favorite.get_by_user_asset(xid, asset_id)
        fav.delete()
        typepadapp.signals.favorite_deleted.send(sender=fav, instance=fav,
            parent=asset, group=request.group)

    return HttpResponse('OK', content_type='text/plain')
Beispiel #4
0
def photo(request, xid, template=None):
    # Ask this up front to get the user object outside of batches.
    authed = request.user.is_authenticated()

    lastface, elsewhere = None, None
    with typepad.client.batch_request():
        photo = Asset.get_by_url_id(xid)
        favs = photo.favorites

        favfaces = dict()
        for face in Favoriteface.objects.all().filter(favorited=xid):
            favfaces[face.favoriter] = Asset.get_by_url_id(face.lastface)

        if authed:
            elsewhere = request.user.elsewhere_accounts
            try:
                lastface_mod = Lastface.objects.get(owner=request.user.xid)
            except Lastface.DoesNotExist:
                pass
            else:
                lastface = Asset.get_by_url_id(lastface_mod.face)

    userfav = None
    if authed:
        elsewhere = sharing_for_elsewhere(elsewhere)
        # Get the Favorite in a separate batch so we can handle if it fails.
        try:
            with typepad.client.batch_request():
                userfav = Favorite.get_by_user_asset(request.user.xid, xid)
        except Favorite.NotFound:
            userfav = None

    # Annotate the favorites with the last faces, so we get them naturally in their loop.
    for fav in favs:
        try:
            fav.lastface = favfaces[fav.author.xid]
        except KeyError:
            pass

    if template is None:
        mobile_context = makeaface.context_processors.mobile(request)
        template = 'mobileface/photo.html' if mobile_context['mobile'] else 'makeaface/photo.html'
    return TemplateResponse(request, template, {
        'photo': photo,
        'favorites': favs,
        'user_favorite': userfav,
        'lastface': lastface,
        'share': elsewhere,
    })