Example #1
0
class ViewGraph(object):
    def __init__(self, model, axis, fields, axis_data={}, attrs={}):
        self.widget = gtk.HBox()
        self._figure = Figure(figsize=(800, 600), dpi=100, facecolor="w")
        self._subplot = self._figure.add_subplot(111, axisbg="#eeeeee")
        self._canvas = FigureCanvas(self._figure)
        self.widget.pack_start(self._canvas, expand=True, fill=True)

        if attrs.get("type", "pie") == "bar":
            if attrs.get("orientation", "vertical") == "vertical":
                self._figure.subplots_adjust(left=0.08, right=0.98, bottom=0.25, top=0.98)
            else:
                self._figure.subplots_adjust(left=0.20, right=0.97, bottom=0.07, top=0.98)
        else:
            self._figure.subplots_adjust(left=0.03, right=0.97, bottom=0.03, top=0.97)

        self.fields = fields
        self.model = model
        self.axis = axis
        self.old_axis = axis
        self.editable = False
        self.key = False
        self.widget.editable = False
        self.axis_data = axis_data
        self.axis_group = {}
        for i in self.axis_data:
            self.axis_data[i]["string"] = self.fields[i]["string"]
            self.axis_data[i]["type"] = self.fields[i]["type"]
            if self.axis_data[i].get("group", False):
                self.axis_group[i] = 1
                self.axis.remove(i)
        self.attrs = attrs

    def display(self, models):
        datas = []
        self.axis = copy.copy(self.old_axis)
        group_by = self.widget.screen.context.get("group_by", False)
        if group_by:
            if not self.key:
                del self.widget.screen.context["group_by"]
                self.key = True
                self.widget.screen.search_filter()
                models = self.widget.screen.models
                self.widget.screen.context["group_by"] = group_by
            self.axis[0] = group_by[0]
            self.axis_data[group_by[0]] = {}
            # This is to get the missing field. if the field is not available in the graph view
            # for use case :graph view loaded directly from a dashboard and user executes groupby
            if self.axis[0] not in models.mfields:
                missing_gb_field = rpc.session.rpc_exec_auth(
                    "/object", "execute", self.model, "fields_get", [self.axis[0]], {}
                )
                if missing_gb_field:
                    models.add_fields(missing_gb_field, models)

        for m in models:
            res = {}
            for x in self.axis_data.keys():
                if not self.axis_data[x]:
                    self.axis_data[x] = self.fields[x]
                field_val = m[x].get_client(m)
                if self.fields[x]["type"] in ("many2one", "char", "time", "text"):
                    res[x] = field_val and str(field_val) or "Undefined"
                elif self.fields[x]["type"] == "selection":
                    selection = dict(m[x].attrs["selection"])
                    if field_val:
                        val = str(field_val)
                        res[x] = selection.get(val, val)
                    else:
                        res[x] = "Undefined"
                elif self.fields[x]["type"] == "date":
                    if field_val:
                        res[x] = datetime_util.server_to_local_timestamp(
                            field_val, DT_FORMAT, user_locale_format.get_date_format(), tz_offset=False
                        )
                    else:
                        res[x] = "Undefined"
                elif self.fields[x]["type"] == "datetime":
                    if field_val:
                        res[x] = datetime_util.server_to_local_timestamp(
                            field_val, DHM_FORMAT, user_locale_format.get_datetime_format(True)
                        )
                    else:
                        res[x] = "Undefined"
                else:
                    res[x] = field_val and float(field_val) or 0.0
            datas.append(res)
        tinygraph.tinygraph(
            self._subplot,
            self.attrs.get("type", "pie"),
            self.axis,
            self.axis_data,
            datas,
            axis_group_field=self.axis_group,
            orientation=self.attrs.get("orientation", "vertical"),
        )
        # the draw function may generate exception but it is not a problem as it will be redraw latter
        try:
            self._subplot.draw(None)
            # XXX it must have some better way to force the redraw but this one works
            self._canvas.queue_resize()
        except:
            pass

    def destroy(self):
        pass
