Skip to content

Commit

Permalink
Add progress_count and progress_total to Project model, compute on POST
Browse files Browse the repository at this point in the history
when using the API and add progress to the project overview (#488)
  • Loading branch information
jochenklar committed Sep 26, 2023
1 parent 024ea30 commit 6572b0c
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ module.exports = {
'react': {
'version': 'detect'
}
}
},
}
7 changes: 7 additions & 0 deletions rdmo/core/static/core/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ angular.module('core', ['ngResource'])
method: 'PUT',
params: {}
};
$resourceProvider.defaults.actions.postAction = {
method: 'POST',
params: {
id: '@id',
detail_action: '@detail_action'
}
};
}])

.filter('capitalize', function() {
Expand Down
23 changes: 23 additions & 0 deletions rdmo/projects/migrations/0059_project_progress.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.2.19 on 2023-08-24 09:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('projects', '0058_meta'),
]

operations = [
migrations.AddField(
model_name='project',
name='progress_count',
field=models.IntegerField(help_text='The number of values for the progress bar.', null=True, verbose_name='Progress count'),
),
migrations.AddField(
model_name='project',
name='progress_total',
field=models.IntegerField(help_text='The total number of expected values for the progress bar.', null=True, verbose_name='Progress total'),
),
]
26 changes: 10 additions & 16 deletions rdmo/projects/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from rdmo.views.models import View

from ..managers import ProjectManager
from ..progress import get_progress


class Project(MPTTModel, Model):
Expand Down Expand Up @@ -64,6 +63,16 @@ class Project(MPTTModel, Model):
verbose_name=_('Views'),
help_text=_('The views that will be used for this project.')
)
progress_total = models.IntegerField(
null=True,
verbose_name=_('Progress total'),
help_text=_('The total number of expected values for the progress bar.')
)
progress_count = models.IntegerField(
null=True,
verbose_name=_('Progress count'),
help_text=_('The number of values for the progress bar.')
)

class Meta:
ordering = ('tree_id', 'level', 'title')
Expand All @@ -85,21 +94,6 @@ def clean(self):
'parent': [_('A project may not be moved to be a child of itself or one of its descendants.')]
})

@property
def progress(self):
values, total = get_progress(self)

try:
ratio = values / total
except ZeroDivisionError:
ratio = 0

return {
'total': total,
'values': values,
'ratio': ratio
}

@property
def catalog_uri(self):
if self.catalog is not None:
Expand Down
2 changes: 1 addition & 1 deletion rdmo/projects/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rdmo.questions.models import Catalog, Section, Page, QuestionSet, Question


def get_progress(project, snapshot=None):
def compute_progress(project, snapshot=None):
# get all values for this project and snapshot
project_values = project.values.filter(snapshot=snapshot).select_related('attribute', 'option')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -932,13 +932,11 @@ angular.module('project_questions')
}
} else {
// update progress
resources.projects.get({
resources.projects.postAction({
id: service.project.id,
detail_action: 'progress'
}, function(response) {
if (service.progress.values != response.values) {
service.progress = response
}
service.progress = response
});

// check if we need to refresh the site
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<div class="progress project-progress">
<div class="progress-bar" role="progressbar" style="width: {$ service.progress.ratio * 100 $}%;">
<span ng-show="service.progress.ratio > 0.25">
{% blocktrans trimmed with values='{$ service.progress.values $}' total='{$ service.progress.total $}' %}
{{ values }} of {{ total }}
{% blocktrans trimmed with count='{$ service.progress.count $}' total='{$ service.progress.total $}' %}
{{ count }} of {{ total }}
{% endblocktrans %}
</span>
</div>
Expand Down
4 changes: 4 additions & 0 deletions rdmo/projects/templates/projects/projects.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ <h1>{% trans 'My Projects' %}</h1>
<thead>
<tr>
<th style="width: 60%;">{% trans 'Name' %}</th>
<th style="width: 20%;">{% trans 'Progress' %}</th>
<th style="width: 10%;">{% trans 'Role' %}</th>
<th style="width: 23%;">{% trans 'Last changed' %}</th>
<th style="width: 7%;"></th>
Expand All @@ -138,6 +139,9 @@ <h1>{% trans 'My Projects' %}</h1>
<strong>{{ project.title }}</strong>
</a>
</td>
<td>
{% project_progress project %}
</td>
<td>
{{ project.role|projects_role }}
</td>
Expand Down
6 changes: 5 additions & 1 deletion rdmo/projects/templates/projects/site_projects.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ <h1>{% blocktrans trimmed with site=request.site %}All projects on {{ site }}{%
<table class="table projects-table">
<thead>
<tr>
<th style="width: 50%;">{% trans 'Name' %}</th>
<th style="width: 40%;">{% trans 'Name' %}</th>
<th style="width: 10%;">{% trans 'Progress' %}</th>
<th style="width: 20%;">{% trans 'Created' %}</th>
<th style="width: 20%;">{% trans 'Last changed' %}</th>
<th style="width: 10%;"></th>
Expand All @@ -76,6 +77,9 @@ <h1>{% blocktrans trimmed with site=request.site %}All projects on {{ site }}{%
<strong>{{ project.title }}</strong>
</a>
</td>
<td>
{% project_progress project %}
</td>
<td>
{{ project.created }}
</td>
Expand Down
13 changes: 13 additions & 0 deletions rdmo/projects/templatetags/projects_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ def projects_indent(level):
return mark_safe('<span class="projects-indent">' + string + '</span>')


@register.simple_tag()
def project_progress(project):
if project.progress_count is None or project.progress_total is None:
return ''

try:
ratio = project.progress_count / project.progress_total
except ZeroDivisionError:
ratio = 0

return f'{ratio:.0%}'


@register.filter()
@stringfilter
def projects_role(role):
Expand Down
22 changes: 19 additions & 3 deletions rdmo/projects/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from .filters import SnapshotFilterBackend, ValueFilterBackend
from .models import Continuation, Integration, Invite, Issue, Membership, Project, Snapshot, Value
from .permissions import HasProjectPagePermission, HasProjectPermission, HasProjectsPermission
from .progress import compute_progress
from .serializers.v1 import (
IntegrationSerializer,
InviteSerializer,
Expand Down Expand Up @@ -158,11 +159,26 @@ def options(self, request, pk=None):
# if it didn't work return 404
raise NotFound()

@action(detail=True, permission_classes=(IsAuthenticated, ))
@action(detail=True, methods=['get', 'post'], permission_classes=(HasModelPermission | HasProjectPermission, ))
def progress(self, request, pk=None):
project = self.get_object()
project.catalog.prefetch_elements()
return Response(project.progress)

if request.method == 'POST' or project.progress_count is None or project.progress_total is None:
# compute the progress and store
project.catalog.prefetch_elements()
project.progress_count, project.progress_total = compute_progress(project)
project.save()

try:
ratio = project.progress_count / project.progress_total
except ZeroDivisionError:
ratio = 0

return Response({
'count': project.progress_count,
'total': project.progress_total,
'ratio': ratio
})

def perform_create(self, serializer):
project = serializer.save(site=get_current_site(self.request))
Expand Down

0 comments on commit 6572b0c

Please sign in to comment.