-1

I'm trying to create a number of lists depending on the number of weeks there are in a month so for example, we have months that have exactly four weeks as we have months that may surpass that, so my requirement is to create lists dynamically depending the month the users select so the number of list is equal to the weeks in the selected month.

Any pointer would be helpful, thank you


    def _get_columns_name(self, options):
        
        
        # the data type = list
        header1 = [
            {'name': '', 'style': 'width:90%'},
            {'name': _('Week 1'), 'class': 'number', 'colspan': 2},
            {'name': options['date']['string'], 'class': 'number', 'colspan': 1},
        ] + [
            {'name': '', 'style': 'width:90%'},
            {'name': _('Week 2'), 'class': 'number', 'colspan': 2},
            {'name': options['date']['string'], 'class': 'number', 'colspan': 1},
        ] + [
            {'name': '', 'style': 'width:90%'},
            {'name': _('Week 3'), 'class': 'number', 'colspan': 2},
            {'name': options['date']['string'], 'class': 'number', 'colspan': 1},
        ] + [
            {'name': '', 'style': 'width:90%'},
            {'name': _('Week 4'), 'class': 'number', 'colspan': 2},
            {'name': options['date']['string'], 'class': 'number', 'colspan': 1},
        ]

        return [header1]

Currently I'm doing this but itdoes not satisfy the requirement correctly.

CZoellner
  • 13,553
  • 3
  • 25
  • 38
Ron
  • 91
  • 8
  • I removed the odoo tags, because i dont see any relevant reference to odoo in your question. But to your question: Why is your solution not enough for the requirements. Please elaborate on this to help others find better solutions. – CZoellner Dec 29 '21 at 16:48
  • Thank you for removing the tag, Ok so the full requirement is creating a report the lists I'm trying to generate are columns in my report and each column is going to have information regarding that week so almost all month excluding February when is not a leap year have 4 weeks and a few more days, for this extra days I want to account for. – Ron Dec 29 '21 at 16:57
  • @Ron, You can create the same using a for loop and appending the same data into list with week number changing, is that what youre looking for ..? – badhusha muhammed Dec 29 '21 at 17:07
  • Isn't it what you are looking for? https://stackoverflow.com/a/32594088/4207793 plus python has an option to get a week number from date. – IgorZ Dec 29 '21 at 17:13
  • @badhushamuhammed exactly this is what I'm looking for creating a for loop that iterates in the month and returns the number of weeks and each of those weeks creates an individual list, but for the moment I'm struck putting together the for loop. ;-; – Ron Dec 29 '21 at 17:31

2 Answers2

2

Maybe the builtin Calendar library will be useful to you: for instance, to get the number of weeks in a certain month, you can use calendar.monthcalendar

import calendar

month_matrix = calendar.monthcalendar(year=2021, month=12)
num_weeks = len(month_matrix)

Then the loop is simple:

header1 = []
for i in range(num_weeks):
   header1.extend([
             {'name': '', 'style': 'width:90%'},
             {'name': _(f'Week {i+1}'), 'class': 'number', 'colspan': 2},
             {'name': options['date']['string'], 'class': 'number', 'colspan': 1},
])
Mason3k
  • 151
  • 1
  • 8
0

Simple way to do this using loops

    def _get_columns_name(options, weeks):
    
    
    # the data type = list
        header1 = [[
          {'name': '', 'style': 'width:90%'},
          {'name': _('Week {}'.format(i)), 'class': 'number', 'colspan': 2},
          {'name': options['date']['string'], 'class': 'number', 'colspan': 1}] for i in range(1, week+1)]

       return [header1]