Example #2
0
class ViewGraph(object):
    def __init__(self, model, axis, fields, axis_data={}, attrs={}):
        self.widget = gtk.HBox()
        self._figure = Figure(figsize=(800, 600), dpi=100, facecolor='w')
        self._subplot = self._figure.add_subplot(111, axisbg='#eeeeee')
        self._canvas = FigureCanvas(self._figure)
        self.widget.pack_start(self._canvas, expand=True, fill=True)

        if attrs.get('type', 'pie') == 'bar':
            if attrs.get('orientation', 'vertical') == 'vertical':
                self._figure.subplots_adjust(left=0.08,
                                             right=0.98,
                                             bottom=0.25,
                                             top=0.98)
            else:
                self._figure.subplots_adjust(left=0.20,
                                             right=0.97,
                                             bottom=0.07,
                                             top=0.98)
        else:
            self._figure.subplots_adjust(left=0.03,
                                         right=0.97,
                                         bottom=0.03,
                                         top=0.97)

        self.fields = fields
        self.model = model
        self.axis = axis
        self.old_axis = axis
        self.editable = False
        self.key = False
        self.widget.editable = False
        self.axis_data = axis_data
        self.axis_group = {}
        for i in self.axis_data:
            self.axis_data[i]['string'] = self.fields[i]['string']
            self.axis_data[i]['type'] = self.fields[i]['type']
            if self.axis_data[i].get('group', False):
                self.axis_group[i] = 1
                self.axis.remove(i)
        self.attrs = attrs

    def display(self, models):
        datas = []
        self.axis = copy.copy(self.old_axis)
        group_by = self.widget.screen.context.get('group_by', False)
        if group_by:
            if not self.key:
                del self.widget.screen.context['group_by']
                self.key = True
                self.widget.screen.search_filter()
                models = self.widget.screen.models
                self.widget.screen.context['group_by'] = group_by
            self.axis[0] = group_by[0]
            self.axis_data[group_by[0]] = {}
            # This is to get the missing field. if the field is not available in the graph view
            # for use case :graph view loaded directly from a dashboard and user executes groupby
            if self.axis[0] not in models.mfields:
                missing_gb_field = rpc.session.rpc_exec_auth(
                    '/object', 'execute', self.model, 'fields_get',
                    [self.axis[0]], {})
                if missing_gb_field:
                    models.add_fields(missing_gb_field, models)

        for m in models:
            res = {}
            for x in self.axis_data.keys():
                if not self.axis_data[x]:
                    self.axis_data[x] = self.fields[x]
                field_val = m[x].get_client(m)
                if self.fields[x]['type'] in ('many2one', 'char', 'time',
                                              'text'):
                    res[x] = field_val and str(field_val) or 'Undefined'
                elif self.fields[x]['type'] == 'selection':
                    selection = dict(m[x].attrs['selection'])
                    if field_val:
                        val = str(field_val)
                        res[x] = selection.get(val, val)
                    else:
                        res[x] = 'Undefined'
                elif self.fields[x]['type'] == 'date':
                    if field_val:
                        res[x] = datetime_util.server_to_local_timestamp(
                            field_val,
                            DT_FORMAT,
                            user_locale_format.get_date_format(),
                            tz_offset=False)
                    else:
                        res[x] = 'Undefined'
                elif self.fields[x]['type'] == 'datetime':
                    if field_val:
                        res[x] = datetime_util.server_to_local_timestamp(
                            field_val, DHM_FORMAT,
                            user_locale_format.get_datetime_format(True))
                    else:
                        res[x] = 'Undefined'
                else:
                    res[x] = field_val and float(field_val) or 0.0
            datas.append(res)
        tinygraph.tinygraph(self._subplot,
                            self.attrs.get('type', 'pie'),
                            self.axis,
                            self.axis_data,
                            datas,
                            axis_group_field=self.axis_group,
                            orientation=self.attrs.get('orientation',
                                                       'vertical'))
        # the draw function may generate exception but it is not a problem as it will be redraw latter
        try:
            self._subplot.draw(None)
            #XXX it must have some better way to force the redraw but this one works
            self._canvas.queue_resize()
        except:
            pass

    def destroy(self):
        pass
