Ejemplo n.º 1
0
class RightManager(object):
    
    def __init__(self):
        self.right_cls = Right
        self._levels = SortedDict()
        self._categories = SortedDict()
        self._rights = SortedDict()
    
    def register_level(self, key, title, description=""):
        self._levels[key] = RightLevel(self, key, title, description)

    def register_category(self, key, title, description=""):
        self._categories[key] = RightCategory(self, key, title, description)
    
    def register(self, key, title, description="", category="", level=""):
        self._rights[key]=Right(self, key, title, description=description, category=category, level=level)
        
    def get_category(self, category_key):
        return self._categories.get(category_key, None)

    def get_level(self, level_key):
        return self._levels.get(level_key, None)
    
    def get_right(self, key):
        return self._rights.get(key)
        
    def get_rights(self):
        return [self.get_right(key) for key in self._rights.keys()]

    def get_rights_dict(self):
        return [self.get_right(key).to_dict() for key in self._rights.keys()]
    def get_data(self):
        instances = []
        try:
            instances = api.nova.server_list(self.request, all_tenants=True)
        except:
            exceptions.handle(self.request,
                              _('Unable to retrieve instance list.'))
        if instances:
            # Gather our flavors to correlate against IDs
            try:
                flavors = api.nova.flavor_list(self.request)
            except:
                flavors = []
                msg = _('Unable to retrieve instance size information.')
                exceptions.handle(self.request, msg)
            # Gather our tenants to correlate against IDs
            try:
                tenants = api.keystone.tenant_list(self.request, admin=True)
            except:
                tenants = []
                msg = _('Unable to retrieve instance tenant information.')
                exceptions.handle(self.request, msg)

            full_flavors = SortedDict([(f.id, f) for f in flavors])
            tenant_dict = SortedDict([(t.id, t) for t in tenants])
            for inst in instances:
                inst.full_flavor = full_flavors.get(inst.flavor["id"], None)
                tenant = tenant_dict.get(inst.tenant_id, None)
                inst.tenant_name = getattr(tenant, "name", None)
        return instances
Ejemplo n.º 3
0
    def fields_for_form(self, form, fields=None, exclude=None, field_callback=None):
        field_list = []
        ignored = []

        for name, f in form.base_fields.items():
            if fields is not None and not name in fields:
                continue
            if exclude and name in exclude:
                continue
    
            if field_callback is None:
                formfield = self.get_field_class(f)()
                formfield.set_name(name)
                formfield.set_source(f)
            elif not callable(field_callback):
                raise TypeError('formfield_callback must be a function or callable')
            else:
                formfield = field_callback(name, f)
    
            if formfield:
                field_list.append((name, formfield))
            else:
                ignored.append(name)
        field_dict = SortedDict(field_list)
        if fields:
            field_dict = SortedDict(
                [(f, field_dict.get(f)) for f in fields
                    if ((not exclude) or (exclude and f not in exclude)) and (f not in ignored)]
            )
        return field_dict
Ejemplo n.º 4
0
    def get_data(self):
        # Gather our volumes
        try:
            volumes = api.volume_list(self.request)
        except:
            volumes = []
            exceptions.handle(self.request,
                              _('Unable to retrieve volume list.'))
        try:
            instance_list = api.server_list(self.request)
        except:
            instance_list = []
            exceptions.handle(self.request,
                              _("Unable to retrieve volume/instance "
                                "attachment information"))

        instances = SortedDict([(inst.id, inst) for inst in instance_list])
        for volume in volumes:
            # It is possible to create a volume with no name through the
            # EC2 API, use the ID in those cases.
            if not volume.display_name:
                volume.display_name = volume.id

            for att in volume.attachments:
                server_id = att.get('server_id', None)
                att['instance'] = instances.get(server_id, None)
        return volumes
Ejemplo n.º 5
0
class AppDirectoriesFinder(BaseFinder):
    """
    A static files finder that looks in the directory of each app as
    specified in the source_dir attribute of the given storage class.
    """
    storage_class = AppStaticStorage

    def __init__(self, apps=None, *args, **kwargs):
        # The list of apps that are handled
        self.apps = []
        # Mapping of app module paths to storage instances
        self.storages = SortedDict()
        if apps is None:
            apps = settings.INSTALLED_APPS
        for app in apps:
            app_storage = self.storage_class(app)
            if os.path.isdir(app_storage.location):
                self.storages[app] = app_storage
                if app not in self.apps:
                    self.apps.append(app)
        super(AppDirectoriesFinder, self).__init__(*args, **kwargs)

    def list(self, ignore_patterns):
        """
        List all files in all app storages.
        """
        for storage in six.itervalues(self.storages):
            if storage.exists(''):  # check if storage location exists
                for path in utils.get_files(storage, ignore_patterns):
                    yield path, storage

    def find(self, path, all=False):
        """
        Looks for files in the app directories.
        """
        matches = []
        for app in self.apps:
            match = self.find_in_app(app, path)
            if match:
                if not all:
                    return match
                matches.append(match)
        return matches

    def find_in_app(self, app, path):
        """
        Find a requested static file in an app's static locations.
        """
        storage = self.storages.get(app, None)
        if storage:
            if storage.prefix:
                prefix = '%s%s' % (storage.prefix, os.sep)
                if not path.startswith(prefix):
                    return None
                path = path[len(prefix):]
            # only try to find a file if the source dir actually exists
            if storage.exists(path):
                matched_path = storage.path(path)
                if matched_path:
                    return matched_path
Ejemplo n.º 6
0
def fields_for_document(document, fields=None, exclude=None,
        formfield_callback=None):
    """
    Returns a ``SortedDict`` containing form fields for the given model.

    ``fields`` is an optional list of field names. If provided, only the named
    fields will be included in the returned fields.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded from the returned fields, even if they are listed
    in the ``fields`` argument.
    """
    field_list = []
    structure = document.structure
    for field_name, field_type in structure.items():
        if fields and not field_name in fields:
            continue
        if exclude and field_name in exclude:
            continue

        form_field = None
        if formfield_callback:
            form_field = formfield_callback(document, field_name)
        if not form_field:
            form_field = formfield_for_document_field(document, field_name)
        if form_field:
            field_list.append((field_name, form_field))

    field_dict = SortedDict(field_list)
    if fields:
        field_dict = SortedDict([(f, field_dict.get(f))
                for f in fields
                if (not exclude) or (exclude and f not in exclude)])
    return field_dict
Ejemplo n.º 7
0
    def get_volume_types_data(self):
        try:
            volume_types = cinder.volume_type_list_with_qos_associations(self.request)
        except Exception:
            volume_types = []
            exceptions.handle(self.request, _("Unable to retrieve volume types"))

        # Gather volume type encryption information
        try:
            vol_type_enc_list = cinder.volume_encryption_type_list(self.request)
        except Exception:
            vol_type_enc_list = []
            msg = _("Unable to retrieve volume type encryption information.")
            exceptions.handle(self.request, msg)

        vol_type_enc_dict = SortedDict([(e.volume_type_id, e) for e in vol_type_enc_list])
        for volume_type in volume_types:
            vol_type_enc = vol_type_enc_dict.get(volume_type.id, None)
            if vol_type_enc is not None:
                volume_type.encryption = vol_type_enc
                volume_type.encryption.name = volume_type.name
            else:
                volume_type.encryption = None

        return volume_types
