Ejemplo n.º 1
0
class EditDynamicQuotaForm(GroupForm):
    def __init__(self, app, name, task):
        super(EditDynamicQuotaForm, self).__init__(app, name, task)

        self.field_tmpl = WidgetTemplate(self, "field_html")
        self.unclaimed_tmpl = WidgetTemplate(self, "unclaimed_html")

        quota = Parameter(app, "quota")
        self.add_parameter(quota)

        self.quotas = ListParameter(app, "quotas", quota)
        self.add_parameter(self.quotas)

        self.group_leader = Parameter(app, "group_leader")
        self.add_parameter(self.group_leader)

        self.chart = PriorityPieChart \
            (app, "chart", self.object, self.group_helper, self.group_leader)
        self.add_child(self.chart)

    def render_title(self, session):
        return "Edit Dynamic Group Quota"

    def render_submit_content(self, session):
        return "Change percentage"

    def render_form_class(self, session):
        return "priorityForm"

    def render_data_col_header(self, session):
        return "Percent"
    
    def render_all_group_values(self, session):
        group_leader = self.group_leader.get(session)
        groups = self.group_helper.get_siblings(session, group_leader)
        info = self.group_helper.get_config_for_groups(session, "GROUP_QUOTA_DYNAMIC", groups)
        values = []
        dynamic_groups = []
        try:
            dynamic_groups = ["%s" % x for x in groups if info[x]["GROUP_QUOTA_DYNAMIC"] != ""]
        except:
            log.error("Error when loading dynamic group values for quota editing.")
        for group in sorted(groups):
            if(group in dynamic_groups):
                values.append(self.render_quota_value(session, group))      
        
       # values.reverse()
        values.append(self.render_unclaimed_value(session, "Unclaimed"))
                
        return values

    def render_all_group_names(self, session):
        group_leader = self.group_leader.get(session)
        groups = self.group_helper.get_siblings(session, group_leader)
        info = self.group_helper.get_config_for_groups(session, "GROUP_QUOTA_DYNAMIC", groups)
        values = []
        dynamic_groups = []
        try:
            dynamic_groups = ["%s" % x for x in groups if info[x]["GROUP_QUOTA_DYNAMIC"] != ""]
        except:
            log.error("Error when loading dynamic group names for quota editing.")
        for group in sorted(groups):
            if(group in dynamic_groups):
                values.append(group)      
        
        values.append("Unclaimed")
                
        return values
    
    def render_groups(self, session):
        writer = Writer()
                
        group_leader = self.group_leader.get(session)
        groups = self.group_helper.get_siblings(session, group_leader)
        info = self.group_helper.get_config_for_groups(session, "GROUP_QUOTA_DYNAMIC", groups)
        dynamic_groups = []
        try:
            dynamic_groups = ["%s" % x for x in groups if info[x]["GROUP_QUOTA_DYNAMIC"] != ""]
        except:
            log.error("Error when loading dynamic groups for quota editing.")
        for group in sorted(groups):
            if(group in dynamic_groups):
                self.field_tmpl.render(writer, session, group)

        self.unclaimed_tmpl.render(writer, session, "Unclaimed")

        return writer.to_string()

    def render_quota_name(self, session, group):
        return self.quotas.path

    def render_quota_value(self, session, group):
        value = self.group_helper.get_config_value(session, group, "GROUP_QUOTA_DYNAMIC")
        try:
            return round(float(value) * 100.0, 2)
        except:
            return 0.0

    def render_unclaimed_value(self, session, group):
        group_leader = self.group_leader.get(session)
        groups = self.group_helper.get_siblings(session, group_leader)
        unclaimed = self.group_helper.get_unclaimed_dyn_quota \
            (session, groups)
        return round(float(unclaimed) * 100.0, 2)

    def render_chart_id(self, session):
        return self.chart.render_id(session)

    def _invoke_tasks(self, session):
        negotiator = self.negotiator.get(session)
        quotas = self.quotas.get(session)
        group_names = self.group_names.get(session)
        original_values = self.original_values.get(session)
        info = self.group_helper.get_config_info(session)

        changed = False
        for group, new_value, original_value in zip(group_names, quotas, original_values):
            if group == "Unclaimed":
                continue
            quota = self.check_quota(new_value, original_value)
            if quota:
                self.task.invoke(session, negotiator,
                                 "GROUP_QUOTA_DYNAMIC_" + group, quota)
                info[group]["GROUP_QUOTA_DYNAMIC"] = quota
                changed = True

        if changed:
            self.task.reconfig(negotiator)
            self.app.model.update_negotiator_config_value(negotiator)

    def process_submit(self, session):
        call_async(None, self._invoke_tasks, session)
        self.task.exit_with_redirect(session)

    def check_quota(self, quota, original):
        try:
            pri = float(quota)
        except:
            return None
        if pri < 0.0 or pri > 100.0:
            return '0'

        try:
            original = float(original)
        except:
            original = 0
        if pri == original:
            return None

        return str(pri / 100)
