[PYTHON] Django shift symbol background with different colors depending on facility

The shift table holds data such as A, B, C, and holidays. Even though I say A, since there are three facilities, I am currently creating it in Excel so that I can understand which base I work at, so It corresponds by changing the color of the cell.

image.png

In Excel, it is created like this. The cafe managed two bases and created a separate shift table. (I haven't seen it yet, so there may be some changes ...)

When I realize it with Django, I have a color field in the shift table, so I will manage it and display it. During the implementation, I noticed that when I chose paid leave or holidays, I could choose the facility, so even during the holidays, it would be colored. It seems that input control is easy to implement if you do it in forms.py, but I have never done it, so I hope to implement it in the future.

I also decided to pass the Shisetsu table to html for coloring.

schedule/views.py



from django.shortcuts import render
from shisetsu.models import *
from accounts.models import *
from .models import *
from django.shortcuts import redirect
import calendar
import datetime
from datetime import timedelta
from datetime import datetime as dt

# Create your views here.

def homeschedule(request):
    from datetime import datetime
    now = datetime.now()
    return redirect('schedule/monthschedule/%s/%s/' % (now.year,now.month,))  #Automatically redirect to this month's shift screen

def monthschedulefunc(request,year_num,month_num):
    user_list = User.objects.all()
    year, month = int(year_num), int(month_num)
    shisetsu_object = Shisetsu.objects.all()
    print(shisetsu_object)
    object_list = Schedule.objects.filter(year = year, month = month).order_by('user', 'date')
    #Get the number of days in the shift range
    startdate = datetime.date(year,month-1,21)
    enddate = datetime.date(year,month,20)
    kaisu = enddate - startdate
    kaisu = int(kaisu.days) + 1
    
    #Create a list of dates and days of the week
    hiduke = str(year) + "-" + str(month-1) + "-" + str(20)
    date_format = "%Y-%m-%d"
    hiduke = dt.strptime(hiduke, date_format)
    weekdays = ["Month","fire","water","wood","Money","soil","Day"]
    calender_object = []
    youbi_object = []
    for i in range(kaisu):
        hiduke = hiduke + timedelta(days=1)    
        calender_object.append(hiduke)
        youbi = weekdays[hiduke.weekday()] 
        youbi_object.append(youbi)
    
    kaisu = str(kaisu)

    context = {
        'object_list': Schedule.objects.all(),
        'staff_list' : User.objects.all(),
        'shisetsu_object': shisetsu_object.all(),
        'kaisu' : kaisu
    }
    return render(request,'schedule/month.html', {
    'object_list': object_list,
    'user_list': user_list,
    'calender_object': calender_object,
    'youbi_object':youbi_object,
    'kaisu':kaisu,
    'shisetsu_object':shisetsu_object
    }, )

schedule/month.html


{% extends 'schedule/base.html' %}
{% block header %}
{% endblock header %}

{% block content %}
<table class="table table-striped table-bordered">
<thead>
    <tr align="center" class="info">    <!--date-->
        <th rowspan="2"></th>
        {% for item in calender_object %}
                <th class="day_{{ item.date }}">{{ item.date | date:"d" }}</th>
        {% endfor %}
    <tr align="center" class="info">   <!--Day of the week-->
        {% for item in youbi_object %}
            <th class="day_{{ item.date }}">{{ item }}</th>
        {% endfor %}
    </tr>
</thead>
<tbody>
    {% for staff in user_list %}
        <tr align="center">
        <th class="staff_name" staff_id="{{ staff.staff_id }}" width="200" >{{ staff.last_name }} {{ staff.first_name }}</th>  <!--staff_id element used in js-->
        {% for item in object_list %} 
            {% if item.user|stringformat:"s" == staff.username|stringformat:"s" %}<!--If the username is the same-->
                <td class="day_{{ item.date }}" id="s{{ staff.id }}d{{ item.date }}"> 
                    {% if item.shift_name_1 != None %}
                        {% if item.shift_name_1|stringformat:"s" == "Yes" or item.shift_name_1|stringformat:"s" == "Closed" %}
                            {{ item.shift_name_1 }}
                        {% else %}
                            {% for shisetsu in shisetsu_object %}
                                {% if item.shisetsu_name_1|stringformat:"s" == shisetsu.name|stringformat:"s" %}                          
                                        <span style="background-color:{{ shisetsu.color }}">{{ item.shift_name_1 }}</span>
                                {% endif %}
                            {% endfor %} 
                        {% endif %}    
                    {% endif %}
                    {% if item.shift_name_2 != None %}
                        {% if item.shift_name_2|stringformat:"s" == "Yes" or item.shift_name_2|stringformat:"s" == "Closed" %}
                            {{ item.shift_name_2 }}
                        {% else %}
                            {% for shisetsu in shisetsu_object %}
                                {% if item.shisetsu_name_2|stringformat:"s" == shisetsu.name|stringformat:"s" %}                          
                                        <span style="background-color:{{ shisetsu.color }}">{{ item.shift_name_2 }}</span>
                                {% endif %}
                            {% endfor %} 
                        {% endif %}    
                    {% endif %}
                    {% if item.shift_name_3 != None %}
                        {% if item.shift_name_3|stringformat:"s" == "Yes" or item.shift_name_3|stringformat:"s" == "Closed" %}
                            {{ item.shift_name_3 }}
                        {% else %}
                            {% for shisetsu in shisetsu_object %}
                                {% if item.shisetsu_name_3|stringformat:"s" == shisetsu.name|stringformat:"s" %}                          
                                        <span style="background-color:{{ shisetsu.color }}">{{ item.shift_name_3 }}</span>
                                {% endif %}
                            {% endfor %} 
                        {% endif %}    
                    {% endif %}
                    {% if item.shift_name_4 != None %}
                        {% if item.shift_name_4|stringformat:"s" == "Yes" or item.shift_name_4|stringformat:"s" == "Closed" %}
                            {{ item.shift_name_4 }}
                        {% else %}
                            {% for shisetsu in shisetsu_object %}
                                {% if item.shisetsu_name_4|stringformat:"s" == shisetsu.name|stringformat:"s" %}                          
                                        <span style="background-color:{{ shisetsu.color }}">{{ item.shift_name_4 }}</span>
                                {% endif %}
                            {% endfor %} 
                        {% endif %}    
                    {% endif %}
                </td>
            {% endif %}            
        {% endfor %}
        </tr>
    {% endfor %}
</tbody>

</table>
{% endblock content %}

I think that one iterative process can be done, but since 1 worked well, I use it in copy and paste. After this, it may be refurbished as the total number of working hours per month is calculated.

And here is the display result.

image.png

No, you're done! The background color is a little small, but it's good (⌒∇⌒)

I arrived in about 3 hours, so I think I was able to do it quite quickly.

Next, I would like to work on calculating the total time of the month. First of all, I would like to confirm that the working hours of the day may be changed or added.

Recommended Posts

Django shift symbol background with different colors depending on facility
Django + Apache with mod_wsgi on Windows Server 2016
A note on enabling PostgreSQL with Django