示例#1
0
文件: views.py 项目: jlbrewe/hub
    def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
        """
        Override to perform invite action before or after sign up.
        """
        # Do the usual processing of the invite so that
        # it's `accepted` etc fields get updated
        response = super().post(request, *args, **kwargs)
        invite = self.object

        if not invite:
            # No invite so just return response
            return response

        if request.user.is_authenticated:
            # Perform the action (if necessary) and redirect to it's URL
            if not invite.completed:
                invite.perform_action(request)
            return redir(invite.redirect_url())
        else:
            # Redirect to sign up page with invite URL
            # as next
            response = redir(
                reverse("ui-users-signup") + "?next=" + invite.redirect_url())
            # Set a cookie so that the action is performed
            # once the user is logged in
            response.set_cookie("invite", invite.key)
            return response
示例#2
0
def retrieve(request: HttpRequest, *args, **kwargs) -> HttpResponse:
    """
    Retrieve a project.

    Currently redirect to the project's file list, but in the future
    could be an overview page, with a preview of the main document etc.
    """
    viewset = ProjectsViewSet.init("retrieve", request, args, kwargs)
    project = viewset.get_object()
    return redir("ui-projects-files-list", project.account.name, project.name)
示例#3
0
文件: views.py 项目: jlbrewe/hub
def redirect(request: HttpRequest, *args, **kwargs) -> HttpResponse:
    """
    Redirect from unmatched /me URLs to the user's account.

    e.g. /me => /anna
    e.g. /me/settings -> /me/settings

    If user is unauthenticated, will be asked to signin first.
    """
    return redir("/{0}/{1}".format(request.user.personal_account,
                                   kwargs["rest"]))
示例#4
0
def redirect(request: HttpRequest, *args, **kwargs) -> HttpResponse:
    """
    Redirect from an account `id` URL to an account `name` URL.

    For instances where we need to redirect to the account using `id`
    (e.g. because its name may have changed in a form).
    This uses `get_object` to ensure the same access control applies
    to the redirect.
    """
    viewset = AccountsViewSet.init("retrieve", request, args, kwargs)
    account = viewset.get_object()
    return redir("/{0}{1}".format(account.name, kwargs["rest"]))
示例#5
0
def billing(request: HttpRequest, *args, **kwargs) -> HttpResponse:
    """
    Allow users to manage their billing.

    Currently implemented as a redirect to Stripe Customer Portal
    (https://stripe.com/docs/billing/subscriptions/customer-portal).
    Creates a new `account.customer` if necessary and then redirects
    them to a portal session.
    """
    viewset = AccountsViewSet.init("update_plan", request, args, kwargs)
    account = viewset.get_object()
    session = account.get_customer_portal_session(request)
    return redir(session.url)
示例#6
0
def claim(request: HttpRequest, *args, **kwargs) -> HttpResponse:
    """
    Allow a user to claim a temporary project.

    If the project is already non-temporary, then the user is redirected to
    it's main page. Otherwise they get a form to change it's name etc
    (after authenticating).
    """
    viewset = ProjectsViewSet.init("partial_update", request, args, kwargs)
    project = viewset.get_object()

    if not project.temporary:
        return redir("ui-projects-retrieve", project.account.name,
                     project.name)

    serializer = viewset.get_serializer(dict(name="", public=True))
    return render(request, "projects/claim.html",
                  dict(project=project, serializer=serializer))
示例#7
0
def open(request: HttpRequest, *args, **kwargs) -> HttpResponse:
    """
    Create a temporary project from a single source.

    This view allows for all users, including anonymous users, to create a
    temporary project which they can later save as a permanent project
    if they wish. It aims to be a quick way to start a project and preview
    publishing of a file.

    TODO: See https://github.com/stencila/hub/pull/552 for more todos
    """
    if request.method == "GET":
        # TODO: If a GET request attempt to get source from end of URL or a query parameter
        return render(request, "projects/open.html")

    if request.method == "POST":
        viewset = ProjectsViewSet.init("create", request, args, kwargs)
        serializer = viewset.get_serializer(
            data=dict(temporary=True, public=True))
        serializer.is_valid(raise_exception=True)
        project = serializer.create(serializer.validated_data)

        url = request.POST.get("url")
        if url:
            Source.from_address(url, project=project, path="main")

        file = request.FILES.get("file")
        if file:
            UploadSource.objects.create(project=project,
                                        path=file.name,
                                        file=file)

        # TODO: Make the source the project's main file. How to do before pulling it?

        # TODO: Create a newer simpler job preview page, that is visible to
        # anon users and redirect to that instead of to the project overview page
        # job = source.pull()

        return redir("ui-projects-retrieve", "temp", project.name)

    raise Http404
示例#8
0
def profile_image(request: HttpRequest, *args, **kwargs) -> HttpResponse:
    """
    Update an account's profile image.

    Also updates the cached URL of the user's image in the session storage.
    See the `session_storage` middleware for how this is set initially.
    """
    if request.method == "POST":
        viewset = AccountsViewSet.init("partial_update", request, args, kwargs)
        account = viewset.get_object()
        form = AccountImageForm(request.POST, request.FILES, instance=account)
        if form.is_valid():
            form.save()

            if account.is_personal:
                if request.session and "user" in request.session:
                    request.session["user"][
                        "image"] = request.user.personal_account.image.medium
                    request.session.modified = True

            return redir("ui-accounts-profile", account.name)
        raise RuntimeError("Error attempting to save the account image.")
    else:
        raise Http404
示例#9
0
def destroy(request, number):
    return redir("/")
示例#10
0
def create(request):
    return redir("/")