2
1

I would like to add a 'draft' or 'private' mode to OSQA: Users should be able to post/answer one or more questions that only they are able to see. Later on they can decide to 'publish' them, so everyone else can see and answer those question too.

What is the best way to implement this?

I did the following: First I added a boolean field called 'private' to models.node.Node (using a forum_module and 'add_to_class' threw errors) A middleware checks when a user is in private or public mode by using a session variable. It then does the following: * In private mode, It wraps around 'NodeManager.get_query_set' to make it exclude all questions except the private questions of the current user. * In public mode, it filters out all private questions. * It changes the default value of the 'private' field according to the current mode: Node._meta.get_field('private').default = request.session['private_mode']

This is very slim (~20 lines) and nice by now. Unfortunately, some caching mechanism seems to get into my way: Once a private questions was accessed by its 'owner'/user, it can also be accessed by others via the same URL. (as long as the user did not access it this isn't the case)

Now I came across the 'state' of Nodes, which I overlooked before. Wouldn't it be better to implement a private mode by adding a 'private' state to OSQA Nodes? Or should I go on with my 'private' field in models.node.Node? What do you consider the best way to implement a private mode?

asked 15 Feb, 12:28

Flipper's gravatar image

Flipper
5615
accept rate: 0%

edited 17 Feb, 04:55


Finally everything works as expected; I did the following:

Added the 'private' field in forum.models.Node.

I want cutom skins, so I created and activated a custom Template-Loader which searches for templates if we are in private mode (it accesses the current thread to know this). The loader is based on forum.skins.SkinsTemplateLoader, and sits in a custom forum_module.

In the normal urls.py I provide the private mode with

(r'^%s' % 'private/' + settings.FORUM_SCRIPT_ALIAS, include('forum.urls')) (r'^%s' % settings.FORUM_SCRIPT_ALIAS, include('forum.urls'))

I created a custom urls_private.py where just the order of this changes:

(r'^%s' % settings.FORUM_SCRIPT_ALIAS, include('forum.urls')), (r'^%s' % 'private/' + settings.FORUM_SCRIPT_ALIAS, include('forum.urls'))

The private mode is then accessible in '/private'. And if we are in private mode all Links will automatically get the 'private/' prefix.

Added middleware which monitors the current private state. It does:

  1. store the private_mode in request.session[’private_mode’], and in the current thread (threading.local.local())
  2. set request.urlconf to 'urls_private' if in private mode
  3. Filter out all unsuitable (i.e. private) questions by adapting forum.models.node.NodeManager.get_query_set
  4. In the private skin we have a custom 'item.html'. forum.templatetags.question_list_tags.QuestionItemNode caches the template 'item.html'. We make it re-read so that eventual changes of the private mode take place.

OSQA caches nodes. Cached private nodes would also be accessible by other users, so we prevent them from being cached in models.base.BaseModel.cache():

if isinstance(self, Node) and self.private: return

And a small function to publish threads, which is also located in a custom forum_module. And of course some minor changes in the templates: The possibility to switch the mode, custom CSS and so on.

Everything works perfectly. The changes to the OSQA core are just 3 lines: the private field and preventing private nodes from being cached. Everything else works via a forum_module and middleware, which of course interfere with the core.

link

answered 01 Mar, 06:43

Flipper's gravatar image

Flipper
5615
accept rate: 0%

edited 01 Mar, 06:52

SOunds like nice work, @Flipper. Part of OSQA's good performance is due to the use of very effective caching, so bypassing node caching could cause slowdown at some point.

(01 Mar, 06:49) rickross ♦♦

Thanks! I'm aware of that. 'Public' nodes are still cached, so just the private mode will be a bit slower. There aren't many private questions yet, but it seems to not be a problem.

(01 Mar, 06:56) Flipper
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×11
×10
×7
×1

Asked: 15 Feb, 12:28

Seen: 199 times

Last updated: 01 Mar, 06:56

powered by OSQA