Skip to content

Commit 466c7ef

Browse files
authored
Merge pull request #16 from Podnapisi-NET/custom_profile_field
Implement interface to access custom profile fields
2 parents a365120 + 6ceee44 commit 466c7ef

5 files changed

Lines changed: 173 additions & 1 deletion

File tree

README.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Settings
3737
+ **USER** - User for connecting to database, default is phpbb3
3838
+ **PASSWORD** - Database user's password, default is empty
3939
+ **TABLE_PREFIX** - Table prefix of phpBB3 tables, default is phpbb\_
40+
+ **CUSTOM_USER_FIELDS** - List of custom fields setup in phpBB3 forum
41+
+ **CUSTOM_STATEMENTS** - Dictionary of prepared statements to add or
42+
override. **Careful** with raw queries, use
43+
`{TABLE_PREFIX}` to re-use configured prefix
4044

4145
* PHPBB3_SESSION_BACKEND - Setting up session backend, it configures the werkzeug cache subsystem
4246

@@ -113,7 +117,7 @@ get_user(user_id)
113117
Gets user settings and profile.
114118

115119
Use string named interpolation format (the psycopg one) to specify kwargs of a function.
116-
Do not forget to use {TABLE_PREFFIX} variable, to add specific table prefix. (First, the
120+
Do not forget to use {TABLE_PREFIX} variable, to add specific table prefix. (First, the
117121
python variables from config get evaluated, and then psycopg variables).
118122

119123
Sessions integration

flask_phpbb3/backends/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def __init__(
3030
self._config = config
3131

3232
self._prepare_statements()
33+
custom_statements = self._config.get('CUSTOM_STATEMENTS', {})
34+
if not isinstance(custom_statements, dict):
35+
custom_statements = {}
36+
self._functions.update(custom_statements)
3337

3438
def _setup_connection(self):
3539
# type: () -> None

flask_phpbb3/backends/psycopg2.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def _prepare_statements(self):
5858
"SELECT * "
5959
"FROM {TABLE_PREFIX}users "
6060
"WHERE user_id = %(user_id)s"),
61+
get_user_profile=(
62+
"SELECT * "
63+
"FROM {TABLE_PREFIX}profile_fields_data "
64+
"WHERE user_id = %(user_id)s"),
6165
has_membership=(
6266
"SELECT ug.group_id "
6367
"FROM {TABLE_PREFIX}user_group ug "
@@ -98,8 +102,30 @@ def _prepare_statements(self):
98102
),
99103
))
100104

105+
self._prepare_custom_fields_statements()
106+
101107
# TODO Add/Move to version specific queries
102108

