-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5182 from akvo/master-rebase
Merge production into master
- Loading branch information
Showing
104 changed files
with
2,682 additions
and
452 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Akvo RSR is covered by the GNU Affero General Public License. | ||
# See more details in the license.txt file located at the root folder of the Akvo RSR module. | ||
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >. | ||
|
||
|
||
from akvo.rest.serializers.rsr_serializer import BaseRSRSerializer | ||
from akvo.rsr.models import IndicatorPeriodAggregationJob | ||
|
||
|
||
class IndicatorPeriodAggregationJobSerializer(BaseRSRSerializer): | ||
class Meta: | ||
model = IndicatorPeriodAggregationJob | ||
fields = "__all__" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Akvo RSR is covered by the GNU Affero General Public License. | ||
# See more details in the license.txt file located at the root folder of the Akvo RSR module. | ||
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >. | ||
from rest_framework import filters | ||
from rest_framework.decorators import action | ||
from rest_framework.exceptions import ValidationError | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.response import Response | ||
|
||
from akvo.rsr.models import IndicatorPeriodAggregationJob | ||
from ..filters import RSRGenericFilterBackend | ||
|
||
from ..serializers import IndicatorPeriodAggregationJobSerializer | ||
from ..viewsets import ReadOnlyPublicProjectViewSet, SafeMethodsPermissions | ||
from ...rsr.usecases.jobs.aggregation import schedule_aggregation_job | ||
|
||
|
||
class IndicatorPeriodAggregationJobViewSet(ReadOnlyPublicProjectViewSet): | ||
queryset = IndicatorPeriodAggregationJob.objects.all().select_related( | ||
IndicatorPeriodAggregationJob.project_relation[:-2] | ||
) | ||
serializer_class = IndicatorPeriodAggregationJobSerializer | ||
project_relation = IndicatorPeriodAggregationJob.project_relation | ||
ordering = ["updated_at"] | ||
filter_backends = (filters.OrderingFilter, RSRGenericFilterBackend,) | ||
|
||
# These are login only resources that shouldn't be interesting to the public | ||
permission_classes = (SafeMethodsPermissions, IsAuthenticated) | ||
|
||
@action(detail=True, methods=['post']) | ||
def reschedule(self, request, **kwargs): | ||
""" | ||
Puts a new job in the queue for the indicator period | ||
The old job is left in order to have a history | ||
""" | ||
job: IndicatorPeriodAggregationJob = self.get_object() | ||
|
||
if job.status != IndicatorPeriodAggregationJob.Status.MAXXED: | ||
raise ValidationError("Maximum number of attempts not reached") | ||
|
||
new_job = schedule_aggregation_job(job.period) | ||
serializer = self.get_serializer(new_job) | ||
|
||
return Response(serializer.data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
akvo/rsr/management/commands/recalculate_program_aggregation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django.core.management.base import BaseCommand | ||
from django.db.models import Count, Q | ||
|
||
from akvo.rsr.models import ProjectHierarchy, IndicatorPeriod, IndicatorPeriodData | ||
from akvo.rsr.usecases.jobs.aggregation import schedule_aggregation_job | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Script for recalculating periods aggregation of a program' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('program_id', type=int) | ||
|
||
def handle(self, *args, **options): | ||
try: | ||
hierarchy = ProjectHierarchy.objects.get(root_project=options['program_id']) | ||
program = hierarchy.root_project | ||
except ProjectHierarchy.DoesNotExist: | ||
print("Program not found") | ||
return | ||
|
||
descendants = program.descendants() | ||
periods = IndicatorPeriod.objects\ | ||
.annotate(approved_count=Count('data', filter=Q(data__status=IndicatorPeriodData.STATUS_APPROVED_CODE)))\ | ||
.filter(approved_count__gte=1, indicator__result__project__in=descendants) | ||
|
||
for period in periods: | ||
schedule_aggregation_job(period) | ||
|
||
print(f"Scheduled period aggregation jobs: {periods.count()}, on program: {program.title}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.core.management.base import BaseCommand | ||
from akvo.rsr.usecases.jobs.aggregation import execute_aggregation_jobs | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Run indicator period aggregation jobs." | ||
|
||
def handle(self, *args, **options): | ||
execute_aggregation_jobs() |
Oops, something went wrong.