forms.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:sadm 作者: prologin 项目源码 文件源码
def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(MatchCreationForm, self).__init__(*args, **kwargs)
        self.helper = BaseFormHelper()
        self.champions = []
        champions = (models.Champion.objects
                           .filter(deleted=False, status='ready')
                           .select_related('author'))
        if (settings.STECHEC_FIGHT_ONLY_OWN_CHAMPIONS and not
            self.request.user.is_staff):
            champions = champions.filter(author=self.request.user)
        for i in range(1, settings.STECHEC_NPLAYERS + 1):
            f = forms.ModelChoiceField(label="Champion %d" % i,
                                       queryset=champions,
                                       widget=forms.Select(attrs={'class': 'select2'}))
            self.fields['champion_%d' % i] = f
            self.helper.append_field('champion_%d' % i)
            self.champions.append(f)

        if settings.STECHEC_USE_MAPS:
            self.fields['map'] = forms.ChoiceField(required=True,
                                                   widget=MapSelect(attrs={'class': 'mapselect select2'}),
                                                   label="Carte utilisée")

            all_maps = models.Map.objects.select_related('author').order_by('author__username', 'name')
            self.fields['map'].choices = [
                ('Officielles', [(map.id, map) for map in all_maps if map.official])
            ] + [
                (author, [(map.id, map) for map in maps])
                for author, maps in groupby(
                    (map for map in all_maps if not map.official),
                    lambda map: map.author
                )
            ]
            self.helper.append_field('map')

        self.helper.append_submit("Lancer le match")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号