Django desired shift batch input screen

It would be better if the staff could enter the shifts for one month at once. I made it because there was a request.

schedule.views



def kiboubulkcreatefunc(request,year_num,month_num,kojin_num):
    if request.method == 'POST':
        form = KiboubulkcreateForm(data=request.POST)
        current_user = request.user
        year, month, kojin = int(year_num), int(month_num), int(kojin_num)
        enddate = datetime.date(year,month,20)
        startdate = enddate + relativedelta(months=-1)+ timedelta(days=1)
        #Delete existing information
        KibouShift.objects.filter(date__range=(startdate, enddate), user = current_user).delete()
        display = request.POST.getlist('shift_name_1') 
        new_object_list =[]
        for shift in display:
            shift = int(shift)
            shift = Shift.objects.get(id = shift)
            new_object = KibouShift(
                date = startdate,
                user = current_user,
                shift_name_1 = shift,
                )
            new_object_list.append(new_object)
            startdate = startdate + timedelta(days=1)
        KibouShift.objects.bulk_create(new_object_list)
        return HttpResponseRedirect('/schedule/kiboulist')
    else:
        form = KiboubulkcreateForm(request.POST or None)
        year, month, kojin = int(year_num), int(month_num), int(kojin_num)
        enddate = datetime.date(year,month,20)
        startdate = enddate + relativedelta(months=-1)
        kaisu = enddate - startdate
        kaisu = int(kaisu.days)
        shutokudate = startdate + timedelta(days=1)

        #Create a list of dates and days of the week
        hiduke = str(startdate)
        date_format = "%Y-%m-%d"
        hiduke = dt.strptime(hiduke, date_format)
        sakusei_date = hiduke
        calender_object = []

        for new_shift in range(kaisu):
            sakusei_date = sakusei_date + timedelta(days=1)
            calender = {'date' : sakusei_date}
            calender_object.append(calender)      

        context = {
            'year': year,
            'month': month,
            'kojin_num': kojin,
            'form' : form,
            'kaisu' : kaisu,
            'calender_object' : calender_object,
        }
        return render(request,'schedule/kiboushift/monthkojin.html', context)

forms


class KiboubulkcreateForm(forms.Form):
    shift_name_1 = forms.ModelChoiceField(queryset=Shift.objects.all(), label='Shift 1')
    fields = ('date', 'shift_name_1') 

monthkojin.html


{% extends 'accounts/base.html' %} {% load static %} {% block customcss %}
<link rel="stylesheet" href="{% static 'schedule/kiboushift/monthkojin.css' %}"> {% endblock customcss %} {% block header %}
<div class="header">
    <div class="cole-md-1">
        <a href="{% url 'schedule:KibouList' %}" class="btn-secondary btn active">List of desired shifts</a></p>
        {% endblock header %} {% block content %}

        <form action="" method="POST" enctype="multipart/form-data">{% csrf_token %}

            <table class="table">
                <thead>
                    <tr>
                        <th>date</th>
                        <th>Shift name</th>
                    </tr>
                </thead>
                <!--date-->
                <tbody>

                    {% for item in calender_object %}
                    <tr>
                        <td>
                            {% if item.date|date:"D" == "soil" %}
                            <font color="blue">{{ item.date|date:"Y year m month d day" }}</font>
                            {% elif item.date|date:"D" == "Day" %}
                            <font color="red">{{ item.date|date:"Y year m month d day" }}</font>
                            {% else %} {{ item.date|date:"Y year m month d day" }} {% endif %} {% if item.date|date:"D" == "soil" %}
                            <font color="blue">{{ item.date | date:"l" }}</font>
                            {% elif item.date|date:"D" == "Day" %}
                            <font color="red">{{ item.date | date:"l" }}</font>
                            {% else %} {{ item.date | date:"l" }} {% endif %}
                        </td>
                        <td>
                            {{ form.shift_name_1 }}
                        </td>
                    </tr>
                    {% endfor %}

                </tbody>
            </table>
            <input type="submit" value="Create"> {% endblock content %}

monthkojin.css


.table {
    width: auto;
    border-collapse: separate;
    border-spacing: 0;
}

table th {
    text-align: center;
    color: white;
    background: linear-gradient(#829ebc, #225588);
    border-left: 1px solid #3c6690;
    border-top: 1px solid #3c6690;
    border-bottom: 1px solid #3c6690;
    box-shadow: 0px 1px 1px rgba(255, 255, 255, 0.3) inset;
    width: auto;
    padding: 10px 0;
}

table td {
    text-align: center;
    border-left: 1px solid #a8b7c5;
    border-bottom: 1px solid #a8b7c5;
    border-top: none;
    box-shadow: 0px -3px 5px 1px #eee inset;
    width: auto;
    padding: 10px 0;
}

table td:last-child {
    border-right: 1px solid #a8b7c5;
}

table tr:last-child td:first-child {
    border-radius: 0 0 0 5px;
}

table tr:last-child td:last-child {
    border-radius: 0 0 5px 0;
}

You can now enter them all at once. From now on, I thought that I needed a temporary save function, but I would like to ask if there is another request.

Click here for the screen image.png

After registering, it will be reflected in the desired shift list. Individual modifications can be made here.

image.png

Recommended Posts

Django desired shift batch input screen
Django2 screen addition flow
Django Python shift table
Django shift creation feature
Create a Django login screen
django data input dumpdata loaddata
Django input check and calendar input
Django admin screen list_filter customization