Ejemplo n.º 8
0
def fields_for_app(app, fields, exclude, owner_class):
    """
    Returns a ``SortedDict`` containing form fields for the given model.

    ``fields`` is an optional list of field names. If provided, only the named
    fields will be included in the returned fields.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded from the returned fields, even if they are listed
    in the ``fields`` argument.
    """
    field_list = []
    try:
        _app = __import__(app, globals(), locals(), ['prefs'])
        app_prefs = _app.prefs
    except:
        pass
    else:
        default_prefs = getattr(app_prefs, 'for_system', {})
        if owner_class is not None:
            default_prefs.update(getattr(app_prefs, 'for_owner', {}).get(owner_class, {}))
        for name, field in default_prefs.items():
            if fields and not name in fields:
                continue
            if exclude and name in exclude:
                continue
            if field:
                field_list.append((name, field))
    field_dict = SortedDict(field_list)
    if fields:
        field_dict = SortedDict([(f, field_dict.get(f)) for f in fields if (not exclude) or (exclude and f not in exclude)])
    return field_dict
Ejemplo n.º 9
0
def fields_for_document(document, fields=None, exclude=None, widgets=None, labels=None, formfield_callback=DocumentFormField()):
    field_list = []
    ignored = []

    #TODO editable
    for f in document._fields.values():
        if fields and not f.db_field in fields:
            continue
        if exclude and f.db_field in exclude:
            continue
        if widgets and f.db_field in widgets:
            kwargs = {'widget': widgets[f.db_field]}
        else:
            kwargs = {}
        # hack for labels
        if labels and f.db_field in labels:
            kwargs['label'] = labels[f.db_field]
        formfield = formfield_callback(f, **kwargs)
        if formfield:
            field_list.append((f.db_field, formfield))
        else:
            ignored.append(f.db_field)
    
    field_dict = SortedDict(field_list)
    #why do this
    if fields:
        field_dict = SortedDict(
            [(f, field_dict.get(f)) for f in fields
                if ((not exclude) or (exclude and f not in exclude)) and (f not in ignored)]
        )
    return field_dict
Ejemplo n.º 10
0
    def list_targets(self):
        tenant_id = self.request.user.tenant_id
        ports = port_list(self.request, tenant_id=tenant_id)
        servers, has_more = nova.server_list(self.request)
        server_dict = SortedDict([(s.id, s.name) for s in servers])
        reachable_subnets = self._get_reachable_subnets(ports)
        if is_service_enabled(self.request,
                              config_name='enable_lb',
                              ext_name='lbaas'):
            # Also get the loadbalancer VIPs
            vip_dict = {v['port_id']: v['name']
                        for v in self.client.list_vips().get('vips', [])}
        else:
            vip_dict = {}

        targets = []
        for p in ports:
            # Remove network ports from Floating IP targets
            if p.device_owner.startswith('network:'):
                continue
            port_id = p.id
            server_name = server_dict.get(p.device_id) or vip_dict.get(port_id)

            for ip in p.fixed_ips:
                if ip['subnet_id'] not in reachable_subnets:
                    continue
                target = {'name': '%s: %s' % (server_name, ip['ip_address']),
                          'id': '%s_%s' % (port_id, ip['ip_address']),
                          'instance_id': p.device_id}
                targets.append(FloatingIpTarget(target))
        return targets
Ejemplo n.º 11
0
    def fields_for_model(self, model, fields=None, exclude=None, field_callback=None, need_readonly=True):
        field_list = []
        ignored = []
        opts = model._meta
        for f in opts.fields + opts.many_to_many:
            if not f.editable and not need_readonly:
                continue
            if fields is not None and not f.name in fields:
                continue
            if exclude and f.name in exclude:
                continue
    
            if field_callback is None:
                formfield = self.get_field_class(f)()
                formfield.set_name(f.name)
                formfield.set_source(f)

            elif not callable(field_callback):
                raise TypeError('formfield_callback must be a function or callable')
            else:
                formfield = field_callback(f.name, f)
    
            if formfield:
                field_list.append((f.name, formfield))
            else:
                ignored.append(f.name)
        field_dict = SortedDict(field_list)
        if fields:
            field_dict = SortedDict(
                [(f, field_dict.get(f)) for f in fields
                    if ((not exclude) or (exclude and f not in exclude)) and (f not in ignored)]
            )
        return field_dict
Ejemplo n.º 12
0
    def list_targets(self):
        tenant_id = self.request.user.tenant_id
        ports = port_list(self.request, tenant_id=tenant_id)
        servers, has_more = nova.server_list(self.request)
        server_dict = SortedDict([(s.id, s.name) for s in servers])
        reachable_subnets = self._get_reachable_subnets(ports)
        if is_service_enabled(self.request, config_name="enable_lb", ext_name="lbaas"):
            # Also get the loadbalancer VIPs
            vip_dict = {v["port_id"]: v["name"] for v in self.client.list_vips().get("vips", [])}
        else:
            vip_dict = {}

        targets = []
        for p in ports:
            # Remove network ports from Floating IP targets
            if p.device_owner.startswith("network:"):
                continue
            port_id = p.id
            server_name = server_dict.get(p.device_id) or vip_dict.get(port_id)

            for ip in p.fixed_ips:
                if ip["subnet_id"] not in reachable_subnets:
                    continue
                target = {
                    "name": "%s: %s" % (server_name, ip["ip_address"]),
                    "id": "%s_%s" % (port_id, ip["ip_address"]),
                    "instance_id": p.device_id,
                }
                targets.append(FloatingIpTarget(target))
        return targets
Ejemplo n.º 13
0
def admin_reorder(context, token):
    """
    Called in admin/base_site.html template override and applies custom ordering
    of apps/models defined by settings.ADMIN_REORDER
    """
    # sort key function - use index of item in order if exists, otherwise item
    sort = lambda order, item: (order.index(item), "") if item in order else (
        len(order), item)
    if "app_list" in context:
        # sort the app list
        order = SortedDict(settings.ADMIN_REORDER)
        context["app_list"].sort(key=lambda app: sort(order.keys(),
            app["app_url"].strip("/").split("/")[-1]))
        for i, app in enumerate(context["app_list"]):
            # sort the model list for each app
            app_name = app["app_url"].strip("/").split("/")[-1]
            if not app_name:
                app_name = app["name"].lower()
            model_order = [m.lower() for m in order.get(app_name, [])]
            context["app_list"][i]["models"].sort(key=lambda model:
            sort(model_order, model["admin_url"].strip("/").split("/")[-1]))
            
            
            while len(context["app_list"][i]["models"]) > len(model_order):
                pos = len(context["app_list"][i]["models"]) - 1
                del context["app_list"][i]["models"][pos]

            
    return ""
Ejemplo n.º 14
0
def cleanup_email_addresses(request, addresses):
    """
    Takes a list of EmailAddress instances and cleans it up, making
    sure only valid ones remain, without multiple primaries etc.

    Order is important: e.g. if multiple primary e-mail addresses
    exist, the first one encountered will be kept as primary.
    """
    from .models import EmailAddress
    adapter = get_adapter()
    # Let's group by `email`
    e2a = SortedDict()  # maps email to EmailAddress
    primary_addresses = []
    verified_addresses = []
    primary_verified_addresses = []
    for address in addresses:
        # Pick up only valid ones...
        email = valid_email_or_none(address.email)
        if not email:
            continue
        # ... and non-conflicting ones...
        if (app_settings.UNIQUE_EMAIL
                and EmailAddress.objects
                .filter(email__iexact=email)
                .exists()):
            continue
        a = e2a.get(email.lower())
        if a:
            a.primary = a.primary or address.primary
            a.verified = a.verified or address.verified
        else:
            a = address
            a.verified = a.verified or adapter.is_email_verified(request,
                                                                 a.email)
            e2a[email.lower()] = a
        if a.primary:
            primary_addresses.append(a)
            if a.verified:
                primary_verified_addresses.append(a)
        if a.verified:
            verified_addresses.append(a)
    # Now that we got things sorted out, let's assign a primary
    if primary_verified_addresses:
        primary_address = primary_verified_addresses[0]
    elif verified_addresses:
        # Pick any verified as primary
        primary_address = verified_addresses[0]
    elif primary_addresses:
        # Okay, let's pick primary then, even if unverified
        primary_address = primary_addresses[0]
    elif e2a:
        # Pick the first
        primary_address = e2a.keys()[0]
    else:
        # Empty
        primary_address = None
    # There can only be one primary
    for a in e2a.values():
        a.primary = primary_address.email.lower() == a.email.lower()
    return list(e2a.values()), primary_address