Ejemplo n.º 2
0
class ExchangeKeysField(FormField):
    def __init__(self, app, name, exchange):
        super(ExchangeKeysField, self).__init__(app, name)

        self.title = "Initial bindings"
        self.exchange = exchange

        name = StringParameter(app, "name")
        self.names = ListParameter(app, "names", name)
        self.add_parameter(self.names)

        value = StringParameter(app, "value")
        self.values = ListParameter(app, "values", value)
        self.add_parameter(self.values)

        self.count = IntegerParameter(app, "count")
        self.count.default = 3
        self.add_parameter(self.count)

        self.inputs_container_tmpl = WidgetTemplate(self, "input_container_html")
        self.inputs_tmpl = WidgetTemplate(self, "inputs_html")

    def init(self):
        """ we added parameters directly to the FormField instead
            of adding FormInputs. XXX should this logic be moved up to FormField? """
        super(ExchangeKeysField, self).init()
        for param in self.parameters:
            self.form.form_params.add(param)

    def render_title(self, session):
        return self.title

    def render_input_fields(self, session, *args):
        count = self.count.get(session)
        writer = Writer()
        for i in range(count):
            self.inputs_tmpl.render(writer, session, i)
        return writer.to_string()

    def render_inputs(self, session, *args):
        writer = Writer()
        self.inputs_container_tmpl.render(writer, session, *args)
        return writer.to_string()

    def render_n_name(self, session, i):
        return self.names.path

    def render_v_name(self, session, i):
        return self.values.path

    def render_n_value(self, session, i):
        names = self.names.get(session)
        return len(names) > i and xml_escape(names[i]) or ""

    def render_v_value(self, session, i):
        values = self.values.get(session)
        return len(values) > i and xml_escape(values[i]) or ""

    def get_exchange(self, session):
        exchange_string = self.exchange.get(session)
        if exchange_string:
            if exchange_string == "Default exchange":
                exchange_string = ""
            obj = self.form.object.get(session)
            cls = self.app.model.org_apache_qpid_broker.Exchange
            vhostid = obj._class._name == "Vhost" and obj._id or obj._vhostRef_id
            exchanges = cls.get_selection(session.cursor, name=exchange_string, _vhostRef_id=vhostid)
            if len(exchanges):
                return exchanges[0]
        return None

    def get(self, session):
        ret_dict = dict()
        exchange = self.get_exchange(session)
        if exchange:
            if exchange.type == "headers" or exchange.type == "xml":
                names = self.names.get(session)
                values = self.values.get(session)

                for name, value in zip(names, values):
                    if name:
                        ret_dict[name] = value
        return ret_dict

    def validate(self, session):
        exchange = self.get_exchange(session)
        names = self.names.get(session)
        values = self.values.get(session)

        if exchange:
            if exchange.type == "headers":
                if not "x-match" in names:
                    error = FormError("x-match argument is required for this exchange")
                    self.form.errors.add(session, error)

                for i in range(len(names)):
                    if names[i]:
                        if names[i] == "x-match":
                            if not values[i] == "all" and not values[i] == "any":
                                error = FormError("Argument name x-match must have a value of <i>all</i> or <i>any</i>")
                                self.form.errors.add(session, error)
                        else:
                            if not values[i]:
                                error = FormError("Missing argument value for name: %s" % names[i])
                                self.form.errors.add(session, error)

                for i in range(len(values)):
                    if values[i]:
                        if not names[i]:
                            error = FormError("Missing argument name for value: %s" % values[i])
                            self.form.errors.add(session, error)

            elif exchange.type == "xml":
                if not "xquery" in names:
                    error = FormError("xquery argument is required for this exchange")
                    self.form.errors.add(session, error)
