Esempio n. 1
0
 def add(self,
         queryset,
         start_field,
         end_field=None,
         color='#ccc',
         action_names=[],
         as_initial_date=False):
     item = dict(queryset=dumps_qs_query(queryset),
                 start_field=start_field,
                 end_field=end_field,
                 color=color,
                 action_names=','.join(action_names))
     self.items.append(item)
     if as_initial_date:
         qs_initial_date = queryset.order_by(start_field).values_list(
             start_field, flat=True)
         if qs_initial_date.exists():
             self.set_initial_date(qs_initial_date[0])
     if queryset.model not in self.models and has_add_permission(
             self.request, queryset.model):
         label = get_metadata(queryset.model, 'verbose_name')
         app_label = get_metadata(queryset.model, 'app_label')
         model_name = queryset.model.__name__.lower()
         url = '/add/{}/{}/?{}='.format(app_label, model_name, start_field)
         link = dict(label=label, url=url)
         self.links.append(link)
         self.models.append(queryset.model)
Esempio n. 2
0
def reload_options(request, app_name, class_name, current_value, lookup, selected_value, lazy):
    l = []
    pks = []
    if not current_value == '0':
        for pk in current_value.split('_'):
            pks.append(int(pk))
    selected_value = int(selected_value)
    lazy = int(lazy)
    cls = apps.get_model(app_name, class_name)
    select_template = get_metadata(cls, 'select_template')
    select_display = get_metadata(cls, 'select_display')
    queryset = cls.objects.filter(**{lookup: selected_value})

    data = dict(selected_value=selected_value, results=[], qs=lazy and dumps_qs_query(queryset) or None)
    if lazy:
        if pks:
            for obj in cls.objects.filter(pk__in=pks):
                html = (select_template or select_display) and render_to_string(select_template or 'select_template.html', dict(obj=obj, select_display=select_display)) or str(obj)
                data['results'].append(dict(id=obj.id, text=str(obj), html=html))
    else:
        for obj in queryset:
            html = (select_template or select_display) and render_to_string(select_template or 'select_template.html', dict(obj=obj, select_display=select_display)) or str(obj)
            data['results'].append(dict(id=obj.id, text=str(obj), html=html))
    s = json.dumps(data)

    return HttpResponse(s)
Esempio n. 3
0
 def add(self,
         queryset,
         start_field,
         end_field=None,
         color='',
         action_names=[]):
     item = dict(queryset=dumps_qs_query(queryset),
                 start_field=start_field,
                 end_field=end_field,
                 color=color,
                 action_names=','.join(action_names))
     self.items.append(item)
     label = get_metadata(queryset.model, 'verbose_name')
     app_label = get_metadata(queryset.model, 'app_label')
     model_name = queryset.model.__name__.lower()
     url = '/add/%s/%s/?%s=' % (app_label, model_name, start_field)
     link = dict(label=label, url=url)
     self.links.append(link)