Ejemplo n.º 15
0
def columns_for_model(model, columns=None, exclude=None):
    """
    Returns a ``SortedDict`` containing form columns for the given model.

    ``columns`` is an optional list of field names. If provided, only the
    named model fields will be included in the returned column list.

    ``exclude`` is an optional list of field names. If provided, the named
    model fields will be excluded from the returned list of columns, even
    if they are listed in the ``fields`` argument.
    """

    field_list = []
    opts = model._meta
    for f in opts.fields + opts.many_to_many:
        if (columns and not f.name in columns) or \
           (exclude and f.name in exclude):
            continue
        column = Column(verbose_name=f.verbose_name) # TODO: chose correct column type, with right options
        if column:
            field_list.append((f.name, column))
    field_dict = SortedDict(field_list)
    if columns:
        field_dict = SortedDict(
            [(c, field_dict.get(c)) for c in columns
                if ((not exclude) or (exclude and c not in exclude))]
        )
    return field_dict
Ejemplo n.º 16
0
def get_floating_ips_odd(request, tenant_id, floating_ip):
    """
    :param request:request object,tenant_id, floating_ip
    :return:view<'virtual_address_manage/floatingIpsIndex_ip.html'>::list of floatingip
    """
    float_ip = []
    floating_pool = {}
    floating_ips = None
    try:
        if None != switch_tenants(request , tenant_id):
            float_ip = api.tenant_floating_ip_list(request)
            floating_pools = api.network.floating_ip_pools_list(request)
            floating_pool = SortedDict([(pool.id , pool.name) for pool in
                                      floating_pools])

        for floating in float_ip:
            if floating.ip == floating_ip:
                floating_ips = floating
                break

        if floating_pool:
            floating_ips._apidict['pool'] = floating_pool.get(floating_ips.pool,
                                                        get_text("invalid ip pool"))
    except Unauthorized:
        raise
    except Exception , exe:
        LOG.exception("ClientException in floating ip index,the error is %s" % exe.message)
Ejemplo n.º 17
0
    def get_volumes_data(self):
        # Gather our volumes
        try:
            volumes = api.volume_list(self.request)
            instances = SortedDict([(inst.id, inst) for inst in
                                    self._get_instances()])
            for volume in volumes:
                # Truncate the description for proper display.
                if len(getattr(volume, 'display_description', '')) > 33:
                    truncated_string = volume.display_description[:30].strip()
                    # Remove non-word, and underscore characters, from the end
                    # of the string before we add the ellepsis.
                    truncated_string = re.sub(ur'[^\w\s]+$',
                                              '',
                                              truncated_string)

                    volume.display_description = truncated_string + u'...'

                for att in volume.attachments:
                    server_id = att.get('server_id', None)
                    att['instance'] = instances.get(server_id, None)
        except:
            volumes = []
            exceptions.handle(self.request,
                              _('Unable to retrieve volume list.'))
        return volumes
Ejemplo n.º 18
0
def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
    """
    Returns a ``SortedDict`` containing form fields for the given model.

    ``fields`` is an optional list of field names. If provided, only the named
    fields will be included in the returned fields.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded from the returned fields, even if they are listed
    in the ``fields`` argument.
    """
    field_list = []
    opts = model._meta
    for f in opts.fields + opts.many_to_many:
        if not f.editable:
            continue
        if fields and not f.name in fields:
            continue
        if exclude and f.name in exclude:
            continue
        if widgets and f.name in widgets:
            kwargs = {'widget': widgets[f.name]}
        else:
            kwargs = {}
        formfield = formfield_callback(f, **kwargs)
        if formfield:
            field_list.append((f.name, formfield))
    field_dict = SortedDict(field_list)
    if fields:
        field_dict = SortedDict([(f, field_dict.get(f)) for f in fields if (not exclude) or (exclude and f not in exclude)])
    return field_dict
Ejemplo n.º 19
0
def get_ms_id_from_image_names(manuscripts, folios):
    '''
        Returns (ret, suggested_shelfmark)
        ret = the id of the itempart which matches the most the given images
        suggested_shelfmark = a suggested shelfmark
        The matching is based on similarity of the name (shelfmark) and locus
    '''
    ret = None

    # a term is part of the search pattern if its frequency is above the threshold
    threshold = 0.75
    
    # find a pattern among the image paths
    pattern = SortedDict()
    
    folio_count = len(folios)
    for folio in folios:
        im_path = folio.iipimage.name
        
        # remove the extension
        im_path = re.sub(ur'\.[^.]{2,4}$', '', im_path)
        
        # only keep the image file name and the parent folder
        parts = im_path.split('/')
        for i in range(max(0, len(parts) - 2), len(parts)):
            part = parts[i]
            for term in re.findall(ur'[^\W_]+', part):
                pattern[term] = pattern.get(term, 0) + 1
Ejemplo n.º 20
0
def process_log_entries(logs):
    '''
    Processes and regroups a list of log entries so they can be rendered
    and passed to the D3 library to render a chart
    '''

    reps = []
    entry_log = SortedDict()
    chart_data = []
    max_weight = {}

    # Group by date
    for entry in logs:
        if entry.reps not in reps:
            reps.append(entry.reps)

        if not entry_log.get(entry.date):
            entry_log[entry.date] = []
        entry_log[entry.date].append(entry)

        # Find the maximum weight per date per repetition.
        # If on a day there are several entries with the same number of
        # repetitions, but different weights, only the entry with the
        # higher weight is shown in the chart
        if not max_weight.get(entry.date):
            max_weight[entry.date] = {entry.reps: entry.weight}

        if not max_weight[entry.date].get(entry.reps):
            max_weight[entry.date][entry.reps] = entry.weight

        if entry.weight > max_weight[entry.date][entry.reps]:
            max_weight[entry.date][entry.reps] = entry.weight

    # Group by repetitions
    reps_list = {}
    for entry in logs:
        temp = {'date': '%s' % entry.date,
                'id': 'workout-log-%s' % entry.id}

        # Only unique date, rep and weight combinations
        if reps_list.get('{0}-{1}-{2}'.format(entry.date, entry.reps, entry.weight)):
            continue
        else:
            reps_list['{0}-{1}-{2}'.format(entry.date, entry.reps, entry.weight)] = True

        # Only add if weight is the maximum for the day
        if entry.weight != max_weight[entry.date][entry.reps]:
            continue

        for rep in reps:
            if entry.reps == rep:
                temp[rep] = entry.weight
            else:
                # Mark entries without data, this is later filtered out by D3.
                # We use the string 'n.a' instead of 0 to differentiate actual exercises
                # where no weight was used.
                temp[rep] = 'n.a'
        chart_data.append(temp)

    return entry_log, json.dumps(chart_data, cls=DecimalJsonEncoder)
Ejemplo n.º 21
0
def fields_for_model(instance, fields=None, exclude=None,
    formfield_callback=None):
    """
    Returns a "SortedDict" containing form fields for the given fom_object.

    ``fields`` is an optional list of field names. If provided, only the named
    fields will be included in the returned fields.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded from the returned fields, even if they are listed
    in the ``fields`` argument.
    """
    field_list = []
    ignored = []
    for f in instance.ordered_fields:
        if fields and not f in fields:
            continue
        if exclude and not f in exclude:
            continue

        formfield = formfield_for_model_field(instance, f)
        if formfield:
            field_list.append((f, formfield))

    field_dict = SortedDict(field_list)
    if fields:
        field_dict = SortedDict(
            [(f, field_dict.get(f)) for f in fields
            if ((not exclude) or (exclude and f not in exclude))]
            )
    return field_dict
