def level_structure(self): level_dict = {field: 0 for field, display in Level.get_choices()} for a in self.attendees: if not getattr(a, 'level'): continue level_dict[getattr(a, 'level')] += 1 return level_dict
def detail(prop_id): prop = Proposal.objects.get_or_404(id=prop_id) gender_dict = prop.gender_structure level_dict = prop.level_structure prop.gender_ratio = "{} / {}".format(gender_dict[Gender.MALE], gender_dict[Gender.FEMALE]) prop.level_ratio = "{} / {} / {}".format( level_dict[Level.get_map()[Level.CADRE]], level_dict[Level.get_map()[Level.MEDIUM]], level_dict[Level.get_map()[Level.NEWBIE]], ) # give every itinerary obj a date str for i in prop.itinerary_list: i.date_str = ( prop.start_date + datetime.timedelta(days=i.day_number - 1)).strftime("%m/%d") return render_template('proposals/detail.html', prop=prop)
class SignupForm(FlaskForm): username = StringField("* 綽號", validators=[ validators.DataRequired("此為必填欄位"), validators.Length(max=10, message="太長了呦,最多10個字") ]) name = StringField( "真實姓名(隱藏)", validators=[validators.Length(max=10, message="太長了呦,最多10個字")]) birthday = StringField("生日(隱藏)") cellphone_number = StringField("手機(隱藏)", validators=[ validators.Optional(), validators.Regexp( "09[0-9]{8}$", message="電話格式錯誤,需為09開頭之數字共10碼") ]) department = StringField("系所") graduation_year = StringField("畢業年份") group = SelectField("嚮導隊", choices=Group.get_choices()) position = SelectField("工作組", choices=Position.get_choices()) level = SelectField("最高位階", choices=Level.get_choices()) identity = SelectField("在校狀態", choices=Identity.get_choices()) introduction = TextAreaField("自我介紹", validators=[validators.Optional()]) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.birthday_dt = None def validate_username(self, field): field.data = field.data.strip() def validate_birthday(self, field): if not field.data: return None try: dt = datetime.datetime.strptime(field.data, "%Y/%m/%d") except ValueError: raise ValidationError('格式錯誤,正確範例為1985/01/05') if dt > datetime.datetime.now() or dt < datetime.datetime(1910, 1, 1): raise ValidationError('日期不合理') self.birthday_dt = dt def validate_graduation_year(self, field): if not field.data: return None max_graduation_year = datetime.datetime.now().year - 1903 try: field.data = int(field.data) except TypeError: raise ValidationError('畢業年份需為數字') if field.data > max_graduation_year or field.data < 40: raise ValidationError('需介於40至{}之間'.format(max_graduation_year))
class SttUser(User): # 基本資料 name = db.StringField() # 真實姓名 password_hash = db.StringField() birthday = db.DateTimeField() cellphone_number = db.StringField() introduction = db.StringField() # 學校資料 department = db.StringField() # 系所,例如:物理、水利 graduation_year = db.IntField() # 畢業年,例如:102 # 社團相關資料 group = db.StringField(choices=Group.get_choices()) # 嚮導隊 position = db.StringField(choices=Position.get_choices()) # 工作組,總務、教學 level = db.StringField(choices=Level.get_choices()) # 新生、隊員、幹部等 member_id = db.ObjectIdField() identity = db.StringField(choices=Identity.get_choices()) # 在校狀態 # 系統紀錄 updated_at = db.DateTimeField() invitation_info = db.EmbeddedDocumentField(InvitationInfo) meta = { 'ordering': ['-created_at'], 'indexes': [ 'name', ] } @property def birthday_str(self): if self.birthday: return self.birthday.strftime("%Y/%m/%d") return "" @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password)
import datetime from sttapp.exts.db import db from sttapp.base.models import RecordModel from sttapp.base.enums import Level, Gender, Difficulty, Group from sttapp.base.utils import get_local_dt CHOICES = { "group": Group.get_choices(), "difficulty": Difficulty.get_choices(), "level": Level.get_choices(), } class Member(RecordModel): # 基本資料 name = db.StringField() nickname = db.StringField() security_number = db.StringField(unique=True, sparse=True) birthday = db.DateTimeField() cellphone_number = db.StringField() # 進階資料 drug_allergy = db.StringField(default="NKDA") blood_type = db.StringField() level = db.StringField(choices=CHOICES["level"]) # 新生、隊員、幹部等 group = db.StringField(choices=CHOICES["group"]) # 學校資訊
def validate_level(self, field): keys = Level.get_map(False).keys() if field.data not in Level.get_map(False).values(): raise ValidationError("等級需為{}的其中一個".format("、".join(keys)))