def handle_foreign_key(self, model, field, **kwargs): if field.null: kwargs['allow_blank'] = True if field.choices is not None: field_obj = SelectQueryField(query=field.choices, **kwargs) else: field_obj = ModelSelectField(model=field.rel_model, **kwargs) return FieldInfo(field.name, field_obj)
def handle_foreign_key(self, model, field, **kwargs): if field.null: kwargs['allow_blank'] = True if field.name in (self.model_admin.foreign_key_lookups or ()): form_field = ModelHiddenField(model=field.rel_model, **kwargs) else: form_field = ModelSelectField(model=field.rel_model, **kwargs) return field.name, form_field
def handle_foreign_key(self, model, field, **kwargs): if field.name in (self.model_admin.foreign_key_lookups or ()): data_source = url_for(self.model_admin.get_url_name('ajax_list')) widget = ChosenAjaxSelectWidget(data_source, field.name) form_field = LimitedModelSelectField(model=field.rel_model, widget=widget, **kwargs) else: form_field = ModelSelectField(model=field.rel_model, **kwargs) return field.name, form_field
'datetime': [DateTimeField], } INV_FIELD_TYPES = dict((v, k) for k in FIELD_TYPES for v in FIELD_TYPES[k]) FIELDS_TO_LOOKUPS = { 'foreign_key': ['eq', 'in'], 'text': ['eq', 'icontains', 'istartswith'], 'numeric': ['eq', 'ne', 'lt', 'lte', 'gt', 'gte', 'in'], 'boolean': ['eq'], 'datetime': ['today', 'yesterday', 'this_week', 'lte_days_ago', 'gte_days_ago'], } CONVERTERS = { (ForeignKeyField, 'eq'): lambda f: ModelSelectField(model=f.to), (ForeignKeyField, 'in'): lambda f: ModelSelectMultipleField(model=f.to), (DateTimeField, 'today'): lambda f: fields.HiddenField(), (DateTimeField, 'yesterday'): lambda f: fields.HiddenField(), (DateTimeField, 'this_week'): lambda f: fields.HiddenField(), (BooleanField, 'eq'): lambda f: BooleanSelectField(), } def is_valid_lookup(model, lookup): lookups = lookup.split('__') lookup_type = 'eq' if len(lookups) > 1 and lookups[-1] in LOOKUP_TYPES: lookup_type = lookups.pop()
class CreateActivityFrom(Form): def __init__(self, *args, **kwargs): super(CreateActivityFrom, self).__init__(*args, **kwargs) obj = kwargs.get("obj", None) team = kwargs.get("team", None) if not isinstance(team, Team): raise AssertionError("must a team") if obj and obj.province: province = obj.province else: province = self.province.choices[0][0] if province: self.city.choices = ChinaCity.get_cities(province) leaders = team.get_members(role="leader") leaders.insert(0, User.get_or_none(id=team.owner_id)) if leaders: self.leader.choices = [(str(user.id), user.name or user.mobile) for user in leaders] groups = team.groups if groups: self.allow_groups.choices = [(str(group.id), group.name) for group in groups] title = f.StringField("活动标题", description="", validators=[ validators.DataRequired(message="必填"), validators.Length(2, 32) ]) cover = FileField("活动图片", description="仅支持格式:jpg、png, 不能超过2M", validators=[ file_allowed(("jpg", "png", "jpeg"), message="仅支持格式:jpg、png") ]) sport = ModelSelectField("运动类型", model=Sport, get_label="name") province = f.SelectField("省份", validators=[ validators.DataRequired(message="必填"), ], choices=ChinaCity.get_provinces()) city = WPSelectField("城市", validators=[ validators.DataRequired(message="必填"), ], choices=[]) address = f.StringField("详细地址", description="非场地运动新填写集合地点", validators=[ validators.DataRequired(message="必填"), ]) lat = f.HiddenField("lat") lng = f.HiddenField("lng") formatted_address = f.HiddenField("address_name") start_time = f.DateTimeField( "开始时间", description="首次活动开始时间", validators=[validators.DataRequired(message="必填")]) end_time = f.DateTimeField("结束时间", validators=[ validators.DataRequired(message="必填"), LaterThan("start_time", message="必须晚于活动开始时间") ]) repeat_type = f.SelectField( "循环活动", description="活动结算后系统自动生成下期活动", choices=[ ("", "不循环"), # ("day", "每天"), ("week", "每周"), # ("month", "每月") ]) repeat_end = f.DateTimeField("结束循环", description="超过此时间活动将不能自动循环", validators=[ validators.Optional(), LaterThan("start_time", message="必须晚于活动开始时间") ]) join_end = f.DateTimeField("报名截止时间", description="", validators=[validators.Optional()]) description = f.TextAreaField("活动说明", description="", validators=[ validators.DataRequired(message="必填"), validators.Length(1, 5000, message="不能超过5000字") ]) max_members = f.IntegerField("人数限制", description="活动限制人数", default=15, validators=[ validators.Optional(), validators.NumberRange( 2, 20000, message="人数限制必须在2到20000人之间") ]) allow_agents = f.IntegerField("允许代报人数", description="代报人数不包含报名人自己", default=0, validators=[ validators.Optional(), validators.NumberRange( 0, 200, message="允许代报人数限制必须在0到200人之间") ]) payment_type = f.SelectField( "支付方式", default="", choices=[("0", "在线支付"), ("1", "线下支付")], validators=[validators.DataRequired(message="必填")]) price = f.DecimalField("价格(人/次)", description="") # female_price = f.DecimalField("女成员价格") vip_price = f.DecimalField("VIP价格(人/次)", description="VIP会员专享价格") allow_free_times = f.BooleanField("允许使用次卡") visible = f.SelectField("允许报名", default="", choices=[("0", "所有人"), ("1", "仅成员")], validators=[validators.Optional()]) allow_groups = f.SelectMultipleField("允许分组", default="", description="例如:限制只能高级组会员可以报名,默认不限", choices=[], validators=[validators.Optional()]) leader = f.SelectField("组织者", choices=[]) need_fields = f.SelectMultipleField( "需要填写的报名信息", description="您希望报名人员填写哪些信息", default=['need_nickname', 'need_gender', 'need_mobile'], choices=[("need_nickname", "昵称"), ("need_mobile", "手机"), ("need_gender", "性别"), ("need_name", "姓名"), ("need_identification", "身份证明"), ("need_emergency_contact", "紧急联系人")]) need_ext1_name = f.StringField("信息名称", description="不需要请留空") need_ext1_type = f.SelectField("类型", choices=(('text', '文本'), ('photo', '照片'))) need_ext2_name = f.StringField("信息名称") need_ext2_type = f.SelectField("类型", choices=(('text', '文本'), ('photo', '照片'))) need_ext3_name = f.StringField("信息名称") need_ext3_type = f.SelectField("类型", choices=(('text', '文本'), ('photo', '照片')))
def handle_foreign_key(self, model, field, **kwargs): return field.descriptor, ModelSelectField(model=field.to, **kwargs)
class CreateMatchFrom(Form): def __init__(self, *args, **kwargs): super(CreateMatchFrom, self).__init__(*args, **kwargs) obj = kwargs.get("obj", None) team = kwargs.get("team", None) if not isinstance(team, Team): raise AssertionError("must a team") if obj and obj.province: province = obj.province else: province = self.province.choices[0][0] if province: self.city.choices = ChinaCity.get_cities(province) title = f.StringField("比赛名称", description="", validators=[ validators.DataRequired(message="必填"), validators.Length(1, 200, message="不能超过200字") ]) coverfile = FileField("封面", description="建议尺寸:1045x464,仅支持格式:jpg、png, 不能超过10M", validators=[ file_required(message="必填"), file_allowed(("jpg", "png", "jpeg"), message="仅支持格式:jpg、png") ]) sport_id = ModelSelectField("运动类型", model=Sport, get_label="name") type = f.SelectField("比赛类型", description="对战型如:足球、蓝球,非对战型:跑步、自行车", validators=[ validators.DataRequired(message="必填"), ], choices=(('0', "对战型"), ('1', "非对战型"))) province = f.SelectField("省份", validators=[ validators.DataRequired(message="必填"), ], choices=ChinaCity.get_provinces()) city = WPSelectField("城市", validators=[ validators.DataRequired(message="必填"), ], choices=[]) address = f.StringField("详细地址", description="非场地运动新填写集合地点", validators=[ validators.DataRequired(message="必填"), ]) lat = f.HiddenField("lat") lng = f.HiddenField("lng") formatted_address = f.HiddenField("address_name") start_time = f.DateTimeField( "开始时间", description="赛事开始时间", validators=[validators.DataRequired(message="必填")]) end_time = f.DateTimeField("结束时间", validators=[ validators.DataRequired(message="必填"), LaterThan("start_time", message="必须晚于开始时间") ]) join_start = f.DateTimeField("报名开始时间", description="限时开始报名,不填赛事上线即可报名", validators=[validators.Optional()]) join_end = f.DateTimeField("报名截止时间", description="不填则开始前均可报名", validators=[ validators.Optional(), BeforeThan("start_time", message="必须早于开始时间"), LaterThan("join_start", message="必须晚于报名开始时间") ]) contact_person = f.StringField("联系人", description="", validators=[ validators.DataRequired(message="必填"), validators.Length(1, 200, message="不能超过200字") ]) contact_phone = f.StringField("联系电话", description="", validators=[ validators.DataRequired(message="必填"), validators.Length(1, 200, message="不能超过200字") ]) description = f.TextAreaField( "简介", description="", validators=[validators.DataRequired(message="必填")]) rules = f.TextAreaField("规程", description="", validators=[validators.DataRequired(message="必填")]) reward = f.StringField("奖励", description="奖励说明,如:冠军1000元,亚军500元", validators=[ validators.Optional(), validators.Length(0, 200, message="不能超过200字") ]) join_type = f.SelectField("报名类型", validators=[ validators.DataRequired(message="必填"), ], choices=(('0', "个人"), ('1', "团队"))) refund_type = f.SelectField("退款策略", validators=[ validators.DataRequired(message="必填"), ], choices=(('0', "开始前可以退款"), ('1', "报名截止前可退"), ('2', "不能退款"))) max_members = f.IntegerField("人数或团队限制", description="比赛人数或团队限制,报满则无法继续报名", default=15, validators=[ validators.Optional(), validators.NumberRange( 0, 20000, message="人数限制必须在2到20000人之间") ]) price = f.DecimalField("报名费(元)", description="设置分组后将以分组报名费为准", validators=[validators.Optional()]) group_type = f.SelectField("分组模式", validators=[ validators.DataRequired(message="必填"), ], choices=(('0', "非分组比赛"), ('1', "分组比赛"))) groups = f.StringField("分组", validators=[validators.Optional()])