Ejemplo n.º 22
0
def _policy_list(request, expand_rule, **kwargs):
    policies = neutronclient(request).list_firewall_policies(**kwargs).get("firewall_policies")
    if expand_rule:
        rules = _rule_list(request, expand_policy=False)
        rule_dict = SortedDict((rule.id, rule) for rule in rules)
        for p in policies:
            p["rules"] = [rule_dict.get(rule) for rule in p["firewall_rules"]]
    return [Policy(p) for p in policies]
Ejemplo n.º 23
0
def _rule_list(request, expand_policy, **kwargs):
    rules = neutronclient(request).list_firewall_rules(**kwargs).get("firewall_rules")
    if expand_policy:
        policies = _policy_list(request, expand_rule=False)
        policy_dict = SortedDict((p.id, p) for p in policies)
        for rule in rules:
            rule["policy"] = policy_dict.get(rule["firewall_policy_id"])
    return [Rule(r) for r in rules]
Ejemplo n.º 24
0
def _member_list(request, expand_pool, **kwargs):
    members = neutronclient(request).list_members(**kwargs).get('members')
    if expand_pool:
        pools = _pool_list(request)
        pool_dict = SortedDict((p.id, p) for p in pools)
        for m in members:
            m['pool_name'] = pool_dict.get(m['pool_id']).name_or_id
    return [Member(m) for m in members]
Ejemplo n.º 25
0
def _firewall_list(request, expand_policy, **kwargs):
    firewalls = neutronclient(request).list_firewalls(**kwargs).get("firewalls")
    if expand_policy:
        policies = _policy_list(request, expand_rule=False)
        policy_dict = SortedDict((p.id, p) for p in policies)
        for fw in firewalls:
            fw["policy"] = policy_dict.get(fw["firewall_policy_id"])
    return [Firewall(f) for f in firewalls]
Ejemplo n.º 26
0
def _get_servers(request):
    servers = []

    image_dict = SortedDict([(img["id"], img)for img in get_images(request)])
    zone_dict = SortedDict([(zone.id, zone.name) for zone in get_zones(request)])
    gateway_dict = SortedDict([(g.hostname, g.vext_ip)
                              for g in api.proxy.gateway_list(request)])

    for server in api.proxy.server_list(request):
        image = image_dict.get(server.image_id, {})
        if zone_dict.has_key(server.zone_id):
            server.zone_id = zone_dict[server.zone_id]

        firewall = [(gateway_dict.get(f.hostname), f.gateway_port)
                    for f in api.proxy.firewall_get(request, server.id)]
        servers.append(server_format(request, server, image, firewall))

    return servers
Ejemplo n.º 27
0
    def get_data(self):
        instances = []
        marker = self.request.GET.get(
            project_tables.AdminInstancesTable._meta.pagination_param, None)
        try:
            instances, self._more = api.nova.server_list(
                self.request,
                search_opts={'marker': marker,
                             'paginate': True},
                all_tenants=True)
        except Exception:
            self._more = False
            exceptions.handle(self.request,
                              _('Unable to retrieve instance list.'))
        if instances:
            try:
                api.network.servers_update_addresses(self.request, instances)
            except Exception:
                exceptions.handle(
                    self.request,
                    message=_('Unable to retrieve IP addresses from Neutron.'),
                    ignore=True)

            # Gather our flavors to correlate against IDs
            try:
                flavors = api.nova.flavor_list(self.request)
            except Exception:
                # If fails to retrieve flavor list, creates an empty list.
                flavors = []

            # Gather our tenants to correlate against IDs
            try:
                tenants, has_more = api.keystone.tenant_list(self.request)
            except Exception:
                tenants = []
                msg = _('Unable to retrieve instance project information.')
                exceptions.handle(self.request, msg)

            full_flavors = SortedDict([(f.id, f) for f in flavors])
            tenant_dict = SortedDict([(t.id, t) for t in tenants])
            # Loop through instances to get flavor and tenant info.
            for inst in instances:
                flavor_id = inst.flavor["id"]
                try:
                    if flavor_id in full_flavors:
                        inst.full_flavor = full_flavors[flavor_id]
                    else:
                        # If the flavor_id is not in full_flavors list,
                        # gets it via nova api.
                        inst.full_flavor = api.nova.flavor_get(
                            self.request, flavor_id)
                except Exception:
                    msg = _('Unable to retrieve instance size information.')
                    exceptions.handle(self.request, msg)
                tenant = tenant_dict.get(inst.tenant_id, None)
                inst.tenant_name = getattr(tenant, "name", None)
        return instances
Ejemplo n.º 28
0
class AppLayerFinder(BaseFinder):
    storage_class = LayerStaticStorage

    def __init__(self, apps=None, *args, **kwargs):
        layers = getattr(settings, "LAYERS", {})
        self.apps = []
        self.storages = SortedDict()
        if apps is None:
            apps = settings.INSTALLED_APPS
        for app in apps:
            for layer in layers.keys():
                app_storage = self.storage_class(app, layer)
                if os.path.isdir(app_storage.location):
                    if app not in self.apps:
                        self.apps.append(app)
                    if app not in self.storages:
                        self.storages[app] = {}

                    self.storages[app][layer] = app_storage
        super(AppLayerFinder, self).__init__(*args, **kwargs)

    def find(self, path, all=False, layer=None):
        """
        Looks for files in the app directories.
        """
        matches = []
        for app in self.apps:
            match = self.find_in_app(app, path, layer)
            if match:
                if not all:
                    return match
                matches.append(match)
        return matches

    def find_in_app(self, app, path, layer=None):
        layer = layer or get_active_layer(get_current_request())
        storage = self.storages.get(app, {}).get(layer, None)
        if storage:
            if layer:
                if storage.exists(path):
                    matched_path = storage.path(path)
                    if matched_path:
                        return matched_path

    def list(self, ignore_patterns, layer=None):
        """
        List all files in all app storages.
        """
        if not layer:
            return

        for storage in self.storages.itervalues():
            layer_storage = storage.get(layer, None)
            if layer_storage and layer_storage.exists(''):
                for path in utils.get_files(layer_storage, ignore_patterns):
                    yield path, layer_storage
Ejemplo n.º 29
0
def fields_for_document(document, fields=None, exclude=None, widgets=None, \
                        formfield_callback=None, field_generator=MongoDefaultFormFieldGenerator):
    """
    Returns a ``SortedDict`` containing form fields for the given model.

    ``fields`` is an optional list of field names. If provided, only the named
    fields will be included in the returned fields.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded from the returned fields, even if they are listed
    in the ``fields`` argument.
    """
    field_list = []
    ignored = []
    if isinstance(field_generator, type):
        field_generator = field_generator()
    
    # This is actually a bad way to sort the fields, but the fields keep the order
    # they were defined on he document (at least with cPython) and I can't see 
    # any other way for now. Oh, yeah, it works because we sort on the memory address
    # and hope that the earlier fields have a lower address.
    sorted_fields = sorted(list(document._fields.values()), key=lambda field: field.__hash__())
    
    for f in sorted_fields:
        if isinstance(f, ObjectIdField):
            continue
        if isinstance(f, ListField) and not (hasattr(f.field,'choices') or isinstance(f.field, ReferenceField)):
            continue
        if fields is not None and not f.name in fields:
            continue
        if exclude and f.name in exclude:
            continue
        if widgets and f.name in widgets:
            kwargs = {'widget': widgets[f.name]}
        else:
            kwargs = {}

        if formfield_callback is None:
            formfield = field_generator.generate(f, **kwargs)
        elif not isinstance(formfield_callback, Callable):
            raise TypeError('formfield_callback must be a function or callable')
        else:
            formfield = formfield_callback(f, **kwargs)

        if formfield:
            field_list.append((f.name, formfield))
        else:
            ignored.append(f.name)

    field_dict = SortedDict(field_list)
    if fields:
        field_dict = SortedDict(
            [(f, field_dict.get(f)) for f in fields
                if ((not exclude) or (exclude and f not in exclude)) and (f not in ignored)]
        )
    return field_dict
