[PYTHON] Added a function to register desired shifts in the Django shift table

At first, when the administrator receives a notification of the desired shift, register it, and when executing shift creation once a month, first create a schedule from the desired shift, and if there is no shift, shift on the same day of the previous month I will try to put it in.

First of all, the desired shift

schedule/models.py


class KibouShift(models.Model):
    id = models.AutoField(verbose_name='Desired shift ID',primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='Employee name')
    date = models.DateField(verbose_name='date')
    shift_name_1 = models.ForeignKey(Shift, verbose_name='1 shift name', related_name='kibou_shift_name1',on_delete=models.SET_NULL,null= True)
    shisetsu_name_1 = models.ForeignKey(Shisetsu, verbose_name='1 facility', related_name='kibou_shisetsu_name1',on_delete=models.SET_NULL,blank=True, null=True)
    shift_name_2 = models.ForeignKey(Shift, verbose_name='2 shift name', related_name='kibou_shift_name2',on_delete=models.SET_NULL,blank=True, null=True)
    shisetsu_name_2 = models.ForeignKey(Shisetsu, verbose_name='2 facilities', related_name='kibou_shisetsu_name2',on_delete=models.SET_NULL,blank=True, null=True)
    shift_name_3 = models.ForeignKey(Shift, verbose_name='3 shift name', related_name='kibou_shift_name3',on_delete=models.SET_NULL,blank=True, null=True)
    shisetsu_name_3 = models.ForeignKey(Shisetsu, verbose_name='3 facilities', related_name='kibou_shisetsu_name3',on_delete=models.SET_NULL,blank=True, null=True)
    shift_name_4 = models.ForeignKey(Shift, verbose_name='4 shift name', related_name='kibou_shift_name4',on_delete=models.SET_NULL,blank=True, null=True)
    shisetsu_name_4 = models.ForeignKey(Shisetsu, verbose_name='4 facilities', related_name='kibou_shisetsu_name4',on_delete=models.SET_NULL,blank=True, null=True)

    class Meta:
        unique_together = ('user', 'date')

schedule/admin.py


class KibouShiftAdmin(admin.ModelAdmin):
    list_display = ('date', 'user')
    ordering = [ "date" ]

admin.site.register(KibouShift, KibouShiftAdmin)

schedule/views.py


def kiboulistfunc(request):
    KibouShift_list = KibouShift.objects.all().order_by('-date')
    User_list = User.objects.all()
    shift_object = Shift.objects.all()
    context = {
        'KibouShift_list': KibouShift_list,
        'User_list': User_list,
        'shift_object': shift_object,
        }

    return render(request,'schedule/kiboushift/list.html', context )
        
class KibouCreate(CreateView):
    template_name = 'schedule/kiboushift/create.html'
    model = KibouShift
    fields = ('user', 'date', 'shift_name_1', 'shisetsu_name_1', 'shift_name_2', 'shisetsu_name_2', 'shift_name_3', 'shisetsu_name_3', 'shift_name_4', 'shisetsu_name_4')
    success_url = reverse_lazy('schedule:KibouList')

class KibouUpdate(UpdateView):
    template_name = 'schedule/kiboushift/update.html'
    model = KibouShift
    fields = ('user', 'date', 'shift_name_1', 'shisetsu_name_1', 'shift_name_2', 'shisetsu_name_2', 'shift_name_3', 'shisetsu_name_3', 'shift_name_4', 'shisetsu_name_4')
    success_url = reverse_lazy('schedule:KibouList')

class KibouDelete(DeleteView):
    template_name = 'schedule/kiboushift/delete.html'
    model = KibouShift
    fields = ('user', 'date', 'shift_name_1', 'shisetsu_name_1', 'shift_name_2', 'shisetsu_name_2', 'shift_name_3', 'shisetsu_name_3', 'shift_name_4', 'shisetsu_name_4')
    success_url = reverse_lazy('schedule:KibouList')

schedule/kiboushift/base.html


<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    {% block customcss %}
    {% endblock customcss %}
    <title>related family</title>
  </head>
  <body>

    
    {% block header %}
    {% endblock header %}

    {% block content %}
    {% endblock content %}

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
  </body>
</html>

schedule/kiboushifr/create.py


{% extends 'schedule/kiboushift/base.html' %}

{% block header %}
<div class="jumbotron jumbotron-fluid">
    <div class="container">
      <h1 class="display-4">Desired shift registration</h1>
      <p class="lead"></p>
    </div>
  </div>
{% endblock header %}

