from django import
forms
from .models import
Profile, MPref, MSyumi
from mycommon import *
import re
from datetime import date, time, datetime
class
ProfileForm(
forms.ModelForm):
class Meta:
model =
Profile
exclude = ('tks', 'insert_date', 'update_date', 'm_pref')
#初期設定(全てのカラムに適用する)
def __init__(self, *args, **kwargs):
super(
ProfileForm, self).__init__(*args, **kwargs)
for field in self.fields.values():
#html5の必須を解除
field.required = False
#都道府県取得
__PREF_option = [(s.id,s.tdk) for s in
MPref.objects.all().exclude(tks='1').order_by('tdk_k')]
__PREF_option.insert(0,(0,'--'))
#趣味取得
__SYUMI_option = [(s.id,s.sname) for s in
MSyumi.objects.all().exclude(tks='1').order_by('sname_k')]
mail =
forms.CharField(
label = 'E-Mail',
max_length = 50,
widget =
forms.TextInput(
attrs = {'size':'30'}
),
)
fname =
forms.CharField(
label ='名',
max_length = 10,
widget =
forms.TextInput(
attrs = {'size':'15'}
),
)
lname =
forms.CharField(
label = '姓',
max_length = 10,
widget =
forms.TextInput(
attrs = {'size':'15'}
),
)
fname_k =
forms.CharField(
label = 'メイ',
max_length = 10,
widget =
forms.TextInput(
attrs = {'size':'15'}
),
)
lname_k =
forms.CharField(
label = 'セイ',
max_length = 10,
widget =
forms.TextInput(
attrs = {'size':'15'}
),
)
sex =
forms.ChoiceField(
label = '性別',
widget =
forms.RadioSelect(),
choices=(('1','男'),('2','女')),
)
#モデルにはない項目(年月日を3つのプルダウンにわける)
birth_y =
forms.ChoiceField(
label = '生年月日',
widget =
forms.Select(),
choices = yearNowToXXX(100,'---'),
)
birth_m =
forms.ChoiceField(
widget =
forms.Select(),
choices = intToXX(1, 12, '--')
)
birth_d =
forms.ChoiceField(
widget =
forms.Select(),
choices = intToXX(1, 31, '--')
)
zipn =
forms.CharField(
label = '郵便番号',
max_length = 8,
widget =
forms.TextInput(
attrs = {'size':'10'}
),
)
m_pref_id =
forms.ChoiceField(
label = '都道府県',
widget =
forms.Select(),
choices = __PREF_option,
)
pref1 =
forms.CharField(
label = '住所1',
max_length = 100,
widget =
forms.TextInput(
attrs = {'size':'40'}
),
)
pref2 =
forms.CharField(
label = '住所2',
max_length = 100,
widget =
forms.TextInput(
attrs = {'size':'40'}
),
)
tel =
forms.CharField(
label = '電話番号',
max_length = 13,
widget =
forms.TextInput(
attrs = {'size':'15'}
),
)
syumi =
forms.MultipleChoiceField(
label = '趣味',
widget =
forms.CheckboxSelectMultiple(),
choices = __SYUMI_option,
)
bikou =
forms.CharField(
label = '備考',
widget =
forms.Textarea(attrs={'cols': 50, 'rows': 4}),
required = False,
)
"""
------------ ここからバリデーション ------------
"""
def clean(self):
""" メールチェック """
s = self.cleaned_data['mail']
if s == '':
self.add_error('mail', 'メールアドレスは必須項目です')
else:
m = re.search(r'^[^0-9][a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*[@][a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*[.][a-zA-Z]{2,4}$', s)
if m == None:
self.add_error('mail', 'メールアドレスの型式ではありません!')
""" 名前 """
name1 = self.cleaned_data['fname']
name2 = self.cleaned_data['lname']
if name1 =='' or name2 == '':
self.add_error('lname', '名前は必須項目です')
""" 名前カナ """
name1 = self.cleaned_data['fname_k']
name2 = self.cleaned_data['lname_k']
if name1 =='' or name2 == '':
self.add_error('lname_k', '名前(カナ)は必須項目です')
else:
m1 = re.search(r'^[ァ-ヶー]+$', name1)
m2 = re.search(r'^[ァ-ヶー]+$', name2)
if m1 == None or m2 == None:
self.add_error('lname_k', '名前(カナ)は全角カタカナで入力してください')
""" 性別 """
s = self.cleaned_data['sex']
if s == '':
self.add_error('sex', '性別を選択してください')
""" 生年月日 """
y = self.cleaned_data['birth_y']
m = self.cleaned_data['birth_m']
d = self.cleaned_data['birth_d']
if y == '0' or m == '0' or d == '0':
self.add_error('birth_y', '生年月日を選択してください')
else:
try:
datetime(int(y), int(m), int(d))
except:
self.add_error('birth_y', '選択された生年月日は存在しません')
""" 郵便番号 """
s = self.cleaned_data['zipn']
if s == '':
self.add_error('zipn', '郵便番号を記入してください')
else:
m = re.search(r'^\d{3}[-]\d{4}$', s)
if m == None:
self.add_error('zipn', '郵便番号の形式ではありません')
""" 住所 """
v1 = self.cleaned_data['m_pref_id']
v2 = self.cleaned_data['pref1']
if v1 == '0' or v2 == '':
self.add_error('pref1', '住所を選択、記入してください')
""" 電話 """
s = self.cleaned_data['tel']
if s == '':
self.add_error('tel', '電話番号は必須です')
else:
m = re.search(r'^[0-9]{2,4}-[0-9]{2,4}-[0-9]{3,4}$', s)
if m == None:
self.add_error('tel', '電話番号の形式ではありません')
""" 趣味 """
s = self.cleaned_data['syumi']
c = len(s)
if c < 2:
self.add_error('syumi', '趣味は最低2つ以上選択してください')
""" 備考 """
s = self.cleaned_data['bikou']
i = len(s) - (s.count('\n') * 2)
if i > 50:
self.add_error('bikou', '備考は50文字以内で記入してください')
return self.cleaned_data