Ejemplo n.º 1
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,
    })
Ejemplo n.º 2
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')