def add_thread_and_post(user_id, forum, thread_title, post_body): result = enki.libutil.ENKILIB_OK if user_id and forum and thread_title and post_body: if len(thread_title) <= THREAD_TITLE_LENGTH_MAX and len( post_body) <= POST_LENGTH_MAX: if enki.libdisplayname.get_EnkiUserDisplayName_by_user_id_current( user_id): thread = EnkiModelThread(author=user_id, forum=int(forum), title=thread_title, num_posts=1) thread.put() post = EnkiModelPost(author=user_id, body=post_body, thread=thread.key.id()) post.put() forum_selected = ndb.Key(EnkiModelForum, int(forum)).get() forum_selected.num_posts += 1 forum_selected.num_threads += 1 forum_selected.put() else: result = ERROR_POST_CREATION else: result = ERROR_POST_LENGTH else: result = ERROR_POST_CREATION return result
def add_thread_and_post(cls, user_id, forum, thread_title, thread_sticky_order, post_body, post_sticky_order): result = enki.libutil.ENKILIB_OK if user_id and forum and thread_title and post_body: if len(thread_title ) <= EnkiModelThread.THREAD_TITLE_LENGTH_MAX and len( post_body) <= cls.POST_LENGTH_MAX: if EnkiModelDisplayName.get_by_user_id_current(user_id): thread = EnkiModelThread( author=user_id, forum=int(forum), title=thread_title, num_posts=1, sticky_order=int(thread_sticky_order)) thread.put() post = cls(author=user_id, body=post_body, thread=thread.key.id(), sticky_order=int(post_sticky_order)) post.put() forum_selected = ndb.Key(EnkiModelForum, int(forum)).get() forum_selected.num_posts += 1 forum_selected.num_threads += 1 forum_selected.put() else: result = cls.ERROR_POST_CREATION else: result = cls.ERROR_POST_LENGTH else: result = cls.ERROR_POST_CREATION return result
def get_author_posts(author_selected): # MOVED TO LIB # get posts by author to display on their profile. If the author hasn't set a display name, return nothing author_display_name = enki.libdisplayname.get_EnkiUserDisplayName_by_user_id_current( int(author_selected)) if author_display_name: forums_url = enki.libutil.get_local_url('forums') author_data = enki.libdisplayname.get_user_id_display_name_url( author_display_name) list = fetch_EnkiPost_by_author(int(author_selected)) if list: for i, item in enumerate(list): thread = EnkiModelThread.get_by_id(item.thread) forum = EnkiModelForum.get_by_id(thread.forum) item.thread_title = thread.title item.thread_url = enki.libutil.get_local_url( 'thread', {'thread': str(item.thread)}) item.forum_title = forum.title item.forum_group = forum.group item.forum_url = enki.libutil.get_local_url( 'forum', {'forum': str(forum.key.id())}) item.post_page = enki.libutil.get_local_url( 'post', {'post': str(item.key.id())}) list[i] = item author_posts_data = authorpostsData(forums_url, author_data, list, markdown2.markdown) return author_posts_data
def get_post_data(post_selected): # get a post forums_url = enki.libutil.get_local_url('forums') post = EnkiModelPost.get_by_id(int(post_selected)) post_page = enki.libutil.get_local_url('post', {'post': str(post.key.id())}) thread = EnkiModelThread.get_by_id(post.thread) thread_url = enki.libutil.get_local_url('thread', {'thread': str(thread.key.id())}) forum = EnkiModelForum.get_by_id(thread.forum) forum_url = enki.libutil.get_local_url('forum', {'forum': str(forum.key.id())}) author_data = enki.libdisplayname.get_user_id_display_name_url( enki.libdisplayname.get_EnkiUserDisplayName_by_user_id_current( post.author)) post_data = postData( forums_url, forum, forum_url, thread, thread_url, post, post_page, author_data, markdown2.markdown, ) return post_data
def get_thread_data(thread_selected, post_requested=POST_DEFAULT, post_count=POSTS_PER_PAGE): # get posts by thread forums_url = enki.libutil.get_local_url('forums') thread = EnkiModelThread.get_by_id(int(thread_selected)) thread_url = enki.libutil.get_local_url('thread', {'thread': str(thread_selected)}) forum = EnkiModelForum.get_by_id(thread.forum) forum_url = enki.libutil.get_local_url('forum', {'forum': str(forum.key.id())}) if post_requested == POST_LAST: post_requested = thread.num_posts list = fetch_EnkiPost_by_thread(int(thread_selected), offset=(int(post_requested) - 1), limit=int(post_count)) if list: for i, item in enumerate(list): item.author_data = enki.libdisplayname.get_user_id_display_name_url( enki.libdisplayname.get_EnkiUserDisplayName_by_user_id_current( item.author)) item.post_page = enki.libutil.get_local_url( 'post', {'post': str(item.key.id())}) list[i] = item thread_data = threadData(forums_url, forum, forum_url, thread, thread_url, list, markdown2.markdown, thread_selected) return thread_data
def get_thread_data(cls, thread_selected, post_requested=POST_DEFAULT, post_count=POSTS_PER_PAGE): # get posts by thread forums_url = enki.libutil.get_local_url('forums') thread = EnkiModelThread.get_by_id(int(thread_selected)) thread_url = enki.libutil.get_local_url( 'thread', {'thread': str(thread_selected)}) forum = EnkiModelForum.get_by_id(thread.forum) forum_url = enki.libutil.get_local_url('forum', {'forum': str(forum.key.id())}) if post_requested == cls.POST_LAST: post_requested = thread.num_posts list = cls.fetch_by_thread(int(thread_selected), offset=(int(post_requested) - 1), limit=int(post_count)) if list: for i, item in enumerate(list): item.author_data = EnkiModelDisplayName.get_user_id_display_name_url( EnkiModelDisplayName.get_by_user_id_current(item.author)) item.post_page = enki.libutil.get_local_url( 'post', {'post': str(item.key.id())}) item.sticky = True if (item.sticky_order > 0) else False list[i] = item thread_data = threadData(forums_url, forum, forum_url, thread, thread_url, list, enki.libutil.markdown_escaped_extras, thread_selected) return thread_data
def get_author_posts(cls, author_selected): # get posts by author to display on their profile. If the author hasn't set a display name, return nothing author_display_name = EnkiModelDisplayName.get_by_user_id_current( int(author_selected)) if author_display_name: forums_url = enki.libutil.get_local_url('forums') author_data = EnkiModelDisplayName.get_user_id_display_name_url( author_display_name) list = cls.fetch_by_author(int(author_selected)) if list: for i, item in enumerate(list): thread = EnkiModelThread.get_by_id(item.thread) forum = EnkiModelForum.get_by_id(thread.forum) item.thread_title = thread.title item.thread_url = enki.libutil.get_local_url( 'thread', {'thread': str(item.thread)}) item.forum_title = forum.title item.forum_group = forum.group item.forum_url = enki.libutil.get_local_url( 'forum', {'forum': str(forum.key.id())}) item.post_page = enki.libutil.get_local_url( 'post', {'post': str(item.key.id())}) item.sticky = True if (item.sticky_order > 0) else False list[i] = item author_posts_data = authorpostsData( forums_url, author_data, list, enki.libutil.markdown_escaped_extras) return author_posts_data
def add_thread_and_post( user_id, forum, thread_title, post_body ): result = enki.libutil.ENKILIB_OK if user_id and forum and thread_title and post_body: if len( thread_title ) <= THREAD_TITLE_LENGTH_MAX and len( post_body ) <= POST_LENGTH_MAX: if enki.libdisplayname.get_EnkiUserDisplayName_by_user_id_current( user_id ): thread = EnkiModelThread( author = user_id, forum = int( forum ), title = thread_title, num_posts = 1 ) thread.put() post = EnkiModelPost( author = user_id, body = post_body, thread = thread.key.id()) post.put() forum_selected = ndb.Key( EnkiModelForum, int( forum )).get() forum_selected.num_posts += 1 forum_selected.num_threads += 1 forum_selected.put() else: result = ERROR_POST_CREATION else: result = ERROR_POST_LENGTH else: result = ERROR_POST_CREATION return result
def get_thread_pagination_data( thread_selected, post_requested = POST_DEFAULT, post_count = POSTS_PER_PAGE ): thread = EnkiModelThread.get_by_id( int( thread_selected )) post_requested = thread.num_posts if post_requested == POST_LAST else int( post_requested ) post_count = int( post_count ) page_first = '' page_previous = '' page_current = [] page_next = '' page_last = '' page_list = [] # first page first_post_first_page = 1 if post_requested <> 1: page_first = enki.libutil.get_local_url( 'thread', { 'thread': thread_selected, 'start': str( first_post_first_page ), 'count': str( post_count ) } ) # last page first_post_last_page = get_first_post_on_page( get_page( thread, thread.num_posts, post_count ), post_count) if post_requested + post_count <= thread.num_posts: page_last = enki.libutil.get_local_url( 'thread', { 'thread': thread_selected, 'start': str( first_post_last_page ), 'count': str( post_count ) } ) # current, previous and next pages first_post_previous_page = get_first_post_on_page( get_page( thread, post_requested, post_count ), post_count ) first_post_next_page = get_first_post_on_page( get_page( thread, ( post_requested + post_count ), post_count ), post_count ) if get_first_post_on_page( get_page( thread, post_requested, post_count ), post_count ) == post_requested: page = enki.libutil.get_local_url( 'thread', { 'thread': thread_selected, 'start': str( post_requested ), 'count': str( post_count ) } ) page_current = [ page, get_page( thread, post_requested, post_count )] if page_current[ 1 ] > first_post_first_page: first_post_previous_page = get_first_post_on_page( page_current[ 1 ] - 1, post_count ) if page_current[ 1 ] < get_page( thread, thread.num_posts, post_count ): first_post_next_page = get_first_post_on_page( page_current[ 1 ] + 1, post_count ) if page_first: page_previous = enki.libutil.get_local_url( 'thread', { 'thread': thread_selected, 'start': str( first_post_previous_page ), 'count': str( post_count ) } ) if page_last: page_next = enki.libutil.get_local_url( 'thread', { 'thread': thread_selected, 'start': str( first_post_next_page ), 'count': str( post_count ) } ) # list of posts start = get_page( thread, post_requested, post_count ) - PAGES_BEFORE while start < 1: start += 1 stop = get_page( thread, post_requested, post_count ) + PAGES_AFTER while stop > get_page( thread, thread.num_posts, post_count ): stop -= 1 index = start while index <= stop : first_post = get_first_post_on_page( index, post_count ) page = enki.libutil.get_local_url( 'thread', { 'thread': thread_selected, 'start': str( first_post ), 'count': str( post_count ) } ) page_list.append([ page, index ]) index += 1 result = pagination( page_first, page_previous, page_current, page_list, page_next, page_last ) return result
def get_post_data ( post_selected ): # get a post forums_url = enki.libutil.get_local_url( 'forums' ) post = EnkiModelPost.get_by_id( int( post_selected )) post_page = enki.libutil.get_local_url( 'post', { 'post': str( post.key.id( ) ) } ) thread = EnkiModelThread.get_by_id( post.thread ) thread_url = enki.libutil.get_local_url( 'thread', { 'thread': str( thread.key.id( ) ) } ) forum = EnkiModelForum.get_by_id( thread.forum ) forum_url = enki.libutil.get_local_url( 'forum', { 'forum': str( forum.key.id( ) ) } ) author_data = enki.libdisplayname.get_user_id_display_name_url( enki.libdisplayname.get_EnkiUserDisplayName_by_user_id_current( post.author ) ) post_data = postData( forums_url, forum, forum_url, thread, thread_url, post, post_page, author_data, markdown2.markdown, ) return post_data
def get( self, forum ): data = '' not_found = '' if forum.isdigit() and EnkiModelForum.get_by_id( int( forum ) ): enki.modelcounter.increment( 'views_forum' ) data = EnkiModelThread.get_forum_data( forum ) else: not_found = MSG.FORUM_NOT_EXIST( ) self.render_tmpl( 'forum.html', False, active_menu = 'forums', data = data, not_found = not_found, maxpostlength = EnkiModelPost.POST_LENGTH_MAX, maxthreadtitlelength = EnkiModelThread.THREAD_TITLE_LENGTH_MAX )
def validate_thread_pagination( thread, post_requested, post_count ): result = enki.libutil.ENKILIB_ERROR if thread: if thread.isdigit(): thread_entity = EnkiModelThread.get_by_id( int( thread )) if thread_entity: if post_requested and post_count: if post_requested.isdigit( ) and post_count.isdigit( ) : if int( post_requested ) > 0 and int( post_requested ) <= thread_entity.num_posts and int( post_count ) > 0: result = enki.libutil.ENKILIB_OK elif post_requested == 'last' and post_count.isdigit( ): result = enki.libutil.ENKILIB_OK elif post_requested == '' and post_count == '': result = enki.libutil.ENKILIB_OK return result
def validate_thread_pagination(thread, post_requested, post_count): result = enki.libutil.ENKILIB_ERROR if thread: if thread.isdigit(): thread_entity = EnkiModelThread.get_by_id(int(thread)) if thread_entity: if post_requested and post_count: if post_requested.isdigit() and post_count.isdigit(): if int(post_requested) > 0 and int( post_requested ) <= thread_entity.num_posts and int(post_count) > 0: result = enki.libutil.ENKILIB_OK elif post_requested == 'last' and post_count.isdigit(): result = enki.libutil.ENKILIB_OK elif post_requested == '' and post_count == '': result = enki.libutil.ENKILIB_OK return result
def get_thread_data( thread_selected, post_requested = POST_DEFAULT, post_count = POSTS_PER_PAGE ): # get posts by thread forums_url = enki.libutil.get_local_url( 'forums' ) thread = EnkiModelThread.get_by_id( int( thread_selected )) thread_url = enki.libutil.get_local_url( 'thread', { 'thread': str( thread_selected ) } ) forum = EnkiModelForum.get_by_id( thread.forum ) forum_url = enki.libutil.get_local_url( 'forum', { 'forum': str( forum.key.id( ) ) } ) if post_requested == POST_LAST: post_requested = thread.num_posts list = fetch_EnkiPost_by_thread( int( thread_selected ), offset = (int( post_requested ) - 1), limit = int( post_count ) ) if list: for i, item in enumerate( list ): item.author_data = enki.libdisplayname.get_user_id_display_name_url( enki.libdisplayname.get_EnkiUserDisplayName_by_user_id_current( item.author ) ) item.post_page = enki.libutil.get_local_url( 'post', { 'post': str( item.key.id( ) ) } ) list[ i ] = item thread_data = threadData( forums_url, forum, forum_url, thread, thread_url, list, markdown2.markdown, thread_selected ) return thread_data
def get_post_data(cls, post_selected): # get a post forums_url = enki.libutil.get_local_url('forums') post = cls.get_by_id(int(post_selected)) sticky = True if (post.sticky_order > 0) else False post_page = enki.libutil.get_local_url('post', {'post': str(post.key.id())}) thread = EnkiModelThread.get_by_id(post.thread) thread_url = enki.libutil.get_local_url( 'thread', {'thread': str(thread.key.id())}) forum = EnkiModelForum.get_by_id(thread.forum) forum_url = enki.libutil.get_local_url('forum', {'forum': str(forum.key.id())}) author_data = EnkiModelDisplayName.get_user_id_display_name_url( EnkiModelDisplayName.get_by_user_id_current(post.author)) post_data = postData(forums_url, forum, forum_url, thread, thread_url, post, sticky, post_page, author_data, enki.libutil.markdown_escaped_extras) return post_data
def get_author_posts( author_selected ): # MOVED TO LIB # get posts by author to display on their profile. If the author hasn't set a display name, return nothing author_display_name = enki.libdisplayname.get_EnkiUserDisplayName_by_user_id_current( int( author_selected ) ) if author_display_name: forums_url = enki.libutil.get_local_url( 'forums' ) author_data = enki.libdisplayname.get_user_id_display_name_url( author_display_name ) list = fetch_EnkiPost_by_author( int( author_selected )) if list: for i, item in enumerate( list ): thread = EnkiModelThread.get_by_id( item.thread ) forum = EnkiModelForum.get_by_id( thread.forum ) item.thread_title = thread.title item.thread_url = enki.libutil.get_local_url( 'thread', { 'thread': str( item.thread ) } ) item.forum_title = forum.title item.forum_group = forum.group item.forum_url = enki.libutil.get_local_url( 'forum', { 'forum': str( forum.key.id( ) ) } ) item.post_page = enki.libutil.get_local_url( 'post', { 'post': str( item.key.id( ) ) } ) list[ i ] = item author_posts_data = authorpostsData( forums_url, author_data, list, markdown2.markdown ) return author_posts_data
def get( self, thread ): data = '' pagination = '' not_found = '' post_requested = str( self.request.get( 'start' )) post_count = str( self.request.get( 'count' )) validation_result = EnkiModelThread.validate_thread_pagination( thread, post_requested, post_count ) if validation_result == enki.libutil.ENKILIB_OK: if not post_requested: post_requested = EnkiModelPost.POST_DEFAULT if not post_count: post_count = EnkiModelPost.POSTS_PER_PAGE data = EnkiModelPost.get_thread_data( thread, post_requested, post_count ) pagination = EnkiModelPost.get_thread_pagination_data( thread, post_requested, post_count ) else: not_found = MSG.POST_THREAD_NOT_EXIST( ) self.render_tmpl( 'thread.html', False, active_menu = 'forums', data = data, pagination = pagination, user_id = self.user_id, not_found = not_found, maxpostlength = EnkiModelPost.POST_LENGTH_MAX )
def post( self, forum ): if self.ensure_is_logged_in() and self.ensure_has_display_name(): if forum.isdigit() and EnkiModelForum.get_by_id( int( forum )): user_id = self.user_id thread_title = self.request.get( 'thread_title' ) post_body = self.request.get( 'post_body' ) thread_sticky_order = xint( self.request.get( 'sticky_order_thread' )) post_sticky_order = xint( self.request.get( 'sticky_order_post' )) submit_type = self.request.get( 'submittype' ) error_message_threadtitle = '' error_message_postbody = '' pmtoken = self.request.get( 'preventmultitoken' ) show_input = True if submit_type == 'cancel' or submit_type == 'input': thread_title = '' post_body = '' thread_sticky_order = 0 post_sticky_order = 0 if submit_type == 'input': pmtoken = EnkiModelTokenVerify.add_preventmultipost_token( 'preventmultipost' ) else: self.check_CSRF() if not thread_title: error_message_threadtitle = MSG.THREAD_TITLE_NEEDED( ) else: exceed = len( thread_title ) - EnkiModelThread.THREAD_TITLE_LENGTH_MAX if exceed > 0: error_message_threadtitle = MSG.THREAD_TITLE_TOO_LONG( exceed ) if not post_body: error_message_postbody = MSG.POST_BODY_NEEDED() else: exceed = len( post_body ) - EnkiModelPost.POST_LENGTH_MAX if exceed > 0: error_message_postbody = MSG.POST_BODY_TOO_LONG( exceed ) if not error_message_threadtitle and not error_message_postbody: if submit_type == 'submit': if EnkiModelTokenVerify.check_and_delete_preventmultipost_token( pmtoken, 'preventmultipost' ): result = EnkiModelPost.add_thread_and_post( user_id, forum, thread_title, thread_sticky_order, post_body, post_sticky_order ) if result == enki.libutil.ENKILIB_OK: self.add_infomessage( MSG.SUCCESS( ), MSG.THREAD_PUBLISHED()) url = enki.libutil.get_local_url( 'forum', { 'forum':forum }) self.send_email_admin( 'FTPA', url ) self.redirect( url ) return else: error_threadtitle = MSG.FAIL_THREAD_SUBMISSION() else: thread_title = '' post_body = '' self.render_tmpl( 'forum.html', CSRFneeded = show_input, active_menu = 'forums', data = EnkiModelThread.get_forum_data( forum ), has_permission_sticky = self.has_permissions( self.enki_user, [ 'PFPS', 'PFTS' ]), show_input = show_input, preventmultitoken = pmtoken, error_threadtitle = error_message_threadtitle, error_postbody = error_message_postbody, maxpostlength = EnkiModelPost.POST_LENGTH_MAX, maxthreadtitlelength = EnkiModelThread.THREAD_TITLE_LENGTH_MAX, threadtitle = thread_title, postbody = post_body, threadstickyorder = thread_sticky_order, poststickyorder = post_sticky_order, preview = True if ( submit_type == 'preview' and thread_title and post_body ) else False)
def fetch_EnkiThread_by_forum( forum ): list = EnkiModelThread.query( EnkiModelThread.forum == forum ).order( -EnkiModelThread.time_created ).fetch() return list
def fetch_EnkiThread_by_forum(forum): list = EnkiModelThread.query(EnkiModelThread.forum == forum).order( -EnkiModelThread.time_created).fetch() return list
def post(self, thread): if self.ensure_is_logged_in() and self.ensure_has_display_name(): if thread.isdigit() and EnkiModelThread.get_by_id(int(thread)): user = self.user_id post_body = self.request.get('post_body') submit_type = self.request.get('submittype') post_count = str(self.request.get('count')) post_requested = str(self.request.get('start')) if not post_count: post_count = enki.libforum.POSTS_PER_PAGE if not post_requested: post_requested = enki.libforum.get_first_post_on_page( enki.libforum.get_page( EnkiModelThread.get_by_id(int(thread)), enki.libforum.POST_LAST, int(post_count)), int(post_count)) error_message = '' preview = '' pmtoken = self.request.get('preventmultitoken') show_input = True if submit_type == 'input': post_body = '' pmtoken = enki.libforum.add_preventmultipost_token() else: self.check_CSRF() if submit_type != 'cancel': if not post_body: error_message = MSG.POST_BODY_NEEDED() else: exceed = len( post_body) - enki.libforum.POST_LENGTH_MAX if exceed > 0: error_message = MSG.POST_BODY_TOO_LONG(exceed) if not error_message: if submit_type == 'submit': if enki.libforum.check_and_delete_preventmultipost_token( pmtoken): result = enki.libforum.add_post( user, thread, post_body) if result == enki.libutil.ENKILIB_OK: self.add_infomessage( 'success', MSG.SUCCESS(), MSG.POST_PUBLISHED()) post_body = '' post_requested = enki.libforum.get_first_post_on_page( enki.libforum.get_page( EnkiModelThread.get_by_id( int(thread)), enki.libforum.POST_LAST, int(post_count)), int(post_count)) self.redirect( enki.libutil.get_local_url( 'thread', { 'thread': thread, 'start': str(post_requested), 'count': str(post_count) })) return else: error_message = MSG.FAIL_POST_SUBMISSION() else: post_body = '' elif submit_type == 'preview': preview = post_body elif submit_type == 'cancel': post_body = '' data = enki.libforum.get_thread_data(thread, post_requested, post_count) pagination = enki.libforum.get_thread_pagination_data( thread, post_requested, post_count) self.render_tmpl('thread.html', CSRFneeded=show_input, active_menu='forums', data=data, pagination=pagination, user_id=self.user_id, show_input=show_input, preventmultitoken=pmtoken, error=error_message, maxpostlength=enki.libforum.POST_LENGTH_MAX, postbody=post_body, preview=preview)
def post( self, thread ): if self.ensure_is_logged_in() and enki.libdisplayname.ensure_has_display_name( self ): if thread.isdigit() and EnkiModelThread.get_by_id( int( thread ) ): self.check_CSRF( 'thread' ) user = self.user_id post_body = self.request.get( 'post_body' ) submit_type = self.request.get( 'submittype' ) post_count = str( self.request.get( 'count' )) post_requested = str( self.request.get( 'start' )) if not post_count: post_count = enki.libforum.POSTS_PER_PAGE if not post_requested: post_requested = enki.libforum.get_first_post_on_page( enki.libforum.get_page( EnkiModelThread.get_by_id( int( thread )), enki.libforum.POST_LAST, int( post_count )), int( post_count )) error_message = '' preview = '' pmtoken = self.request.get( 'preventmultitoken' ) show_input = True if submit_type == 'input': post_body = '' pmtoken = enki.libforum.add_preventmultipost_token( ) else: if submit_type != 'cancel': if not post_body: error_message = MSG.POST_BODY_NEEDED() else: exceed = len( post_body ) - enki.libforum.POST_LENGTH_MAX if exceed > 0: error_message = MSG.POST_BODY_TOO_LONG( exceed ) if not error_message: if submit_type == 'submit': if enki.libforum.check_and_delete_preventmultipost_token( pmtoken ): result = enki.libforum.add_post( user, thread, post_body ) if result == enki.libutil.ENKILIB_OK: self.add_infomessage( 'success', MSG.SUCCESS( ), MSG.POST_PUBLISHED()) post_body = '' post_requested = enki.libforum.get_first_post_on_page( enki.libforum.get_page( EnkiModelThread.get_by_id( int( thread )), enki.libforum.POST_LAST, int( post_count )), int( post_count )) self.redirect( enki.libutil.get_local_url( 'thread', { 'thread': thread, 'start': str( post_requested ), 'count': str( post_count )})) return else: error_message = MSG.FAIL_POST_SUBMISSION() else: post_body = '' elif submit_type == 'preview': preview = post_body elif submit_type == 'cancel': post_body = '' data = enki.libforum.get_thread_data( thread, post_requested, post_count ) pagination = enki.libforum.get_thread_pagination_data( thread, post_requested, post_count ) self.render_tmpl( 'thread.html', active_page = 'forums', CSRFtoken = self.create_CSRF( 'thread' ), data = data, pagination = pagination, user_id = self.user_id, show_input = show_input, preventmultitoken = pmtoken, error = error_message, maxpostlength = enki.libforum.POST_LENGTH_MAX, postbody = post_body, preview = preview )
def get_thread_pagination_data(cls, thread_selected, post_requested=POST_DEFAULT, post_count=POSTS_PER_PAGE): thread = EnkiModelThread.get_by_id(int(thread_selected)) post_requested = thread.num_posts if post_requested == cls.POST_LAST else int( post_requested) post_count = int(post_count) page_first = '' page_previous = '' page_current = [] page_next = '' page_last = '' page_list = [] # first page first_post_first_page = 1 if post_requested <> 1: page_first = enki.libutil.get_local_url( 'thread', { 'thread': thread_selected, 'start': str(first_post_first_page), 'count': str(post_count) }) # last page first_post_last_page = cls.get_first_post_on_page( cls.get_page(thread, thread.num_posts, post_count), post_count) if post_requested + post_count <= thread.num_posts: page_last = enki.libutil.get_local_url( 'thread', { 'thread': thread_selected, 'start': str(first_post_last_page), 'count': str(post_count) }) # current, previous and next pages first_post_previous_page = cls.get_first_post_on_page( cls.get_page(thread, post_requested, post_count), post_count) first_post_next_page = cls.get_first_post_on_page( cls.get_page(thread, (post_requested + post_count), post_count), post_count) if cls.get_first_post_on_page( cls.get_page(thread, post_requested, post_count), post_count) == post_requested: page = enki.libutil.get_local_url( 'thread', { 'thread': thread_selected, 'start': str(post_requested), 'count': str(post_count) }) page_current = [ page, cls.get_page(thread, post_requested, post_count) ] if page_current[1] > first_post_first_page: first_post_previous_page = cls.get_first_post_on_page( page_current[1] - 1, post_count) if page_current[1] < cls.get_page(thread, thread.num_posts, post_count): first_post_next_page = cls.get_first_post_on_page( page_current[1] + 1, post_count) if page_first: page_previous = enki.libutil.get_local_url( 'thread', { 'thread': thread_selected, 'start': str(first_post_previous_page), 'count': str(post_count) }) if page_last: page_next = enki.libutil.get_local_url( 'thread', { 'thread': thread_selected, 'start': str(first_post_next_page), 'count': str(post_count) }) # list of posts start = cls.get_page(thread, post_requested, post_count) - cls.PAGES_BEFORE while start < 1: start += 1 stop = cls.get_page(thread, post_requested, post_count) + cls.PAGES_AFTER while stop > cls.get_page(thread, thread.num_posts, post_count): stop -= 1 index = start while index <= stop: first_post = cls.get_first_post_on_page(index, post_count) page = enki.libutil.get_local_url( 'thread', { 'thread': thread_selected, 'start': str(first_post), 'count': str(post_count) }) page_list.append([page, index]) index += 1 result = pagination(page_first, page_previous, page_current, page_list, page_next, page_last) return result