Example #3
0
class ViewGraph(object):
    def __init__(self, model, axis, fields, axis_data={}, attrs={}):
        self.widget = gtk.HBox()
        self._figure = Figure(figsize=(800, 600), dpi=100, facecolor='w')
        self._subplot = self._figure.add_subplot(111, axisbg='#eeeeee')
        self._canvas = FigureCanvas(self._figure)
        self.widget.pack_start(self._canvas, expand=True, fill=True)

        if attrs.get('type', 'pie') == 'bar':
            if attrs.get('orientation', 'vertical') == 'vertical':
                self._figure.subplots_adjust(left=0.08,
                                             right=0.98,
                                             bottom=0.25,
                                             top=0.98)
            else:
                self._figure.subplots_adjust(left=0.20,
                                             right=0.97,
                                             bottom=0.07,
                                             top=0.98)
        else:
            self._figure.subplots_adjust(left=0.03,
                                         right=0.97,
                                         bottom=0.03,
                                         top=0.97)

        self.fields = fields
        self.model = model
        self.axis = axis
        self.editable = False
        self.widget.editable = False
        self.axis_data = axis_data
        self.axis_group = {}
        for i in self.axis_data:
            self.axis_data[i]['string'] = self.fields[i]['string']
            if self.axis_data[i].get('group', False):
                self.axis_group[i] = 1
                self.axis.remove(i)
        self.attrs = attrs

    def display(self, models):
        datas = []
        for m in models:
            res = {}
            for x in self.axis_data.keys():
                if self.fields[x]['type'] in ('many2one', 'char', 'time',
                                              'text'):
                    res[x] = str(m[x].get_client(m))
                elif self.fields[x]['type'] == 'selection':
                    selection = dict(m[x].attrs['selection'])
                    val = str(m[x].get_client(m))
                    res[x] = selection.get(val, val)
                elif self.fields[x]['type'] == 'date':
                    if m[x].get_client(m):
                        date = time.strptime(m[x].get_client(m), DT_FORMAT)
                        res[x] = time.strftime(LDFMT, date)
                    else:
                        res[x] = ''
                elif self.fields[x]['type'] == 'datetime':
                    if m[x].get_client(m):
                        date = time.strptime(m[x].get_client(m), DHM_FORMAT)
                        if rpc.session.context.get('tz'):
                            try:
                                lzone = pytz.timezone(
                                    rpc.session.context['tz'])
                                szone = pytz.timezone(rpc.session.timezone)
                                dt = DT.datetime(date[0], date[1], date[2],
                                                 date[3], date[4], date[5],
                                                 date[6])
                                sdt = szone.localize(dt, is_dst=True)
                                ldt = sdt.astimezone(lzone)
                                date = ldt.timetuple()
                            except pytz.UnknownTimeZoneError:
                                # Timezones are sometimes invalid under Windows
                                # and hard to figure out, so as a low-risk fix
                                # in stable branch we will simply ignore the
                                # exception and consider client in server TZ
                                # (and sorry about the code duplication as well,
                                # this is fixed properly in trunk)
                                pass
                        res[x] = time.strftime(LDFMT + ' %H:%M:%S', date)
                    else:
                        res[x] = ''
                else:
                    res[x] = float(m[x].get_client(m))
            datas.append(res)
        tinygraph.tinygraph(self._subplot,
                            self.attrs.get('type', 'pie'),
                            self.axis,
                            self.axis_data,
                            datas,
                            axis_group_field=self.axis_group,
                            orientation=self.attrs.get('orientation',
                                                       'vertical'))
        # the draw function may generate exception but it is not a problem as it will be redraw latter
        try:
            self._subplot.draw()
            #XXX it must have some better way to force the redraw but this one works
            self._canvas.queue_resize()
        except:
            pass
