Ejemplo n.º 1
0
class DashboardEntryAdminForm(forms.ModelForm):
    class Meta:
        model = DashboardEntry
        exclude = ()

    layout_uid = forms.ChoiceField(choices=get_registered_layouts())
    plugin_uid = forms.ChoiceField(choices=get_registered_plugins())
Ejemplo n.º 2
0
def get_user_plugins(user):
    """
    Gets a list of user plugins in a form if tuple (plugin name, plugin description). If not yet autodiscovered,
    autodiscovers them.

    :return list:
    """
    ensure_autodiscover()

    if not RESTRICT_PLUGIN_ACCESS or getattr(user, 'is_superuser', False):
        return get_registered_plugins()

    registered_plugins = []

    allowed_plugin_uids = get_allowed_plugin_uids(user)

    for uid, plugin in plugin_registry._registry.items():
        if uid in allowed_plugin_uids:
            registered_plugins.append((uid, force_text(plugin.name)))

    return registered_plugins
Ejemplo n.º 3
0
def get_user_plugins(user):
    """
    Gets a list of user plugins in a form if tuple (plugin name, plugin
    description). If not yet autodiscovered, autodiscovers them.

    :return list:
    """
    ensure_autodiscover()

    if not RESTRICT_PLUGIN_ACCESS or getattr(user, 'is_superuser', False):
        return get_registered_plugins()

    registered_plugins = []

    allowed_plugin_uids = get_allowed_plugin_uids(user)

    for uid, plugin in plugin_registry._registry.items():
        if uid in allowed_plugin_uids:
            registered_plugins.append((uid, safe_text(plugin.name)))

    return registered_plugins
Ejemplo n.º 4
0
 def __unicode__(self):
     return "{0} ({1})".format(dict(get_registered_plugins()).get(self.plugin_uid, ''), self.plugin_uid)
Ejemplo n.º 5
0
 def __unicode__(self):
     return "{0} ({1})".format(
         dict(get_registered_plugins()).get(self.plugin_uid, ''),
         self.plugin_uid)
Ejemplo n.º 6
0
class DashboardPlugin(models.Model):
    """
    Dashboard plugin. Used when ``dash.settings.RESTRICT_PLUGIN_ACCESS`` is
    set to True.

    :Properties:

        - `plugin_uid` (str): Plugin UID.
        - `users` (django.contrib.auth.models.User): White list of the users
          allowed to use the dashboard plugin.
        - `groups` (django.contrib.auth.models.Group): White list of the user
          groups allowed to use the dashboard plugin.
    """
    plugin_uid = models.CharField(_("Plugin UID"), max_length=255, \
                                  choices=get_registered_plugins(), \
                                  unique=True, editable=False)
    users = models.ManyToManyField(User, verbose_name=_("User"), null=True, \
                                   blank=True)
    groups = models.ManyToManyField(Group, verbose_name=_("Group"), null=True, \
                                    blank=True)

    objects = DashboardPluginManager()

    class Meta:
        verbose_name = _("Dashboard plugin")
        verbose_name_plural = _("Dashboard plugins")

    def __unicode__(self):
        return "{0} ({1})".format(
            dict(get_registered_plugins()).get(self.plugin_uid, ''),
            self.plugin_uid)

    def plugin_uid_code(self):
        """
        Mainly used in admin.
        """
        return self.plugin_uid

    plugin_uid_code.allow_tags = True
    plugin_uid_code.short_description = _('UID')

    def plugin_uid_admin(self):
        """
        Mainly used in admin.
        """
        return self.__unicode__()

    plugin_uid_admin.allow_tags = True
    plugin_uid_admin.short_description = _('Plugin')

    def groups_list(self):
        """
        Flat list (comma separated string) of groups allowed to use the
        dashboard plugin. Used in Django admin.

        :return string:
        """
        return ', '.join([g.name for g in self.groups.all()])

    groups_list.allow_tags = True
    groups_list.short_description = _('Groups')

    def users_list(self):
        """
        Flat list (comma separated string) of users allowed to use the
        dashboard plugin. Used in Django admin.

        :return string:
        """
        return ', '.join([u.username for u in self.users.all()])

    users_list.allow_tags = True
    users_list.short_description = _('Users')
Ejemplo n.º 7
0
class DashboardEntry(models.Model):
    """
    Dashboard entry (widget).

    Since workspace can be nullable (default), we duplicate the `layout_uid`.

    :Properties:

        - `user` (django.contrib.auth.models.User: User owning the plugin.
        - `workspace` (dash.models.DashboardWorkspace): Workspace to which the
          plugin belongs to. If left blank, entry belongs to default workspace.
        - `layout_uid` (str): Layout to which the entry belongs to.
        - `placeholder_uid` (str): Placeholder to which the entry belongs to.
        - `plugin_uid` (str): Plugin name.
        - `plugin_data` (str): JSON formatted string with plugin data.
        - `position` (int): Entry position.
    """
    user = models.ForeignKey(User, verbose_name=_("User"))
    workspace = models.ForeignKey(DashboardWorkspace, null=True, blank=True, \
                                  verbose_name=_("Workspace"), )
    layout_uid = models.CharField(_("Layout"), max_length=25, \
                                  choices=get_registered_layouts())
    placeholder_uid = models.CharField(_("Placeholder"), max_length=255)
    plugin_uid = models.CharField(_("Plugin name"), max_length=255, \
                                  choices=get_registered_plugins())
    plugin_data = models.TextField(verbose_name=_("Plugin data"), null=True, \
                                   blank=True)
    position = models.PositiveIntegerField(_("Position"),
                                           null=True,
                                           blank=True)

    objects = DashboardEntryManager()

    class Meta:
        verbose_name = _("Dashboard entry")
        verbose_name_plural = _("Dashboard entries")

    def __unicode__(self):
        return "{0} plugin for user {1}".format(self.plugin_uid, self.user)

    def get_plugin(self, fetch_related_data=False, request=None):
        """
        Gets the plugin class (by ``plugin_uid`` property), makes an instance
        of it, serves the data stored in ``plugin_data`` field (if available).
        Once all is done, plugin is ready to be rendered.

        :param bool fetch_related_data: When set to True, plugin is told to
            re-fetch all related data (stored in models or other sources).
        :return dash.base.DashboardPlugin: Subclass of
            ``dash.base.DashboardPlugin``.
        """
        # Getting plugin from registry.
        cls = plugin_registry.get(self.plugin_uid)

        if not cls:
            # No need to log here, since already logged in registry.
            return None

        # Creating plugin instance.
        plugin = cls(self.layout_uid,
                     self.placeholder_uid,
                     workspace=self.workspace,
                     user=self.user,
                     position=self.position)

        # So that plugin has the request object
        plugin.request = request

        return plugin.process(self.plugin_data,
                              fetch_related_data=fetch_related_data)

    def plugin_uid_code(self):
        """
        Mainly used in admin.
        """
        return self.plugin_uid

    plugin_uid_code.allow_tags = True
    plugin_uid_code.short_description = _('UID')
Ejemplo n.º 8
0
class DashboardPluginAdminForm(forms.ModelForm):
    class Meta:
        model = DashboardPlugin
        exclude = ()

    plugin_uid = forms.ChoiceField(choices=get_registered_plugins())
Ejemplo n.º 9
0
 def plugin_uid_admin(self):
     """
     Mainly used in admin.
     """
     # TODO - shall be showing the dimensions.
     return "{0} ({1})".format(dict(get_registered_plugins()).get(self.plugin_uid, ''), self.plugin_uid)