예제 #1
0
def aws_token(request):
    seconds = 60 * 60 * 24
    token = aws_sts.get_s3_upload_token(request.user, seconds)
    serializer = AwsTokenSerializer(data={
        'aws_access_key': token.credentials.access_key,
        'aws_secret_key': token.credentials.secret_key,
        'aws_session_token': token.credentials.session_token,
        'expires': timezone.now() + datetime.timedelta(seconds=seconds)
    })
    if not serializer.is_valid():
        raise RuntimeError(serializer.errors)
    return Response(serializer.data)
예제 #2
0
def album(request, pk):
    album = get_object_or_404(Album, pk=pk)
    if not album.is_user_member(request.user.id):
        # TODO ...
        pass

    num_photos_added = 0
    num_photos_failed = 0

    if request.POST:
        if "add_photos" in request.POST:
            pending_photo_ids = []
            for f in request.FILES.getlist("photo_files"):

                pending_photo = Photo.objects.upload_request(author=request.user)

                if settings.USING_LOCAL_PHOTOS:
                    image_uploads.process_file_upload(pending_photo, f.chunks())
                else:
                    # Forward request to photo upload server
                    userAuthToken = AuthToken.objects.create_auth_token(
                        request.user, "Internal Photo Upload Auth Token", timezone.now()
                    )

                    r = requests.put(
                        settings.PHOTO_UPLOAD_SERVER_URL + "/photos/upload/" + pending_photo.photo_id + "/original/",
                        headers={"Authorization": "Token " + userAuthToken.key},
                        data=f.chunks(),
                    )
                    r.raise_for_status()

                pending_photo_ids.append(pending_photo.photo_id)
                num_photos_added += 1

            # Upload pending photos
            done = False
            total_retry_time = 0
            while not done:
                now = timezone.now()
                try:
                    photo_operations.add_pending_photos_to_album(pending_photo_ids, album.id, now)
                    done = True
                except RuntimeError:
                    # TODO This is a really bad workaround to deal with the
                    # situation where the photos aren't "done" yet (in which
                    # case "add_pending_photos_to_album" currently raises a
                    # "RuntimeError")
                    RETRY_TIME = 5
                    MAX_RETRY_TIME = 600  # 10 minutes

                    if total_retry_time >= MAX_RETRY_TIME:
                        raise

                    time.sleep(RETRY_TIME)
                    total_retry_time += RETRY_TIME

            # TODO: If this function will be refactored to use Class Based Views
            # change sender from `request` to `self` (View instance)
            photos_added_to_album.send(sender=request, photos=pending_photo_ids, by_user=request.user, to_album=album)

    aws_token = aws_sts.get_s3_upload_token(request.user)

    data = {
        "album": album,
        "photos": album.get_photos(),
        "members": album.get_member_users(),
        "num_photos_added": num_photos_added,
        "num_photos_failed": num_photos_failed,
        "aws_token": aws_token,
    }
    return render_to_response("frontend/album.html", data, context_instance=RequestContext(request))