Skip to content

Redirects: allow update #9593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions readthedocs/projects/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,11 @@ class RedirectForm(forms.ModelForm):

"""Form for project redirects."""

project = forms.CharField(widget=forms.HiddenInput(), required=False, disabled=True)

class Meta:
model = Redirect
fields = ["redirect_type", "from_url", "to_url", "force"]
fields = ["project", "redirect_type", "from_url", "to_url", "force"]

def __init__(self, *args, **kwargs):
self.project = kwargs.pop('project', None)
Expand All @@ -685,18 +687,8 @@ def __init__(self, *args, **kwargs):
else:
self.fields.pop("force")

def save(self, **_): # pylint: disable=arguments-differ
# TODO this should respect the unused argument `commit`. It's not clear
# why this needs to be a call to `create`, instead of relying on the
# super `save()` call.
redirect = Redirect.objects.create(
project=self.project,
redirect_type=self.cleaned_data["redirect_type"],
from_url=self.cleaned_data["from_url"],
to_url=self.cleaned_data["to_url"],
force=self.cleaned_data.get("force", False),
)
return redirect
Comment on lines -688 to -699
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were doing this override to allow passing the project, but by using the trick of listing the project field and always using the project we pass to form this isn't needed anymore.

def clean_project(self):
return self.project


class DomainBaseForm(forms.ModelForm):
Expand Down
18 changes: 15 additions & 3 deletions readthedocs/projects/urls/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
ProjectDelete,
ProjectNotifications,
ProjectNotificationsDelete,
ProjectRedirects,
ProjectRedirectsCreate,
ProjectRedirectsDelete,
ProjectRedirectsList,
ProjectRedirectsUpdate,
ProjectTranslationsDelete,
ProjectTranslationsListAndCreate,
ProjectUpdate,
Expand Down Expand Up @@ -125,11 +127,21 @@
),
re_path(
r'^(?P<project_slug>[-\w]+)/redirects/$',
ProjectRedirects.as_view(),
ProjectRedirectsList.as_view(),
name='projects_redirects',
),
re_path(
r'^(?P<project_slug>[-\w]+)/redirects/delete/$',
r"^(?P<project_slug>[-\w]+)/redirects/create/$",
ProjectRedirectsCreate.as_view(),
name="projects_redirects_create",
),
re_path(
r"^(?P<project_slug>[-\w]+)/redirects/(?P<redirect_pk>[-\w]+)/edit/$",
ProjectRedirectsUpdate.as_view(),
name="projects_redirects_edit",
),
re_path(
r"^(?P<project_slug>[-\w]+)/redirects/(?P<redirect_pk>[-\w]+)/delete/$",
ProjectRedirectsDelete.as_view(),
name='projects_redirects_delete',
),
Expand Down
44 changes: 20 additions & 24 deletions readthedocs/projects/views/private.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,44 +712,40 @@ class ProjectRedirectsMixin(ProjectAdminMixin, PrivateViewMixin):

"""Project redirects view and form view."""

form_class = RedirectForm
template_name = "redirects/redirect_form.html"
context_object_name = "redirect"
lookup_url_kwarg = "redirect_pk"

def get_success_url(self):
return reverse(
'projects_redirects',
args=[self.get_project().slug],
)

def get_queryset(self):
return self.get_project().redirects.all()

class ProjectRedirects(ProjectRedirectsMixin, FormView):

form_class = RedirectForm
template_name = 'projects/project_redirects.html'
class ProjectRedirectsList(ProjectRedirectsMixin, ListView):

def form_valid(self, form):
form.save()
return HttpResponseRedirect(self.get_success_url())
template_name = "redirects/redirect_list.html"
context_object_name = "redirects"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
project = self.get_project()
context['redirects'] = project.redirects.all()
return context

class ProjectRedirectsCreate(ProjectRedirectsMixin, CreateView):

pass

class ProjectRedirectsDelete(ProjectRedirectsMixin, GenericView):

http_method_names = ['post']
class ProjectRedirectsUpdate(ProjectRedirectsMixin, UpdateView):

def post(self, request, *args, **kwargs):
project = self.get_project()
redirect = get_object_or_404(
project.redirects,
pk=request.POST.get('id_pk'),
)
if redirect.project == project:
redirect.delete()
else:
raise Http404
return HttpResponseRedirect(self.get_success_url())
pass


class ProjectRedirectsDelete(ProjectRedirectsMixin, DeleteView):

http_method_names = ['post']


class DomainMixin(ProjectAdminMixin, PrivateViewMixin):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,33 @@

{% load i18n %}

{% block title %}{% trans "Edit Redirects" %}{% endblock %}
{% block title %}{% trans "Redirect" %}{% endblock %}

{% block project-redirects-active %}active{% endblock %}
{% block project_edit_content_header %}{% trans "Redirects" %}{% endblock %}

{% block extra_scripts %}
<script type="text/javascript">

