Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

python - passing the dates between two dates back into the formatday method

I'm learning Python and Django and am building a job management app, I have the following, which is all working ok, and displays the jobs on a HTML calendar. It displays the start and end dates okay, however if the job is more than two days there is a gap in the calendar as there is no date related to the job to show.

screen shot of calendar view

I can get the dates in between by doing the following;

start = job.start_time
end = job.end_time
between = end - start
for i in range(between.days + 1):
    datesbetween = [start + timedelta(days=i)]

Below is my views.py file:

class AdminCalendarView(LoginRequiredMixin,PermissionRequiredMixin, ListView):
    model = Job
    template_name = 'jobs/admin_diary.html'
    permission_required = 'jobs.add_job', 'raise_exception=True'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        # use today's date for the calendar
        d = get_date(self.request.GET.get('month', None))


        # Instantiate our calendar class with today's year and date
        cal = AdminCalendar(d.year, d.month)

        jobs = Job.objects.filter(start_time__year=d.year, start_time__month=d.month)

        # Call the formatmonth method, which returns our calendar as a table
        html_cal = cal.formatmonth(jobs, withyear=True)
        context['calendar'] = mark_safe(html_cal)
        context['prev_month'] = prev_month(d)
        context['next_month'] = next_month(d)
        return context

The utils.py file:

from datetime import datetime, timedelta, date
from calendar import HTMLCalendar
from .models import Job
from django.db.models import Q

class AdminCalendar(HTMLCalendar):

    def __init__(self, year=None, month=None):
        self.year = year
        self.month = month
        super(AdminCalendar, self).__init__()

    # formats a day as a td
    # filter jobs by day

    def formatday(self, day, jobs):
        jobs_per_day = jobs.filter(Q(start_time__day=day) | Q(end_time__day=day))
        
        d = ''
        for job in jobs_per_day:
            d += f"<a href='/job/{job.id}'> <li class='job-diary'> {job.client} Job: {job.id}</li> </a>"

        if day != 0:
            return f"<td><span class='date'>{day}</span><ul> {d} </ul></td>"
        return '<td></td>'

        
    # formats a week as a tr
    def formatweek(self, theweek, jobs):
        week = ''
        i = [a_tuple[0] for a_tuple in theweek]
        for d in i:
            week += self.formatday(d, jobs)
        return f'<tr> {week} </tr>'


    # formats a month as a table
    # filter jobs by year and month
    def formatmonth(self, jobs, withyear=True):
        cal = f'<table border="0" cellpadding="0" cellspacing="0" class="calendar table-responsive">
'
        cal += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}
'
        cal += f'{self.formatweekheader()}
'
        for week in self.monthdays2calendar(self.year, self.month):
            cal += f'{self.formatweek(week, jobs)}
'
        cal += f'</table>'
        return cal

However, I'm not sure how I need to approach passing these days back into the formatday() method, or if I should be looking at a different way of doing it, as it all new to me, any help would be much appreciated.

Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your formatday function you can filter for jobs that are between start and end date (including those dates) by doing something like:

def formatday(self, day, jobs):
    jobs_per_day = jobs.filter(start_time__day__gte=day, end_time__day__lte=day)

This will give you jobs that span any days from start to end including start and end. Then your current code will work to display a job on any day it spans. I believe this is what you were asking for, but feel free to clarify if not.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...