Skip to content
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

Global topics #18

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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
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);



95 changes: 95 additions & 0 deletions tests/integration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,101 @@ 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 ("
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whitespace in strings are missing (simulating you would be writing into a file).

Also, I see you could congest each row into one line. While keeping field list with each column in its own line (for readability).

Example:

        "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,'topic title 0',10",'name'",0,3)"
        ", (1, 0, topic title 1', 13, 'second poster', 1, 3)"
        ", (2, 0, 'topic title 2', 200, 'post it', 2, 3)"
        ...

"0"
",0"
",'topic title 0'"
",10"
",'name'"
",0,3"
"), ("
"1,"
"0,"
"'topic title 1',"
"13,"
"'second poster',"
"1,"
"3"
"), ("
"2"
",0"
",'topic title 2'"
",200"
",'post it'"
",2,"
"3"
"), ("
"3"
",0"
",'topic title 3'"
",256"
",'posted it'"
",3"
",3"
"), ("
"4"
",0"
",'topic title 4'"
",666"
",'posted it again'"
",3"
",3"
") , ("
"6"
",0"
",'topic title 5'"
",777"
",'posted as 7, expecting 6'"
",3"
",3"
") , ("
"7"
",0"
",'different data type'"
",999"
",'inserted into table, expecting different type'"
",3"
",3"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this number be something else (topic_type)?

I see in your dataset you have only topic_type 3.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have set up one example where topic_type is different from 3. But the select expression for global topics has a where clause with a condition that topic_type = 3. So I think it would make sense to have most of examples fulfilling this condition.

")"
)

cursor.execute(
" insert into phpbb_posts ("
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful!

You have INNER JOIN in the query. Which means other topics won't show up, because their posts do not exist!

Add posts before changing your tests to see if it will break your tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Posts have been added in the next commit.

"post_id"
",post_subject"
",post_text"
") values ("
"0"
",'topic one'"
", 'hello'"
"), ("
"1"
",'topic two'"
",'hello world'"
"), ("
"2"
",'topic three'"
",'hello hello'"
"), ("
"3"
",'topic three'"
",'hello times four'"
")"
)


def _create_privilege(cursor, privilege_id, privilege):
# type: (psycopg2.extensions.cursor, int, str) -> None
cursor.execute(
Expand Down
Loading