Coverage for .tox / coverage / lib / python3.11 / site-packages / wuttaweb / views / people.py: 100%
71 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-06 19:57 -0600
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-06 19:57 -0600
1# -*- coding: utf-8; -*-
2################################################################################
3#
4# wuttaweb -- Web App for Wutta Framework
5# Copyright © 2024-2025 Lance Edgar
6#
7# This file is part of Wutta Framework.
8#
9# Wutta Framework is free software: you can redistribute it and/or modify it
10# under the terms of the GNU General Public License as published by the Free
11# Software Foundation, either version 3 of the License, or (at your option) any
12# later version.
13#
14# Wutta Framework is distributed in the hope that it will be useful, but
15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17# more details.
18#
19# You should have received a copy of the GNU General Public License along with
20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>.
21#
22################################################################################
23"""
24Views for people
25"""
27import sqlalchemy as sa
29from wuttjamaican.db.model import Person
30from wuttaweb.views import MasterView
31from wuttaweb.util import make_users_grid
34class PersonView(MasterView): # pylint: disable=abstract-method
35 """
36 Master view for people.
38 Default route prefix is ``people``.
40 Notable URLs provided by this class:
42 * ``/people/``
43 * ``/people/new``
44 * ``/people/XXX``
45 * ``/people/XXX/edit``
46 * ``/people/XXX/delete``
47 """
49 model_class = Person
50 model_title_plural = "People"
51 route_prefix = "people"
52 sort_defaults = "full_name"
53 has_autocomplete = True
55 grid_columns = [
56 "full_name",
57 "first_name",
58 "middle_name",
59 "last_name",
60 ]
62 filter_defaults = {
63 "full_name": {"active": True},
64 }
66 form_fields = [
67 "full_name",
68 "first_name",
69 "middle_name",
70 "last_name",
71 "users",
72 ]
74 def configure_grid(self, grid): # pylint: disable=empty-docstring
75 """ """
76 g = grid
77 super().configure_grid(g)
79 # full_name
80 g.set_link("full_name")
82 # first_name
83 g.set_link("first_name")
85 # last_name
86 g.set_link("last_name")
88 def configure_form(self, form): # pylint: disable=empty-docstring
89 """ """
90 f = form
91 super().configure_form(f)
92 person = f.model_instance
94 # full_name
95 if self.creating or self.editing:
96 f.remove("full_name")
98 # users
99 if self.creating or self.editing:
100 f.remove("users")
101 else:
102 f.set_grid("users", self.make_users_grid(person))
104 def make_users_grid(self, person):
105 """
106 Make and return the grid for the Users field.
108 This grid is shown for the Users field when viewing a Person.
110 :returns: Fully configured :class:`~wuttaweb.grids.base.Grid`
111 instance.
112 """
113 return make_users_grid(
114 self.request,
115 route_prefix=self.get_route_prefix(),
116 data=person.users,
117 columns=[
118 "username",
119 "active",
120 ],
121 )
123 def objectify(self, form): # pylint: disable=empty-docstring
124 """ """
125 person = super().objectify(form)
127 # full_name
128 person.full_name = self.app.make_full_name(person.first_name, person.last_name)
130 return person
132 def autocomplete_query(self, term): # pylint: disable=empty-docstring
133 """ """
134 model = self.app.model
135 session = self.Session()
136 query = session.query(model.Person)
137 criteria = [model.Person.full_name.ilike(f"%{word}%") for word in term.split()]
138 query = query.filter(sa.and_(*criteria)).order_by(model.Person.full_name)
139 return query
141 def view_profile(self, session=None): # pylint: disable=empty-docstring
142 """ """
143 person = self.get_instance(session=session)
144 context = {
145 "person": person,
146 "instance": person,
147 }
148 return self.render_to_response("view_profile", context)
150 def make_user(self): # pylint: disable=empty-docstring
151 """ """
152 self.request.session.flash("TODO: this feature is not yet supported", "error")
153 return self.redirect(self.request.get_referrer())
155 @classmethod
156 def defaults(cls, config): # pylint: disable=empty-docstring
157 """ """
159 # nb. Person may come from custom model
160 wutta_config = config.registry.settings["wutta_config"]
161 app = wutta_config.get_app()
162 cls.model_class = app.model.Person
164 cls._defaults(config)
165 cls._people_defaults(config)
167 @classmethod
168 def _people_defaults(cls, config):
169 route_prefix = cls.get_route_prefix()
170 url_prefix = cls.get_url_prefix()
171 instance_url_prefix = cls.get_instance_url_prefix()
172 permission_prefix = cls.get_permission_prefix()
174 # view profile
175 config.add_route(
176 f"{route_prefix}.view_profile",
177 f"{instance_url_prefix}/profile",
178 request_method="GET",
179 )
180 config.add_view(
181 cls,
182 attr="view_profile",
183 route_name=f"{route_prefix}.view_profile",
184 permission=f"{permission_prefix}.view_profile",
185 )
187 # make user for person
188 config.add_route(
189 f"{route_prefix}.make_user",
190 f"{url_prefix}/make-user",
191 request_method="POST",
192 )
193 config.add_view(
194 cls,
195 attr="make_user",
196 route_name=f"{route_prefix}.make_user",
197 permission="users.create",
198 )
201def defaults(config, **kwargs): # pylint: disable=missing-function-docstring
202 base = globals()
204 PersonView = kwargs.get( # pylint: disable=invalid-name,redefined-outer-name
205 "PersonView", base["PersonView"]
206 )
207 PersonView.defaults(config)
210def includeme(config): # pylint: disable=missing-function-docstring
211 defaults(config)