Ejemplo n.º 30
0
def network_list(request, **params):
    LOG.debug("network_list(): params=%s" % (params))
    networks = neutronclient(request).list_networks(**params).get('networks')
    # Get subnet list to expand subnet info in network list.
    subnets = subnet_list(request)
    subnet_dict = SortedDict([(s['id'], s) for s in subnets])
    # Expand subnet list from subnet_id to values.
    for n in networks:
        n['subnets'] = [subnet_dict.get(s) for s in n.get('subnets', [])]
    return [Network(n) for n in networks]
Ejemplo n.º 31
0
def correlate_tenants(request, instances):
    # Gather our tenants to correlate against IDs
    try:
        tenants = api.keystone.tenant_list(request, admin=True)
    except:
        tenants = []
        msg = _('Unable to retrieve instance tenant information.')
        exceptions.handle(request, msg)
    tenant_dict = SortedDict([(t.id, t) for t in tenants])
    for inst in instances:
        tenant = tenant_dict.get(inst.tenant_id, None)
        inst._apiresource._info['tenant'] = tenant._info
        inst.tenant = tenant
Ejemplo n.º 32
0
def correlate_users(request, instances):
    # Gather our users to correlate against IDs
    try:
        users = api.keystone.user_list(request)
    except:
        users = []
        msg = _('Unable to retrieve instance user information.')
        exceptions.handle(request, msg)
    user_dict = SortedDict([(u.id, u) for u in users])
    for inst in instances:
        user = user_dict.get(inst.user_id, None)
        inst._apiresource._info['user'] = user._info
        inst.user = user
Ejemplo n.º 33
0
def _policy_get(request, policy_id, expand_rule):
    policy = neutronclient(request).show_firewall_policy(
        policy_id).get('firewall_policy')
    if expand_rule:
        policy_rules = policy['firewall_rules']
        if policy_rules:
            rules = _rule_list(request, expand_policy=False,
                               firewall_policy_id=policy_id)
            rule_dict = SortedDict((rule.id, rule) for rule in rules)
            policy['rules'] = [rule_dict.get(rule) for rule in policy_rules]
        else:
            policy['rules'] = []
    return Policy(policy)
Ejemplo n.º 34
0
def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_callback=None, localized_fields=None):
    """
    Returns a ``SortedDict`` containing form fields for the given model.

    ``fields`` is an optional list of field names. If provided, only the named
    fields will be included in the returned fields.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded from the returned fields, even if they are listed
    in the ``fields`` argument.

    ``widgets`` is a dictionary of model field names mapped to a widget

    ``formfield_callback`` is a callable that takes a model field and returns
    a form field.
    """
    field_list = []
    ignored = []
    opts = model._meta
    for f in sorted(opts.concrete_fields + opts.many_to_many):
        if not f.editable:
            continue
        if fields is not None and not f.name in fields:
            continue
        if exclude and f.name in exclude:
            continue

        kwargs = {}
        if widgets and f.name in widgets:
            kwargs['widget'] = widgets[f.name]
        if localized_fields == ALL_FIELDS or (localized_fields and f.name in localized_fields):
            kwargs['localize'] = True

        if formfield_callback is None:
            formfield = f.formfield(**kwargs)
        elif not callable(formfield_callback):
            raise TypeError('formfield_callback must be a function or callable')
        else:
            formfield = formfield_callback(f, **kwargs)

        if formfield:
            field_list.append((f.name, formfield))
        else:
            ignored.append(f.name)
    field_dict = SortedDict(field_list)
    if fields:
        field_dict = SortedDict(
            [(f, field_dict.get(f)) for f in fields
                if ((not exclude) or (exclude and f not in exclude)) and (f not in ignored)]
        )
    return field_dict
Ejemplo n.º 35
0
    def fields_for_model(model,
                         fields=None,
                         exclude=None,
                         widgets=None,
                         formfield_callback=None):
        opts = model._meta

        if not getattr(opts, "enable_composite", False):
            return original_fields_for_model(model,
                                             fields=fields,
                                             exclude=fields,
                                             widgets=fields,
                                             formfield_callback=fields)

        # todo: maybe is possible don't rewrite all function
        field_list = []
        ignored = []
        # hack added composite_foreignkeys_fields
        for f in sorted(opts.fields + opts.many_to_many +
                        opts.composite_special_fields):
            if not f.editable:
                continue
            if fields is not None and not f.name in fields:
                continue
            if exclude and f.name in exclude:
                continue
            if widgets and f.name in widgets:
                kwargs = {'widget': widgets[f.name]}
            else:
                kwargs = {}

            if formfield_callback is None:
                formfield = f.formfield(**kwargs)
            elif not callable(formfield_callback):
                raise TypeError(
                    'formfield_callback must be a function or callable')
            else:
                formfield = formfield_callback(f, **kwargs)

            if formfield:
                field_list.append((f.name, formfield))
            else:
                ignored.append(f.name)
        field_dict = SortedDict(field_list)
        if fields:
            field_dict = SortedDict([
                (f, field_dict.get(f)) for f in fields
                if ((not exclude) or (exclude and f not in exclude)) and (
                    f not in ignored)
            ])
        return field_dict
Ejemplo n.º 36
0
    def process_log_entries(self, logs):
        '''
        Processes and regroups a list of log entries so they can be rendered
        and passed to the D3 library to render a chart
        '''

        reps = []
        entry_log = SortedDict()
        chart_data = []
        max_weight = {}

        # Group by date
        for entry in logs:
            if entry.reps not in reps:
                reps.append(entry.reps)

            if not entry_log.get(entry.date):
                entry_log[entry.date] = []
            entry_log[entry.date].append(entry)

            # Find the maximum weight per date per repetition.
            # If on a day there are several entries with the same number of
            # repetitions, but different weights, only the entry with the
            # higher weight is shown in the chart
            if not max_weight.get(entry.date):
                max_weight[entry.date] = {entry.reps: entry.weight}

            if not max_weight[entry.date].get(entry.reps):
                max_weight[entry.date][entry.reps] = entry.weight

            if entry.weight > max_weight[entry.date][entry.reps]:
                max_weight[entry.date][entry.reps] = entry.weight

        # Group by repetitions
        for entry in logs:
            temp = {
                'date': '%s' % entry.date,
                'id': 'workout-log-%s' % entry.id
            }

            for rep in reps:
                if entry.reps == rep:

                    # Only add if entry is the maximum for the day
                    if entry.weight == max_weight[entry.date][entry.reps]:
                        temp[rep] = entry.weight
                else:
                    temp[rep] = 0
            chart_data.append(temp)

        return (entry_log, json.dumps(chart_data, cls=DecimalJsonEncoder))
Ejemplo n.º 37
0
def correlate_flavors(request, instances):
    # Gather our flavors to correlate against IDs
    try:
        flavors = api.nova.flavor_list(request)
    except:
        flavors = []
        msg = _('Unable to retrieve instance size information.')
        exceptions.handle(request, msg)

    flavors_dict = SortedDict([(f.id, f) for f in flavors])
    for inst in instances:
        flavor = flavors_dict.get(inst.flavor["id"], None)
        inst._apiresource._info['flavor'] = flavor._info
        inst.flavor = flavor
