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

[ENG-6945] Added draft registrations page in admin app #10962

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
1 change: 1 addition & 0 deletions admin/base/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
re_path(r'^schema_responses/', include('admin.schema_responses.urls', namespace='schema_responses')),
re_path(r'^registration_schemas/', include('admin.registration_schemas.urls', namespace='registration_schemas')),
re_path(r'^cedar_metadata_templates/', include('admin.cedar.urls', namespace='cedar_metadata_templates')),
re_path(r'^draft_registrations/', include('admin.draft_registrations.urls', namespace='draft_registrations')),
]),
),
]
Expand Down
Empty file.
12 changes: 12 additions & 0 deletions admin/draft_registrations/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.urls import re_path

from admin.draft_registrations import views


app_name = 'draft_registrations'


urlpatterns = [
re_path(r'^$', views.UserDraftRegistrationSearchView.as_view(), name='search'),
re_path(r'^(?P<draft_registration_id>\w+)/$', views.DraftRegistrationView.as_view(), name='detail'),
]
54 changes: 54 additions & 0 deletions admin/draft_registrations/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django.urls import NoReverseMatch, reverse
from django.contrib import messages
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.shortcuts import redirect
from django.views.generic import FormView
from django.views.generic import DetailView

from admin.base.forms import GuidForm
from osf.models.registrations import DraftRegistration


class DraftRegistrationMixin(PermissionRequiredMixin):

def get_object(self):
draft_registration = DraftRegistration.load(self.kwargs['draft_registration_id'])
draft_registration.guid = draft_registration._id
return draft_registration

def get_success_url(self):
return reverse('draft_registrations:detail', kwargs={
'draft_registration_id': self.kwargs['draft_registration_id']
})


class UserDraftRegistrationSearchView(PermissionRequiredMixin, FormView):
""" Allows authorized users to search for user's draft registrations by his guid.
"""
template_name = 'draft_registrations/search.html'
permission_required = 'osf.view_osfuser'
raise_exception = True
form_class = GuidForm

def form_valid(self, form):
guid = form.cleaned_data['guid']
if guid:
try:
return redirect(reverse('users:draft-registrations', kwargs={'guid': guid}))
except NoReverseMatch as e:
messages.error(self.request, str(e))

return super().form_valid(form)


class DraftRegistrationView(DraftRegistrationMixin, DetailView):
""" Allows authorized users to view draft registration
"""
template_name = 'draft_registrations/detail.html'
permission_required = 'osf.view_draftregistration'

def get_context_data(self, **kwargs):
draft_registration = self.get_object()
return super().get_context_data(**{
'draft_registration': draft_registration
}, **kwargs)
11 changes: 10 additions & 1 deletion admin/nodes/templatetags/node_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
AbstractNode,
Contributor,
Preprint,
PreprintContributor
PreprintContributor,
DraftRegistration,
DraftRegistrationContributor
)

from osf.models.spam import SpamStatus
Expand Down Expand Up @@ -57,6 +59,11 @@ def reverse_schema_response(value):
return reverse('schema_responses:detail', kwargs={'schema_response_id': value.id})


@register.filter
def reverse_draft_registration(value):
return reverse('draft_registrations:detail', kwargs={'draft_registration_id': value._id})


@register.filter
def order_by(queryset, args):
args = [x.strip() for x in args.split(',')]
Expand All @@ -69,6 +76,8 @@ def get_permissions(user, resource):
return Contributor.objects.get(user=user, node=resource).permission
elif isinstance(resource, Preprint):
return PreprintContributor.objects.get(user=user, preprint=resource).permission
elif isinstance(resource, DraftRegistration):
return DraftRegistrationContributor.objects.get(user=user, draft_registration=resource).permission


