From beb13102a3643715fd2718d5baa51e5e38004cf0 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 24 Jun 2023 19:15:49 +0000 Subject: [PATCH] users: Add index page Signed-off-by: Michael Tremer --- Makefile.am | 10 ++- src/backend/accounts.py | 42 +++++++++- .../people/modules/accounts-new.html | 5 -- src/templates/people/users.html | 13 --- src/templates/users/index.html | 81 +++++++++++++++++++ src/templates/users/modules/list.html | 29 +++++++ src/web/__init__.py | 6 +- src/web/people.py | 23 ------ src/web/users.py | 22 +++++ 9 files changed, 183 insertions(+), 48 deletions(-) delete mode 100644 src/templates/people/modules/accounts-new.html delete mode 100644 src/templates/people/users.html create mode 100644 src/templates/users/index.html create mode 100644 src/templates/users/modules/list.html diff --git a/Makefile.am b/Makefile.am index e08d4b5d..0c821370 100644 --- a/Makefile.am +++ b/Makefile.am @@ -273,8 +273,7 @@ templates_people_DATA = \ src/templates/people/subscribed.html \ src/templates/people/unsubscribe.html \ src/templates/people/unsubscribed.html \ - src/templates/people/user-edit.html \ - src/templates/people/users.html + src/templates/people/user-edit.html templates_peopledir = $(templatesdir)/people @@ -285,7 +284,6 @@ templates_people_messagesdir = $(templates_peopledir)/messages templates_people_modules_DATA = \ src/templates/people/modules/accounts-list.html \ - src/templates/people/modules/accounts-new.html \ src/templates/people/modules/agent.html \ src/templates/people/modules/cdr.html \ src/templates/people/modules/channels.html \ @@ -304,10 +302,16 @@ templates_static_DATA = \ templates_staticdir = $(templatesdir)/static templates_users_DATA = \ + src/templates/users/index.html \ src/templates/users/show.html templates_usersdir = $(templatesdir)/users +templates_users_modules_DATA = \ + src/templates/users/modules/list.html + +templates_users_modulesdir = $(templates_usersdir)/modules + templates_wiki_DATA = \ src/templates/wiki/404.html \ src/templates/wiki/base.html \ diff --git a/src/backend/accounts.py b/src/backend/accounts.py index 5dabbef0..6a3cb42f 100644 --- a/src/backend/accounts.py +++ b/src/backend/accounts.py @@ -297,6 +297,22 @@ class Accounts(Object): def _format_date(t): return t.strftime("%Y%m%d%H%M%SZ") + def get_recently_registered(self, limit=None): + # Check the last two weeks + t = datetime.datetime.utcnow() - datetime.timedelta(days=14) + + # Fetch all accounts created after t + accounts = self.get_created_after(t) + + # Order by creation date and put latest first + accounts.sort(key=lambda a: a.created_at, reverse=True) + + # Cap at the limit + if accounts and limit: + accounts = accounts[:limit] + + return accounts + def get_created_after(self, ts): return self._search("(&(objectClass=person)(createTimestamp>=%s))" % self._format_date(ts)) @@ -304,8 +320,30 @@ class Accounts(Object): return self._count("(&(objectClass=person)(createTimestamp>=%s))" % self._format_date(ts)) def search(self, query): - accounts = self._search("(&(objectClass=person)(|(cn=*%s*)(uid=*%s*)(displayName=*%s*)(mail=*%s*)))" \ - % (query, query, query, query)) + # Try finding an exact match + account = self._search_one( + "(&" + "(objectClass=person)" + "(|" + "(uid=%s)" + "(mail=%s)" + "(mailAlternateAddress=%s)" + ")" + ")" % (query, query, query)) + if account: + return [account] + + # Otherwise search for a substring match + accounts = self._search( + "(&" + "(objectClass=person)" + "(|" + "(cn=*%s*)" + "(uid=*%s*)" + "(displayName=*%s*)" + "(mail=*%s*)" + ")" + ")" % (query, query, query, query)) return sorted(accounts) diff --git a/src/templates/people/modules/accounts-new.html b/src/templates/people/modules/accounts-new.html deleted file mode 100644 index 5898cb72..00000000 --- a/src/templates/people/modules/accounts-new.html +++ /dev/null @@ -1,5 +0,0 @@ -{% if accounts %} -

{{ _("Recently Created Accounts") }}

- - {% module AccountsList(accounts) %} -{% end %} diff --git a/src/templates/people/users.html b/src/templates/people/users.html deleted file mode 100644 index f8841029..00000000 --- a/src/templates/people/users.html +++ /dev/null @@ -1,13 +0,0 @@ -{% extends "../base.html" %} - -{% block title %}{{ _("Users") }}{% end block %} - -{% block content %} -
-
-

{{ _("Users") }}

- - {% module NewAccounts() %} -
-
-{% end block %} diff --git a/src/templates/users/index.html b/src/templates/users/index.html new file mode 100644 index 00000000..c933bd51 --- /dev/null +++ b/src/templates/users/index.html @@ -0,0 +1,81 @@ +{% extends "../base.html" %} + +{% block title %}{{ _("Users") }}{% end block %} + +{% block container %} +
+
+
+ + +

{{ _("Users") }}

