Development is proceeding at the same pace as before, but I haven't been able to post.
This time, when registering something, I will try to select only the store to which I belong. This is to prevent you from making a mistake when registering your desired shift, even though you belong only to Pu-a-Pu.
Before correction
When I looked it up, I was able to deal with it by adding forms.py and views.
views.py
def get_form_kwargs(self):
kwargs = super(KibouCreate, self).get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
I haven't grasped the image because I am doing it by myself, but when I get the form information, I imagine that I will pass my own information, get the result processed by the form and return it to Html. I will.
forms.py
class kibouCreateForm(forms.ModelForm):
class Meta:
shift_object = Shift.objects.all()
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')
widgets = {
'date': datetimepicker.DatePickerInput(
format='%Y-%m-%d',
options={
'locale': 'ja',
'dayViewHeaderFormat': 'YYYY year MMMM',
'ignoreReadonly': True,
'allowInputToggle': True,
}
),
}
def __init__(self, *args, **kwargs):
#Only the affiliation list can be selected
user = kwargs.pop('user')
UserShozoku_list = UserShozokuModel.objects.filter(user = user.id).values_list("shisetsu_name", flat=True)
UserShozoku_list = list(UserShozoku_list)
super(kibouCreateForm, self).__init__(*args, **kwargs)
self.fields['shisetsu_name_1'].queryset = Shisetsu.objects.filter(id__in = UserShozoku_list).all()
self.fields['shisetsu_name_2'].queryset = Shisetsu.objects.filter(id__in = UserShozoku_list).all()
self.fields['shisetsu_name_3'].queryset = Shisetsu.objects.filter(id__in = UserShozoku_list).all()
self.fields['shisetsu_name_4'].queryset = Shisetsu.objects.filter(id__in = UserShozoku_list).all()
"""
Deadline check
"""
def clean_date(self):
dt_now = datetime.datetime.now()
dt_date = self.cleaned_data.get('date')
#Enter only after 20th when 5th
if dt_now.day > 5:
startdate = datetime.date(dt_now.year,dt_now.month,20) + relativedelta(months=1)
if dt_date < startdate:
raise forms.ValidationError(
"The deadline has passed, so please contact the administrator.",
)
else:
startdate = datetime.date(dt_now.year,dt_now.month,20)
if dt_date < startdate:
raise forms.ValidationError(
"The deadline has passed, so please contact the administrator.",
)
return self.cleaned_data.get('date')
def clean(self):
############
#double check
############
dt_user = self.cleaned_data.get('user')
dt_date = self.cleaned_data.get('date')
if KibouShift.objects.filter(user=dt_user, date=dt_date).count() > 0:
raise forms.ValidationError("I cannot register because the desired shift on the same day has already been registered. Please correct it.")
dt_shift_name1 = self.cleaned_data.get('shift_name_1')
dt_shift_name2 = self.cleaned_data.get('shift_name_2')
dt_shift_name3 = self.cleaned_data.get('shift_name_3')
dt_shift_name4 = self.cleaned_data.get('shift_name_4')
dt_shisetsu_name_1 = self.cleaned_data.get('shisetsu_name_1')
dt_shisetsu_name_2 = self.cleaned_data.get('shisetsu_name_2')
dt_shisetsu_name_3 = self.cleaned_data.get('shisetsu_name_3')
dt_shisetsu_name_4 = self.cleaned_data.get('shisetsu_name_4')
#If the shift is closed, the facility will be left blank
if str(dt_shift_name1) == "Closed" or str(dt_shift_name1) == "Yes" or str(dt_shift_name1) == "Not":
if dt_shift_name2 != None or dt_shift_name3 != None or dt_shift_name4 != None or dt_shisetsu_name_1 != None or dt_shisetsu_name_2 != None or dt_shisetsu_name_3 != None or dt_shisetsu_name_4 != None:
raise forms.ValidationError("No other input is required for shifts "holiday", "yes", and "no".")
Then
It is now only the facility to which I belong.
By the way, I would like to make user selection only possible for those who have permission to add schedules.
I just added a little bit. Even so, I researched it for about an hour and implemented it (laugh)
Originally, I think that it is best to be able to select a facility for each selected user, but it is necessary to implement it with javascript or get it by pressing a button.
You need to improve your skills in the future.
forms.py
def __init__(self, *args, **kwargs):
#Only the affiliation list can be selected
user = kwargs.pop('user')
UserShozoku_list = UserShozokuModel.objects.filter(user = user.id).values_list("shisetsu_name", flat=True)
UserShozoku_list = list(UserShozoku_list)
super(kibouCreateForm, self).__init__(*args, **kwargs)
self.fields['shisetsu_name_1'].queryset = Shisetsu.objects.filter(id__in = UserShozoku_list).all()
self.fields['shisetsu_name_2'].queryset = Shisetsu.objects.filter(id__in = UserShozoku_list).all()
self.fields['shisetsu_name_3'].queryset = Shisetsu.objects.filter(id__in = UserShozoku_list).all()
self.fields['shisetsu_name_4'].queryset = Shisetsu.objects.filter(id__in = UserShozoku_list).all()
#It is better to change for each selected user, but it is necessary to get and operate with Javascript or button
#Change user selection if you have permission to add schedule
permissions = Permission.objects.filter(user=user)
if user.has_perm('schedule.add_KibouShift'):
self.fields['user'].queryset = CustomUser.objects.all()
else:
self.fields['user'].queryset = CustomUser.objects.filter(username=user.username).all()
By the way, I changed it to CustomUser instead of the originally prepared User object. I cleared the database and started over, so the data became par.
Recommended Posts