Beispiel #1
0
    def __init__(self, *args, **kwargs):
        """Init GroupForm"""
        super().__init__(*args, **kwargs)  # populates the post
        from trionyx.trionyx.views import create_permission_jstree

        selected_permissions = []
        if self.instance:
            try:
                selected_permissions = self.instance.permissions.all()
            except ValueError:
                if 'permissions' in self.initial:
                    selected_permissions = self.initial['permissions']
        elif 'permissions' in self.initial:
            selected_permissions = self.initial['permissions']

        jstree = create_permission_jstree(selected_permissions)

        self.helper = FormHelper()
        self.helper.layout = Layout(
            'name',
            HtmlTemplate(
                'trionyx/forms/permissions.html', {
                    'field_name':
                    'permissions',
                    'permission_jstree':
                    jstree,
                    'permission_ids':
                    filter(None, [
                        item['permission_id']
                        for item in jstree if 'permission_id' in item
                    ]),
                }))
Beispiel #2
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.helper = FormHelper()
        self.helper.layout = Layout(
            'project',
            Div(
                Div(
                    'item_type',
                    css_class='col-md-6',
                ),
                Div(
                    'priority',
                    css_class='col-md-6',
                ),
                css_class='row',
            ),
            'name',
            Div(
                Div(
                    'estimate',
                    css_class='col-md-6',
                ),
                Div(
                    'non_billable',
                    css_class='col-md-6',
                ),
                css_class='row',
            ),
            'description',
        )
Beispiel #3
0
 def __init__(self, instance=None, *args, **kwargs):
     """Init user form"""
     super().__init__(*args, instance=instance, **kwargs)
     self.fields['language'].choices = [
         (key, _(value)) for key, value in self.fields['language'].choices
     ]
     self.helper = FormHelper()
     self.helper.layout = Layout(
         'email',
         Div(Fieldset(
             _('Personal info'),
             'first_name',
             'last_name',
             css_class="col-md-6",
         ),
             Div(
                 'avatar',
                 css_class="col-md-6",
             ),
             css_class="row"),
         Div(Fieldset(_('Change password'),
                      'new_password1',
                      'new_password2',
                      css_class='col-md-6'),
             Fieldset(_('Settings'),
                      'language',
                      'timezone',
                      css_class='col-md-6'),
             css_class='row'),
     )
Beispiel #4
0
    def display_dialog(
            self,
            form: Optional[ModelForm] = None,
            success_message: Optional[str] = None) -> Dict[str, Any]:
        """Display form and success message when set"""
        initial = {
            **{key: value
               for key, value in self.request.GET.items()},
        }

        if not form:
            form = self.get_form_class()(initial=initial, instance=self.object)

        helper = getattr(form, 'helper', FormHelper(form))
        helper.form_tag = False
        setattr(form, 'helper', helper)

        return {
            'title':
            form.get_title() if hasattr(
                form, 'get_title') else self.title.format(  # type: ignore
                    model_name=self.get_model_config().model_name,
                    object=str(self.object) if self.object else '',
                ),
            'content':
            self.render_to_string(self.template, {
                'form': form,
                'success_message': success_message,
            }),
            'submit_label':
            form.get_submit_label() if hasattr(form, 'get_submit_label') else
            self.submit_label,  # type: ignore
            'success':
            bool(success_message),
        }
Beispiel #5
0
    def get_form(self, form_class=None):
        """Get form for model"""
        if not hasattr(self, 'form'):
            self.form = super().get_form(form_class)

        if not getattr(self.form, 'helper', None):
            self.form.helper = FormHelper(self.form)
            self.form.helper.form_tag = False
        else:
            self.form.helper.form_tag = False
        return self.form
