Skip to content
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

Surface "abandoned" state on projects in UI #4995

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
09d8213
admin func for to update the abandoned project's slug
dojutsu-user Dec 13, 2018
d212759
add is_abandoned boolean field in Project model
dojutsu-user Dec 13, 2018
906d0c4
generate migrations file
dojutsu-user Dec 13, 2018
8f034b7
update admin action docs string and set is_abandoned flag
dojutsu-user Dec 13, 2018
c1c25ed
Add templates
dojutsu-user Dec 13, 2018
99dde0c
Add admin action for requesting namespace
dojutsu-user Dec 13, 2018
33c58ff
Add abandoned state on sidebar
dojutsu-user Dec 18, 2018
6378a4a
fix indentation
dojutsu-user Dec 18, 2018
62ef610
send an email notifying the user
dojutsu-user Dec 18, 2018
c699012
Add tests
dojutsu-user Dec 18, 2018
843f2a9
remove redundant import
dojutsu-user Dec 18, 2018
033f58b
use inbuilt notification system
dojutsu-user Dec 20, 2018
eb9a1af
Update tests with slight improvement
dojutsu-user Dec 20, 2018
b03400a
Add test for task
dojutsu-user Dec 20, 2018
4df938f
fix lint
dojutsu-user Dec 20, 2018
c36b421
Remove unnecessary imports
dojutsu-user Dec 20, 2018
b03d597
remove abandon_project_confirm.html/txt
dojutsu-user Dec 21, 2018
633be03
improve tests
dojutsu-user Dec 25, 2018
965d540
Add log.info
dojutsu-user Jan 5, 2019
ae2bda8
Merge branch 'master' into abandoned-projects-additions
dojutsu-user Jan 11, 2019
c4104b8
rename migrations file
dojutsu-user Jan 11, 2019
23c2123
Merge branch 'abandoned-projects-additions' of https://github.com/doj…
dojutsu-user Jan 11, 2019
8ee2d67
Merge branch 'master' into abandoned-projects-additions
dojutsu-user Jan 18, 2019
8d29c0b
Merge branch 'master' into abandoned-projects-additions
dojutsu-user Jan 22, 2019
236c37d
Merge branch 'master' into abandoned-projects-additions
dojutsu-user Jan 22, 2019
113e11e
Merge branch 'master' into abandoned-projects-additions
dojutsu-user Jan 24, 2019
d90bdd0
Merge branch 'master' into abandoned-projects-additions
dojutsu-user Feb 21, 2019
36dcb16
Rename migrations file
dojutsu-user Feb 21, 2019
cac446d
Merge master branch
dojutsu-user Mar 19, 2019
a67d640
fix imports
dojutsu-user Mar 19, 2019
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
74 changes: 72 additions & 2 deletions readthedocs/projects/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

"""Django administration interface for `projects.models`."""

import os

from django.contrib import admin, messages
from django.contrib.admin.actions import delete_selected
from django.utils.translation import ugettext_lazy as _
from guardian.admin import GuardedModelAdmin

from readthedocs.builds.models import Version
from readthedocs.core.models import UserProfile
from readthedocs.core.utils import broadcast, trigger_build
from readthedocs.core.utils import broadcast, trigger_build, send_email
from readthedocs.notifications.views import SendNotificationView
from readthedocs.redirects.models import Redirect
from readthedocs.search.utils import _indexing_helper
Expand All @@ -29,9 +31,10 @@
from .notifications import (
DeprecatedBuildWebhookNotification,
DeprecatedGitHubWebhookNotification,
AbandonedProjectNotification,
ResourceUsageNotification,
)
from .tasks import remove_dirs
from .tasks import remove_dirs, rename_project_dir


class ProjectSendNotificationView(SendNotificationView):
Expand Down Expand Up @@ -149,6 +152,8 @@ class ProjectAdmin(GuardedModelAdmin):
actions = [
'send_owner_email',
'ban_owner',
'mark_as_abandoned',
'request_namespace',
'build_default_version',
'reindex_active_versions',
'wipe_all_versions',
Expand Down Expand Up @@ -215,6 +220,71 @@ def delete_selected_and_artifacts(self, request, queryset):
)
return delete_selected(self, request, queryset)

def mark_as_abandoned(self, request, queryset):
"""
Marks selected projects as abandoned.

It performs the following sub-tasks:
* Adds '-abandoned' to the slug.
* Updates the project's directory name to
match the new slug.
* Sets Project.is_abandoned=True.
* Adds a persistent error notification for the
user notifying the changes.
* Notify user via email.
"""
qs_iterator = queryset.iterator()
for project in qs_iterator:
new_slug = '{}-abandoned'.format(project.slug)
old_doc_path = project.doc_path
new_doc_path = os.path.join(
os.path.dirname(project.doc_path),
new_slug
)
broadcast(
type='web', task=rename_project_dir,
args=[old_doc_path, new_doc_path]
)
project.slug = new_slug
project.is_abandoned = True
project.save()

