Skip to content

Global topics #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion flask_phpbb3/backends/psycopg2.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ def _prepare_statements(self):
" AND nt.notification_type_enabled=1 "
" AND n.notification_read=0"
),
fetch_global_topics=(
"SELECT"
" t.topic_id,"
" t.forum_id,"
" t.topic_title,"
" t.topic_time,"
" t.topic_first_poster_name,"
" p.post_subject,"
" p.post_text "
" FROM "
" {TABLE_PREFIX}topics t"
" INNER JOIN {TABLE_PREFIX}posts p ON"
" p.post_id = t.topic_first_post_id"
" WHERE"
" t.topic_type = 3"
" AND t.forum_id = %(forum_id)s"
" order by t.topic_id "
),
))

self._prepare_custom_fields_statements()
Expand Down Expand Up @@ -231,7 +249,6 @@ def execute(
))

operation = command.split('_')[0]

func_or_query = self._functions[command]
if callable(func_or_query):
return func_or_query(**kwargs)
Expand Down
19 changes: 19 additions & 0 deletions flask_phpbb3/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,25 @@ def get_unread_notifications_count(
) # type: typing.Optional[dict]
return output

def fetch_global_topics(
self,
forum_id=0, # type: int
skip=0, # type: int
limit=10, # type: int
cache=False, # type: bool
cache_ttl=None, # type: typing.Optional[int]
):
# type: (...) -> typing.Optional[dict]
output = self._backend.execute(
'fetch_global_topics',
forum_id=forum_id,
skip=skip,
limit=limit,
cache=cache,
cache_ttl=cache_ttl,
) # type: typing.Optional[dict]
return output

def get_user_acl(self, raw_user_permissions):
# type: (str) -> flask_phpbb3.backends.base.UserAcl
return self._backend.get_user_acl(raw_user_permissions)
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/postgres/global_topics_test_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
INSERT INTO phpbb_topics(
topic_id, forum_id,
topic_title, topic_time, topic_first_poster_name, topic_first_post_id, topic_type)
VALUES (0,0,'naslov teme 0',10,'ime',1,3),
(1,0,'naslov teme 1',13,'drugi poster',2,3),
(2,0,'naslov teme 2',200,'post it',0,3),
(3,0,'naslov teme 3',256,'posted it',3,3);



INSERT INTO phpbb_posts(post_id,post_subject,post_text
)
VALUES
(1,'prva tema', 'bla'),
(2,'druga tema','blabla'),
(0,'tretja tema','blablabla'),
(3,'tretja tema','bla krat 4');

153 changes: 153 additions & 0 deletions tests/fixtures/postgres/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,156 @@ create table phpbb_notification_types (
notification_type_name character varying(255) not null default ''::character varying,
notification_type_enabled smallint not null default (1)::smallint
);