Esempio n. 4
0
    def render(self, name, value, attrs=None):
        attrs['class'] = 'form-control'
        attrs['data-placeholder'] = u' '
        queryset = None
        templates = []
        if hasattr(self.choices, 'queryset'):
            queryset = self.choices.queryset.all()
            if self.lazy:
                self.choices.queryset = self.choices.queryset.model.objects.filter(
                    pk__in=value or [])
            else:
                select_template = get_metadata(queryset.model,
                                               'select_template')
                select_display = get_metadata(queryset.model, 'select_display')
                if select_template or select_display:
                    templates_var_name = name.replace('-', '_')
                    templates.append(
                        'templateResult: function (item) {%s_templates = Array();'
                        % templates_var_name)
                    if hasattr(self.choices.queryset.model,
                               'get_tree_index_field'):
                        tree_index_field = self.choices.queryset.model.get_tree_index_field(
                        )
                        if tree_index_field:
                            self.choices.queryset = self.choices.queryset.order_by(
                                tree_index_field.name)
                    for obj in self.choices.queryset.all():
                        obj_html = render_to_string(
                            select_template or 'select_template.html',
                            dict(
                                obj=obj,
                                select_display=select_display)) or unicode(obj)
                        templates.append('%s_templates[%s] = \'%s\';' %
                                         (templates_var_name, obj.pk,
                                          obj_html.replace('\n', '')))
                    templates.append('return %s_templates[item.id];},' %
                                     templates_var_name)
        html = super(SelectMultipleWidget, self).render(name, value, attrs)
        links = []
        if queryset:
            models = queryset.model.__subclasses__() or [queryset.model]
            if hasattr(self, 'user'):
                for tmp in models:
                    class_name = tmp.__name__.lower()
                    app_label = get_metadata(tmp, 'app_label')
                    perm = u'%s.add_%s' % (app_label, class_name)
                    if self.user.has_perm(perm):
                        links.append(ADD_LINK %
                                     (name, app_label, class_name, name,
                                      get_metadata(tmp, 'verbose_name')))

        function_name = name.replace('-', '__')
        if queryset.model and self.lazy:
            app_label = get_metadata(queryset.model, 'app_label')
            model_name = queryset.model.__name__.lower()
            qs_dump = dumps_qs_query(queryset)
            html = AJAX_INIT_SCRIPT % dict(html=html,
                                           name=name,
                                           function_name=function_name,
                                           qs_dump=qs_dump,
                                           app_label=app_label,
                                           model_name=model_name,
                                           links=''.join(links))
        else:
            html = INIT_SCRIPT % dict(html=html,
                                      name=name,
                                      function_name=function_name,
                                      links=''.join(links),
                                      templates=''.join(templates))

        if queryset.model:
            l = []
            if value:
                for pk in value:
                    l.append(str(pk))
            else:
                l.append('0')
            lazy = self.lazy and 1 or 0
            for field_name, lookup in self.form_filters:
                app_label = get_metadata(queryset.model, 'app_label')
                model_name = queryset.model.__name__.lower()
                value = '_'.join(l)
                function_name = name.replace('-', '__')
                reload_script = RELOAD_SCRIPT % dict(
                    function_name=function_name,
                    field_name=field_name,
                    app_label=app_label,
                    model_name=model_name,
                    value=value,
                    lookup=lookup,
                    lazy=lazy,
                    name=name)
                html = '%s %s' % (html, reload_script)

        return mark_safe(html)
Esempio n. 5
0
    def render(self, name, value, attrs=None, renderer=None):

        if test.CACHE['HEADLESS']:
            self.lazy = False

        attrs['class'] = 'form-control'
        attrs['data-placeholder'] = ' '
        queryset = None
        templates = []
        if hasattr(self.choices, 'queryset'):
            queryset = self.choices.queryset.all()
            if self.lazy:
                self.choices.queryset = self.choices.queryset.model.objects.filter(pk__in=value or [])
            else:
                select_template = get_metadata(queryset.model, 'select_template')
                select_display = get_metadata(queryset.model, 'select_display')
                if select_template or select_display:
                    templates_var_name = name.replace('-', '_')
                    templates.append('templateResult: function (item) {{{}_templates = Array();'.format(templates_var_name))
                    if hasattr(self.choices.queryset.model, 'get_tree_index_field'):
                        tree_index_field = self.choices.queryset.model.get_tree_index_field()
                        if tree_index_field:
                            self.choices.queryset = self.choices.queryset.order_by(tree_index_field.name)
                    for obj in self.choices.queryset.all():
                        obj_html = render_to_string(select_template or 'select_template.html', dict(obj=obj, select_display=select_display)) or str(obj)
                        templates.append('{}_templates[{}] = \'{}\';'.format(templates_var_name, obj.pk, obj_html.replace('\n', '').replace('\'', '\\\'')))
                    templates.append('return {}_templates[item.id];}},'.format(templates_var_name))
        html = super(SelectMultipleWidget, self).render(name, value, attrs)
        links = []
        if queryset:
            models = queryset.model.__subclasses__() or [queryset.model]
            if hasattr(self, 'user'):
                for tmp in models:
                    class_name = tmp.__name__.lower()
                    app_label = get_metadata(tmp, 'app_label')
                    perm = '{}.add_{}'.format(app_label, class_name)
                    if self.user.has_perm(perm):
                        links.append(ADD_LINK.format(name, app_label, class_name, name, 'relation_name', 'relation_pk', get_metadata(tmp, 'verbose_name')))

        function_name = name.replace('-', '__')
        if queryset.model and self.lazy:
            app_label = get_metadata(queryset.model, 'app_label')
            model_name = queryset.model.__name__.lower()
            qs_dump = dumps_qs_query(queryset)
            html = AJAX_INIT_SCRIPT.format(html=html, name=name, function_name=function_name, qs_dump=qs_dump, app_label=app_label, model_name=model_name, links=''.join(links), minimum_input_length=self.minimum_input_length)
        else:
            html = INIT_SCRIPT.format(html=html, name=name, function_name=function_name, links=''.join(links), templates=''.join(templates))

        if queryset.model:
            l = []
            if value:
                for pk in value:
                    l.append(str(pk))
            else:
                l.append('0')
            lazy = self.lazy and 1 or 0
            for field_name, lookup in self.form_filters:
                app_label = get_metadata(queryset.model, 'app_label')
                model_name = queryset.model.__name__.lower()
                value = '_'.join(l)
                function_name = name.replace('-', '__')
                popup = 'popup' in function_name and 'popup-' or ''
                reload_script = RELOAD_SCRIPT.format(function_name=function_name, field_name=field_name, popup=popup,
                    app_label=app_label, model_name=model_name, value=value, lookup=lookup, lazy=lazy, name=name, links=''.join(links))
                html = '{} {}'.format(html, reload_script)

        return mark_safe(html)