+
+
+
+ + {# Search #} + +
+
+
+
+
+
+
+ + + + +
+
+
+
+
+
+
+ + {# Search Results #} + + {% if q %} +
+
+

{{ _("Search Results for '%s'") % q }}

+ + {% if results %} + {% module UsersList(results) %} + {% else %} +
+ {{ _("Nothing found for '%s'") % q }} +
+ {% end %} +
+
+ + {% else %} + {# Recently Joined #} + + {% set recently_registered = backend.accounts.get_recently_registered(limit=4) %} + {% if recently_registered %} +
+
+

{{ _("Recently Joined") }}

+ + {% module UsersList(recently_registered, show_created_at=True) %} +
+
+ {% end %} + {% end %} +{% end block %} diff --git a/src/templates/users/modules/list.html b/src/templates/users/modules/list.html new file mode 100644 index 00000000..bb2f60ae --- /dev/null +++ b/src/templates/users/modules/list.html @@ -0,0 +1,29 @@ +
+
+ {% for account in accounts %} +
+
+
+
+
+ +
+
+ +
+
+ {{ account }} +
+ + {% if show_created_at %} +

+ {{ _("Joined %s") % locale.format_date(account.created_at, shorter=True) }} +

+ {% end %} +
+
+
+
+ {% end %} +
+
diff --git a/src/web/__init__.py b/src/web/__init__.py index f28ff8f6..9372efde 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -75,7 +75,6 @@ class Application(tornado.web.Application): "CDR" : people.CDRModule, "Channels" : people.ChannelsModule, "MOS" : people.MOSModule, - "NewAccounts" : people.NewAccountsModule, "Password" : people.PasswordModule, "Registrations" : people.RegistrationsModule, @@ -87,6 +86,9 @@ class Application(tornado.web.Application): "FireinfoDeviceAndGroupsTable" : fireinfo.DeviceAndGroupsTableModule, + # Users + "UsersList" : users.ListModule, + # Wiki "WikiDiff" : wiki.WikiDiffModule, "WikiNavbar" : wiki.WikiNavbarModule, @@ -149,6 +151,7 @@ class Application(tornado.web.Application): (r"/donation", tornado.web.RedirectHandler, { "url" : "/donate" }), # Users + (r"/users", users.IndexHandler), (r"/users/([\w]+)", users.ShowHandler), # RSS feed @@ -301,7 +304,6 @@ class Application(tornado.web.Application): (r"/groups/([a-z_][a-z0-9_-]{0,31})", people.GroupHandler), (r"/register", auth.RegisterHandler), (r"/search", people.SearchHandler), - (r"/users", people.UsersHandler), (r"/users/([a-z_][a-z0-9_-]{0,31})\.jpg", people.AvatarHandler), (r"/users/([a-z_][a-z0-9_-]{0,31})/calls/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})", people.CallHandler), (r"/users/([a-z_][a-z0-9_-]{0,31})/calls(?:/(\d{4}-\d{2}-\d{2}))?", people.CallsHandler), diff --git a/src/web/people.py b/src/web/people.py index 68c29734..4a469c40 100644 --- a/src/web/people.py +++ b/src/web/people.py @@ -260,16 +260,6 @@ class SIPHandler(auth.CacheMixin, base.BaseHandler): self.render("people/sip.html", account=account) -class UsersHandler(auth.CacheMixin, base.BaseHandler): - @tornado.web.authenticated - def get(self): - # Only staff can see other users - if not self.current_user.is_staff(): - raise tornado.web.HTTPError(403) - - self.render("people/users.html") - - class UserEditHandler(auth.CacheMixin, base.BaseHandler): @tornado.web.authenticated def get(self, uid): @@ -430,19 +420,6 @@ class SSODiscourse(auth.CacheMixin, base.BaseHandler): self.redirect("%s?%s" % (params.get("return_sso_url"), qs)) -class NewAccountsModule(ui_modules.UIModule): - def render(self, days=14): - t = datetime.datetime.utcnow() - datetime.timedelta(days=days) - - # Fetch all accounts created after t - accounts = self.backend.accounts.get_created_after(t) - - accounts.sort(key=lambda a: a.created_at, reverse=True) - - return self.render_string("people/modules/accounts-new.html", - accounts=accounts, t=t) - - class AccountsListModule(ui_modules.UIModule): def render(self, accounts=None): if accounts is None: diff --git a/src/web/users.py b/src/web/users.py index d3544863..74b4d0a5 100644 --- a/src/web/users.py +++ b/src/web/users.py @@ -4,6 +4,22 @@ import tornado.web from . import auth from . import base +from . import ui_modules + +class IndexHandler(auth.CacheMixin, base.BaseHandler): + @tornado.web.authenticated + def get(self): + results = None + + # Query Term + q = self.get_argument("q", None) + + # Peform search + if q: + results = self.backend.accounts.search(q) + + self.render("users/index.html", q=q, results=results) + class ShowHandler(auth.CacheMixin, base.BaseHandler): @tornado.web.authenticated @@ -13,3 +29,9 @@ class ShowHandler(auth.CacheMixin, base.BaseHandler): raise tornado.web.HTTPError(404, "Could not find account %s" % uid) self.render("users/show.html", account=account) + + +class ListModule(ui_modules.UIModule): + def render(self, accounts, show_created_at=False): + return self.render_string("users/modules/list.html", accounts=accounts, + show_created_at=show_created_at) -- 2.47.3