Exemple #1
0
 def init(self):
     self.label = label_for_field(self.field_name, self.obj.__class__,
         model_admin = self.admin_view,
         return_attr = False
     )
     try:
         f, attr, value = lookup_field(self.field_name, self.obj, self.admin_view)
     except (AttributeError, ObjectDoesNotExist):
         self.text
     else:
         if f is None:
             self.allow_tags = getattr(attr, 'allow_tags', False)
             boolean = getattr(attr, 'boolean', False)
             if boolean:
                 self.allow_tags = True
                 self.text = boolean_icon(value)
             else:
                 self.text = smart_unicode(value)
         else:
             if isinstance(f.rel, models.ManyToOneRel):
                 self.text = getattr(self.obj, f.name)
             else:
                 self.text = display_for_field(value, f)
         self.field = f
         self.attr = attr
         self.value = value
Exemple #2
0
    def get(self, request, name):
        if not self.data_charts.has_key(name):
            return HttpResponseNotFound()

        self.chart = self.data_charts[name]

        self.x_field = self.chart['x-field']
        y_fields = self.chart['y-field']
        self.y_fields = (y_fields,) if type(y_fields) not in (list, tuple) else y_fields

        datas = [{"data":[], "label": label_for_field(i, self.model, model_admin=self)} for i in self.y_fields]

        self.make_result_list()
        
        for obj in self.result_list:
            xf, attrs, value = lookup_field(self.x_field, obj, self)
            for i, yfname in enumerate(self.y_fields):
                yf, yattrs, yv = lookup_field(yfname, obj, self)
                datas[i]["data"].append((value, yv))

        option = {'series': {'lines': { 'show': True }, 'points': { 'show': False }},
                'grid': { 'hoverable': True, 'clickable': True }}
        try:
            xfield = self.opts.get_field(self.x_field)
            if type(xfield) in (models.DateTimeField, models.DateField, models.TimeField):
                option['xaxis'] = { 'mode': "time", 'tickLength': 5}
                if type(xfield) is models.DateField:
                    option['xaxis']['timeformat'] = "%y/%m/%d";
                elif type(xfield) is models.TimeField:
                    option['xaxis']['timeformat'] = "%H:%M:%S";
                else:
                    option['xaxis']['timeformat'] = "%y/%m/%d %H:%M:%S";
        except Exception:
            pass
            
        option.update(self.chart.get('option', {}))

        content = {'data': datas, 'option': option}
        json = simplejson.dumps(content, cls=JSONEncoder, ensure_ascii=False)

        return HttpResponse(json)
Exemple #3
0
    def result_item(self, item, obj, field_name, row):
        if self.list_editable and item.field and item.field.editable and (field_name in self.list_editable):
            pk = getattr(obj, obj._meta.pk.attname)
            form = self._get_form_admin(obj).form_obj
            form.prefix = str(pk)

            field_label = label_for_field(field_name, obj,
                model_admin = self.admin_view,
                return_attr = False
            )
            data_attr = {
                'name': field_name,
                'action': self.admin_view.model_admin_urlname('patch', pk),
                'title': _(u"Enter %s") % field_label,
                'field': form[field_name]
            }
            item.wraps.insert(0, '<span class="editable-field">%s</span>')
            item.btns.append(loader.render_to_string('admin/blocks/editable.html', data_attr))

            if not self.editable_need_fields.has_key(field_name):
                self.editable_need_fields[field_name] = item.field
        return item
