diff --git a/organization/network/admin.py b/organization/network/admin.py index 1f1b3878..d7689a80 100644 --- a/organization/network/admin.py +++ b/organization/network/admin.py @@ -22,7 +22,10 @@ import csv +from django import forms from django.contrib import admin +from django.contrib.admin.helpers import ActionForm +from django.utils.translation import ugettext_lazy as _ from django.http import HttpResponse from guardian.admin import GuardedModelAdmin from mezzanine.core.admin import StackedDynamicInlineAdmin,\ @@ -50,7 +53,7 @@ ActivityFunction, ActivityGrade, ActivityFramework, ActivityStatus, TrainingType,\ TrainingLevel, TrainingSpeciality, TrainingTopic, BudgetCode, RecordPiece,\ PersonActivityTimeSheet, OrganizationLinked, OrganizationType, DepartmentPage,\ - TeamPage, PersonListBlock + TeamPage, PersonListBlock, MONTH_CHOICES from organization.network.utils import TimesheetXLS, set_timesheets_validation_date,\ flatten_activities from organization.pages.admin import PageImageInline, PageBlockInline,\ @@ -480,6 +483,12 @@ class RecordPieceAdmin(BaseTranslationModelAdmin): ordering = ['name', ] +class CopyActivityTimeSheetActionForm(ActionForm): + + first_month = forms.IntegerField(required=False) + last_month = forms.IntegerField(required=False) + + class PersonActivityTimeSheetAdmin(BaseTranslationOrderedModelAdmin): model = PersonActivityTimeSheet search_fields = ['year', 'month', 'activity__person__last_name', "project__title"] @@ -495,9 +504,10 @@ class PersonActivityTimeSheetAdmin(BaseTranslationOrderedModelAdmin): 'validation' ] list_filter = ['activity__person', 'year', 'month', 'project'] - actions = ['export_xls', 'validate_timesheets'] + actions = ['export_xls', 'validate_timesheets', 'copy_activity_timesheets'] first_fields = ['title', ] form = PersonActivityTimeSheetAdminForm + action_form = CopyActivityTimeSheetActionForm def person(self, instance): if instance.activity: @@ -521,6 +531,30 @@ def validate_timesheets(self, request, queryset): export_xls.short_description = "Export person timesheets" + def copy_activity_timesheets(self, request, queryset): + first_month = int(request.POST['first_month']) + last_month = int(request.POST['last_month']) + timesheet = queryset[0] + + for month in range(first_month, last_month+1): + work_packages = timesheet.work_packages.all() + timesheet.id = None + timesheet.month = month + last_other_timesheets = PersonActivityTimeSheet.objects.filter( + activity=timesheet.activity, + year=timesheet.year, + month=timesheet.month).order_by('-validation') + + if last_other_timesheets: + timesheet.accounting = last_other_timesheets[0].accounting + timesheet.validation = last_other_timesheets[0].validation + + timesheet.save() + for work_package in work_packages: + timesheet.work_packages.add(work_package) + + copy_activity_timesheets.short_description = 'Copy activity timesheets to new months' + admin.site.register(OrganizationLinked, OrganizationLinkedAdmin) admin.site.register(Organization, OrganizationAdmin) diff --git a/organization/network/views.py b/organization/network/views.py index 8b4783d5..82b1b0f4 100644 --- a/organization/network/views.py +++ b/organization/network/views.py @@ -739,7 +739,7 @@ def get_result_label(self, item): item.__str__() def get_queryset(self): - if not self.request.user.is_authenticated(): + if not self.request.user.is_authenticated: return PersonActivity.objects.none() qs = PersonActivity.objects.all() @@ -761,7 +761,7 @@ def get_result_label(self, item): return item.project.title + ' - ' + item.title def get_queryset(self): - if not self.request.user.is_authenticated(): + if not self.request.user.is_authenticated: return ProjectWorkPackage.objects.none() qs = ProjectWorkPackage.objects.all()