diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 54f67c1..0bd36f6 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2946,3 +2946,44 @@ def is_watching(session, user, reponame, repouser=None): return True return False + + +def user_watch_list(session, user): + ''' Returns list of all the projects which the user is watching ''' + + user_obj = search_user(session, username=user) + if not user_obj: + return [] + + unwatched = session.query( + model.Watcher + ).filter( + model.Watcher.user_id == user_obj.id + ).filter( + model.Watcher.watch == False + ) + + unwatched_list = [] + if unwatched: + unwatched_list = [unwatch.project for unwatch in unwatched.all()] + + watched = session.query( + model.Watcher + ).filter( + model.Watcher.user_id == user_obj.id + ).filter( + model.Watcher.watch == True + ) + + watched_list = [] + if watched: + watched_list = [watch.project for watch in watched.all()] + + user_projects = search_projects(session, username=user_obj.user) + watch = set(watched_list + user_projects) + + for project in user_projects: + if project in unwatched_list: + watch.remove(project) + + return sorted(list(watch), key=lambda proj: proj.name) diff --git a/pagure/templates/index_auth.html b/pagure/templates/index_auth.html index 7f87988..f8d082f 100644 --- a/pagure/templates/index_auth.html +++ b/pagure/templates/index_auth.html @@ -189,6 +189,34 @@ {% endfor %} +
+
+ My Watch List {{ watch_list | count }} +
+ {% for repo in watch_list %} +
+ {% if repo.is_fork %} + +
+  {{ repo.user.username }}/{{ repo.name }} +
+
+ {% else %} + +
+  {{ repo.name }} +
+
+ {% endif %} +
+ {% else %} +
+

No project in watch list

+
+ {% endfor %} +
diff --git a/pagure/ui/app.py b/pagure/ui/app.py index b8568bb..17475fb 100644 --- a/pagure/ui/app.py +++ b/pagure/ui/app.py @@ -116,12 +116,17 @@ def index_auth(): fork=True, count=True) + watch_list = pagure.lib.user_watch_list( + SESSION, + user=flask.g.fas_user.username) + return flask.render_template( 'index_auth.html', username=flask.g.fas_user.username, user=user, forks=forks, repos=repos, + watch_list=watch_list, repopage=repopage, forkpage=forkpage, repos_length=repos_length, diff --git a/tests/test_pagure_flask_ui_app.py b/tests/test_pagure_flask_ui_app.py index 1236f14..eb08845 100644 --- a/tests/test_pagure_flask_ui_app.py +++ b/tests/test_pagure_flask_ui_app.py @@ -91,7 +91,36 @@ class PagureFlaskApptests(tests.Modeltests): self.assertEqual( output.data.count('

No group found

'), 1) self.assertEqual( - output.data.count('
'), 3) + output.data.count('
'), 4) + + def test_watch_list(self): + ''' Test for watch list of a user ''' + + user = tests.FakeUser(username='pingou') + with tests.user_set(pagure.APP, user): + output = self.app.get('/') + self.assertIn( + '
You have no projects
', + output.data) + self.assertIn( + '

You have no forks

', + output.data) + self.assertIn( + '

No project in watch list

', + output.data) + + tests.create_projects(self.session) + + output = self.app.get('/') + self.assertIn( + 'My Projects 2', + output.data) + self.assertIn( + 'My Forks 0', + output.data) + self.assertIn( + 'My Watch List 2', + output.data) def test_view_users(self): """ Test the view_users endpoint. """ diff --git a/tests/test_pagure_lib.py b/tests/test_pagure_lib.py index 6952975..85fc630 100644 --- a/tests/test_pagure_lib.py +++ b/tests/test_pagure_lib.py @@ -2483,6 +2483,40 @@ class PagureLibtests(tests.Modeltests): self.assertFalse(watch) + def test_user_watch_list(self): + ''' test user watch list method of pagure.lib ''' + + tests.create_projects(self.session) + + # He should be watching + user = tests.FakeUser() + user.username = 'pingou' + watch_list_objs = pagure.lib.user_watch_list( + session=self.session, + user='pingou', + ) + watch_list = [obj.name for obj in watch_list_objs] + self.assertEqual(watch_list, ['test', 'test2']) + + # He isn't in the db, thus not watching anything + user.username = 'vivek' + watch_list_objs = pagure.lib.user_watch_list( + session=self.session, + user='vivek', + ) + watch_list = [obj.name for obj in watch_list_objs] + self.assertEqual(watch_list, []) + + # He shouldn't be watching anything + user.username = 'foo' + watch_list_objs = pagure.lib.user_watch_list( + session=self.session, + user='foo', + ) + watch_list = [obj.name for obj in watch_list_objs] + self.assertEqual(watch_list, []) + + if __name__ == '__main__': SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureLibtests) unittest.TextTestRunner(verbosity=2).run(SUITE)