示例#1
0
def process_attachments(request, comment):
    """
    For the provided request, check if files are to be attached or deleted
    from the provided comment. Raises a ValidationError if any files are larger
    than 30MB.
    """

    # Check if any existing comment attachments are to be deleted.
    if request.POST.get('attachments-delete'):
        attachments_delete = request.POST.get('attachments-delete').split(",")
        for fileHash in attachments_delete:
            Attachment.delete(request.get_host(), Comment.api_path_fragment,
                              comment.id, fileHash)

    # Check if any files have been uploaded with the request.
    if request.FILES.has_key('attachments'):
        for f in request.FILES.getlist('attachments'):
            file_request = FileMetadata.from_create_form(f)
            # Maximum file size is 30 MB.
            if len(file_request.file[f.name]) >= 31457280:
                raise ValidationError
            # Associate attachment with comment using attachments API.
            else:
                file_metadata = file_request.create(request.get_host(),
                                                    request.access_token)
                Attachment.create(request.get_host(),
                                  file_metadata.file_hash,
                                  comment_id=comment.id,
                                  access_token=request.access_token,
                                  file_name=f.name)
示例#2
0
def process_attachments(request, comment):
    """
    For the provided request, check if files are to be attached or deleted
    from the provided comment. Raises a ValidationError if any files are larger
    than 30MB.
    """

    # Check if any existing comment attachments are to be deleted.
    if request.POST.get('attachments-delete'):
        attachments_delete = request.POST.get('attachments-delete').split(",")
        for fileHash in attachments_delete:
            Attachment.delete(request.get_host(), Comment.api_path_fragment, comment.id, fileHash)

    # Check if any files have been uploaded with the request.
    if request.FILES.has_key('attachments'):
        for f in request.FILES.getlist('attachments'):
            file_request = FileMetadata.from_create_form(f)
            # Maximum file size is 30 MB.
            if len(file_request.file[f.name]) >= 31457280:
                raise ValidationError
            # Associate attachment with comment using attachments API.
            else:
                file_metadata = file_request.create(request.get_host(), request.access_token)
                Attachment.create(request.get_host(), file_metadata.file_hash,
                                  comment_id=comment.id, access_token=request.access_token, file_name=f.name)
示例#3
0
def edit_microcosm(request, microcosm_id):
    try:
        responses = response_list_to_dict(grequests.map(request.view_requests))
    except APIException as exc:
        return respond_with_error(request, exc)
    view_data = {
        'user': Profile(responses[request.whoami_url], summary=False),
        'site': Site(responses[request.site_url]),
    }

    if request.method == 'POST':
        form = microcosm_edit_form(request.POST)
        if not form.is_valid():
            view_data['form'] = form
            return render(request, microcosm_form_template, view_data)

        payload = form.cleaned_data

        if request.POST.get('remove_logo'):
            payload['logoUrl'] = ''
            payload['removeLogo'] = True
        elif request.FILES.has_key('logo'):
            file_request = FileMetadata.from_create_form(
                request.FILES['logo'], )
            try:
                metadata = file_request.create(
                    request.get_host(),
                    request.access_token,
                    width=64,
                    height=64,
                )
            except APIException as exc:
                return respond_with_error(request, exc)

            logo_url = get_subdomain_url(
                request.get_host()) + '/api/v1/files/' + metadata.file_hash
            if hasattr(metadata, 'file_ext'):
                logo_url = logo_url + '.' + metadata.file_ext
            payload['logoUrl'] = logo_url

        microcosm_request = Microcosm.from_edit_form(payload)
        try:
            microcosm_response = microcosm_request.update(
                request.get_host(), request.access_token)
        except APIException as exc:
            return respond_with_error(request, exc)
        return HttpResponseRedirect(
            reverse('single-microcosm', args=(microcosm_response.id, )))

    if request.method == 'GET':
        try:
            microcosm = Microcosm.retrieve(request.get_host(),
                                           id=microcosm_id,
                                           access_token=request.access_token)
        except APIException as exc:
            return respond_with_error(request, exc)
        view_data['form'] = microcosm_edit_form(microcosm.as_dict)

        return render(request, microcosm_form_template, view_data)
示例#4
0
def edit_microcosm(request, microcosm_id):
    try:
        responses = response_list_to_dict(grequests.map(request.view_requests))
    except APIException as exc:
        return respond_with_error(request, exc)
    view_data = {
        'user': Profile(responses[request.whoami_url], summary=False),
        'site': Site(responses[request.site_url]),
    }

    if request.method == 'POST':
        form = microcosm_edit_form(request.POST)
        if not form.is_valid():
            view_data['form'] = form
            return render(request, microcosm_form_template, view_data)

        payload = form.cleaned_data

        if request.POST.get('remove_logo'):
            payload['logoUrl'] = ''
            payload['removeLogo'] = True
        elif request.FILES.has_key('logo'):
            file_request = FileMetadata.from_create_form(
                request.FILES['logo'],
            )
            try:
                metadata = file_request.create(
                    request.get_host(),
                    request.access_token,
                    width=64,
                    height=64,
                )
            except APIException as exc:
                return respond_with_error(request, exc)

            logo_url = get_subdomain_url(request.get_host()) + '/api/v1/files/' + metadata.file_hash
            if hasattr(metadata, 'file_ext'):
                logo_url = logo_url + '.' + metadata.file_ext
            payload['logoUrl'] = logo_url

        microcosm_request = Microcosm.from_edit_form(payload)
        try:
            microcosm_response = microcosm_request.update(request.get_host(), request.access_token)
        except APIException as exc:
            return respond_with_error(request, exc)
        return HttpResponseRedirect(reverse('single-microcosm', args=(microcosm_response.id,)))

    if request.method == 'GET':
        try:
            microcosm = Microcosm.retrieve(request.get_host(), id=microcosm_id,
                access_token=request.access_token)
        except APIException as exc:
            return respond_with_error(request, exc)
        view_data['form'] = microcosm_edit_form(microcosm.as_dict)

        return render(request, microcosm_form_template, view_data)
