From 049c2a137368b4c729097e895b88610889cd24a9 Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Mar 15 2017 13:46:16 +0000
Subject: [PATCH 1/2] watch: add reset button for watch feature


Signed-off-by: Vivek Anand <vivekanand1101@gmail.com>

---

diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py
index b4aad6d..0c555f7 100644
--- a/pagure/lib/__init__.py
+++ b/pagure/lib/__init__.py
@@ -3485,6 +3485,14 @@ def update_watch_status(session, project, user, watch):
         )
     ).first()
 
+    if watch == '-1':
+        if not watcher:
+            return 'Watch status is already reset'
+
+        session.delete(watcher)
+        session.flush()
+        return 'Watch status reset'
+
     if not watcher:
         watcher = model.Watcher(
             project_id=project.id,
diff --git a/pagure/templates/repo_master.html b/pagure/templates/repo_master.html
index 0a0f0bd..4a1580c 100644
--- a/pagure/templates/repo_master.html
+++ b/pagure/templates/repo_master.html
@@ -90,13 +90,14 @@
                   repo.user.user if repo.is_fork else None,
                   repo.namespace)
                %}
-               <a class="dropdown-item" id="unwatch_button">Stop watching {{
+               <a class="dropdown-item" id="unwatch_button">Unwatch {{
                   repo.name }}</a>
                {% else %}
                <a class="dropdown-item" id="watch_button">Watch {{
                   repo.name }}</a>
                {% endif -%}
-
+               <a class="dropdown-item" id="reset_button">Reset Watch Status {{
+                  repo.name }}</a>
                 {{ forkbuttonform.csrf_token }}
               </form>
             </div>
@@ -305,10 +306,14 @@ $(document).ready(function() {
   $(".watch-menu a").click(function(){
     var selectedValue = $(this).attr('id');
     var action = $("#watch_project").attr('action');
-      if (selectedValue != "unwatch_button") {
+      if (selectedValue === "watch_button") {
         action = action.replace('/settings/0', '/settings/1');
         $('#watch_project').attr('action', action);
+      } else if (selectedValue === "reset_button") {
+        action = action.replace('/settings/0', '/settings/-1');
+        $('#watch_project').attr('action', action);
       }
+
       $('#watch_project').submit();
 
   });
diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py
index f2bb386..46f9e3e 100644
--- a/pagure/ui/repo.py
+++ b/pagure/ui/repo.py
@@ -2342,7 +2342,7 @@ def watch_repo(repo, watch, username=None, namespace=None):
     if not form.validate_on_submit():
         flask.abort(400)
 
-    if str(watch) not in ['0', '1']:
+    if str(watch) not in ['0', '1', '-1']:
         flask.abort(400)
 
     try:

From 5ba4e2fdbe152ec447828df92ecbf981b18d6c7c Mon Sep 17 00:00:00 2001
From: Vivek Anand <vivekanand1101@gmail.com>
Date: Mar 15 2017 13:46:16 +0000
Subject: [PATCH 2/2] watch feature: Unit test for reset option


Signed-off-by: Vivek Anand <vivekanand1101@gmail.com>

---

diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py
index 95f3784..aa81748 100644
--- a/tests/test_pagure_flask_ui_repo.py
+++ b/tests/test_pagure_flask_ui_repo.py
@@ -4134,6 +4134,13 @@ index 0000000..fb7093d
             pygit2.init_repository(gitrepo, bare=True)
 
             output = self.app.post(
+                '/fork/foo/test/watch/settings/-1', data=data,
+                follow_redirects=True)
+            self.assertIn(
+                '</button>\n                      Watch status is already reset',
+                output.data)
+
+            output = self.app.post(
                 '/fork/foo/test/watch/settings/0', data=data,
                 follow_redirects=True)
             self.assertIn(
@@ -4147,6 +4154,14 @@ index 0000000..fb7093d
                 '</button>\n                      You are now'
                 ' watching this repo.', output.data)
 
+            output = self.app.post(
+                '/fork/foo/test/watch/settings/-1', data=data,
+                follow_redirects=True)
+            self.assertIn(
+                '</button>\n                      Watch status reset',
+                output.data)
+
+
     def test_delete_report(self):
         """ Test the  delete_report endpoint. """
 
diff --git a/tests/test_pagure_lib.py b/tests/test_pagure_lib.py
index 5d22525..0697f5b 100644
--- a/tests/test_pagure_lib.py
+++ b/tests/test_pagure_lib.py
@@ -3034,6 +3034,16 @@ class PagureLibtests(tests.Modeltests):
             watch=True,
         )
 
+        # All good and when user seleted reset watch option.
+        msg = pagure.lib.update_watch_status(
+            session=self.session,
+            project=project,
+            user='pingou',
+            watch='-1',
+        )
+        self.session.commit()
+        self.assertEqual(msg, 'Watch status is already reset')
+
         # All good and when user seleted watch option.
         msg = pagure.lib.update_watch_status(
             session=self.session,
@@ -3054,6 +3064,16 @@ class PagureLibtests(tests.Modeltests):
         self.session.commit()
         self.assertEqual(msg, 'You are no longer watching this repo.')
 
+        # All good and when user seleted reset watch option.
+        msg = pagure.lib.update_watch_status(
+            session=self.session,
+            project=project,
+            user='pingou',
+            watch='-1',
+        )
+        self.session.commit()
+        self.assertEqual(msg, 'Watch status reset')
+
     def test_is_watching(self):
         """ Test the is_watching method of pagure.lib. """
         tests.create_projects(self.session)

