#329 Add a human-readable label to widgets, and use it in the UI
Merged 7 years ago by abompard. Opened 7 years ago by abompard.
abompard/fedora-hubs widget-label  into  develop

@@ -12,8 +12,8 @@ 

        </div>

        <div class="modal-body">

          <select id="widget" class="c-select" name="widget">

-             {% for w in widgets %}

-             <option value="{{ w }}">{{ w }}</option>

+             {% for widget in widgets %}

+             <option value="{{ widget.name }}">{{ widget.label }}</option>

              {% endfor %}

          </select>

        </div>

@@ -42,10 +42,10 @@ 

              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"

file modified
+3 -3
@@ -18,15 +18,15 @@ 

      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():

@@ -28,7 +28,7 @@ 

      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

file modified
+8 -2
@@ -56,6 +56,8 @@ 

          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 @@ 

      """

  

      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 @@ 

  

          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 +

@@ -14,6 +14,7 @@ 

  class Bugzilla(Widget):

  

      name = "bugzilla"

+     label = "Bugzilla issues"

      position = "right"

      parameters = [dict(

          name="username",
@@ -33,7 +34,7 @@ 

      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()

              )

@@ -16,6 +16,7 @@ 

  class FedmsgStats(Widget):

  

      name = "fedmsgstats"

+     label = "Fedmsg stats"

      position = "both"

      parameters = [dict(

          name="username",

@@ -11,6 +11,7 @@ 

  class Feed(Widget):

  

      name = "feed"

+     label = "Live feed"

      position = "left"

      parameters = [

          {
@@ -40,7 +41,7 @@ 

          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,

@@ -10,6 +10,7 @@ 

  class FedoraHosted(Widget):

  

      name = "fhosted"

+     label = "Fedorahosted: Open Tickets"

      position = "right"

      parameters = [

          dict(
@@ -36,7 +37,7 @@ 

      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())

@@ -16,6 +16,7 @@ 

  class GitHubPRs(Widget):

  

      name = "github_pr"

+     label = "Github: Pull Requests"

      position = "right"

      parameters = [

          dict(
@@ -45,7 +46,7 @@ 

          context = dict(

              organization=org,

              display_number=instance.config["display_number"],

-             title="Github: Pull Requests",

+             title=self.widget.label,

          )

          context.update(get_prs())

          return context

@@ -10,6 +10,7 @@ 

  class GitHubIssues(Widget):

  

      name = "githubissues"

+     label = "Github: Newest Open Tickets"

      position = "right"

      parameters = [

          dict(
@@ -45,7 +46,7 @@ 

              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(),

          )

  

@@ -33,6 +33,6 @@ 

              if u.strip()

          ]

          return dict(

-             title="Library",

+             title=self.widget.label,

              urls=urls,

              )

@@ -7,6 +7,7 @@ 

  class Linechart(Widget):

  

      name = "linechart"

+     label = "Weekly Activity"

      position = "left"

      parameters = [dict(

          name="username",
@@ -41,6 +42,6 @@ 

          url = url + "&" + categories

          url = url.format(username=username)

          return dict(

-             title="Weekly Activity",

+             title=self.widget.label,

              url=url,

              )

@@ -46,7 +46,7 @@ 

      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(),

              )

@@ -11,6 +11,7 @@ 

  class Memberships(Widget):

  

      name = "memberships"

+     label = "Hubs"

      position = "both"

  

  
@@ -38,7 +39,7 @@ 

          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),

              )

@@ -12,6 +12,7 @@ 

  class PagurePRs(Widget):

  

      name = "pagure_pr"

+     label = "Pagure: Newest Open Pull Requests"

      position = "right"

      parameters = [

          dict(
@@ -32,7 +33,7 @@ 

      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())

@@ -12,6 +12,7 @@ 

  class PagureIssues(Widget):

  

      name = "pagureissues"

+     label = "Pagure: Newest Open Tickets"

      position = "right"

      parameters = [

          dict(
@@ -32,7 +33,7 @@ 

      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())

@@ -7,6 +7,7 @@ 

  class Sticky(Widget):

  

      name = "sticky"

+     label = "Sticky Note"

      position = "both"

      parameters = [

          dict(
@@ -28,6 +29,6 @@ 

          # TODO -- render with markdown

          return dict(

              text=instance.config["text"],

-             title="Sticky Note",

+             title=self.widget.label,

              panel_css_class="card-info",

              )

@@ -11,6 +11,7 @@ 

  class PendingACLs(Widget):

  

      name = "workflow.pendingacls"

+     label = 'Pending ACL Requests'

      position = "right"

      parameters = [

          dict(
@@ -32,7 +33,7 @@ 

      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())

@@ -15,6 +15,7 @@ 

  class Updates2Stable(Widget):

  

      name = "workflow.updates2stable"

+     label = 'Updates Ready for Stable'

      position = "right"

      parameters = [

          dict(
@@ -36,7 +37,7 @@ 

      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())