@register.simple_tag
Expand Down
10 changes: 10 additions & 0 deletions admin/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@
</ul>
</div>
{% endif %}
{% if perms.osf.view_draftregistration %}
<li><a role="button" data-toggle="collapse" href="#collapseDraftRegistrations">
<i class='fa fa-caret-down'></i> Draft Registrations
</a></li>
<div class="collapse" id="collapseDraftRegistrations">
<ul class="sidebar-menu sidebar-menu-inner">
<li><a href="{% url 'draft_registrations:search' %}"><i class='fa fa-link'></i> <span>Search Draft Registrations</span></a></li>
</ul>
</div>
{% endif %}
{% if perms.osf.view_spam %}
<li><a href="{% url 'comments:comments' %}"><i class='fa fa-link'></i> <span>Flagged Comments</span></a></li>
{% endif %}
Expand Down
29 changes: 29 additions & 0 deletions admin/templates/draft_registrations/contributors.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% load node_extras %}

<tr>
<td>Contributors</td>
<td>
<table class="table table-bordered table-hover">
<thead>
<tr>
<td>Email</td>
<td>Name</td>
<td>Permissions</td>
</tr>
</thead>
<tbody>
{% for user in draft_registration.contributors %}
<tr>
<td>
<a href="{{ user | reverse_user }}">
{{ user }}
</a>
</td>
<td>{{ user.fullname }}</td>
<td>{% get_permissions user draft_registration %}</td>
</tr>
{% endfor %}
</tbody>
</table>
</td>
</tr>
67 changes: 67 additions & 0 deletions admin/templates/draft_registrations/detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{% extends 'base.html' %}
{% load static %}
{% load node_extras %}
{% block title %}
<title>{{ draft_registration.type|cut:'osf.'|title }}: {{draft_registration.guid}} </title>
{% endblock title %}
{% block content %}
<div class="container-fluid">
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
<div class="row">
<h2>Draft Registration: <b>{{ draft_registration.title }}</b> <a href="{{ draft_registration.absolute_url }}"> ({{draft_registration.guid}})</a> </h2>
<table class="table table-striped">
<thead>
<tr>
<th>Field</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>ID</td>
<td>{{ draft_registration.guid }}</td>
</tr>
<tr>
<td>Url</td>
<td><a href="{{ draft_registration.absolute_url }}">{{ draft_registration.absolute_url }}</a></td>
</tr>
<tr>
<td>Title</td>
<td>{{ draft_registration.title }}</td>
</tr>
<tr>
<td>Creator</td>
<td><a href="{{ draft_registration.creator | reverse_user }}">{{ draft_registration.creator }}</a></td>
</tr>
<tr>
<td>Deleted</td>
<td>{{ draft_registration.deleted }}</td>
</tr>
<tr>
<td>Public</td>
<td>{{ draft_registration.is_public }}</td>
</tr>
<tr>
<td>Provider</td>
{% if draft_registration.provider %}
<td><a href="{{ draft_registration | reverse_registration_provider }}">{{ draft_registration.provider.name }}</a></td>
{% else %}
<td>None</td>
{% endif %}
</tr>
{% include "draft_registrations/contributors.html" with draft_registration=draft_registration %}
<tr>
<td>Node storage usage</td>
<td>
<b>Current usage:</b> {{ draft_registration.storage_usage }}<br>
</td>
</tr>
</tbody>
</table>
</div>
</div>
{% endblock content %}
23 changes: 23 additions & 0 deletions admin/templates/draft_registrations/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% extends 'base.html' %}
{% load static %}
{#{% load node_extras %}#}
{% block title %}
<title>Draft Registrations Search</title>
{% endblock title %}
{% block content %}
<div class="container-fluid">
<div class="row">
<form action="{% url 'draft_registrations:search' %}" method="post">
{% csrf_token %}
{% if form.errors %}
<div class="alert alert-danger">{{ form.errors }}</div>
{% endif %}
<div class="form-group">
<label for="guid">User GUID:</label>
{{ form.guid }}
</div>
<input type="submit" class="btn btn-primary" value="Submit" />
</form>
</div>
</div>
{% endblock content %}
42 changes: 42 additions & 0 deletions admin/templates/users/draft-registrations.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% extends 'base.html' %}
{% load node_extras %}
{% include "util/pagination.html" with items=page status=status %}
{% block content %}
<table class="table table-striped table-hover table-responsive">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Date created</th>
<th>Public</th>
<th>Contributors</th>
</tr>
</thead>
<tbody>
{% for draft_registration in draft_registrations %}
<tr>
<td>
<a href="{{ draft_registration | reverse_draft_registration }}"
class="btn btn-primary">
{{ draft_registration.guid }}
</a>
</td>
<td>
{{draft_registration.title}}
</td>
<td>
{{ draft_registration.created| date }}
</td>
<td>
{{ draft_registration.is_public }}
</td>
<td>
{% for user in draft_registration.contributors %}
<a href="{{ user | reverse_user }}">{{ user.username }}</a>{% if not forloop.last %}, {% endif %}
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
1 change: 1 addition & 0 deletions admin/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
re_path(r'^(?P<guid>[a-z0-9]+)/reindex_elastic_user/$', views.UserReindexElastic.as_view(),
name='reindex-elastic-user'),
re_path(r'^(?P<guid>[a-z0-9]+)/merge_accounts/$', views.UserMergeAccounts.as_view(), name='merge-accounts'),
re_path(r'^(?P<guid>[a-z0-9]+)/draft_registrations/$', views.UserDraftRegistrationsList.as_view(), name='draft-registrations'),
]
26 changes: 26 additions & 0 deletions admin/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,29 @@ def post(self, request, *args, **kwargs):
action_flag=REINDEX_ELASTIC
)
return redirect(self.get_success_url())


