2008-10-24 18:51:19 +00:00
|
|
|
import cherrypy
|
|
|
|
from model.User import User
|
|
|
|
from model.Notebook import Notebook
|
2008-04-25 20:34:01 +00:00
|
|
|
from Expose import expose
|
2008-10-24 18:51:19 +00:00
|
|
|
from Validate import validate, Valid_string
|
2008-04-25 20:34:01 +00:00
|
|
|
from Database import Valid_id, end_transaction
|
|
|
|
from Users import grab_user_id
|
2008-10-24 18:51:19 +00:00
|
|
|
from Notebooks import Notebooks
|
2008-04-25 20:34:01 +00:00
|
|
|
from view.Forums_page import Forums_page
|
2008-10-24 18:51:19 +00:00
|
|
|
from view.Forum_page import Forum_page
|
|
|
|
from view.Main_page import Main_page
|
2008-04-25 20:34:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Forums( object ):
|
|
|
|
"""
|
|
|
|
Controller for dealing with discussion forums, corresponding to the "/forums" URL.
|
|
|
|
"""
|
2008-10-24 18:51:19 +00:00
|
|
|
def __init__( self, database, notebooks, users ):
|
2008-04-25 20:34:01 +00:00
|
|
|
"""
|
|
|
|
Create a new Forums object.
|
|
|
|
|
|
|
|
@type database: controller.Database
|
|
|
|
@param database: database that forums are stored in
|
2008-10-24 18:51:19 +00:00
|
|
|
@type notebooks: controller.Users
|
|
|
|
@param notebooks: controller for all notebooks
|
2008-04-25 20:34:01 +00:00
|
|
|
@type users: controller.Users
|
|
|
|
@param users: controller for all users
|
|
|
|
@rtype: Forums
|
|
|
|
@return: newly constructed Forums
|
|
|
|
"""
|
|
|
|
self.__database = database
|
2008-10-24 18:51:19 +00:00
|
|
|
self.__notebooks = notebooks
|
2008-04-25 20:34:01 +00:00
|
|
|
self.__users = users
|
|
|
|
|
|
|
|
@expose( view = Forums_page )
|
|
|
|
@end_transaction
|
|
|
|
@grab_user_id
|
|
|
|
@validate(
|
|
|
|
user_id = Valid_id( none_okay = True ),
|
|
|
|
)
|
|
|
|
def index( self, user_id ):
|
|
|
|
"""
|
|
|
|
Provide the information necessary to display the listing of available forums (currently hard-coded).
|
2008-10-24 18:51:19 +00:00
|
|
|
|
|
|
|
@type user_id: unicode or NoneType
|
|
|
|
@param user_id: id of the current user
|
|
|
|
"""
|
|
|
|
result = self.__users.current( user_id )
|
|
|
|
parents = [ notebook for notebook in result[ u"notebooks" ] if notebook.trash_id and not notebook.deleted ]
|
|
|
|
if len( parents ) > 0:
|
|
|
|
result[ "first_notebook" ] = parents[ 0 ]
|
|
|
|
else:
|
|
|
|
result[ "first_notebook" ] = None
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
@expose( view = Forum_page )
|
|
|
|
@end_transaction
|
|
|
|
@grab_user_id
|
|
|
|
@validate(
|
|
|
|
forum_name = Valid_string( max = 100 ),
|
|
|
|
user_id = Valid_id( none_okay = True ),
|
|
|
|
)
|
|
|
|
def default( self, forum_name, user_id ):
|
|
|
|
"""
|
|
|
|
Provide the information necessary to display the current threads within a forum.
|
|
|
|
|
|
|
|
@type forum_name: unicode
|
|
|
|
@param forum_name: name of the forum to display
|
|
|
|
@type user_id: unicode or NoneType
|
|
|
|
@param user_id: id of the current user
|
2008-04-25 20:34:01 +00:00
|
|
|
"""
|
|
|
|
result = self.__users.current( user_id )
|
|
|
|
parents = [ notebook for notebook in result[ u"notebooks" ] if notebook.trash_id and not notebook.deleted ]
|
|
|
|
if len( parents ) > 0:
|
|
|
|
result[ "first_notebook" ] = parents[ 0 ]
|
|
|
|
else:
|
|
|
|
result[ "first_notebook" ] = None
|
|
|
|
|
2008-10-24 18:51:19 +00:00
|
|
|
anonymous = self.__database.select_one( User, User.sql_load_by_username( u"anonymous" ), use_cache = True )
|
|
|
|
if anonymous is None:
|
|
|
|
raise Access_error()
|
|
|
|
|
|
|
|
# TODO: this needs to sort by either thread/note creation or modification date
|
|
|
|
threads = self.__database.select_many(
|
|
|
|
Notebook,
|
|
|
|
anonymous.sql_load_notebooks(
|
|
|
|
parents_only = False, undeleted_only = True, tag_name = u"forum", tag_value = forum_name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# if there are no matching threads, then this forum doesn't exist
|
|
|
|
if len( threads ) == 0:
|
|
|
|
raise cherrypy.NotFound
|
|
|
|
|
|
|
|
result[ "forum_name" ] = forum_name
|
|
|
|
result[ "threads" ] = threads
|
2008-04-25 20:34:01 +00:00
|
|
|
return result
|
2008-10-24 18:51:19 +00:00
|
|
|
|
|
|
|
# threads() is just an alias for Notebooks.default()
|
2008-10-24 19:58:41 +00:00
|
|
|
def threads( self, *args, **kwargs ):
|
|
|
|
return self.__notebooks.default( *args, **kwargs )
|
|
|
|
threads.exposed = True
|