Ejemplo n.º 38
0
def _pool_list(request, expand_subnet=False, expand_vip=False, **kwargs):
    pools = neutronclient(request).list_pools(**kwargs).get('pools')
    if expand_subnet:
        subnets = neutron.subnet_list(request)
        subnet_dict = SortedDict((s.id, s) for s in subnets)
        for p in pools:
            subnet = subnet_dict.get(p['subnet_id'])
            p['subnet_name'] = subnet.cidr if subnet else None
    if expand_vip:
        vips = vip_list(request)
        vip_dict = SortedDict((v.id, v) for v in vips)
        for p in pools:
            p['vip'] = _get_vip(request, p, vip_dict)
    return [Pool(p) for p in pools]
Ejemplo n.º 39
0
def _ipsecsiteconnection_list(request, expand_ikepolicies=False,
                              expand_ipsecpolicies=False,
                              expand_vpnservices=False, **kwargs):
    ipsecsiteconnections = neutronclient(request).list_ipsec_site_connections(
        **kwargs).get('ipsec_site_connections')
    if expand_ikepolicies:
        ikepolicies = _ikepolicy_list(request, **kwargs)
        policy_dict = SortedDict((p.id, p) for p in ikepolicies)
        for c in ipsecsiteconnections:
            c['ikepolicy_name'] = policy_dict.get(c['ikepolicy_id']).name_or_id
    if expand_ipsecpolicies:
        ipsecpolicies = _ipsecpolicy_list(request, **kwargs)
        policy_dict = SortedDict((p.id, p) for p in ipsecpolicies)
        for c in ipsecsiteconnections:
            c['ipsecpolicy_name'] = policy_dict.get(c['ipsecpolicy_id']
                                                    ).name_or_id
    if expand_vpnservices:
        vpnservices = _vpnservice_list(request, **kwargs)
        service_dict = SortedDict((s.id, s) for s in vpnservices)
        for c in ipsecsiteconnections:
            c['vpnservice_name'] = service_dict.get(c['vpnservice_id']
                                                    ).name_or_id
    return [IPSecSiteConnection(v) for v in ipsecsiteconnections]
Ejemplo n.º 40
0
    def get_data(self):
        instances = []
        marker = self.request.GET.get(
            project_tables.AdminInstancesTable._meta.pagination_param, None)
        try:
            instances, self._more = api.nova.server_list(self.request,
                                                         search_opts={
                                                             'marker': marker,
                                                             'paginate': True
                                                         },
                                                         all_tenants=True)
        except Exception:
            self._more = False
            exceptions.handle(self.request,
                              _('Unable to retrieve instance list.'))
        if instances:
            # Gather our flavors to correlate against IDs
            try:
                flavors = api.nova.flavor_list(self.request)
            except Exception:
                # If fails to retrieve flavor list, creates an empty list.
                flavors = []

            # Gather our tenants to correlate against IDs
            try:
                tenants, has_more = api.keystone.tenant_list(self.request)
            except Exception:
                tenants = []
                msg = _('Unable to retrieve instance project information.')
                exceptions.handle(self.request, msg)

            full_flavors = SortedDict([(f.id, f) for f in flavors])
            tenant_dict = SortedDict([(t.id, t) for t in tenants])
            # Loop through instances to get flavor and tenant info.
            for inst in instances:
                flavor_id = inst.flavor["id"]
                try:
                    if flavor_id in full_flavors:
                        inst.full_flavor = full_flavors[flavor_id]
                    else:
                        # If the flavor_id is not in full_flavors list,
                        # gets it via nova api.
                        inst.full_flavor = api.nova.flavor_get(
                            self.request, flavor_id)
                except Exception:
                    msg = _('Unable to retrieve instance size information.')
                    exceptions.handle(self.request, msg)
                tenant = tenant_dict.get(inst.tenant_id, None)
                inst.tenant_name = getattr(tenant, "name", None)
        return instances
Ejemplo n.º 41
0
def fields_for_model(model,
                     fields=None,
                     exclude=None,
                     widgets=None,
                     formfield_callback=None):
    """
    Returns a ``SortedDict`` containing form fields for the given model.

    ``fields`` is an optional list of field names. If provided, only the named
    fields will be included in the returned fields.

    ``exclude`` is an optional list of field names. If provided, the named
    fields will be excluded from the returned fields, even if they are listed
    in the ``fields`` argument.
    """
    field_list = []
    ignored = []
    opts = model._meta
    for f in opts.fields + opts.many_to_many:
        if not f.editable:
            continue
        if fields and not f.name in fields:
            continue
        if exclude and f.name in exclude:
            continue
        if widgets and f.name in widgets:
            kwargs = {'widget': widgets[f.name]}
        else:
            kwargs = {}

        if formfield_callback is None:
            formfield = f.formfield(**kwargs)
        elif not callable(formfield_callback):
            raise TypeError(
                'formfield_callback must be a function or callable')
        else:
            formfield = formfield_callback(f, **kwargs)

        if formfield:
            field_list.append((f.name, formfield))
        else:
            ignored.append(f.name)
    field_dict = SortedDict(field_list)
    if fields:
        field_dict = SortedDict([
            (f, field_dict.get(f)) for f in fields
            if ((not exclude) or (exclude and f not in exclude)) and (
                f not in ignored)
        ])
    return field_dict
Ejemplo n.º 42
0
def _vpnservice_list(request,
                     expand_subnet=False,
                     expand_router=False,
                     expand_conns=False,
                     **kwargs):
    vpnservices = neutronclient(request).list_vpnservices(
        **kwargs).get('vpnservices')
    if expand_subnet:
        subnets = neutron.subnet_list(request)
        subnet_dict = SortedDict((s.id, s) for s in subnets)
        for s in vpnservices:
            s['subnet_name'] = subnet_dict.get(s['subnet_id']).cidr
    if expand_router:
        routers = neutron.router_list(request)
        router_dict = SortedDict((r.id, r) for r in routers)
        for s in vpnservices:
            s['router_name'] = router_dict.get(s['router_id']).name_or_id
    if expand_conns:
        ipsecsiteconns = _ipsecsiteconnection_list(request, **kwargs)
        for s in vpnservices:
            s['ipsecsiteconns'] = [
                c.id for c in ipsecsiteconns if c.vpnservice_id == s['id']
            ]
    return [VPNService(v) for v in vpnservices]
Ejemplo n.º 43
0
    def get_data(self, request, volume_id):
        volume = cinder.volume_get(request, volume_id)
        try:
            tenants, has_more = api.keystone.tenant_list(request)
        except Exception:
            tenants = []
            msg = _('Unable to retrieve volume project information.')
            exceptions.handle(request, msg)

        tenant_dict = SortedDict([(t.id, t) for t in tenants])
        tenant_id = getattr(volume, "os-vol-tenant-attr:tenant_id", None)
        tenant = tenant_dict.get(tenant_id, None)
        volume.tenant_name = getattr(tenant, "name", None)

        return volume
Ejemplo n.º 44
0
 def list_targets(self):
     ports = port_list(self.request)
     servers, has_more = nova.server_list(self.request)
     server_dict = SortedDict([(s.id, s.name) for s in servers])
     targets = []
     for p in ports:
         # Remove network ports from Floating IP targets
         if p.device_owner.startswith('network:'):
             continue
         port_id = p.id
         server_name = server_dict.get(p.device_id)
         for ip in p.fixed_ips:
             target = {'name': '%s: %s' % (server_name, ip['ip_address']),
                       'id': '%s_%s' % (port_id, ip['ip_address'])}
             targets.append(FloatingIpTarget(target))
     return targets