Esempio n. 6
0
    def render(self, name, value, attrs=None):

        if 'HEADLESS' in os.environ:
            self.lazy = False

        attrs['class'] = 'form-control'
        if 'data-placeholder' not in self.attrs:
            attrs['data-placeholder'] = ' '
        queryset = None
        if self.lazy and hasattr(self.choices, 'queryset'):
            queryset = self.choices.queryset
            self.choices.queryset = self.choices.queryset.model.objects.filter(
                pk=value or 0)

        model = None
        links = []
        templates = []
        if hasattr(self.choices, 'queryset'):
            model = self.choices.queryset.model
            models = model.__subclasses__() or [model]

            if not self.lazy:
                select_template = get_metadata(model, 'select_template')
                select_display = get_metadata(model, 'select_display')
                if select_template or select_display:
                    templates_var_name = name.replace('-', '_')
                    templates.append(
                        'templateResult: function (item) {{{}_templates = Array();'
                        .format(templates_var_name))
                    if hasattr(self.choices.queryset.model,
                               'get_tree_index_field'):
                        tree_index_field = self.choices.queryset.model.get_tree_index_field(
                        )
                        if tree_index_field:
                            self.choices.queryset = self.choices.queryset.order_by(
                                tree_index_field.name)
                    for obj in self.choices.queryset:
                        obj_html = render_to_string(
                            select_template or 'select_template.html',
                            dict(obj=obj,
                                 select_display=select_display)) or str(obj)
                        templates.append('{}_templates[{}] = \'{}\';'.format(
                            templates_var_name, obj.pk,
                            obj_html.replace('\n', '')))
                    templates.append('return {}_templates[item.id];}},'.format(
                        templates_var_name))

            if hasattr(self, 'user'):
                for tmp in models:
                    class_name = tmp.__name__.lower()
                    app_label = get_metadata(tmp, 'app_label')
                    perm = '{}.add_{}'.format(app_label, class_name)
                    if self.user.has_perm(perm):
                        links.append(
                            ADD_LINK.format(name, app_label, class_name, name,
                                            get_metadata(tmp, 'verbose_name')))

        html = super(SelectWidget, self).render(name, value, attrs)
        html = html.replace('---------', '')
        function_name = name.replace('-', '__')
        if model and self.lazy:
            app_label = get_metadata(model, 'app_label')
            model_name = model.__name__.lower()
            qs_dump = dumps_qs_query(queryset)
            html = AJAX_INIT_SCRIPT.format(
                html=html,
                name=name,
                function_name=function_name,
                qs_dump=qs_dump,
                app_label=app_label,
                model_name=model_name,
                links=''.join(links),
                minimum_input_length=self.minimum_input_length)
        else:
            html = INIT_SCRIPT.format(html=html,
                                      name=name,
                                      function_name=function_name,
                                      links=''.join(links),
                                      templates=''.join(templates))

        if model:
            value = value or 0
            lazy = self.lazy and 1 or 0
            for field_name, lookup in self.form_filters:
                if '-' in name:
                    field_name = '{}-{}'.format(name.split('-')[0], field_name)
                app_label = get_metadata(model, 'app_label')
                model_name = model.__name__.lower()
                function_name = name.replace('-', '__')
                reload_script = RELOAD_SCRIPT.format(
                    function_name=function_name,
                    field_name=field_name,
                    app_label=app_label,
                    model_name=model_name,
                    value=value,
                    lookup=lookup,
                    lazy=lazy,
                    name=name)
                html = '{} {}'.format(html, reload_script)
        return mark_safe(html)