def test_01_registered_layouts(self): """ Test registered layouts (`get_registered_layouts`). """ res = get_registered_layouts() self.assertTrue(len(res) > 0) return res
class DashboardSettings(models.Model): """ Dashboard settings. :Properties: - `user` (django.contrib.auth.models.User: User owning the plugin. - `layout_uid` (str): Users' preferred layout. - `title` (str): Dashboard title. - `is_public` (bool): If set to True, available as public (read-only mode). """ user = models.ForeignKey(User, verbose_name=_("User"), unique=True) layout_uid = models.CharField(_("Layout"), max_length=25, \ choices=get_registered_layouts()) title = models.CharField(_("Title"), max_length=255) allow_different_layouts = models.BooleanField(_("Allow different layouts per workspace?"), default=False, \ help_text=_("Allows you to use different layouts for each " "workspace.")) is_public = models.BooleanField(_("Is public?"), default=False, \ help_text=_("Makes your dashboard to be " "visible to the public. " "Visibility of workspaces " "could be adjust separately " "for each workspace, however " "setting your dashboard to be " "visible to public, makes " "your default workspace " "visible to public too.")) class Meta: verbose_name = _("Dashboard settings") verbose_name_plural = _("Dashboard settings") def __unicode__(self): return self.title
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())
class DashboardWorkspace(models.Model): """ Dashboard workspace. :Properties: - `user` (django.contrib.auth.models.User: User owning the plugin. - `layout_uid` (str): Layout to which the entry belongs to. - `name` (str): Dashboard name. - `slug` (str): Dashboard slug. - `position` (int): Dashboard position. - `is_public` (int): If set to True, is visible to public. - `is_clonable` (bool): If set to True, is clonable. - `shared_with` (django.db.models.ManyToManyField): Users the workspace shared with. If workspace is shared with specific user, then the user it's shared with can also clone the workspace. """ user = models.ForeignKey(User, verbose_name=_("User")) layout_uid = models.CharField(_("Layout"), max_length=25, \ choices=get_registered_layouts()) name = models.CharField(_("Name"), max_length=255) slug = AutoSlugField(populate_from='name', verbose_name=_("Slug"), \ unique=True, slugify=slugify_workspace) position = OrderField(_("Position"), null=True, blank=True) is_public = models.BooleanField(_("Is public?"), default=False, \ help_text=_("Makes your workspace to be " "visible to the public.")) is_clonable = models.BooleanField(_("Is cloneable?"), default=False, \ help_text=_("Makes your workspace to be " "cloneable by other users.")) class Meta: verbose_name = _("Dashboard workspace") verbose_name_plural = _("Dashboard workspaces") unique_together = ( ('user', 'slug'), ('user', 'name'), ) def __unicode__(self): return self.name def get_entries(self, user): """ Gets all dashboard entries for user given. :param django.contrib.auth.models.User user: :return iterable: """ return DashboardEntry._default_manager.get_for_workspace( user=self.user, layout_uid=self.layout_uid, workspace=self.slug) def get_absolute_url(self): """ Absolute URL, which goes to the dashboard workspace page. :return string: """ return reverse('dash.dashboard', kwargs={'workspace': self.slug})
class DashboardSettingsForm(forms.ModelForm): """ Dashboard settings form. """ class Meta: model = DashboardSettings fields = ('user', 'layout_uid', 'title', 'is_public') layout_uid = forms.ChoiceField(choices=get_registered_layouts()) def __init__(self, *args, **kwargs): super(DashboardSettingsForm, self).__init__(*args, **kwargs) self.fields['user'].widget = forms.widgets.HiddenInput()
class DashboardWorkspaceForm(forms.ModelForm): """ Dashboard workspace form. """ layout_uid = forms.TypedChoiceField( label=_('Layout'), choices=get_registered_layouts(), empty_value=None, ) class Meta: model = DashboardWorkspace fields = ('layout_uid', 'user', 'name', 'is_public', 'is_clonable') def __init__(self, *args, **kwargs): different_layouts = kwargs.pop('different_layouts', False) super(DashboardWorkspaceForm, self).__init__(*args, **kwargs) self.fields['user'].widget = forms.widgets.HiddenInput() if not different_layouts: self.fields['layout_uid'].widget = forms.widgets.HiddenInput()
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')
class DashboardSettingsAdminForm(forms.ModelForm): class Meta: model = DashboardSettings exclude = () layout_uid = forms.ChoiceField(choices=get_registered_layouts())