From 77bfe56dd07f5dd658c2fbd9d01c463b8b19485e Mon Sep 17 00:00:00 2001
From: Dhriti Shikhar <dhrish20@gmail.com>
Date: Nov 28 2015 15:47:36 +0000
Subject: Adds a pagure issues widget


---

diff --git a/hubs/defaults.py b/hubs/defaults.py
index ce29c74..fc47746 100644
--- a/hubs/defaults.py
+++ b/hubs/defaults.py
@@ -20,7 +20,7 @@ def add_user_widgets(session, hub, username, fullname):
             'username': username,
             'fmn_context': 'irc',  # TODO -- make this 'hubs'
         }))
-    hub.widgets.append(widget)
+    hub.widgets.append(widget)    
 
     # Right Side Widgets
     widget = hubs.models.Widget(
@@ -53,4 +53,10 @@ def add_user_widgets(session, hub, username, fullname):
             'username': username,
         }))
     hub.widgets.append(widget)
+    widget = hubs.models.Widget(
+        plugin='pagureissues', index=5,
+        _config=json.dumps({
+            'repo': 'pagure',
+        }))
+    hub.widgets.append(widget)
     return hub
diff --git a/hubs/validators.py b/hubs/validators.py
index 5bdf053..8248b29 100644
--- a/hubs/validators.py
+++ b/hubs/validators.py
@@ -22,3 +22,6 @@ def fmn_context(session, value):
     return value in [
         'irc', 'email', 'android', 'desktop', 'hubs',
     ]
+
+def repo(session, value):
+    return value
diff --git a/hubs/widgets/__init__.py b/hubs/widgets/__init__.py
index d3ae5b5..be7d384 100644
--- a/hubs/widgets/__init__.py
+++ b/hubs/widgets/__init__.py
@@ -11,6 +11,7 @@ from hubs.widgets import feed
 from hubs.widgets import subscriptions
 from hubs.widgets import cobwebs
 from hubs.widgets import meetings
+from hubs.widgets import pagureissues
 
 from hubs.widgets.workflow import pendingacls
 from hubs.widgets.workflow import updates2stable
