Skip to content

Commit

Permalink
Merge branch 'feature/copy-timesheets'
Browse files Browse the repository at this point in the history
  • Loading branch information
yomguy committed Mar 24, 2022
2 parents 9489b92 + 485dc5c commit 96c3a17
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
38 changes: 36 additions & 2 deletions organization/network/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,\
Expand Down Expand Up @@ -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,\
Expand Down Expand Up @@ -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"]
Expand All @@ -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:
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions organization/network/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand 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()
Expand Down

0 comments on commit 96c3a17

Please sign in to comment.