class UserDraftRegistrationsList(UserMixin, ListView):
template_name = 'users/draft-registrations.html'
permission_required = 'osf.view_draftregistration'
raise_exception = True
ordering = ('-created')
paginate_by = 25

def get_queryset(self):
# Django template does not like attributes with underscores for some reason, so we annotate.
return self.get_object().draft_registrations_active.annotate(
guid=F('_id')
).order_by(self.ordering)

def get_context_data(self, **kwargs):
draft_registrations = self.get_queryset()
page_size = self.get_paginate_by(draft_registrations)
paginator, page, query_set, is_paginated = self.paginate_queryset(draft_registrations, page_size)
return super().get_context_data(
**kwargs,
**{
'page': page,
'draft_registrations': query_set
}
)
10 changes: 9 additions & 1 deletion api/caching/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,16 @@ def get_storage_usage_total(target_obj):

def update_storage_usage(target):
Preprint = apps.get_model('osf.preprint')
DraftRegistration = apps.get_model('osf.draftregistration')

if settings.ENABLE_STORAGE_USAGE_CACHE and not isinstance(target, Preprint) and not target.is_quickfiles:
if not settings.ENABLE_STORAGE_USAGE_CACHE:
return

# draft registrations don't inherit from GuidMixin, so they don't have guid.
# for fetching files we use AbstractNode instances, this is why we use branched_from property
if isinstance(target, DraftRegistration):
enqueue_postcommit_task(update_storage_usage_cache, (target.branched_from.id, target.branched_from._id), {}, celery=True)
elif not isinstance(target, Preprint) and not target.is_quickfiles:
enqueue_postcommit_task(update_storage_usage_cache, (target.id, target._id), {}, celery=True)

def update_storage_usage_with_size(payload):
Expand Down
13 changes: 13 additions & 0 deletions osf/models/registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
ApprovalStates,
SanctionTypes
)
from api.caching.tasks import update_storage_usage
from api.caching import settings as cache_settings
from api.caching.utils import storage_usage_cache
from website import settings
from website.archiver import ARCHIVER_INITIATED
from website.identifiers.tasks import update_doi_metadata_on_change
Expand Down Expand Up @@ -1461,6 +1464,16 @@ def update(self, fields, auth=None, save=True):

return updated

@property
def storage_usage(self):
key = cache_settings.STORAGE_USAGE_KEY.format(target_id=self.branched_from._id)
storage_usage_total = storage_usage_cache.get(key)
if storage_usage_total is not None:
return storage_usage_total

update_storage_usage(self) # sets cache
return storage_usage_cache.get(key)


class DraftRegistrationUserObjectPermission(UserObjectPermissionBase):
"""
Expand Down
Loading