Beispiel #6
0
    def __init__(self, instance=None, *args, **kwargs):
        """Init user form"""
        super().__init__(*args, instance=instance, **kwargs)
        from trionyx.trionyx.views import create_permission_jstree

        selected_permissions = []
        if self.instance and self.instance.id:
            selected_permissions = self.instance.user_permissions.all()
        elif 'user_permissions' in self.initial:
            selected_permissions = self.initial['user_permissions']

        jstree = create_permission_jstree(selected_permissions)

        self.helper = FormHelper()
        self.helper.layout = Layout(
            'email',
            Div(Div(
                Fieldset(
                    _('Personal info'),
                    'first_name',
                    'last_name',
                    'avatar',
                ),
                Fieldset(
                    _('Change password'),
                    'new_password1',
                    'new_password2',
                ),
                css_class="col-md-6",
            ),
                Fieldset(
                    _('Permissions'),
                    'is_active',
                    'is_superuser',
                    'groups',
                    HtmlTemplate(
                        'trionyx/forms/permissions.html', {
                            'field_name':
                            'user_permissions',
                            'permission_jstree':
                            jstree,
                            'permission_ids':
                            filter(None, [
                                item['permission_id']
                                for item in jstree if 'permission_id' in item
                            ]),
                        }),
                    css_class="col-md-6",
                ),
                css_class="row"),
        )
Beispiel #7
0
    def display_dialog(self):
        """Handle widget config"""
        code = self.kwargs.get('code')
        if code not in widgets:
            return {'title': _('Widget does not exists')}

        if '__post__' in self.request.POST:
            post = self.request.POST
            initial = None
        else:
            post = None
            initial = self.request.POST

        widget = widgets.get(code)
        form = widget.config_form_class(
            post, initial=initial) if widget.config_form_class else None

        config = None
        if form:
            if not hasattr(form, "helper"):
                form.helper = FormHelper(form)
            form.helper.form_tag = False

            if form.is_valid():
                config = form.cleaned_data
                config['title'] = self.request.POST.get('title')
                config['refresh'] = self.request.POST.get('refresh')
            elif '__post__' in self.request.POST:
                logger.error(json.dumps(form.errors))
        elif '__post__' in self.request.POST:
            config = {
                'title': self.request.POST.get('title'),
                'refresh': self.request.POST.get('refresh')
            }

        return {
            'title':
            _('Widget config'),
            'content':
            self.render_to_string(
                'trionyx/dialog/widget_config.html', {
                    'form': form,
                    'title': self.request.POST.get('title'),
                    'refresh': self.request.POST.get('refresh'),
                }),
            'submit_label':
            _('Save'),
            'config':
            config
        }
Beispiel #8
0
    def __init__(self, *args, **kwargs):
        """Init"""
        super().__init__(*args, **kwargs)
        if utils.get_current_user().id == self.instance.id:
            text = _(
                'Are you sure you want to invalidate your current API key and generate a new one?'
            )
        else:
            text = _(
                'Are you sure you want to invalidate the API key and generate a new one for {user}?'
                .format(user=self.instance))

        self.helper = FormHelper()
        self.helper.layout = Layout(
            Div(HTML(text), css_class='alert alert-warning'))
Beispiel #9
0
    def __init__(self, *args, **kwargs):
        """Init form"""
        super().__init__(*args, **kwargs)

        if self.instance and self.instance.id:
            account = self.instance.account
            self.initial['default_shipping'] = self.instance == account.shipping_address
            self.initial['default_billing'] = self.instance == account.billing_address
        elif 'account' in self.initial:
            account = Account.objects.get(id=self.initial['account'])
            if not account.shipping_address:
                self.initial['default_shipping'] = True
            if not account.billing_address:
                self.initial['default_billing'] = True

        self.helper = FormHelper()
        self.helper.layout = Layout(
            'account',
            'street',
            Div(
                Div(
                    'postcode',
                    css_class='col-md-6'
                ),
                Div(
                    'city',
                    css_class='col-md-6'
                ),
                css_class='row',
            ),
            Div(
                Div(
                    'state',
                    css_class='col-md-6'
                ),
                Div(
                    'country',
                    css_class='col-md-6'
                ),
                css_class='row',
            ),
            'default_shipping',
            'default_billing'
        )