示例#5
0
def edit(request, profile_id):
    """
    Edit a user profile (profile name or avatar).
    """

    try:
        responses = response_list_to_dict(grequests.map(request.view_requests))
    except APIException as exc:
        return respond_with_error(request, exc)
    user = Profile(responses[request.whoami_url], summary=False)
    view_data = {
        'user': user,
        'site': Site(responses[request.site_url]),
    }

    if request.method == 'POST':
        form = edit_form(request.POST)
        if form.is_valid():
            # Upload new avatar if present.
            if request.FILES.has_key('avatar'):
                file_request = FileMetadata.from_create_form(request.FILES['avatar'])
                file_metadata = file_request.create(request.get_host(), request.access_token, 100, 100)
                try:
                    Attachment.create(request.get_host(), file_metadata.file_hash, profile_id=user.id,
                        access_token=request.access_token, file_name=request.FILES['avatar'].name)
                except APIException as exc:
                    return respond_with_error(request, exc)

            # Update the actual profile resource.
            profile_request = Profile(form.cleaned_data)
            profile_response = profile_request.update(request.get_host(), request.access_token)

            # Create, update or delete comment on profile (single comment acts as a bio).
            if request.POST.has_key('markdown'):
                profile_comment = {
                    'itemType': 'profile',
                    'itemId': profile_response.id,
                    'markdown': request.POST['markdown'],
                    'inReplyTo': 0
                }

                # If profile already has an attached comment update it, otherwise create a new one.
                if hasattr(profile_response, 'profile_comment'):
                    profile_comment['id'] = profile_response.profile_comment.id
                    comment_request = Comment.from_edit_form(profile_comment)
                    try:
                        if profile_comment['markdown']:
                            comment_request.update(request.get_host(), access_token=request.access_token)
                        else:
                            # If the comment body is empty, assume the user wants to delete it.
                            comment_request.delete(request.get_host(), request.access_token)
                    except APIException as exc:
                        return respond_with_error(request, exc)
                else:
                    if profile_comment['markdown']:
                        comment = Comment.from_create_form(profile_comment)
                        try:
                            comment.create(request.get_host(), request.access_token)
                        except APIException as exc:
                            return respond_with_error(request, exc)

            return HttpResponseRedirect(reverse('single-profile', args=(profile_response.id,)))
        else:
            view_data['form'] = form
            return render(request, form_template, view_data)

    if request.method == 'GET':
        try:
            user_profile = Profile.retrieve(request.get_host(), profile_id, request.access_token)
        except APIException as exc:
            return respond_with_error(request, exc)
        view_data['form'] = edit_form(user_profile.as_dict)
        return render(request, form_template, view_data)
示例#6
0
def edit(request, profile_id):
    """
    Edit a user profile (profile name or avatar).
    """

    try:
        responses = response_list_to_dict(grequests.map(request.view_requests))
    except APIException as exc:
        return respond_with_error(request, exc)
    user = Profile(responses[request.whoami_url], summary=False)
    view_data = {
        'user': user,
        'site': Site(responses[request.site_url]),
    }

    if request.method == 'POST':
        form = edit_form(request.POST)
        if form.is_valid():
            # Upload new avatar if present.
            if request.FILES.has_key('avatar'):
                file_request = FileMetadata.from_create_form(
                    request.FILES['avatar'])
                file_metadata = file_request.create(request.get_host(),
                                                    request.access_token, 100,
                                                    100)
                try:
                    Attachment.create(request.get_host(),
                                      file_metadata.file_hash,
                                      profile_id=user.id,
                                      access_token=request.access_token,
                                      file_name=request.FILES['avatar'].name)
                except APIException as exc:
                    return respond_with_error(request, exc)

            # Update the actual profile resource.
            profile_request = Profile(form.cleaned_data)
            profile_response = profile_request.update(request.get_host(),
                                                      request.access_token)

            # Create, update or delete comment on profile (single comment acts as a bio).
            if request.POST.has_key('markdown'):
                profile_comment = {
                    'itemType': 'profile',
                    'itemId': profile_response.id,
                    'markdown': request.POST['markdown'],
                    'inReplyTo': 0
                }

                # If profile already has an attached comment update it, otherwise create a new one.
                if hasattr(profile_response, 'profile_comment'):
                    profile_comment['id'] = profile_response.profile_comment.id
                    comment_request = Comment.from_edit_form(profile_comment)
                    try:
                        if profile_comment['markdown']:
                            comment_request.update(
                                request.get_host(),
                                access_token=request.access_token)
                        else:
                            # If the comment body is empty, assume the user wants to delete it.
                            comment_request.delete(request.get_host(),
                                                   request.access_token)
                    except APIException as exc:
                        return respond_with_error(request, exc)
                else:
                    if profile_comment['markdown']:
                        comment = Comment.from_create_form(profile_comment)
                        try:
                            comment.create(request.get_host(),
                                           request.access_token)
                        except APIException as exc:
                            return respond_with_error(request, exc)

            return HttpResponseRedirect(
                reverse('single-profile', args=(profile_response.id, )))
        else:
            view_data['form'] = form
            return render(request, form_template, view_data)

    if request.method == 'GET':
        try:
            user_profile = Profile.retrieve(request.get_host(), profile_id,
                                            request.access_token)
        except APIException as exc:
            return respond_with_error(request, exc)
        view_data['form'] = edit_form(user_profile.as_dict)
        return render(request, form_template, view_data)