CREATE TABLE phpbb_topics (
topic_id integer DEFAULT 0 NOT NULL,
forum_id integer DEFAULT 0 NOT NULL,
icon_id integer DEFAULT 0 NOT NULL,
topic_attachment smallint DEFAULT (0)::smallint NOT NULL,
topic_reported smallint DEFAULT (0)::smallint NOT NULL,
topic_title character varying(255) DEFAULT ''::character varying NOT NULL,
topic_poster integer DEFAULT 0 NOT NULL,
topic_time integer DEFAULT 0 NOT NULL,
topic_time_limit integer DEFAULT 0 NOT NULL,
topic_views integer DEFAULT 0 NOT NULL,
topic_status smallint DEFAULT (0)::smallint NOT NULL,
topic_type smallint DEFAULT (0)::smallint NOT NULL,
topic_first_post_id integer DEFAULT 0 NOT NULL,
topic_first_poster_name character varying(255) DEFAULT ''::character varying NOT NULL,
topic_first_poster_colour character varying(6) DEFAULT ''::character varying NOT NULL,
topic_last_post_id integer DEFAULT 0 NOT NULL,
topic_last_poster_id integer DEFAULT 0 NOT NULL,
topic_last_poster_name character varying(255) DEFAULT ''::character varying NOT NULL,
topic_last_poster_colour character varying(6) DEFAULT ''::character varying NOT NULL,
topic_last_post_subject character varying(255) DEFAULT ''::character varying NOT NULL,
topic_last_post_time integer DEFAULT 0 NOT NULL,
topic_last_view_time integer DEFAULT 0 NOT NULL,
topic_moved_id integer DEFAULT 0 NOT NULL,
topic_bumped smallint DEFAULT (0)::smallint NOT NULL,
topic_bumper integer DEFAULT 0 NOT NULL,
poll_title character varying(255) DEFAULT ''::character varying NOT NULL,
poll_start integer DEFAULT 0 NOT NULL,
poll_length integer DEFAULT 0 NOT NULL,
poll_max_options smallint DEFAULT (1)::smallint NOT NULL,
poll_last_vote integer DEFAULT 0 NOT NULL,
poll_vote_change smallint DEFAULT (0)::smallint NOT NULL,
topic_visibility smallint DEFAULT (0)::smallint NOT NULL,
topic_delete_time integer DEFAULT 0 NOT NULL,
topic_delete_reason character varying(255) DEFAULT ''::character varying NOT NULL,
topic_delete_user integer DEFAULT 0 NOT NULL,
topic_posts_approved integer DEFAULT 0 NOT NULL,
topic_posts_unapproved integer DEFAULT 0 NOT NULL,
topic_posts_softdeleted integer DEFAULT 0 NOT NULL,
CONSTRAINT phpbb_topics_forum_id_check CHECK ((forum_id >= 0)),
CONSTRAINT phpbb_topics_icon_id_check CHECK ((icon_id >= 0)),
CONSTRAINT phpbb_topics_poll_last_vote_check CHECK ((poll_last_vote >= 0)),
CONSTRAINT phpbb_topics_poll_length_check CHECK ((poll_length >= 0)),
CONSTRAINT phpbb_topics_poll_start_check CHECK ((poll_start >= 0)),
CONSTRAINT phpbb_topics_poll_vote_change_check CHECK ((poll_vote_change >= 0)),
CONSTRAINT phpbb_topics_topic_attachment_check CHECK ((topic_attachment >= 0)),
CONSTRAINT phpbb_topics_topic_bumped_check CHECK ((topic_bumped >= 0)),
CONSTRAINT phpbb_topics_topic_bumper_check CHECK ((topic_bumper >= 0)),
CONSTRAINT phpbb_topics_topic_delete_time_check CHECK ((topic_delete_time >= 0)),
CONSTRAINT phpbb_topics_topic_delete_user_check CHECK ((topic_delete_user >= 0)),
CONSTRAINT phpbb_topics_topic_delete_user_check1 CHECK ((topic_delete_user >= 0)),
CONSTRAINT phpbb_topics_topic_first_post_id_check CHECK ((topic_first_post_id >= 0)),
CONSTRAINT phpbb_topics_topic_first_post_id_check1 CHECK ((topic_first_post_id >= 0)),
CONSTRAINT phpbb_topics_topic_id_check CHECK ((topic_id >= 0)),
CONSTRAINT phpbb_topics_topic_last_post_id_check CHECK ((topic_last_post_id >= 0)),
CONSTRAINT phpbb_topics_topic_last_post_id_check1 CHECK ((topic_last_post_id >= 0)),
CONSTRAINT phpbb_topics_topic_last_post_time_check CHECK ((topic_last_post_time >= 0)),
CONSTRAINT phpbb_topics_topic_last_poster_id_check CHECK ((topic_last_poster_id >= 0)),
CONSTRAINT phpbb_topics_topic_last_poster_id_check1 CHECK ((topic_last_poster_id >= 0)),
CONSTRAINT phpbb_topics_topic_last_view_time_check CHECK ((topic_last_view_time >= 0)),
CONSTRAINT phpbb_topics_topic_moved_id_check CHECK ((topic_moved_id >= 0)),
CONSTRAINT phpbb_topics_topic_moved_id_check1 CHECK ((topic_moved_id >= 0)),
CONSTRAINT phpbb_topics_topic_poster_check CHECK ((topic_poster >= 0)),
CONSTRAINT phpbb_topics_topic_poster_check1 CHECK ((topic_poster >= 0)),
CONSTRAINT phpbb_topics_topic_posts_approved_check CHECK ((topic_posts_approved >= 0)),
CONSTRAINT phpbb_topics_topic_posts_softdeleted_check CHECK ((topic_posts_softdeleted >= 0)),
CONSTRAINT phpbb_topics_topic_posts_unapproved_check CHECK ((topic_posts_unapproved >= 0)),
CONSTRAINT phpbb_topics_topic_reported_check CHECK ((topic_reported >= 0)),
CONSTRAINT phpbb_topics_topic_time_check CHECK ((topic_time >= 0)),
CONSTRAINT phpbb_topics_topic_time_limit_check CHECK ((topic_time_limit >= 0)),
CONSTRAINT phpbb_topics_topic_views_check CHECK ((topic_views >= 0))
);
ALTER TABLE ONLY phpbb_topics
ADD CONSTRAINT phpbb_topics_pkey PRIMARY KEY (topic_id);
CREATE INDEX phpbb_topics_fid_time_moved ON phpbb_topics USING btree (forum_id, topic_last_post_time, topic_moved_id);
CREATE INDEX phpbb_topics_forum_id ON phpbb_topics USING btree (forum_id);
CREATE INDEX phpbb_topics_forum_id_type ON phpbb_topics USING btree (forum_id, topic_type);
CREATE INDEX phpbb_topics_forum_vis_last ON phpbb_topics USING btree (forum_id, topic_visibility, topic_last_post_id);
CREATE INDEX phpbb_topics_last_post_time ON phpbb_topics USING btree (topic_last_post_time);
CREATE INDEX phpbb_topics_latest_topics ON phpbb_topics USING btree (forum_id, topic_last_post_time, topic_last_post_id, topic_moved_id);
CREATE INDEX phpbb_topics_topic_visibility ON phpbb_topics USING btree (topic_visibility);