Exemple #4
0
    def result_header(self, field_name, row):
        ordering_field_columns = self.ordering_field_columns
        item = ResultHeader(field_name, row)
        text, attr = label_for_field(field_name, self.model,
            model_admin = self,
            return_attr = True
        )
        item.text = text
        item.attr = attr
        if attr and not getattr(attr, "admin_order_field", None):
            return item

        # OK, it is sortable if we got this far
        th_classes = ['sortable']
        order_type = ''
        new_order_type = 'desc'
        sort_priority = 0
        sorted = False
        # Is it currently being sorted on?
        if field_name in ordering_field_columns:
            sorted = True
            order_type = ordering_field_columns.get(field_name).lower()
            sort_priority = ordering_field_columns.keys().index(field_name) + 1
            th_classes.append('sorted %sending' % order_type)
            new_order_type = {'asc': 'desc', 'desc': 'asc'}[order_type]

        # build new ordering param
        o_list_asc = [] # URL for making this field the primary sort
        o_list_desc = [] # URL for making this field the primary sort
        o_list_remove  = [] # URL for removing this field from sort
        o_list_toggle  = [] # URL for toggling order type for this field
        make_qs_param = lambda t, n: ('-' if t == 'desc' else '') + str(n)

        for j, ot in ordering_field_columns.items():
            if j == field_name: # Same column
                param = make_qs_param(new_order_type, j)
                # We want clicking on this header to bring the ordering to the
                # front
                o_list_asc.insert(0, j)
                o_list_desc.insert(0, '-'+j)
                o_list_toggle.append(param)
                # o_list_remove - omit
            else:
                param = make_qs_param(ot, j)
                o_list_asc.append(param)
                o_list_desc.append(param)
                o_list_toggle.append(param)
                o_list_remove.append(param)

        if field_name not in ordering_field_columns:
            o_list_asc.insert(0, field_name)
            o_list_desc.insert(0, '-'+field_name)

        item.sorted = sorted
        item.sortable = True
        item.ascending = (order_type == "asc")
        item.sort_priority = sort_priority

        menus = [
            ('asc', o_list_asc, 'caret-up', _(u'Sort ASC')),
            ('desc', o_list_desc, 'caret-down', _(u'Sort DESC')),
        ]
        if sorted:
            row['num_sorted_fields'] = row['num_sorted_fields'] + 1
            menus.append((None, o_list_remove, 'remove', _(u'Cancel Sort')))
            item.btns.append('<a class="toggle" href="%s"><i class="icon-%s"></i></a>' % (
                self.get_query_string({ORDER_VAR: '.'.join(o_list_toggle)}), 'sort-up' if order_type == "asc" else 'sort-down'))

        item.menus.extend(['<li%s><a href="%s" class="active"><i class="icon-%s"></i> %s</a></li>' % \
            ((' class="active"' if sorted and order_type==i[0] else ''), \
                self.get_query_string({ORDER_VAR: '.'.join(i[1])}), i[2], i[3]) for i in menus])
        item.classes.extend(th_classes)

        return item
Exemple #5
0
    def result_header(self, field_name, row):
        ordering_field_columns = self.ordering_field_columns
        item = ResultHeader(field_name, row)
        text, attr = label_for_field(field_name, self.model, model_admin=self, return_attr=True)
        item.text = text
        item.attr = attr
        if attr and not getattr(attr, "admin_order_field", None):
            return item

        # OK, it is sortable if we got this far
        th_classes = ["sortable"]
        order_type = ""
        new_order_type = "desc"
        sort_priority = 0
        sorted = False
        # Is it currently being sorted on?
        if field_name in ordering_field_columns:
            sorted = True
            order_type = ordering_field_columns.get(field_name).lower()
            sort_priority = ordering_field_columns.keys().index(field_name) + 1
            th_classes.append("sorted %sending" % order_type)
            new_order_type = {"asc": "desc", "desc": "asc"}[order_type]

        # build new ordering param
        o_list_asc = []  # URL for making this field the primary sort
        o_list_desc = []  # URL for making this field the primary sort
        o_list_remove = []  # URL for removing this field from sort
        o_list_toggle = []  # URL for toggling order type for this field
        make_qs_param = lambda t, n: ("-" if t == "desc" else "") + str(n)

        for j, ot in ordering_field_columns.items():
            if j == field_name:  # Same column
                param = make_qs_param(new_order_type, j)
                # We want clicking on this header to bring the ordering to the
                # front
                o_list_asc.insert(0, j)
                o_list_desc.insert(0, "-" + j)
                o_list_toggle.append(param)
                # o_list_remove - omit
            else:
                param = make_qs_param(ot, j)
                o_list_asc.append(param)
                o_list_desc.append(param)
                o_list_toggle.append(param)
                o_list_remove.append(param)

        if field_name not in ordering_field_columns:
            o_list_asc.insert(0, field_name)
            o_list_desc.insert(0, "-" + field_name)

        item.sorted = sorted
        item.sortable = True
        item.ascending = order_type == "asc"
        item.sort_priority = sort_priority

        menus = [("asc", o_list_asc, "caret-up", _(u"Sort ASC")), ("desc", o_list_desc, "caret-down", _(u"Sort DESC"))]
        if sorted:
            row["num_sorted_fields"] = row["num_sorted_fields"] + 1
            menus.append((None, o_list_remove, "remove", _(u"Cancel Sort")))
            item.btns.append(
                '<a class="toggle" href="%s"><i class="icon-%s"></i></a>'
                % (
                    self.get_query_string({ORDER_VAR: ".".join(o_list_toggle)}),
                    "sort-up" if order_type == "asc" else "sort-down",
                )
            )

        item.menus.extend(
            [
                '<li%s><a href="%s" class="active"><i class="icon-%s"></i> %s</a></li>'
                % (
                    (' class="active"' if sorted and order_type == i[0] else ""),
                    self.get_query_string({ORDER_VAR: ".".join(i[1])}),
                    i[2],
                    i[3],
                )
                for i in menus
            ]
        )
        item.classes.extend(th_classes)

        return item