Beispiel #10
0
 def __init__(self, instance=None, *args, **kwargs):
     """Init user form"""
     super().__init__(*args, instance=instance, **kwargs)
     self.helper = FormHelper()
     self.helper.layout = Layout(
         Div(Fieldset(
             _('Login'),
             'email',
             'new_password1',
             'new_password2',
             css_class="col-md-6",
         ),
             Fieldset(
                 _('Personal info'),
                 'first_name',
                 'last_name',
                 css_class="col-md-6",
             ),
             css_class="row"), )
Beispiel #11
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        if apps.is_installed("trionyx_accounts"):
            from trionyx_accounts.models import Account
            self.fields['account'] = forms.ChoiceField(
                label=_('Account'),
                choices=[('', '-----'),
                         *Account.objects.values_list('id', 'verbose_name')],
                required=False,
                initial=self.instance.for_object_id,
            )

        self.helper = FormHelper()
        self.helper.layout = Layout(
            Div(
                Div(
                    Div(
                        Div(
                            'name',
                            css_class='col-md-8',
                        ),
                        Div(
                            'code',
                            css_class='col-md-2',
                        ),
                        Div(
                            'status',
                            css_class='col-md-2',
                        ),
                        css_class='row',
                    ),
                    Div(
                        Div(
                            'account'
                            if apps.is_installed("trionyx_accounts") else None,
                            css_class='col-md-8',
                        ),
                        Div(
                            DateTimePicker('deadline', format='%Y-%m-%d'),
                            css_class='col-md-4',
                        ),
                        css_class='row',
                    ),
                    Div(
                        Div(
                            'project_type',
                            css_class='col-md-8',
                        ),
                        Div(
                            Depend(
                                [('project_type', '10')],
                                'fixed_price',
                            ),
                            Depend(
                                [('project_type', '20')],
                                'project_hourly_rate',
                            ),
                            css_class='col-md-4',
                        ),
                        css_class='row',
                    ),
                    css_class='col-md-6',
                ),
                Div(
                    'description',
                    css_class='col-md-6',
                ),
                css_class='row',
            ), )
Beispiel #12
0
    def __init__(self, *args, **kwargs):
        """Init form"""
        super().__init__(*args, **kwargs)

        if self.instance and self.instance.id:
            self.fields['billing_address'].queryset = Address.objects.filter(account=self.instance)
            self.fields['shipping_address'].queryset = Address.objects.filter(account=self.instance)
        else:
            self.fields['billing_address'].queryset = Address.objects.filter(account_id=-1)
            self.fields['shipping_address'].queryset = Address.objects.filter(account_id=-1)

        self.helper = FormHelper()
        self.helper.layout = Layout(
            Div(
                Fieldset(
                    _('General'),
                    Div(
                        Div(
                            'name',
                            css_class='col-md-6'
                        ),
                        Div(
                            'website',
                            css_class='col-md-6'
                        ),
                        css_class='row',
                    ),
                    Div(
                        Div(
                            'type',
                            css_class='col-md-6'
                        ),
                        css_class='row',
                    ),
                    Div(
                        Div(
                            'phone',
                            css_class='col-md-6'
                        ),
                        Div(
                            'email',
                            css_class='col-md-6'
                        ),
                        css_class='row',
                    ),
                    Div(
                        Div(
                            'billing_address',
                            css_class='col-md-6'
                        ),
                        Div(
                            'shipping_address',
                            css_class='col-md-6'
                        ),
                        css_class='row',
                    ),
                    css_class='col-md-6',
                ),
                Fieldset(
                    _('Info'),
                    'assigned_user',
                    'description',
                    css_class='col-md-6',
                ),
            ),
        )
