I added one user to create a shift table creation function, but suddenly I decided to implement sorting by display order.
However, I got stuck here. I linked User and Profil with User, but I tried to get first_name and last_name in User, but I couldn't get them after all ...
Finally, it was supported by adding a judgment on HTML whether the Id of profil and the Id of User are the same. I'm new, but I think it's not beautiful ... I checked it for about 3 hours, tried it, and checked it again.
I did my best (⌒∇⌒)
python:schedule.views.py
from django.shortcuts import render, redirect, HttpResponseRedirect
from shisetsu.models import *
from accounts.models import *
from .models import *
import calendar
import datetime
from datetime import timedelta
from datetime import datetime as dt
from django.db.models import Sum
from django.contrib.auth.models import User
from django.views.generic import FormView, UpdateView
from django.urls import reverse_lazy
from .forms import *
from dateutil.relativedelta import relativedelta
from django.contrib.auth.models import User
# Create your views here.
def homeschedule(request):
from datetime import datetime
now = datetime.now()
return HttpResponseRedirect('/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()
profile_list = Profile.objects.select_related().values().order_by('hyoujijyun')
print(profile_list)
print(user_list)
year, month = int(year_num), int(month_num)
shisetsu_object = Shisetsu.objects.all()
shift_object = Shift.objects.all()
object_list = Schedule.objects.filter(year = year, month = month).order_by('user', 'date')
month_total = Schedule.objects.select_related('User').filter(year = year, month = month).values("user").order_by("user").annotate(month_total_worktime = Sum("day_total_worktime"))
#Get the number of days in the shift range
enddate = datetime.date(year,month,20)
startdate = enddate + relativedelta(months=-1)
kaisu = enddate - startdate
kaisu = int(kaisu.days)
kikan = str(startdate) +"~"+ str(enddate)
#Create a list of dates and days of the week
hiduke = str(startdate)
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 = {
'year': year,
'month': month,
'kikan': kikan,
'object_list': object_list,
'user_list': user_list,
'shift_object': shift_object,
'calender_object': calender_object,
'youbi_object': youbi_object,
'kaisu': kaisu,
'shisetsu_object': shisetsu_object,
'month_total' : month_total,
'profile_list' : profile_list,
}
return render(request,'schedule/month.html', context)
def scheduleUpdatefunc(request,pk):
Schedule_list = Schedule.objects.get(pk = int(pk))
User_list = User.objects.get(username = Schedule_list.user)
shift_object = Shift.objects.all()
if request.method == 'POST':
form = ScheduleUpdateForm(data=request.POST)
year = Schedule_list.year
month = Schedule_list.month
if form.is_valid():
Schedule_list.shift_name_1 = form.cleaned_data['shift_name_1']
Schedule_list.shisetsu_name_1 = form.cleaned_data['shisetsu_name_1']
Schedule_list.shift_name_2 = form.cleaned_data['shift_name_2']
Schedule_list.shisetsu_name_2 = form.cleaned_data['shisetsu_name_2']
Schedule_list.shift_name_3 = form.cleaned_data['shift_name_3']
Schedule_list.shisetsu_name_3 = form.cleaned_data['shisetsu_name_3']
Schedule_list.shift_name_4 = form.cleaned_data['shift_name_4']
Schedule_list.shisetsu_name_4 = form.cleaned_data['shisetsu_name_4']
Schedule_list.day_total_worktime = form.cleaned_data['day_total_worktime']
Schedule_list.save()
return HttpResponseRedirect('/schedule/monthschedule/%s/%s/' % (year,month,))
else:
item = {
"shift_name_1":Schedule_list.shift_name_1,
"shisetsu_name_1": Schedule_list.shisetsu_name_1,
"shift_name_2": Schedule_list.shift_name_2,
"shisetsu_name_2": Schedule_list.shisetsu_name_2,
"shift_name_3": Schedule_list.shift_name_3,
"shisetsu_name_3": Schedule_list.shisetsu_name_3,
"shift_name_4": Schedule_list.shift_name_4,
"shisetsu_name_4": Schedule_list.shisetsu_name_4,
}
form = ScheduleUpdateForm(initial=item)
context = {
'form' : form,
'Schedule_list': Schedule_list,
'User_list': User_list,
'shift_object': shift_object,
}
return render(request,'schedule/update.html', context )
'''
#### **`schedule/month.py`**
```html
{% extends 'schedule/base.html' %}
{% block header %}
<ul class="page">
<div align="left">
<li class="previous">
{% ifnotequal month 1 %}
<a href="{% url'schedule:monthschedule' year month | add:'-1'%} "class =" btn-info btn active "> previous month </a>
{% else %}
<a href="{% url'schedule:monthschedule' year | add:'-1' 12%} "class =" btn-info btn active "> previous month </a>
{% endifnotequal %}
</li>
</div>
<div align="right">
<li class="next">
{% ifnotequal month 12 %}
<a href="{% url'schedule:monthschedule' year month | add: '1'%} "class =" btn-info btn active "> next month </a>
{% else %}
<a href="{% url'schedule:monthschedule' year | add: '1' 1%} "class =" btn-info btn active "> Next month </a>
{% endifnotequal %}
</li>
</div>
</ul>
<p>
{% for shisetsu in shisetsu_object %}
<span style="background-color:{{ shisetsu.color }}">{{ shisetsu.name }}</span>
{% endfor %}
</p>
{{ kikan }}
{% for shift in shift_object %}
{% if shift.name! = "holiday" and shift.name! = "Yes"%}
{{ shift.name }} : {{ shift.start_time | date:"G"}}~{{ shift.end_time | date:"G"}}
{% endif %}
{% endfor %}
{% 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 profile in profile_list %}
{% for staff in user_list %}
{% if profile.user_id == staff.id %}
<tr align="center">
<th rowspan = "1" class = "staff_name" staff_id = "{{staff_id}}" width = "200"> {{staff.last_name}} {{staff.first_name}} </ th> <!- -staff_id element is 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" 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 %}
{% endif %}
{% endfor %}
</td>
<tr align="center">
{% for month in month_total %}
{% if month.user == staff.id%} <!-If the username is the same->
<td><b>{{ month.month_total_worktime }}</b></td>
{% endif %}
{% endfor %}
{% for item in object_list %}
{% if item.user|stringformat:"s" == staff.username|stringformat:"s" %}<!--If the username is the same-->
<td class="day" id="s{{ staff.id }}d{{ item.date }}">
<a href="{% url 'schedule:update' item.pk %}">{{ item.day_total_worktime }} </a>
</td>
{% endif %}
{% endfor %}
</tr>
{% endif %}
{% endfor %}
{% endfor %}
</tbody>
</table>
{% endblock content %}
I will enter the shift creation function from here. There are more people who work for double work, so there are people who come in with hope when they come in, and people who come in regularly but take a rest on this day.
So, first of all, I will try to be able to enter the shift request.
Recommended Posts