# Sending notification emails
users = project.users.get_queryset()
for user in users:
notification = AbandonedProjectNotification(
user=user,
success=False,
extra_context={'project': project}
)
notification.send()

self.message_user(
request,
'{} is marked as abandoned.'.format(project.name),
level=messages.SUCCESS
)

mark_as_abandoned.short_description = 'Mark as abandoned'

def request_namespace(self, request, queryset):
"""Notify user that their project's namespace has been requested via email"""
qs_iterator = queryset.iterator()
for project in qs_iterator:
users = project.users.get_queryset()
for user in users:
send_email(
recipient=user.email,
dojutsu-user marked this conversation as resolved.
Show resolved Hide resolved
subject='Rename request for abandoned project',
template='projects/email/abandon_project.txt',
template_html='projects/email/abandon_project.html',
context={'proj_name': project.name}
)
success_msg = 'Email sent to {}'.format(user.email)
self.message_user(request, success_msg, level=messages.SUCCESS)

request_namespace.short_description = 'Request namespace'

def build_default_version(self, request, queryset):
"""Trigger a build for the project version."""
total = 0
Expand Down
20 changes: 20 additions & 0 deletions readthedocs/projects/migrations/0042_add_is_abandoned_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.16 on 2018-12-13 10:35
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('projects', '0041_index-repo-field'),
]

operations = [
migrations.AddField(
model_name='project',
name='is_abandoned',
field=models.BooleanField(default=False, verbose_name='Is abandoned'),
),
]
1 change: 1 addition & 0 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ class Project(models.Model):
)

featured = models.BooleanField(_('Featured'), default=False)
is_abandoned = models.BooleanField(_('Is abandoned'), default=False)
skip = models.BooleanField(_('Skip'), default=False)
install_project = models.BooleanField(
_('Install Project'),
Expand Down
30 changes: 28 additions & 2 deletions readthedocs/projects/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

"""Project notifications."""

from django.urls import reverse
from django.http import HttpRequest
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from django.http import HttpRequest
from django.urls import reverse
from messages_extends.constants import ERROR_PERSISTENT

from readthedocs.notifications import Notification, SiteNotification
Expand All @@ -19,6 +20,31 @@ class ResourceUsageNotification(Notification):
level = REQUIREMENT


class AbandonedProjectNotification(SiteNotification):

level = REQUIREMENT
send_email = True
failure_level = ERROR_PERSISTENT
subject = 'Abandoned project {{ proj_name }}'
failure_message = _(
'Your project {{ proj_name }} is marked as abandoned. Update link '
'for the project docs is <a href="{{ proj_url }}">{{ proj_url }}</a>.'
)

def get_context_data(self):
context = super(AbandonedProjectNotification, self).get_context_data()
project = self.extra_context.get('project')
proj_url = '{base}{url}'.format(
base=settings.PRODUCTION_DOMAIN,
url=project.get_absolute_url()
)
context.update({
'proj_name': project.name,
'proj_url': proj_url
})
return context


class EmailConfirmNotification(SiteNotification):

failure_level = ERROR_PERSISTENT
Expand Down
7 changes: 7 additions & 0 deletions readthedocs/projects/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,13 @@ def finish_inactive_builds():
)


@app.task()
def rename_project_dir(old_path, new_path):
"""Renames the project's directory."""
log.info('Moving %s to %s', old_path, new_path)
shutil.move(old_path, new_path)


@app.task(queue='web')
def retry_domain_verification(domain_pk):
"""
Expand Down
96 changes: 95 additions & 1 deletion readthedocs/rtd_tests/tests/projects/test_admin_actions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# -*- coding: utf-8 -*-
import django_dynamic_fixture as fixture

import mock

from django import urls
import django_dynamic_fixture as fixture
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.test import TestCase
from django.conf import settings
from messages_extends.models import Message as PersistentMessage

from readthedocs.core.models import UserProfile
from readthedocs.projects.models import Project
Expand Down Expand Up @@ -78,3 +83,92 @@ def test_project_delete(self, broadcast):
type='app', task=remove_dirs, args=[(self.project.doc_path,)],
),
])

@mock.patch('readthedocs.projects.admin.send_email')
def test_request_namespace(self, mock_send_email):
"""Test the requesting of project's namespace"""
action_data = {
ACTION_CHECKBOX_NAME: [self.project.pk],
'action': 'request_namespace',
}
resp = self.client.post(
urls.reverse('admin:projects_project_changelist'),
action_data
)

proj = self.project
self.assertEqual(proj.users.get_queryset().count(), 1)
user = proj.users.get_queryset().first()

mock_send_email.assert_called_once_with(
recipient=user.email,
subject='Rename request for abandoned project',
template='projects/email/abandon_project.txt',
template_html='projects/email/abandon_project.html',
context={'proj_name': proj.name}
)