CREATE TABLE phpbb_posts (
post_id integer DEFAULT 0 NOT NULL,
topic_id integer DEFAULT 0 NOT NULL,
forum_id integer DEFAULT 0 NOT NULL,
poster_id integer DEFAULT 0 NOT NULL,
icon_id integer DEFAULT 0 NOT NULL,
poster_ip character varying(40) DEFAULT ''::character varying NOT NULL,
post_time integer DEFAULT 0 NOT NULL,
post_reported smallint DEFAULT (0)::smallint NOT NULL,
enable_bbcode smallint DEFAULT (1)::smallint NOT NULL,
enable_smilies smallint DEFAULT (1)::smallint NOT NULL,
enable_magic_url smallint DEFAULT (1)::smallint NOT NULL,
enable_sig smallint DEFAULT (1)::smallint NOT NULL,
post_username character varying(255) DEFAULT ''::character varying NOT NULL,
post_subject character varying(255) DEFAULT ''::character varying NOT NULL,
post_text text DEFAULT ''::text NOT NULL,
post_checksum character varying(32) DEFAULT ''::character varying NOT NULL,
post_attachment smallint DEFAULT (0)::smallint NOT NULL,
bbcode_bitfield character varying(255) DEFAULT ''::character varying NOT NULL,
bbcode_uid character varying(8) DEFAULT ''::character varying NOT NULL,
post_postcount smallint DEFAULT (1)::smallint NOT NULL,
post_edit_time integer DEFAULT 0 NOT NULL,
post_edit_reason character varying(255) DEFAULT ''::character varying NOT NULL,
post_edit_user integer DEFAULT 0 NOT NULL,
post_edit_count smallint DEFAULT (0)::smallint NOT NULL,
post_edit_locked smallint DEFAULT (0)::smallint NOT NULL,
post_visibility smallint DEFAULT (0)::smallint NOT NULL,
post_delete_time integer DEFAULT 0 NOT NULL,
post_delete_reason character varying(255) DEFAULT ''::character varying NOT NULL,
post_delete_user integer DEFAULT 0 NOT NULL,
CONSTRAINT phpbb_posts_enable_bbcode_check CHECK ((enable_bbcode >= 0)),
CONSTRAINT phpbb_posts_enable_magic_url_check CHECK ((enable_magic_url >= 0)),
CONSTRAINT phpbb_posts_enable_sig_check CHECK ((enable_sig >= 0)),
CONSTRAINT phpbb_posts_enable_smilies_check CHECK ((enable_smilies >= 0)),
CONSTRAINT phpbb_posts_forum_id_check CHECK ((forum_id >= 0)),
CONSTRAINT phpbb_posts_icon_id_check CHECK ((icon_id >= 0)),
CONSTRAINT phpbb_posts_post_attachment_check CHECK ((post_attachment >= 0)),
CONSTRAINT phpbb_posts_post_delete_time_check CHECK ((post_delete_time >= 0)),
CONSTRAINT phpbb_posts_post_delete_user_check CHECK ((post_delete_user >= 0)),
CONSTRAINT phpbb_posts_post_delete_user_check1 CHECK ((post_delete_user >= 0)),
CONSTRAINT phpbb_posts_post_edit_count_check CHECK ((post_edit_count >= 0)),
CONSTRAINT phpbb_posts_post_edit_locked_check CHECK ((post_edit_locked >= 0)),
CONSTRAINT phpbb_posts_post_edit_time_check CHECK ((post_edit_time >= 0)),
CONSTRAINT phpbb_posts_post_edit_user_check CHECK ((post_edit_user >= 0)),
CONSTRAINT phpbb_posts_post_edit_user_check1 CHECK ((post_edit_user >= 0)),
CONSTRAINT phpbb_posts_post_id_check CHECK ((post_id >= 0)),
CONSTRAINT phpbb_posts_post_postcount_check CHECK ((post_postcount >= 0)),
CONSTRAINT phpbb_posts_post_reported_check CHECK ((post_reported >= 0)),
CONSTRAINT phpbb_posts_post_time_check CHECK ((post_time >= 0)),
CONSTRAINT phpbb_posts_poster_id_check CHECK ((poster_id >= 0)),
CONSTRAINT phpbb_posts_poster_id_check1 CHECK ((poster_id >= 0)),
CONSTRAINT phpbb_posts_topic_id_check CHECK ((topic_id >= 0)),
CONSTRAINT phpbb_posts_topic_id_check1 CHECK ((topic_id >= 0))
);
ALTER TABLE ONLY phpbb_posts
ADD CONSTRAINT phpbb_posts_pkey PRIMARY KEY (post_id);
CREATE INDEX phpbb_posts_forum_id ON phpbb_posts USING btree (forum_id);
CREATE INDEX phpbb_posts_post_username ON phpbb_posts USING btree (post_username);
CREATE INDEX phpbb_posts_post_visibility ON phpbb_posts USING btree (post_visibility);
CREATE INDEX phpbb_posts_poster_id ON phpbb_posts USING btree (poster_id);
CREATE INDEX phpbb_posts_poster_ip ON phpbb_posts USING btree (poster_ip);
CREATE INDEX phpbb_posts_simple_post_content ON phpbb_posts USING gin (to_tsvector('simple'::regconfig, ((post_text || ' '::text) || (post_subject)::text)));
CREATE INDEX phpbb_posts_simple_post_subject ON phpbb_posts USING gin (to_tsvector('simple'::regconfig, (post_subject)::text));
CREATE INDEX phpbb_posts_tid_post_time ON phpbb_posts USING btree (topic_id, post_time);
CREATE INDEX phpbb_posts_topic_id ON phpbb_posts USING btree (topic_id);