var update_outcome = function(ev) {
var update_outcome = function() {
$('#id_from_url').parent().hide()
$('#id_to_url').parent().hide()
$('#dynamic-redirect').hide()

let redirect_type = $('#id_redirect_type').val();
if (redirect_type == 'prefix') {
$('#id_from_url').parent().show()
$('#id_to_url').parent().hide()
}
else if (redirect_type == 'page') {
$('#id_from_url').parent().show()
$('#id_to_url').parent().show()
}
else if (redirect_type == 'exact') {
$('#id_from_url').parent().show()
$('#id_to_url').parent().show()
}

var field_from_url = $('#id_from_url');
field_to_url = $('#id_to_url');
option = $('#id_redirect_type').val();
Expand Down Expand Up @@ -39,31 +57,11 @@
};

$(document).ready(function() {
$('#id_from_url').parent().hide()
$('#id_to_url').parent().hide()
$('#dynamic-redirect').hide()

update_outcome();
$('#id_redirect_type').bind('change', function(ev) {
$('#id_from_url').val('');
$('#id_to_url').val('');
$('#id_from_url').parent().hide()
$('#id_to_url').parent().hide()

if (this.value == 'prefix') {
$('#id_from_url').parent().show()
$('#id_to_url').parent().hide()
}
else if (this.value == 'page') {
$('#id_from_url').parent().show()
$('#id_to_url').parent().show()
}
else if (this.value == 'exact') {
$('#id_from_url').parent().show()
$('#id_to_url').parent().show()
}
Comment on lines -42 to -63
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this logic to be inside the update_outcome function, this way it works when the form has pre-populated fields when editing an existing redirect.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't fully check this Javascript code, but looks good!

update_outcome(ev);
});
$('#id_from_url, #id_to_url').keyup(update_outcome);
update_outcome();
});
$('#id_from_url, #id_to_url').keyup(update_outcome);
});

</script>
Expand All @@ -76,44 +74,18 @@
{% endblocktrans %}
</p>

{% if redirects|length > 0 %}
<h3>{% trans "Redirects" %}</h3>
<div class="module">
<div class="module-list">
<div class="module-list-wrapper">
<ul>
{% for redirect in redirects %}
<li class="module-item">
<div>
{{ redirect.get_redirect_type_display }}
</div>
{% if redirect.get_from_to_url_display %}
<div>
{{ redirect.get_from_to_url_display }}
</div>
{% endif %}
<ul class="module-item-menu">
<li>
<form method="post" action="{% url "projects_redirects_delete" project.slug %}">
{% csrf_token %}
<input type="hidden" name="id_pk" value="{{ redirect.pk }}" />
<input type="submit" value="{% trans "Remove" %}">
</form>
</li>
</ul>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endif %}

<form method="post" action=".">{% csrf_token %}
{{ form.as_p }}
<div id="dynamic-redirect" class="empty"></div>
<p>
<input style="display: inline;" type="submit" value="{% trans "Add" %}">
</p>
<input type="submit" value="{% trans "Save" %}">
</form>

{% if redirect %}
<form
method="post"
action="{% url 'projects_redirects_delete' project_slug=project.slug redirect_pk=redirect.pk %}">
{% csrf_token %}
<input type="submit" value="{% trans "Delete" %}">
</form>
{% endif %}
{% endblock %}
61 changes: 61 additions & 0 deletions readthedocs/redirects/templates/redirects/redirect_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{% extends "projects/project_edit_base.html" %}

{% load i18n %}

{% block title %}{% trans "Redirects" %}{% endblock %}

{% block project-redirects-active %}active{% endblock %}
{% block project_edit_content_header %}{% trans "Redirects" %}{% endblock %}

{% block project_edit_content %}
<p class="help_text">
{% blocktrans trimmed with docs_url='https://docs.readthedocs.io/page/user-defined-redirects.html' %}
Add redirects for your project. This allows you to fix links to old pages that are 404ing. <a href="{{ docs_url }}">Learn more</a>.
{% endblocktrans %}
</p>

<div class="button-bar">
<ul>
<li>
<a class="button"
href="{% url 'projects_redirects_create' project_slug=project.slug %}">
{% trans "Add Redirect" %}
</a>
</li>
</ul>
</div>

<div class="module">
<div class="module-list">
<div class="module-list-wrapper">
<ul>
{% for redirect in redirects %}
<li class="module-item">
<div>
{{ redirect.get_redirect_type_display }}
</div>
{% if redirect.get_from_to_url_display %}
<div>
{{ redirect.get_from_to_url_display }}
</div>
{% endif %}
<ul class="module-item-menu">
<li>
<a href="{% url 'projects_redirects_edit' project_slug=project.slug redirect_pk=redirect.pk %}">
{% trans "Edit" %}
</a>
</li>
</ul>
</li>
{% empty %}
<li class="module-item">
<p class="quiet">
{% trans "No redirects found." %}
</p>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
{% endblock %}
Empty file.
Loading