{% block content %}
<form action="" method="POST">{% csrf_token %}
    {{form.as_p}}
    <p><input type="submit" value="Create" class="btn-info btn active">
    <a href="{% url 'schedule:KibouList' %}" class="btn-secondary btn active">Return</a></p>
</form>
{% endblock content %}

schedule/kiboushifr/delete.py


{% extends 'schedule/kiboushift/base.html' %}

{% block header %}
<div class="jumbotron jumbotron-fluid">
    <div class="container">
      <h1 class="display-4">Delete desired shift</h1>
      <p class="lead"></p>
    </div>
  </div>
{% endblock header %}

{% block content %}
<form action="" method="POST">{% csrf_token %}
  <p>Employee:{{ object.user }}</p>
  <p>date:{{ object.date }}</p>
  <p>Shift name:{{ object.shift_name_1|default:"" }}</p>
  <p>Facility:{{ object.shisetsu_name_1|default:"" }}</p>
  <p>Shift name:{{ object.shift_name_2|default:"" }}</p>
  <p>Facility:{{ object.shisetsu_name_2|default:"" }}</p>
  <p>Shift name:{{ object.shift_name_3|default:"" }}</p>
  <p>Facility:{{ object.shisetsu_name_3|default:"" }}</p>
  <p>Shift name:{{ object.shift_name_4|default:"" }}</p>
  <p>Facility:{{ object.shisetsu_name_4|default:"" }}</p>
  <p><input type="submit" value="Delete" class="btn-info btn active">
  <a href="{% url 'schedule:KibouList' %}" class="btn-secondary btn active">Return</a></p>
</form>
{% endblock content %}

schedule/kiboushifr/delete.py


{% extends 'schedule/kiboushift/base.html' %}

{% block header %}
<div class="jumbotron jumbotron-fluid">
    <div class="container">
      <h1 class="display-6">List of desired shifts</h1>
      <p class="lead"></p>
      <a href="{% url 'schedule:KibouCreate' %}" class="btn-info btn active">sign up</a>
      <a href="{% url 'schedule:homeschedule' %}" class="btn-info btn active">Shift table</a>
    </div>
  </div>
{% endblock header %}
{% block content %}
<table class="table table-striped table-bordered">
<div class='container'>
  <thead>
    <th>date</th>
    <th>Employee name</th>
    <th>Hope shift 1</th>
    <th>Desired facility 1</th>
    <th>Hope shift 2</th>
    <th>Desired facility 2</th>
    <th>Hope shift 3</th>
    <th>Hope shift 3</th>
    <th>Hope shift 4</th>
    <th>Desired facility 4</th>
  </thead>
  <tbody>
    {% for item in KibouShift_list %}
      {% for staff in User_list %}
      {% if item.user|stringformat:"s" == staff.username|stringformat:"s" %}
        <tr>
          <td>
            <a href="{% url 'schedule:KibouUpdate' item.pk %}" class="btn btn-info" role="button" aria-pressed="true">Fix</a>
            <a href="{% url 'schedule:kibouDelete' item.pk %}" class="btn btn-info" role="button" aria-pressed="true">Delete</a>
            {{ item.date }}
          </td>
          <td >{{ staff.last_name }} {{ staff.first_name }}</td>
          <td >{{ item.shift_name_1|default:"" }} </td>
          <td >{{ item.shisetsu_name_1|default:"" }}</td>
          <td >{{ item.shift_name_2|default:"" }} </td>
          <td >{{ item.shisetsu_name_2|default:"" }}</td>
          <td >{{ item.shift_name_3|default:"" }} </td>
          <td >{{ item.shisetsu_name_3|default:"" }}</td>
          <td >{{ item.shift_name_4|default:"" }} </td>
          <td >{{ item.shisetsu_name_4|default:"" }}</td>
        </tr>
        {% endif %}
        {% endfor %}
    {% endfor %}
  </tbody>
  </div>
</table>
{% endblock content %}

schedule/kiboushifr/update.py


{% extends 'schedule/kiboushift/base.html' %}

{% block header %}
{% endblock header %}

{% block content %}
<form action="" method="POST">{% csrf_token %}
<div class="Container">
    {{ form.as_p }}
    {% csrf_token %}
    <p><input class="btn btn-primary" type="submit" value="update">
    <a href="{% url 'schedule:KibouList' %}" class="btn-secondary btn active">Return</a></p>
    {% for shift in shift_object %}
        {% if shift.name != "Closed" and shift.name != "Yes" %}
            {{ shift.name }} : {{ shift.start_time }}~{{ shift.end_time }}
        {% endif %}
    {% endfor %}