109+
def _prepare_custom_fields_statements(self):
110+
# type: () -> None
111+
"""
112+
Prepares statements for custom fields
113+
"""
114+
# Setters for custom fields
115+
custom_fields = self._config.get('CUSTOM_USER_FIELDS', [])
116+
for custom_field in custom_fields:
117+
self._functions["set_{0}".format(custom_field)] = (
118+
"UPDATE"
119+
" {TABLE_PREFIX}profile_fields_data"
120+
" SET"
121+
" `pf_{field}` = %(value)s"
122+
" WHERE"
123+
" user_id = %(user_id)s"
124+
).format(
125+
TABLE_PREFIX=self._config['TABLE_PREFIX'],
126+
field=custom_field,
127+
)
128+
103129
def _sql_query(
104130
self,
105131
operation, # type: str

flask_phpbb3/extension.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def _ensure_default_config(cls, app):
6868
app.config['PHPBB3_DATABASE'].setdefault('USER', 'phpbb3')
6969
app.config['PHPBB3_DATABASE'].setdefault('PASSWORD', '')
7070
app.config['PHPBB3_DATABASE'].setdefault('TABLE_PREFIX', 'phpbb_')
71+
app.config['PHPBB3_DATABASE'].setdefault('CUSTOM_USER_FIELDS', [])
72+
app.config['PHPBB3_DATABASE'].setdefault('CUSTOM_STATEMENTS', {})
7173
app.config.setdefault('PHPBB3_SESSION_BACKEND', {})
7274
app.config['PHPBB3_SESSION_BACKEND'].setdefault('TYPE', 'simple')
7375

tests/unit/backends/test_psycopg2.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,139 @@ def test_unknown_query(self, mocked_db):
106106

107107
# Internal function, this should never happen
108108
self.assertIsNone(actual_value)
109+
110+
111+
@mock.patch('flask_phpbb3.backends.psycopg2.Psycopg2Backend._db')
112+
class TestPreparedCustomFieldsStatements(unittest.TestCase):
113+
def test_empty(self, mocked_db):
114+
connection = flask_phpbb3.backends.psycopg2.Psycopg2Backend(
115+
werkzeug.contrib.cache.SimpleCache(),
116+
{
117+
'TABLE_PREFIX': '',
118+
'CUSTOM_USER_FIELDS': [],
119+
}
120+
)
121+
122+
self.assertListEqual(
123+
connection._functions.keys(),
124+
[
125+
'has_membership_resolve',
126+
'get_autologin',
127+
'get_session',
128+
'has_membership',
129+
'fetch_acl_options',
130+
'get_unread_notifications_count',
131+
'get_user',
132+
'get_user_profile',
133+
]
134+
)
135+
136+
def test_valid(self, mocked_db):
137+
connection = flask_phpbb3.backends.psycopg2.Psycopg2Backend(
138+
werkzeug.contrib.cache.SimpleCache(),
139+
{
140+
'TABLE_PREFIX': '',
141+
'CUSTOM_USER_FIELDS': ['some_field', 'another_field'],
142+
}
143+
)
144+
145+
self.assertSetEqual(
146+
set(connection._functions.keys()),
147+
set([
148+
'has_membership_resolve',
149+
'get_autologin',
150+
'get_session',
151+
'set_some_field',
152+
'has_membership',
153+
'fetch_acl_options',
154+
'set_another_field',
155+
'get_unread_notifications_count',
156+
'get_user',
157+
'get_user_profile',
158+
]),
159+
)
160+
161+
162+
@mock.patch('flask_phpbb3.backends.psycopg2.Psycopg2Backend._db')
163+
class TestPreparedCustomStatements(unittest.TestCase):
164+
def test_empty(self, mocked_db):
165+
connection = flask_phpbb3.backends.psycopg2.Psycopg2Backend(
166+
werkzeug.contrib.cache.SimpleCache(),
167+
{
168+
'TABLE_PREFIX': '',
169+
'CUSTOM_STATEMENTS': {},
170+
}
171+
)
172+
173+
self.assertListEqual(
174+
connection._functions.keys(),
175+
[
176+
'has_membership_resolve',
177+
'get_autologin',
178+
'get_session',
179+
'has_membership',
180+
'fetch_acl_options',
181+
'get_unread_notifications_count',
182+
'get_user',
183+
'get_user_profile',
184+
]
185+
)
186+
187+
def test_addition(self, mocked_db):
188+
connection = flask_phpbb3.backends.psycopg2.Psycopg2Backend(
189+
werkzeug.contrib.cache.SimpleCache(),
190+
{
191+
'TABLE_PREFIX': '',
192+
'CUSTOM_STATEMENTS': {
193+
'some_custom_statement': 'some query',
194+
},
195+
}
196+
)
197+
198+
self.assertListEqual(
199+
connection._functions.keys(),
200+
[
201+
'has_membership_resolve',
202+
'get_autologin',
203+
'get_session',
204+
'has_membership',
205+
'fetch_acl_options',
206+
'get_unread_notifications_count',
207+
'some_custom_statement',
208+
'get_user',
209+
'get_user_profile',
210+
]
211+
)
212+
self.assertEqual(
213+
connection._functions['some_custom_statement'],
214+
'some query',
215+
)
216+
217+
def test_override(self, mocked_db):
218+
connection = flask_phpbb3.backends.psycopg2.Psycopg2Backend(
219+
werkzeug.contrib.cache.SimpleCache(),
220+
{
221+
'TABLE_PREFIX': '',
222+
'CUSTOM_STATEMENTS': {
223+
'get_autologin': 'overriden',
224+
},
225+
}
226+
)
227+
228+
self.assertListEqual(
229+
connection._functions.keys(),
230+
[
231+
'has_membership_resolve',
232+
'get_autologin',
233+
'get_session',
234+
'has_membership',
235+
'fetch_acl_options',
236+
'get_unread_notifications_count',
237+
'get_user',
238+
'get_user_profile',
239+
]
240+
)
241+
self.assertEqual(
242+
connection._functions['get_autologin'],
243+
'overriden',
244+
)

0 commit comments

Comments
 (0)