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 %} +
No project in watch list
+No group found
'), 1) self.assertEqual( - output.data.count('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)