</div>
</form>
{% endblock content %}

This completes. Click here for the list

image.png

I was able to make it for the time being, but in reality, the administrator can select a person so that he can register himself, but general users can register only for themselves, I wanted to display the Japanese name properly, but it is quite good I didn't go, and I thought I'd bring it to the point where it could work once, so I cut it off!

image.png

image.png

image.png

image.png

Since there is no title part above only the correction screen, I will correct it here

schedule/update.html


{% extends 'schedule/kiboushift/base.html' %}

{% block header %}
<div class="jumbotron jumbotron-fluid">
    <div class="container">
      <h1 class="display-4">Desired shift correction</h1>
      <p class="lead"></p>
    </div>
  </div>
{% endblock header %}

{% block content %}
<form action="" method="POST">{% csrf_token %}
<div class="Container">
    {{ form.as_p }}
    {% csrf_token %}
    <p><input class="btn btn-primary" type="submit" value="update">
    <a href="{% url 'schedule:KibouList' %}" class="btn-secondary btn active">Return</a></p>
    {% for shift in shift_object %}
        {% if shift.name != "Closed" and shift.name != "Yes" %}
            {{ shift.name }} : {{ shift.start_time }}~{{ shift.end_time }}
        {% endif %}
    {% endfor %}
</div>
</form>
{% endblock content %}

image.png

I was able to fix it! After that, it is necessary to add input assistance and input check. I will implement it again here.

Next, I would like to implement the function to create a shift from the shift request.

Recommended Posts

Added a function to register desired shifts in the Django shift table
I want to scroll the Django shift table, but ...
A memorandum to register the library written in Hy in PyPI
I made a command to generate a table comment in Django
How to generate a query using the IN operator in Django
Django shift table Shows only the facilities to which you belong
Create a function to get the contents of the database in Go
Attempt to extend a function in the library (add copy function to pathlib)
Implemented public and unpublished features in Django shift tables Added background color control for desired shifts
Django shift table shift data creation function completed
Function to extract the maximum and minimum values ​​in a slice with Go
The _authenticate_with_backend function was obsolete in django auth.autenticate
Django shift creation function Added basic work shift registration function for days of the week
Automatically register function arguments to argparse in Python
To execute a Python enumerate function in JavaScript
Get the caller of a function in Python
I added a function to CPython (ternary operator)
The first thing to check when a No Reverse Match occurs in Django
How to create a Rest Api in Django
How to Mock a Public function in Pytest
Display the Django shift table! Supports 20-day closing
Try to make a blackjack strategy by reinforcement learning (② Register the environment in gym)
How to unit test a function containing the current time using freezegun in python
I tried to create a table only with Django
Register a task in cron for the first time
[Django] Perform Truncate Table (delete all data in the table)
How to display DataFrame as a table in Markdown
[Django] Added new question creation function to polls app
Transit to the update screen with the Django a tag
I made a command to markdown the table clipboard
I added a function to CPython (build & structure grasp)
How to reference static files in a Django project
What does the last () in a function mean in Python?
Django Python shift table
A super introduction to Django by Python beginners! Part 6 I tried to implement the login function
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
Steps to change table and column names in your Django model at the same time
How to create a record by pasting a relation to the inheriting source Model in the Model inherited by Django
A story that makes it easier to see Model debugging in the Django + SQLAlchemy environment
I also tried to imitate the function monad and State monad with a generator in Python
The story I was addicted to when I specified nil as a function argument in Go
Define a task to set the fabric env in YAML
How to write custom validations in the Django REST Framework
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
How to print characters as a table with Python's print function
Put the lists together in pandas to make a DataFrame
A handy function to add a column anywhere in a Pandas DataFrame
To return char * in a callback function using ctypes in Python
How to get the last (last) value in a list in Python
Display the time in Django according to the user's region (UTC)
To write a test in Go, first design the interface
[python] Manage functions in a dictionary (command table, function table, function pointer)
In omegaconf, let's pass the direct parameter file to the function
I tried to implement the mail sending function in Python
I can't log in to the admin page with Django3
Let's create a function to hold down Button in Tkinter
In Django, how to abbreviate the long displayed string as ....
[Python] Create a program to delete line breaks in the clipboard + Register as a shortcut with windows
I made a function to check if the webhook is received in Lambda for the time being
Covector to think in function