21 changes: 21 additions & 0 deletions tests/integration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ def _create_session(cursor, session_id, user_id):
)


def _create_global_topics(cursor):
# type: (psycopg2.extensions.cursor) -> None
cursor.execute(
"insert into"
" phpbb_topics(topic_id, forum_id, topic_title, topic_time,"
" topic_first_poster_name, topic_first_post_id, topic_type)"
" values (0,0,'naslov_teme_0',10,'ime',0,3),"
" (1,0,'naslov teme 1',13,'drugi poster',1,3),"
" (2,0,'naslov teme 2',200,'post it',2,3),"
" (3,0,'naslov teme 3',256,'posted it',3,3)"
)
cursor.execute(
" insert into "
" phpbb_posts(post_id,post_subject,post_text)"
" values (0,'prva tema', 'bla'),"
" (1,'druga tema','blabla'),"
" (2,'tretja tema','blablabla'),"
" (3,'tretja tema','bla krat 4')"
)


def _create_privilege(cursor, privilege_id, privilege):
# type: (psycopg2.extensions.cursor, int, str) -> None
cursor.execute(
Expand Down
45 changes: 45 additions & 0 deletions tests/integration/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,50 @@ def test_paging(self):
privilege = self.app.phpbb3.fetch_acl_options(skip=skip, limit=1)
self.assertEqual((skip, privilege), expected_privileges[skip])

def test_fetch_global_topics(self):
# type: () -> None
base._create_global_topics(self.cursor)

expected_topics = [(0, [{
'topic_id': 0,
'forum_id': 0,
'topic_title': 'naslov_teme_0',
'topic_time': 10,
'topic_first_poster_name': 'ime',
'post_subject': 'prva tema',
'post_text': 'bla'}]),

(1, [{
'topic_id': 1,
'forum_id': 0,
'topic_title': 'naslov teme 1',
'topic_time': 13,
'topic_first_poster_name': 'drugi poster',
'post_subject': 'druga tema',
'post_text': 'blabla'}]),
(2, [{
'topic_id': 2,
'forum_id': 0,
'topic_title': 'naslov teme 2',
'topic_time': 200,
'topic_first_poster_name': 'post it',
'post_subject': 'tretja tema',
'post_text': 'blablabla'}]),
(3, [{
'topic_id': 3,
'forum_id': 0,
'topic_title': 'naslov teme 3',
'topic_time': 256,
'topic_first_poster_name': 'posted it',
'post_subject': 'tretja tema',
'post_text': 'bla x4'}])]
for skip in range(0, 3):
topic = self.app.phpbb3.fetch_global_topics(
skip=skip,
limit=1,
forum_id=0)
self.assertEqual((skip, topic), expected_topics[skip])


class TestSession(base.TestWithDatabase):
def setUp(self):
Expand Down Expand Up @@ -136,6 +180,7 @@ def test_privilege(self):
base._create_session(self.cursor, self.session_id, 2)
base._create_privilege(self.cursor, 1, 'm_edit')
base._grant_privilege(self.cursor, 2)
base._create_global_topics(self.cursor)

data = self.client.get('/priv_test').data
self.assertEqual(data, 'False,False,False')
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/backends/test_psycopg2.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def test_empty(self, mocked_db):
'get_autologin',
'get_session',
'has_membership',
'fetch_global_topics',
'fetch_acl_options',
'get_unread_notifications_count',
'get_user',
Expand All @@ -150,6 +151,7 @@ def test_valid(self, mocked_db):
'get_session',
'set_some_field',
'has_membership',
'fetch_global_topics',
'fetch_acl_options',
'set_another_field',
'get_unread_notifications_count',
Expand Down Expand Up @@ -177,6 +179,7 @@ def test_empty(self, mocked_db):
'get_autologin',
'get_session',
'has_membership',
'fetch_global_topics',
'fetch_acl_options',
'get_unread_notifications_count',
'get_user',
Expand All @@ -202,6 +205,7 @@ def test_addition(self, mocked_db):
'get_autologin',
'get_session',
'has_membership',
'fetch_global_topics',
'fetch_acl_options',
'get_unread_notifications_count',
'some_custom_statement',
Expand Down Expand Up @@ -232,6 +236,7 @@ def test_override(self, mocked_db):
'get_autologin',
'get_session',
'has_membership',
'fetch_global_topics',
'fetch_acl_options',
'get_unread_notifications_count',
'get_user',
Expand Down