Ejemplo n.º 3
0
class PieStatSet(DefinitionSet):
    radius = 60
    def __init__(self, app, name, object, title="stats"):
        super(PieStatSet, self).__init__(app, name, object, title)

        self.color_scheme = PieChartPage.RAINBOW

        # determines if a line is drawn under the legend
        self.has_total = False
        self.has_pie_chart = True

        # if this gets defined, this widget will have a link to display
        # the chart in a popup
        self.chart_link = None
        self.popup_chart = self.DefinitionSetPopup(app, "popup_chart")
        self.add_child(self.popup_chart)
        
        self.pie_width = "150px"
        self.pie_height = "150px"

        self.pie_tmpl = WidgetTemplate(self, "pie_html")

    def do_render(self, session):
        # don't show this if our object isn't there
        # Some of our children depend on an object, and some
        # have overloaded do_get_items to read different data,
        # so if self.object has been set to None skip the check.
        if self.object:
            if not self.object.get(session):
                return
        return super(PieStatSet, self).do_render(session)

    # we generate the style sheet for the legend based on
    # the colors defined in the PieChartPage
    def render_legend_styles(self, session):
        colors = PieChartPage.color_schemes[self.color_scheme]
        styles = list()
        for color in colors:
            hx = rgb_to_string(*color)
            styles.append(".DefinitionSet dt.legend%s span { background-color: #%s;}" % (hx, hx))
        return "\n".join(styles)

    # don't put a line under the legend if there is a total
    def render_class(self, session):
        cls = super(PieStatSet, self).render_class(session)
        return "%s%s" % (cls, self.has_total and " hastotal" or "")

    def is_total(self, item):
        return False

    # the color block next to each legend line is based on the class 
    def render_legend_class(self, session, item):
        cls = ""
        if self.is_total(item):
            cls = "total"
        else:
            cls = self.get_legend_class(session, item)
        return cls

    def render_dd_class(self, session, item):
        dt_class = self.render_legend_class(session, item)
        return "total" is dt_class and " class=\"total\"" or ""

    def render_popup_chart(self, session):
        if self.chart_link:
            return self.popup_chart.render(session)

    def render_chart(self, session):
        if self.has_pie_chart:
            writer = Writer()
            self.pie_tmpl.render(writer, session)

            return writer.to_string()

    def render_pie_src(self, session):
        slices = self.get_pie_slices(session)
        return PieChartPage.get_href(slices, self.color_scheme, self.radius)
    
    def render_pie_slices(self, session):
        items = self.do_get_items(session)
        return [int(self.get_item_value(session, x)) or 0 for x in items if not self.is_total(x)]

    def render_width(self, session):
        return self.radius * 2

    def render_height(self, session):
        return self.render_width(session)

    def get_pie_slices(self, session):
        # don't include total lines, convert None to 0
        items = self.do_get_items(session)
        return [self.get_item_value(session, x) or 0 for x in items if not self.is_total(x)]
    
    def render_id_nodots(self, session):
        return self.render_id(session).replace(".", "_")
    
    def render_pie_width(self, session):
        if self.has_pie_chart:
            return self.pie_width
        else:
            return "0px"
    
    def render_pie_height(self, session):
        if self.has_pie_chart:
            return self.pie_height
        else:
            return "0px"
        
    def render_colors(self, session):
        colors = PieChartPage.color_schemes[self.color_scheme]
        color_list = ["#%s" % rgb_to_string(*color) for color in colors]
        return color_list       

    class DefinitionSetPopup(Widget):
        def render_chart_href(self, session):
            return self.page.get_popup_url(session, self.parent.chart_link)

        def render_title(self, session):
            return self.parent.render_title(session)
Ejemplo n.º 4
0
class ExchangeInput(Widget):
    def __init__(self, app, name):
        super(ExchangeInput, self).__init__(app, name)

        self.exchange = None
        self.instance_data = None

        self.name_tmpl = WidgetTemplate(self, "name_html")
        self.key_tmpl = WidgetTemplate(self, "key_html")

        self.form = None

    def init(self):
        super(ExchangeInput, self).init()

        for anc in self.ancestors:
            if isinstance(anc, Form):
                self.form = anc

    def get_exchange_info(self, session, exchange):
        binding_info = self.form.bindings.dict_param.get(session)
        if str(exchange._id) in binding_info:
            return binding_info[str(exchange.id)]

    def get_exchange_info_for(self, session, exchange, key):
        exchange_info = self.get_exchange_info(session, exchange)
        if exchange_info:
            if key in exchange_info:
                return exchange_info[key]

    def render_exchange_name(self, session, exchange):
        return exchange.name

    def render_exchange_fmt_name(self, session, exchange):
        return fmt_shorten(exchange.name)

    def render_name_path(self, session, *args):
        return DictParameter.sep().join((self.instance_data, "name"))

    def render_exchange_type(self, session, exchange):
        return exchange.type

    def render_exchange_type_path(self, session, exchange):
        return DictParameter.sep().join((self.instance_data, "type"))

    def render_exchange_id(self, session, exchange):
        return exchange._id

    def render_exchange_checked(self, session, exchange):
        exchange_info = self.get_exchange_info(session, exchange)
        if exchange_info:
            if "name" in exchange_info:
                return "checked=\"checked\""

    def render_exchange_name_input(self, session, exchange):
        writer = Writer()
        self.name_tmpl.render(writer, session, exchange)
        return writer.to_string()

    def render_exchange_key_input(self, session, exchange):
        writer = Writer()
        self.key_tmpl.render(writer, session, exchange)
        return writer.to_string()

    def render_onclick(self, session, exchange):
        pass

    def render_list_error(self, session, exchange):
        errors = self.parent.binding_errors.get(session)
        if exchange.name in errors:
            return "<ul class=\"errors\" style=\"margin:0; float:left;\"><li>%s</li></ul>" % \
                "</li><li>".join(errors[exchange.name])

    def render_dict_error(self, session, exchange, key):
        errors = self.parent.binding_errors.get(session)
        if exchange.name in errors:
            exchange_errors = errors[exchange.name]
            if key in exchange_errors:
                return "<ul class=\"errors\" style=\"margin:0; float:left;\"><li>%s</li></ul>" % \
                "</li><li>".join(exchange_errors[key])

    def set_instance_data(self, exchange, dict_key):
        self.exchange = exchange
        self.instance_data = dict_key