]> git.parisson.com Git - mezzo.git/commitdiff
[Timesheet] : import ircam time sheet : add logging
authorEmilie Zawadzki <zawadzki@ircam.fr>
Thu, 5 Jan 2017 12:11:59 +0000 (13:11 +0100)
committerEmilie Zawadzki <zawadzki@ircam.fr>
Thu, 5 Jan 2017 12:11:59 +0000 (13:11 +0100)
app/organization/network/management/commands/import-ircam-timesheet-xls.py

index 4a33c4dfd95bd6b1aae7bafc6cb1ce884d25ed61..11ea39824778193fc92eb208a2ad0592bd3d257a 100644 (file)
@@ -29,7 +29,7 @@ import xlrd
 from itertools import takewhile
 from re import findall
 import dateutil.parser
-
+# from string import split
 from django.conf import settings
 from django.core.management.base import BaseCommand, CommandError
 from django.contrib.auth.models import User
@@ -103,16 +103,13 @@ class IrcamTimeSheet(object):
         """ Set for a year percentage worked by month
         on a project
         """
-        pats = PersonActivityTimeSheet.objects.create(activity = activity,
+        pats = PersonActivityTimeSheet.objects.get_or_create(activity = activity,
                                                     project = project,
                                                     percentage = percentage,
                                                     month = month,
                                                     year = year
                                                     )
 
-        pats.save()
-
-
 
     def set_work_package(self, person_activity_timesheet):
         """ set contract id of the project """
@@ -150,7 +147,7 @@ class Command(BaseCommand):
         for sheet in xls.sheets:
             person_register_id = sheet.cell_value(xls.register_id_row, xls.register_id_col)
             persons = Person.objects.filter(register_id=int(person_register_id))
-
+            processing_counter = 0
             # database not enough clear, possible multiple entries for some persons
             # iterating over one person
             for person in persons:
@@ -160,7 +157,7 @@ class Command(BaseCommand):
                 date_to = dateutil.parser.parse(periods[1])
                 curr_year = date_to.year
 
-                self.logger.info('Person', 'processing person : '+str(person.id)+ ' | '+person.title + ' | year ' +str(curr_year))
+                self.logger.info('Processing', '******************* PERSON : ' + str(person.id) + ' | '+person.title + " *******************" )
                 its = IrcamTimeSheet(person, date_from, date_to)
 
                 # iterating on each month
@@ -171,40 +168,86 @@ class Command(BaseCommand):
 
                     # get month
                     month = int(sheet.cell_value(xls.first_month_row, col_index))
-
+                    self.logger.info('Processing', 'year : ' + str(curr_year) + " | month : " + str(month))
                     # calculate the current date
                     curr_date = datetime.date(curr_year, month, 1)
 
-                    # find the right activity corresponding to the current month / year
-                    activity = person.activities.filter(Q(date_from__lte=curr_date) & Q(date_to__gte=curr_date)).first()
+                    # find the right activities corresponding to the current month / year
+                    activities = person.activities.filter(Q(date_from__lte=curr_date) & Q(date_to__gte=curr_date))
+
+                    # for each activities
+                    for activity in activities :
+                        # iterating over projects cells
+                        self.logger.info('Processing', 'activity : ' + str(activity.id) + ' | ' + activity.__str__())
+                        project_row_index = xls.first_project_row
+                        while sheet.cell_value(project_row_index, xls.first_project_col) != "Total final":
+
+                            # get percent
+                            percent = sheet.cell_value(project_row_index, col_index) if sheet.cell_value(project_row_index, col_index) else 0
+
+                            # try to find project
+                            project_id_str = sheet.cell_value(project_row_index, xls.first_project_col - 1)
+                            if isinstance(project_id_str, float) :
+                                # by default, numbers are retrived as float
+                                project_id_str = str(int(project_id_str))
+
+                            # processing projects
+                            if end_project_list_row == 0:
+                                # check if project exists
+                                project = Project.objects.filter(external_id__icontains=project_id_str).first()
+                                if project :
+                                    # save timesheet without work packages
+                                    its.set_person_activity_timesheet(activity, project, percent, month, curr_year)
+                                    processing_counter += 1
+                                else :
+                                    self.logger.info('Not Found', 'project : ' + project_external_id)
+
+                            # increment index
+                            project_row_index += 1
+
+                        # processing work package
+                        work_package_row_index = project_row_index + 1
+                        while sheet.cell_value(work_package_row_index, xls.first_project_col) != "Date entrée":
+
+                            # get project
+                            project_external_id = int(sheet.cell_value(work_package_row_index, xls.first_project_col - 1))
+                            project = Project.objects.get(external_id__icontains=str(project_external_id))
 
-                    # iterating over projects cells
-                    project_row_index = xls.first_project_row
-                    while sheet.cell_value(project_row_index, xls.first_project_col) != "Total final":
+                            # check if project exists
+                            if project:
+                                self.logger.info('Processing', 'project : ' + str(project.id) + " | " + project.__str__())
 
-                        # get percent
-                        percent = sheet.cell_value(project_row_index, col_index) if sheet.cell_value(project_row_index, col_index) else 0
+                                # list all work package
+                                wk_p_str = sheet.cell_value(work_package_row_index, col_index)
+                                wk_p_list = wk_p_str.split(",")
 
-                        # try to find project
-                        project_id_str = sheet.cell_value(project_row_index, xls.first_project_col - 1)
+                                # link work packages to timesheet
+                                for wk_p_num in wk_p_list:
+                                    wk_p_num = str(wk_p_num)
 
-                        # processing projects
-                        if end_project_list_row == 0:
-                            # check if project exists
-                            project = Project.objects.filter(external_id__icontains=project_id_str).first()
-                            if project :
-                                # save timesheet without work packages
-                                print(activity, project, percent, month, curr_year)
-                                its.set_person_activity_timesheet(activity, project, percent, month, curr_year)
-                            else :
-                                self.logger.info('Project', 'Not found ' + sheet.cell_value(project_row_index, xls.first_project_col))
-
-                        # increment index
-                        project_row_index += 1
-
-                    # processing work package
-                    work_package_row_index = project_row_index + 1
-                    while sheet.cell_value(project_row_index, xls.first_project_col) != "Date entrée":
-
-                        # increment index
-                        work_package_row_index += 1
+                                    # create or get ProjectWorkPackage
+                                    wk_obj, wk_created = ProjectWorkPackage.objects.get_or_create(title="wk_"+wk_p_num, number=wk_p_num, project=project)
+                                    pat = PersonActivityTimeSheet.objects.filter(activity=activity, project=project, month=month, year=curr_year)
+
+                                    # for each PersonActivityTimeSheet link work package
+                                    for timesheet in pat:
+                                        timesheet.work_packages.add(wk_obj)
+                                        timesheet.save()
+
+
+                            # increment index
+                            work_package_row_index += 1
+
+                        # processing accounting and validation date
+                        dates_row_index = work_package_row_index
+                        date_accounting = xlrd.xldate.xldate_as_datetime(sheet.cell_value(dates_row_index, col_index), 1)
+                        date_validation = xlrd.xldate.xldate_as_datetime(sheet.cell_value(dates_row_index + 1, col_index), 1)
+
+                        # get all timesheets, function of the activity, month and year
+                        pats = PersonActivityTimeSheet.objects.filter(activity=activity, month=month, year=curr_year)
+                        for pat in pats :
+                            pat.accounting = date_accounting
+                            pat.validation = date_validation
+                            pat.save()
+
+                self.logger.info('Processing', '_________________________ Number of record : ' + str(processing_counter) + ' _________________________')