Example #4
0
class ViewGraph(object):
    def __init__(self, model, axis, fields, axis_data={}, attrs={}):
        self.widget = gtk.HBox()
        self._figure = Figure(figsize=(800,600), dpi=100, facecolor='w')
        self._subplot = self._figure.add_subplot(111,axisbg='#eeeeee')
        self._canvas = FigureCanvas(self._figure)
        self.widget.pack_start(self._canvas, expand=True, fill=True)

        if attrs.get('type', 'pie')=='bar':
            if attrs.get('orientation', 'vertical')=='vertical':
                self._figure.subplots_adjust(left=0.08,right=0.98,bottom=0.25,top=0.98)
            else:
                self._figure.subplots_adjust(left=0.20,right=0.97,bottom=0.07,top=0.98)
        else:
            self._figure.subplots_adjust(left=0.03,right=0.97,bottom=0.03,top=0.97)

        self.fields = fields
        self.model = model
        self.axis = axis
        self.editable = False
        self.widget.editable = False
        self.axis_data = axis_data
        self.axis_group = {}
        for i in self.axis_data:
            self.axis_data[i]['string'] = self.fields[i]['string']
            if self.axis_data[i].get('group', False):
                self.axis_group[i]=1
                self.axis.remove(i)
        self.attrs = attrs

    def display(self, models):
        datas = []
        for m in models:
            res = {}
            for x in self.axis_data.keys():
                if self.fields[x]['type'] in ('many2one', 'char','time','text'):
                    res[x] = str(m[x].get_client(m))
                elif self.fields[x]['type'] == 'selection':
                    selection = dict(m[x].attrs['selection'])
                    val = str(m[x].get_client(m))
                    res[x] = selection.get(val, val)
                elif self.fields[x]['type'] == 'date':
                    if m[x].get_client(m):
                        date = time.strptime(m[x].get_client(m), DT_FORMAT)
                        res[x] = time.strftime(LDFMT, date)
                    else:
                        res[x]=''
                elif self.fields[x]['type'] == 'datetime':
                    if m[x].get_client(m):
                        date = time.strptime(m[x].get_client(m), DHM_FORMAT)
                        if rpc.session.context.get('tz'):
                            try:
                                lzone = pytz.timezone(rpc.session.context['tz'])
                                szone = pytz.timezone(rpc.session.timezone)
                                dt = DT.datetime(date[0], date[1], date[2], date[3], date[4], date[5], date[6])
                                sdt = szone.localize(dt, is_dst=True)
                                ldt = sdt.astimezone(lzone)
                                date = ldt.timetuple()
                            except pytz.UnknownTimeZoneError:
                                # Timezones are sometimes invalid under Windows
                                # and hard to figure out, so as a low-risk fix
                                # in stable branch we will simply ignore the
                                # exception and consider client in server TZ
                                # (and sorry about the code duplication as well,
                                # this is fixed properly in trunk)
                                pass
                        res[x] = time.strftime(LDFMT + ' %H:%M:%S', date)
                    else:
                        res[x] = ''
                else:
                    res[x] = float(m[x].get_client(m))
            datas.append(res)
        tinygraph.tinygraph(self._subplot, self.attrs.get('type', 'pie'), self.axis, self.axis_data, datas, axis_group_field=self.axis_group, orientation=self.attrs.get('orientation', 'vertical'))
        # the draw function may generate exception but it is not a problem as it will be redraw latter
        try:
            self._subplot.draw()
            #XXX it must have some better way to force the redraw but this one works
            self._canvas.queue_resize()
        except:
            pass