Ejemplo n.º 45
0
    def get_data(self, request, snapshot_id):
        snapshot = cinder.volume_snapshot_get(request, snapshot_id)
        snapshot._volume = cinder.volume_get(request, snapshot.volume_id)
        try:
            tenants, has_more = api.keystone.tenant_list(request)
        except Exception:
            tenants = []
            msg = _('Unable to retrieve volume project information.')

        tenant_dict = SortedDict([(t.id, t) for t in tenants])
        tenant_id = getattr(snapshot,
                            "os-extended-snapshot-attributes:project_id", None)
        tenant = tenant_dict.get(tenant_id, None)
        snapshot.tenant_name = getattr(tenant, "name", None)

        return snapshot
Ejemplo n.º 46
0
    def faceted_filters(self):
        url = QueryURLObject(self.url)
        filter_mapping = SortedDict(
            (filter_['slug'], filter_) for filter_ in self.serialized_filters)

        filter_groups = SortedDict()

        for slug, facet in self.facet_counts().items():
            # if not isinstance(facet, dict):
            #     # let's just blankly ignore any non-filter or non-query filters
            #     continue

            filter_ = filter_mapping.get(slug, None)
            if filter_ is None:
                filter_name = slug
                group_name = None
                group_slug = None
            else:
                # Let's check if we can get the name from the gettext catalog
                filter_name = _(filter_['name'])
                group_name = _(filter_['group']['name'])
                group_slug = filter_['group']['slug']

            filter_groups.setdefault(
                (group_name, group_slug, filter_['group']['order']),
                []).append(
                    Filter(url=url,
                           page=self.current_page,
                           name=filter_name,
                           slug=slug,
                           count=facet['count'],
                           active=slug in self.selected_filters,
                           group_name=group_name,
                           group_slug=group_slug))

        # return a sorted list of filters here
        grouped_filters = []
        for group_options, filters in filter_groups.items():
            group_name, group_slug, group_order = group_options
            sorted_filters = sorted(filters, key=attrgetter('name'))
            grouped_filters.append(
                FilterGroup(name=group_name,
                            slug=group_slug,
                            order=group_order,
                            options=sorted_filters))
        return sorted(grouped_filters, key=attrgetter('order'), reverse=True)
Ejemplo n.º 47
0
def filters_for_model(model, fields=None, exclude=None, filter_for_field=None):
    field_list = []
    opts = model._meta
    for f in sorted(opts.fields + opts.many_to_many):
        if fields is not None and f.name not in fields:
            continue
        if exclude is not None and f.name in exclude:
            continue
        filter_ = filter_for_field(f, f.name)
        if filter_:
            field_list.append((f.name, filter_))
    field_dict = SortedDict(field_list)
    if fields:
        field_dict = SortedDict([
            (f, field_dict.get(f)) for f in fields
            if (not exclude) or (exclude and f not in exclude)
        ])
    return field_dict
Ejemplo n.º 48
0
Archivo: group.py Proyecto: pugpe/pugpe
def group(queryset, expression):
    '''
    Similar to regroup, but without restrict respect order


    Usage: {% group people 'gender' as grouped  %}

    '''
    key = attrgetter(expression)

    group = SortedDict()
    for item in queryset:
        try:
            k = key(item)
        except AttributeError:
            k = None
        group[k] = group.get(k, []) + [item]
    return group
Ejemplo n.º 49
0
 def list(self, all_tenants=False, **search_opts):
     if not all_tenants:
         tenant_id = self.request.user.tenant_id
         # In Neutron, list_floatingips returns Floating IPs from
         # all tenants when the API is called with admin role, so
         # we need to filter them with tenant_id.
         search_opts['tenant_id'] = tenant_id
         port_search_opts = {'tenant_id': tenant_id}
     else:
         port_search_opts = {}
     fips = self.client.list_floatingips(**search_opts)
     fips = fips.get('floatingips')
     # Get port list to add instance_id to floating IP list
     # instance_id is stored in device_id attribute
     ports = port_list(self.request, **port_search_opts)
     port_dict = SortedDict([(p['id'], p) for p in ports])
     for fip in fips:
         self._set_instance_info(fip, port_dict.get(fip['port_id']))
     return [FloatingIp(fip) for fip in fips]
Ejemplo n.º 50
0
    def ensure_attachment_server(request, volumes):
        instances, has_more = api.nova.server_list(
            request,
            search_opts=None,
            all_tenants=request.GET.get('all_tenants', False),
        )
        instance_dict = SortedDict([(i.id, i) for i in instances])

        for item in volumes:
            if not item['attachments']:
                continue
            for i, atmt in enumerate(item['attachments']):
                instance = instance_dict.get(atmt.get('server_id'), None)
                if instance:
                    instance_name = {'instance_name': instance.name}
                    item['attachments'][i].update(instance_name)
                    if item.get('name') == '':
                        item.update({'name': instance.name})
        return volumes
Ejemplo n.º 51
0
def _get_servers(request):
    try:
        instances, has_more = api.nova.server_list(request)
    except Exception:
        instances = []
        exceptions.handle(request, _('Unable to retrieve instances.'))

    servers = []

    if instances:
        try:
            images, more, prev = api.glance.image_list_detailed(request)
        except Exception:
            images = []
            exceptions.handle(request, ignore=True)

        try:
            gateways = api.daolicloud.gateway_list(request)
        except Exception:
            gateways = []
            exceptions.handle(request, ignore=True)

        image_map = SortedDict([(str(image.id), image)
                                for image in images])
        gateway_map = SortedDict([(gateway.hostname, gateway.vext_ip)
                                  for gateway in gateways])


        for instance in instances:
            if hasattr(instance, 'image'):
                if isinstance(instance.image, dict):
                    if instance.image.get('id') in image_map:
                        instance.image = image_map[instance.image['id']]

            instance.full_firewalls = [firewall.to_dict() for firewall in
                api.daolicloud.firewall_get(request, instance.id)]

            for firewall in instance.full_firewalls:
                firewall['gateway_ip'] = gateway_map.get(firewall['hostname'])

            servers.append(server_format(request, instance))

    return servers
Ejemplo n.º 52
0
    def get_data(self):
        instances = []
        try:
            instances = api.nova.server_list(self.request, all_tenants=True)
        except:
            exceptions.handle(self.request,
                              _('Unable to retrieve instance list.'))
        if instances:
            # Gather our flavors to correlate against IDs
            try:
                flavors = api.nova.flavor_list(self.request)
            except:
                # If fails to retrieve flavor list, creates an empty list.
                flavors = []

            # Gather our tenants to correlate against IDs
            try:
                tenants = api.keystone.tenant_list(self.request, admin=True)
            except:
                tenants = []
                msg = _('Unable to retrieve instance tenant information.')
                exceptions.handle(self.request, msg)

            full_flavors = SortedDict([(f.id, f) for f in flavors])
            tenant_dict = SortedDict([(t.id, t) for t in tenants])
            # Loop through instances to get flavor and tenant info.
            for inst in instances:
                flavor_id = inst.flavor["id"]
                try:
                    if flavor_id in full_flavors:
                        inst.full_flavor = full_flavors[flavor_id]
                    else:
                        # If the flavor_id is not in full_flavors list,
                        # gets it via nova api.
                        inst.full_flavor = api.nova.flavor_get(
                            self.request, flavor_id)
                except:
                    msg = _('Unable to retrieve instance size information.')
                    exceptions.handle(self.request, msg)
                tenant = tenant_dict.get(inst.tenant_id, None)
                inst.tenant_name = getattr(tenant, "name", None)
        return instances