@mock.patch('readthedocs.projects.admin.broadcast')
@mock.patch('readthedocs.notifications.backends.send_email')
def test_mark_as_abandoned(self, mock_send_email, mock_broadcast):
"""Test the marking of project as abandoned."""
from readthedocs.projects.tasks import rename_project_dir

project = fixture.get(Project, users=[self.admin])
action_data = {
ACTION_CHECKBOX_NAME: [project.pk],
'action': 'mark_as_abandoned',
}

# before marking project as abandoned.
current_slug = project.slug
current_doc_path = project.doc_path
self.assertEqual(project.users.get_queryset().count(), 1)
user = project.users.get_queryset().first()

self.assertEqual(PersistentMessage.objects.count(), 0)

resp = self.client.post(
urls.reverse('admin:projects_project_changelist'),
action_data,
)

project.refresh_from_db()

# after marking the project as abandoned.
new_doc_path = project.doc_path
new_slug = '{}-abandoned'.format(current_slug)

self.assertEqual(
project.get_absolute_url(),
'/projects/{}/'.format(new_slug)
)
new_url = '{base}{url}'.format(
base=settings.PRODUCTION_DOMAIN,
url=project.get_absolute_url()
)

self.assertTrue(project.is_abandoned, True)
self.assertEqual(project.slug, new_slug)
mock_broadcast.assert_called_once_with(
type='web',
task=rename_project_dir,
args=[current_doc_path, new_doc_path]
)
failure_message = _(
'Your project {name} is marked as abandoned. Update link '
'for the project docs is <a href="{url}">{url}</a>.'
)
# user must have to notified via email.
mock_send_email.assert_has_calls([
mock.call(
template='core/email/common.txt',
context={'content': failure_message.format(name=project.name, url=new_url)},
subject=u'Abandoned project {}'.format(project.name),
template_html='core/email/common.html',
recipient=user.email,
request=None
)
])
# Persistent error msg is added to notify user.
self.assertEqual(PersistentMessage.objects.filter(read=False).count(), 1)
12 changes: 12 additions & 0 deletions readthedocs/rtd_tests/tests/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ def public_task_exception():
},
)

def test_rename_proj_dir(self):
directory = mkdtemp()
self.assertTrue(exists(directory))
new_dir = os.path.join(
os.path.dirname(directory),
'NewDirNameHere',
)
self.assertFalse(exists(new_dir))
tasks.rename_project_dir.delay(directory, new_dir)
self.assertFalse(exists(directory))
self.assertTrue(exists(new_dir))

@patch('readthedocs.builds.managers.log')
def test_sync_files_logging_when_wrong_version_pk(self, mock_logger):
self.assertFalse(Version.objects.filter(pk=345343).exists())
Expand Down
6 changes: 6 additions & 0 deletions readthedocs/templates/core/project_detail_right.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
{% load gravatar %}
{% load projects_tags %}

{% block abandoned %}
{% if project.is_abandoned %}
<p class="notification">This project is abandoned.</p>
{% endif %}
{% endblock %}

{% block repo %}
{% if project.repo %}
<h3>{% trans "Repository" %}</h3>
Expand Down
19 changes: 19 additions & 0 deletions readthedocs/templates/projects/email/abandon_project.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends "core/email/common.html" %}

{% block top-right %}
{{ proj_name }}
{% endblock %}

{% block content %}
<p>
We've had a request from one of our users for the project name {{ proj_name }} on Read the Docs. You are the current owner, and we wanted to reach out to you in accordance with our Abandoned Project policy (<a href="http://docs.readthedocs.io/en/latest/abandoned-projects.html">http://docs.readthedocs.io/en/latest/abandoned-projects.html</a>).
</p>

<p>
Since this is a fork of the existing owners project, {{ proj_name }}, you must show us the additional work that has been done in order to keep the new project name, that is different than the existing version of the project.
</p>

<p>
Please reply to this email either allowing or disallowing the transfer of the name on Read the Docs within 6 weeks, otherwise we will take the action of *initiating the transfer to a new owner* by default.
</p>
{% endblock %}
6 changes: 6 additions & 0 deletions readthedocs/templates/projects/email/abandon_project.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "core/email/common.txt" %}
{% block content %}
We've had a request from one of our users for the project name {name} on Read the Docs. You are the current owner, and we wanted to reach out to you in accordance with our Abandoned Project policy (http://docs.readthedocs.io/en/latest/abandoned-projects.html).
Since this is a fork of the existing owners project, {name}, you must show us the additional work that has been done in order to keep the new project name, that is different than the existing version of the project.
Please reply to this email either allowing or disallowing the transfer of the name on Read the Docs within 6 weeks, otherwise we will take the action of *initiating the transfer to a new owner* by default.
{% endblock %}