Example #1
0
def upload_photo(request):
    if request.method != 'POST':
        return HttpResponse('POST required at this url', status=400, content_type='text/plain')

    content_type = request.META['CONTENT_TYPE']
    assert content_type.startswith('image/')
    bodyfile = StringIO(request.raw_post_data)

    target_url = request.group.photo_assets._location
    target_parts = urlparse(target_url)
    target_path = target_parts.path.replace('.json', '')
    log.debug('Using %r as target URL', target_path)

    asset = Asset()
    asset.title = "a face"
    resp, content = typepad.api.browser_upload.upload(asset, bodyfile,
        content_type=content_type, redirect_to='http://example.com/',
        target_url=target_path, post_type='photo')

    if resp.status != 302:
        log.debug('%d response from typepad: %s', resp.status, content)
    assert resp.status == 302

    typepadapp.signals.asset_created.send(sender=asset, instance=asset,
        group=request.group, parent=request.group.photo_assets)

    if 'location' not in resp:
        log.debug('No Location in response, only %r', resp.keys())
    loc = resp['location']
    loc_parts = parse_qs(urlparse(loc).query)

    if 'asset_url' not in loc_parts:
        log.warning('New location was %r', loc)
        log.warning('Original response/content were %r, %r', resp, content)
    loc = loc_parts['asset_url'][0]

    log.debug('LOCATION IS A %s %r', type(loc).__name__, loc)
    with typepad.client.batch_request():
        asset = Asset.get(loc)
    image_url = asset.image_link.square(150).url

    # Save the photo as a new last face for the poster.
    Lastface(owner=request.user.xid, face=asset.xid).save()

    # Flash doodad needs a 200, not a redirect.
    return HttpResponse(image_url, content_type='text/plain')