Ejemplo n.º 1
0
 def __init__(self, *args, **kwargs):
     self.user = kwargs.pop('user', None)
     super(FormBaseMixin, self).__init__(*args, **kwargs)
     if 'mark' in self.fields:
         self.fields['mark'].widget = forms.HiddenInput()
     if self.user is not None:
         onidc_id = self.user.onidc_id
         effective = {
             'onidc_id': onidc_id,
             'deleted': False,
             'actived': True}
         for field_name in self.fields:
             field = self.fields.get(field_name)
             if isinstance(
                     field,
                     (forms.fields.SlugField,
                      forms.fields.CharField)):
                 self.fields[field_name].widget.attrs.update(
                     {'autocomplete': "off"})
             if isinstance(field, forms.fields.DateTimeField):
                 self.fields[field_name].widget.attrs.update(
                     {'data-datetime': "true"})
             if isinstance(field.widget, forms.widgets.Textarea):
                 self.fields[field_name].widget.attrs.update({'rows': "3"})
             if isinstance(field, (
                     forms.models.ModelChoiceField,
                     forms.models.ModelMultipleChoiceField)):
                 fl = ''
                 if getattr(field.queryset.model, 'mark', False):
                     field.queryset = shared_queryset(
                         field.queryset, onidc_id)
                     if field.queryset.model is Option:
                         _prefix = self._meta.model._meta.model_name
                         _postfix = field_name.capitalize()
                         flag = _prefix.capitalize() + '-' + _postfix
                         fl = flag
                         field_initial = field.queryset.filter(
                             master=True, flag=flag)
                         if field_initial.exists():
                             field.initial = field_initial.first()
                 else:
                     field.queryset = field.queryset.filter(**effective)
                 mn = field.queryset.model._meta
                 if can_create(mn, self.user) and fl:
                     fk_url = format_html(
                         ''' <a title="点击添加一个 {}"'''
                         ''' href="/new/{}/?flag={}">'''
                         '''<i class="fa fa-plus"></i></a>'''.format(
                             field.label, mn.model_name, fl))
                 elif can_create(mn, self.user) and not fl:
                     fk_url = format_html(
                         ''' <a title="点击添加一个 {}"'''
                         ''' href="/new/{}">'''
                         '''<i class="fa fa-plus"></i></a>'''.format(
                             field.label, mn.model_name))
                 else:
                     fk_url = ''
                 field.help_text = field.help_text + fk_url
             self.fields[field_name].widget.attrs.update(
                 {'class': "form-control"})
Ejemplo n.º 2
0
 def get_context_data(self, **kwargs):
     context = super(ListModelView, self).get_context_data(**kwargs)
     objects = context.get('object_list')
     _extra = {
         'can_create': can_create(self.opts, self.request.user),
         'actions': get_actions(self.opts, self.request.user),
         'thead': self.make_thead(),
         'tbody': self.make_tbody(objects),
         'paginate': self.make_paginate(self.object_list.count())
     }
     context.update(**_extra)
     return context