Beispiel #13
0
    def __init__(self, *args, **kwargs):
        """Init form"""
        super().__init__(*args, **kwargs)

        if self.instance and self.instance.id:
            self.fields['address'].queryset = Address.objects.filter(account=self.instance.account)
        elif 'account' in self.initial:
            self.fields['address'].queryset = Address.objects.filter(account_id=self.initial['account'])
        elif self.data and 'account' in self.data:
            self.fields['address'].queryset = Address.objects.filter(account_id=self.data['account'])

        self.fields['description'].widget.attrs['rows'] = 3
        self.helper = FormHelper()
        self.helper.layout = Layout(
            'account',
            Div(
                Div(
                    'first_name',
                    css_class='col-md-6'
                ),
                Div(
                    'last_name',
                    css_class='col-md-6'
                ),
                css_class='row',
            ),
            Div(
                Div(
                    'email',
                    css_class='col-md-6'
                ),
                Div(
                    'title',
                    css_class='col-md-6'
                ),
                css_class='row',
            ),
            Div(
                Div(
                    'phone',
                    css_class='col-md-6'
                ),
                Div(
                    'mobile_phone',
                    css_class='col-md-6'
                ),
                css_class='row',
            ),
            Div(
                Div(
                    'address',
                    css_class='col-md-6'
                ),
                Div(
                    'assigned_user',
                    css_class='col-md-6'
                ),
                css_class='row',
            ),
            'description'
        )
Beispiel #14
0
    def __init__(self, *args, **kwargs):
        """Init form"""
        super().__init__(*args, **kwargs)

        from trionyx.widgets import widget_data, GraphWidget
        self.fields['source'].choices = [
            ('', '------'),
            *[(code, data['name'])
              for code, data in widget_data.get_all_data(GraphWidget).items()],
            ('__custom__', _('Custom'))
        ]

        content_type_map = ContentType.objects.get_for_models(
            *list(models_config.get_all_models(utils.get_current_user())))
        interval_fields = {}
        graph_fields = {}

        for config in models_config.get_all_configs():
            if config.model not in content_type_map:
                continue

            graph_fields[content_type_map[config.model].id] = [{
                'id':
                '__count__',
                'text':
                str(_('Count records')),
            }, *[{
                'id': field.name,
                'text': str(field.verbose_name)
            } for field in config.get_fields()
                 if config.get_field_type(field) in ['int', 'float']]]

            interval_fields[content_type_map[config.model].id] = [
                {
                    'id': field.name,
                    'text': str(field.verbose_name)
                } for field in config.get_fields(True)
                if config.get_field_type(field) in ['date', 'datetime']
            ]

        self.fields['model'].choices = [
            (content_type.id, model._meta.verbose_name.capitalize())
            for model, content_type in content_type_map.items()
        ]

        self.helper = FormHelper()
        self.helper.layout = Layout(
            'source',
            Depend([('source', r'__custom__')],
                   'icon',
                   Div(Div(
                       'model',
                       css_class="col-md-6",
                   ),
                       Div(
                           HtmlTemplate(
                               'widgets/forms/model_field.html', {
                                   'model_id': '#id_model',
                                   'label': _('Field'),
                                   'name': 'field',
                                   'fields': graph_fields,
                                   'value': self.initial.get('field'),
                               }),
                           css_class="col-md-6",
                       ),
                       css_class="row"),
                   Div(Div(
                       HtmlTemplate(
                           'widgets/forms/model_field.html', {
                               'model_id': '#id_model',
                               'label': _('Interval field'),
                               'name': 'interval_field',
                               'fields': interval_fields,
                               'value': self.initial.get('interval_field'),
                           }),
                       css_class="col-md-6",
                   ),
                       Div(
                           'interval_period',
                           css_class="col-md-6",
                       ),
                       css_class="row"),
                   Fieldset(
                       _('Filters'),
                       Filters('filters', content_type_input_id='id_model'),
                   ),
                   css_id='widget-custom-source'))