From 3e4daa2355826f6aa4596d01a82ad2d1a4edf415 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Feb 23 2017 09:15:05 +0000 Subject: Add a human-readable label to widgets, and use it in the UI --- diff --git a/hubs/templates/add_widget.html b/hubs/templates/add_widget.html index ce20d36..a194aa3 100644 --- a/hubs/templates/add_widget.html +++ b/hubs/templates/add_widget.html @@ -12,8 +12,8 @@ diff --git a/hubs/tests/test_widget_base.py b/hubs/tests/test_widget_base.py index 9b53e57..9d0e64d 100644 --- a/hubs/tests/test_widget_base.py +++ b/hubs/tests/test_widget_base.py @@ -42,10 +42,10 @@ class WidgetTest(APPTest): views = {} def get_views(self): return self.views - test_widget = LocalTestWidget() self.assertRaisesRegexp( - AttributeError, '.* "name" .*', test_widget.validate) - test_widget.name = "invalid name \xe2\x98\xba" + AttributeError, '.* "name" .*', LocalTestWidget) + LocalTestWidget.name = "invalid name \xe2\x98\xba" + test_widget = LocalTestWidget() self.assertRaisesRegexp( AttributeError, '^invalid widget name: ', test_widget.validate) test_widget.name = "localtest" diff --git a/hubs/views/widget.py b/hubs/views/widget.py index 1d21e17..39bb8f7 100644 --- a/hubs/views/widget.py +++ b/hubs/views/widget.py @@ -18,15 +18,15 @@ def hub_add_widget(name): position = get_position() widgets = [ widget - for widget in registry - if registry[widget].position in ['both', position] + for widget in registry.values() + if widget.position in ['both', position] ] if flask.request.method == 'POST': widget_name = flask.request.form.get('widget') if not widget_name: flask.abort(400, 'Invalid request sent') - if widget_name not in widgets: + if widget_name not in [w.name for w in widgets]: flask.abort(404, 'Unknown widget called') widget = registry[widget_name] if widget.get_parameters(): diff --git a/hubs/widgets/badges/__init__.py b/hubs/widgets/badges/__init__.py index c609776..00eae24 100644 --- a/hubs/widgets/badges/__init__.py +++ b/hubs/widgets/badges/__init__.py @@ -28,7 +28,7 @@ class BaseView(WidgetView): template_name = "badges.html" def get_context(self, instance, *args, **kwargs): - context = {"title": "Badges"} + context = {"title": self.widget.label} get_badges = GetBadges(instance) context.update(get_badges()) return context diff --git a/hubs/widgets/base.py b/hubs/widgets/base.py index c2a8dca..aa8c5b8 100644 --- a/hubs/widgets/base.py +++ b/hubs/widgets/base.py @@ -56,6 +56,8 @@ class Widget(object): name (str): The widget name. It will not be displayed in the UI, but will appear in some URLs, so be careful to only use simple, URL-compatible characters. + label (str): A humanized name for the widget, which will be shown in + the UI. position (str): The position of the widget in the rendered page. It should be one of the following values: ``left``, ``right``, or ``both``. @@ -70,12 +72,18 @@ class Widget(object): """ name = None + label = None position = None parameters = [] views_module = None cached_functions_module = None def __init__(self): + if self.name is None: + raise AttributeError('widgets must have a "name" attribute') + if self.label is None: + # Try to be smart-ish with the default label. + self.label = self.name.replace("_", " ").replace(".", ": ").title() self._template_environment = None def validate(self): @@ -84,8 +92,6 @@ class Widget(object): Raises: AttributeError """ - if self.name is None: - raise AttributeError('widgets must have a "name" attribute') if not re.match('^[\w_.-]+$', self.name): raise AttributeError( 'invalid widget name: %r. ' % self.name + diff --git a/hubs/widgets/bugzilla/__init__.py b/hubs/widgets/bugzilla/__init__.py index 99d02f1..a9e0c4f 100644 --- a/hubs/widgets/bugzilla/__init__.py +++ b/hubs/widgets/bugzilla/__init__.py @@ -14,6 +14,7 @@ PKGDB_URL = "https://admin.fedoraproject.org/pkgdb/api/packager/package" class Bugzilla(Widget): name = "bugzilla" + label = "Bugzilla issues" position = "right" parameters = [dict( name="username", @@ -33,7 +34,7 @@ class BaseView(WidgetView): def get_context(self, instance, *args, **kwargs): get_issues = GetIssues(instance) return dict( - title="Bugzilla: Issues", + title=self.widget.label, username=instance.config["username"], issues=get_issues() ) diff --git a/hubs/widgets/fedmsgstats/__init__.py b/hubs/widgets/fedmsgstats/__init__.py index 0b68bcb..ac1ba24 100644 --- a/hubs/widgets/fedmsgstats/__init__.py +++ b/hubs/widgets/fedmsgstats/__init__.py @@ -16,6 +16,7 @@ fedmsg_config = fedmsg.config.load_config() class FedmsgStats(Widget): name = "fedmsgstats" + label = "Fedmsg stats" position = "both" parameters = [dict( name="username", diff --git a/hubs/widgets/feed/__init__.py b/hubs/widgets/feed/__init__.py index 30b39eb..ebea21b 100644 --- a/hubs/widgets/feed/__init__.py +++ b/hubs/widgets/feed/__init__.py @@ -11,6 +11,7 @@ log = logging.getLogger('hubs') class Feed(Widget): name = "feed" + label = "Live feed" position = "left" parameters = [ { @@ -40,7 +41,7 @@ class BaseView(WidgetView): username = instance.config["username"] feed_url = app.config['SSE_URL'] + username return dict( - title="Live Feed", + title=self.widget.label, matches=[], message_limit=instance.config["message_limit"], feed_url=feed_url, diff --git a/hubs/widgets/fhosted/__init__.py b/hubs/widgets/fhosted/__init__.py index 7b83881..38e69e6 100644 --- a/hubs/widgets/fhosted/__init__.py +++ b/hubs/widgets/fhosted/__init__.py @@ -10,6 +10,7 @@ from hubs.widgets.caching import CachedFunction class FedoraHosted(Widget): name = "fhosted" + label = "Fedorahosted: Open Tickets" position = "right" parameters = [ dict( @@ -36,7 +37,7 @@ class BaseView(WidgetView): def get_context(self, instance, *args, **kwargs): get_tickets = GetTickets(instance) context = dict( - title="Fedorahosted: Open Tickets", + title=self.widget.label, project=instance.config["project"], ) context.update(get_tickets()) diff --git a/hubs/widgets/github_pr/__init__.py b/hubs/widgets/github_pr/__init__.py index 1d41502..07374fb 100644 --- a/hubs/widgets/github_pr/__init__.py +++ b/hubs/widgets/github_pr/__init__.py @@ -16,6 +16,7 @@ log = logging.getLogger(__name__) class GitHubPRs(Widget): name = "github_pr" + label = "Github: Pull Requests" position = "right" parameters = [ dict( @@ -45,7 +46,7 @@ class BaseView(WidgetView): context = dict( organization=org, display_number=instance.config["display_number"], - title="Github: Pull Requests", + title=self.widget.label, ) context.update(get_prs()) return context diff --git a/hubs/widgets/githubissues/__init__.py b/hubs/widgets/githubissues/__init__.py index 39d5aa0..4da6f9f 100644 --- a/hubs/widgets/githubissues/__init__.py +++ b/hubs/widgets/githubissues/__init__.py @@ -10,6 +10,7 @@ from hubs.widgets.caching import CachedFunction class GitHubIssues(Widget): name = "githubissues" + label = "Github: Newest Open Tickets" position = "right" parameters = [ dict( @@ -45,7 +46,7 @@ class BaseView(WidgetView): org=instance.config["org"], repo=instance.config["repo"], display_number=instance.config["display_number"], - title="Github: Newest Open Tickets", + title=self.widget.label, all_issues=get_issues(), ) diff --git a/hubs/widgets/library/__init__.py b/hubs/widgets/library/__init__.py index e1d4511..803b56a 100644 --- a/hubs/widgets/library/__init__.py +++ b/hubs/widgets/library/__init__.py @@ -33,6 +33,6 @@ class BaseView(WidgetView): if u.strip() ] return dict( - title="Library", + title=self.widget.label, urls=urls, ) diff --git a/hubs/widgets/linechart/__init__.py b/hubs/widgets/linechart/__init__.py index e9765ca..49388a2 100644 --- a/hubs/widgets/linechart/__init__.py +++ b/hubs/widgets/linechart/__init__.py @@ -7,6 +7,7 @@ from hubs.widgets.base import Widget, WidgetView class Linechart(Widget): name = "linechart" + label = "Weekly Activity" position = "left" parameters = [dict( name="username", @@ -41,6 +42,6 @@ class BaseView(WidgetView): url = url + "&" + categories url = url.format(username=username) return dict( - title="Weekly Activity", + title=self.widget.label, url=url, ) diff --git a/hubs/widgets/meetings/__init__.py b/hubs/widgets/meetings/__init__.py index 568a04b..b737f0a 100644 --- a/hubs/widgets/meetings/__init__.py +++ b/hubs/widgets/meetings/__init__.py @@ -46,7 +46,7 @@ class BaseView(WidgetView): def get_context(self, instance, *args, **kwargs): get_meetings = GetMeetings(instance) return dict( - title="Meetings", + title=self.widget.label, calendar=instance.config["calendar"], meetings=get_meetings(), ) diff --git a/hubs/widgets/memberships/__init__.py b/hubs/widgets/memberships/__init__.py index 213f024..20f14c0 100644 --- a/hubs/widgets/memberships/__init__.py +++ b/hubs/widgets/memberships/__init__.py @@ -11,6 +11,7 @@ ELLIPSIS_LIMIT = 3 class Memberships(Widget): name = "memberships" + label = "Hubs" position = "both" @@ -38,7 +39,7 @@ class BaseView(WidgetView): oldest_members = sorted( members, key=lambda m: m.get('created_on'))[:ELLIPSIS_LIMIT] return dict( - title="Hubs", + title=self.widget.label, memberships=list(members), oldest_members=list(oldest_members), ) diff --git a/hubs/widgets/pagure_pr/__init__.py b/hubs/widgets/pagure_pr/__init__.py index 48c57a0..cce7322 100644 --- a/hubs/widgets/pagure_pr/__init__.py +++ b/hubs/widgets/pagure_pr/__init__.py @@ -12,6 +12,7 @@ pagure_url = "https://pagure.io/api/0" class PagurePRs(Widget): name = "pagure_pr" + label = "Pagure: Newest Open Pull Requests" position = "right" parameters = [ dict( @@ -32,7 +33,7 @@ class BaseView(WidgetView): def get_context(self, instance, *args, **kwargs): get_prs = GetPRs(instance) context = dict( - title="Newest Open Pull Requests on Pagure", + title=self.widget.label, repo=instance.config["repo"], ) context.update(get_prs()) diff --git a/hubs/widgets/pagureissues/__init__.py b/hubs/widgets/pagureissues/__init__.py index 7aa36c7..85f7110 100644 --- a/hubs/widgets/pagureissues/__init__.py +++ b/hubs/widgets/pagureissues/__init__.py @@ -12,6 +12,7 @@ pagure_url = "https://pagure.io/api/0" class PagureIssues(Widget): name = "pagureissues" + label = "Pagure: Newest Open Tickets" position = "right" parameters = [ dict( @@ -32,7 +33,7 @@ class BaseView(WidgetView): def get_context(self, instance, *args, **kwargs): get_issues = GetIssues(instance) context = dict( - title="Newest Open Tickets on Pagure", + title=self.widget.label, repo=instance.config["repo"], ) context.update(get_issues()) diff --git a/hubs/widgets/sticky/__init__.py b/hubs/widgets/sticky/__init__.py index baec759..ee22bc6 100644 --- a/hubs/widgets/sticky/__init__.py +++ b/hubs/widgets/sticky/__init__.py @@ -7,6 +7,7 @@ from hubs.widgets.base import Widget, WidgetView class Sticky(Widget): name = "sticky" + label = "Sticky Note" position = "both" parameters = [ dict( @@ -28,6 +29,6 @@ class BaseView(WidgetView): # TODO -- render with markdown return dict( text=instance.config["text"], - title="Sticky Note", + title=self.widget.label, panel_css_class="card-info", ) diff --git a/hubs/widgets/workflow/pendingacls.py b/hubs/widgets/workflow/pendingacls.py index 40c19ec..b30a208 100644 --- a/hubs/widgets/workflow/pendingacls.py +++ b/hubs/widgets/workflow/pendingacls.py @@ -11,6 +11,7 @@ from hubs.widgets.caching import CachedFunction class PendingACLs(Widget): name = "workflow.pendingacls" + label = 'Pending ACL Requests' position = "right" parameters = [ dict( @@ -32,7 +33,7 @@ class BaseView(WidgetView): def get_context(self, instance, *args, **kwargs): get_pending = GetPending(instance) context = dict( - title='Pending ACL Requests', + title=self.widget.label, username=instance.config["username"], ) context.update(get_pending()) diff --git a/hubs/widgets/workflow/updates2stable.py b/hubs/widgets/workflow/updates2stable.py index 4f6f462..6f36260 100644 --- a/hubs/widgets/workflow/updates2stable.py +++ b/hubs/widgets/workflow/updates2stable.py @@ -15,6 +15,7 @@ giveaway = 'can be pushed to stable now' class Updates2Stable(Widget): name = "workflow.updates2stable" + label = 'Updates Ready for Stable' position = "right" parameters = [ dict( @@ -36,7 +37,7 @@ class BaseView(WidgetView): def get_context(self, instance, *args, **kwargs): get_pending = GetPending(instance) context = dict( - title='Updates Ready for Stable', + title=self.widget.label, username=instance.config["username"], ) context.update(get_pending())