Ejemplo n.º 53
0
    def get_volumes_total(self):
        self._prev = False
        volumes = self._get_volumes()
        instances = self._get_instances()
        self._set_attachments_string(volumes, instances)

        try:
            tenants, has_more = keystone.tenant_list(self.request)
        except Exception:
            tenants = []
            msg = _('Unable to retrieve volume project information.')
            exceptions.handle(self.request, msg)

        tenant_dict = SortedDict([(t.id, t) for t in tenants])
        for volume in volumes:
            tenant_id = getattr(volume, "os-vol-tenant-attr:tenant_id", None)
            tenant = tenant_dict.get(tenant_id, None)
            volume.tenant_name = getattr(tenant, "name", None)

        return volumes
Ejemplo n.º 54
0
    def get_volumes_data(self):
        volumes = self._get_volumes(search_opts={'all_tenants': True})
        instances = self._get_instances(search_opts={'all_tenants': True})
        self._set_attachments_string(volumes, instances)

        # Gather our tenants to correlate against IDs
        try:
            tenants, has_more = keystone.tenant_list(self.request)
        except Exception:
            tenants = []
            msg = _('Unable to retrieve volume project information.')
            exceptions.handle(self.request, msg)

        tenant_dict = SortedDict([(t.id, t) for t in tenants])
        for volume in volumes:
            tenant_id = getattr(volume, "os-vol-tenant-attr:tenant_id", None)
            tenant = tenant_dict.get(tenant_id, None)
            volume.tenant_name = getattr(tenant, "name", None)

        return volumes
Ejemplo n.º 55
0
def player_list(request):
    '''角色信息排序
    '''
    page_size = 100
    page_num = int(request.REQUEST.get("page_num", "1"))
    is_block = int(request.REQUEST.get("block", 0))
    group_id = int(request.REQUEST.get("group_id", 0))
    server_id = int(request.REQUEST.get("server_id", "0"))
    sort_keys = [
        x for x in request.REQUEST.get('sort_keys', '').split(',') if x
    ]  #排序key顺勋
    country_code = int(request.REQUEST.get('nat', '-1') or -1)
    total_record = 0
    ths = ["pi", "pn", "plv", "vip", "nat", "of", "pc", "olt"]
    _sort_condition = []
    condition_map = SortedDict([(t, PLAYER_INFO_MAP.get(t, {}).get('name', ''))
                                for t in ths])
    condition_country = {"nat": country_code} if country_code != -999 else {}
    for k in sort_keys:
        if condition_map.get(k, ''):
            _v = request.POST.get(k, '')
            _sort_condition.append((k, int(_v)))

    if server_id:
        try:
            server = Server.objects.get(id=server_id)
            db_name, mongo_conn = server.get_mongo_conn()
            total_record = mongo_conn[db_name].tk.player.find(
                condition_country).count()
            if page_num < 1:
                page_num = 1
            offset = (page_num - 1) * page_size
            if not _sort_condition:
                _sort_condition = [("olt", -1)]  #默认最后登录时间
            _list = mongo_conn[db_name].tk.player.find(condition_country).sort(
                _sort_condition).skip(offset).limit(page_size)
            player_list = player_list_handle(ths, _list)
            mongo_conn.close()
        except Exception, e:
            traceback.print_exc()
            err_msg = str(e)
Ejemplo n.º 56
0
def get_floating_ips_list(request, tenant_id):
    """
    :param request:request object,tenant_id
    :return:view<'virtual_address_manage/floatingIpsIndex.html'>::list of floatingIp
    """
    args = {}
    floating_ips = []
    floating_pool = {}
    floatingips = []
    try:
        if None != switch_tenants(request, tenant_id):
            floating_ips = api.network.tenant_floating_ip_list(request)
            floating_pools = api.network.floating_ip_pools_list(request)
            floating_pool = SortedDict([(pool.id, pool.name)
                                        for pool in floating_pools])

        for floating_ip in floating_ips:
            if tenant_id == floating_ip.tenant_id:
                floating_ip._apidict['pool'] = floating_pool.get(
                    floating_ip.pool, get_text("invalid ip pool"))
                floatingips.append(floating_ip)

        instances = []
        tenant_quota = api.nova.nova_tenant_quota_get(request, tenant_id)
        for item in tenant_quota:
            if item.name == "floating_ips":
                ip_num = item.limit
        servers = api.server_list(request)
        for server in servers:
            # to be removed when nova can support unique names
            server_name = server.name
            if any(s.id != server.id and s.name == server.name
                   for s in servers):
                # duplicate instance name
                server_name = "%s [%s]" % (server.name, server.id)
            instances.append((server.id, server_name))
    except Unauthorized:
        raise
    except Exception, exe:
        LOG.exception("ClientException in floating ip index,the error is %s " %
                      exe.message)
Ejemplo n.º 57
0
    def get_inline_instances(self, request, obj=None):
        inline_instances = []

        if obj and obj.pk:
            variables = get_variables_from_template(obj.template.path).items()
            types = SortedDict()
            for label, variable in variables:
                model_class = variable['type']
                initial_field_values = variable['initial_field_values']
                types[model_class] = types.get(model_class,
                                               0)  # can't use defaultdict :(
                types[model_class] += 1
                model_class.objects.get_or_create(
                    text_ng=obj, label=label, defaults=initial_field_values)

            for model_class in types.keys():
                inline = self.get_inline_for_model(model_class)
                inline.max_num = types[model_class]
                inline_instance = inline(self.model, self.admin_site)
                inline_instances.append(inline_instance)
        return inline_instances
Ejemplo n.º 58
0
def get(request, instance_id):
    instance = api.nova.server_get(request, instance_id)
 
    try:
        gateways = api.daolicloud.gateway_list(request)
    except Exception:
        gateways = []
        exceptions.handle(request, ignore=True)

    instance.image = api.glance.image_get(request, instance.image['id'])
    gateway_map = SortedDict([(gateway.hostname, gateway.vext_ip)
                              for gateway in gateways])

    instance.full_firewalls = [firewall.to_dict() for firewall in
        api.daolicloud.firewall_get(request, instance.id)]

    for firewall in instance.full_firewalls:
        firewall['gateway_ip'] = gateway_map.get(firewall['hostname'])

    return HttpResponse(json.dumps(server_format(request, instance)),
                        content_type='application/json')
Ejemplo n.º 59
0
    def list_targets(self):
        tenant_id = self.request.user.tenant_id
        ports = port_list(self.request, tenant_id=tenant_id)
        servers, has_more = nova.server_list(self.request)
        server_dict = SortedDict([(s.id, s.name) for s in servers])
        reachable_subnets = self._get_reachable_subnets(ports)

        targets = []
        for p in ports:
            # Remove network ports from Floating IP targets
            if p.device_owner.startswith('network:'):
                continue
            port_id = p.id
            server_name = server_dict.get(p.device_id)
            for ip in p.fixed_ips:
                if ip['subnet_id'] not in reachable_subnets:
                    continue
                target = {'name': '%s: %s' % (server_name, ip['ip_address']),
                          'id': '%s_%s' % (port_id, ip['ip_address']),
                          'instance_id': p.device_id}
                targets.append(FloatingIpTarget(target))
        return targets
Ejemplo n.º 60
0
    def get_data(self):
        tenants = api.keystone.tenant_list(self.request)
        tenants_dict = SortedDict([t.name, t] for t in tenants)
        admin_tenant = tenants_dict.get("admin", None)

        users_list = api.keystone.user_list(self.request, admin_tenant.id)

        if self.request.user.username != "admin":
            users_list = [
                user for user in users_list if user.id == self.request.user.id
            ]

        result = []
        for user in users_list:
            user = {
                "id": user.id,
                "name": user.name,
            }
            result.append(user)
        LOG.info("Users list is %s" % str(result))

        return result