@@ -31,6 +32,7 @@ registry = {
     'subscriptions': subscriptions,
     'cobwebs': cobwebs,
     'meetings': meetings,
+    'pagureissues': pagureissues,
 
     'workflow.pendingacls': pendingacls,
     'workflow.updates2stable': updates2stable,
diff --git a/hubs/widgets/pagureissues.py b/hubs/widgets/pagureissues.py
new file mode 100644
index 0000000..f64f0b4
--- /dev/null
+++ b/hubs/widgets/pagureissues.py
@@ -0,0 +1,150 @@
+from hubs.hinting import hint
+from hubs.widgets.chrome import panel
+from hubs.widgets.base import argument
+import hubs.validators as validators
+import jinja2
+import requests
+
+chrome = panel("Pagure: Newest Open Tickets")
+
+pagure_url = "https://pagure.io/api/0"
+
+template = jinja2.Template("""
+  <br /> 
+  {% if message == "None" %}
+    <div class="container">
+      {% if total<3 %}
+        {% for i in range(0,total) %}
+          <div> 
+            <table class="table-responsive table-condensed">
+              <tr>
+                <td valign="top" class="panel-heading">
+                  <b>#{{ all_issues[i]['id'] }}</b>
+                </td>
+                <td width="260px">
+                  <b>{{ all_issues[i]['title'] }}</b>
+                </td>
+              </tr>
+            </table>
+            <table class="table-responsive table-condensed">
+              <tr>
+                <td width="260px">
+                  <font size="2">
+                    <i>Opened by <a href="https://pagure.io/user/{{ all_issues[i]['user'] }}" target="_blank">{{ all_issues[i]['user'] }}</a></i>
+                    {% if all_issues[i]['assignee']|string() == "None" %}
+                      <br /><i>Unclaimed</i>
+                    {% else %}
+                      <br /><i>Assigned by <a href="https://pagure.io/user/{{ all_issues[i]['assignee'] }}" target="_blank">{{ all_issues[i]['assignee'] }}</a></i>
+                    {% endif %}
+                  </font>               
+                </td>
+                <td>
+	          <a href="https://pagure.io/{{ repo }}/issue/{{ all_issues[i]['id'] }}" target="_blank">
+                    <button type="button" class="btn btn-default" aria-label="Left Align">
+                      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
+                    </button>
+	          </a>
+                </td>
+              </tr>
+            </table>
+          </div>
+        {% endfor %}
+      {% else %}
+        {% for i in range(0,3) %}
+          <div> 
+            <table class="table-responsive table-condensed">
+              <tr>
+                <td valign="top" class="panel-heading">
+                  <b>#{{ all_issues[i]['id'] }}</b>
+                </td>
+                <td width="260px">
+                  <b>{{ all_issues[i]['title'] }}</b>
+                </td>
+              </tr>
+            </table>
+            <table class="table-responsive table-condensed">
+              <tr>
+                <td width="260px">
+                  <font size="2">
+                    <i>Opened by <a href="https://pagure.io/user/{{ all_issues[i]['user'] }}" target="_blank">{{ all_issues[i]['user'] }}</a></i>
+                    {% if all_issues[i]['assignee']|string() == "None" %}
+                      <br /><i>Unclaimed</i>
+                    {% else %}
+                      <br /><i>Assigned to <a href="https://pagure.io/user/{{ all_issues[i]['assignee'] }}" target="_blank">{{ all_issues[i]['assignee'] }}</a></i>
+                    {% endif %}
+                  </font>               
+                </td>
+                <td>
+	          <a href="https://pagure.io/{{ repo }}/issue/{{ all_issues[i]['id'] }}" target="_blank">
+                    <button type="button" class="btn btn-default" aria-label="Left Align">
+                      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
+                    </button>
+	          </a>
+                </td>
+              </tr>
+            </table>
+          </div>
+        {% endfor %}
+      {% endif %}
+    </div>    
+    <div class="row">
+      <a href="https://pagure.io/{{ repo }}/issues" target="_blank"><center>All Issues</center></a>
+    </div>
+  {% else %}
+    <div class="container">
+      <b>Not a valid repo</b>
+    </div>
+  {% endif %}
+""")
+
+@argument(name="repo",
+          default=None,
+          validator=validators.repo,
+          help="Repo")
+def data(session, widget, repo):
+    repo = "pagure"
+
+    projects_response = requests.get("https://pagure.io/api/0/projects")
+    projects_data = projects_response.json()
+    total_projects = projects_data['total_projects']
+    all_projects = list()
+    for i in range(0, total_projects):
+      all_projects.append(projects_data['projects'][i]['name'])
+    if repo not in all_projects:
+      message = "Not a valid project"
+      return dict(
+        message=message,
+      )
+    else:
+      message = "None"
+      url = '/'.join([pagure_url, repo, "issues"])
+      issue_response = requests.get(url)
+      data = issue_response.json()
+      total = data['total_issues']
+      all_issues = list()
+      for i in range(0,total):
+        issue_details = dict()
+        issue_id=data['issues'][i]['id']
+        issue_title=data['issues'][i]['title']
+        issue_user=data['issues'][i]['user']['name']
+        if(data['issues'][i]['assignee']!=None):
+          issue_assignee=data['issues'][i]['assignee']['name']
+        else:
+          issue_assignee=data['issues'][i]['assignee']
+        issue_details['id']=issue_id
+        issue_details['title']=issue_title
+        issue_details['user']=issue_user
+        issue_details['assignee']=issue_assignee
+        all_issues.append(issue_details)
+      all_issues.reverse()
+      return dict(
+        repo=repo,
+        total=total,
+        all_issues=all_issues,
+        message = message,
+      )
+ 
+
+@hint()
+def should_invalidate(message, session, widget):
+   raise NotImplementedError

