Sphene Community Tools

Community

Copyright © 2007-2018 by Herbert Poul

You are not logged in.
Login
Register

Change Language:



AddThis Social Bookmark Button

A Django site.

Powered by Sphene Community Tools
Board » General » problems with wiki preview

Hi :)

I have some strange problems with the preview in the wiki.

I made my own view that does a test for a cancel press and then calls the editSnip(...) function.

The problems does only appear if I have links in the wiki snip.

Do you have any idea of what I might have broken or if there are some deeper bug here?

my function
def edit_wiki_snip(request, project_id, snipName, versionId=None):
    "Override sphwiki edit snip and insert: cancel-test + permissions test."

    project = get_object_or_404(models.Project, pk=project_id)
    (r, w, d) = get_permissions(project, request.user)

    if not w:
        return do_login_or_403(request)

    g = project.ensure_group()

    print g

    if request.method == 'POST':

        # cancel test here                                                                                   
        if form_cancel(request):
            if DEBUG:
                print "cancel."

            # TODO: this is too ugly!                                                                        
            path = u"/software/%d/wiki/show/%s/" % (project.id, snipName)
            return HttpResponseRedirect(path)
        else:
            # something was saved or preview was pressed                                                     
            if request.POST.has_key(u'type') and request.POST[u'type'] == 'preview':
                if DEBUG:
                    print "Do not send message, this is just a PREVIEW."
            else:
                if DEBUG:
                    print "Set message - we pressed save/submit/whatever."
                request.user.message_set.create(message=u"Saving changes in '%s'." % snipName)


    if DEBUG:
        print "call super function"

    return editSnip(request, g, snipName, versionId)


debug changes in original code
root@dactyl:/usr/lib/python2.5/site-packages/sphene# svn info sphwiki/
Path: sphwiki
URL: http://yourhell.com/svn/root/django/communitytools/trunk/sphenecoll/sphene/sphwiki
Repository Root: http://yourhell.com/svn/root
Repository UUID: 79c7a89d-1512-0410-865a-fa53d4be4eaf
Revision: 629
Node Kind: directory
Schedule: normal
Last Changed Author: kahless
Last Changed Rev: 625
Last Changed Date: 2008-05-27 10:53:07 +0200 (Tue, 27 May 2008)

root@dactyl:/usr/lib/python2.5/site-packages/sphene# svn diff 
Index: sphwiki/views.py
===================================================================
--- sphwiki/views.py    (revision 629)
+++ sphwiki/views.py    (working copy)
@@ -258,6 +258,8 @@
 
 
 def editSnip(request, group, snipName, versionId = None):
+    print group
+
     version = None
     try:
         snip = WikiSnip.objects.get( group = group,
@@ -286,7 +288,21 @@
 
     if request.method == 'POST':
         if 'type' in request.POST and request.POST['type'] == 'preview':
-            return HttpResponse( unicode(WikiSnip(body = request.POST['body']).render() ))
+
+            import traceback
+            
+            print "PREVIEW"
+            try:
+                print group
+                print group.__class__
+                return HttpResponse( unicode(WikiSnip(body = request.POST['body'], group = group).render() ))
+            except:
+
+                print '-'*60
+                print traceback.format_exc()
+                print '-'*60
+
+                return HttpResponse('preview failed')
         changemessage = request.POST['message']
         form = SnipForm(request.POST)
 
root@dactyl:/usr/lib/python2.5/site-packages/sphene# 


output when I press preview and have a link:
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
1
Do not send message, this is just a PREVIEW.
call super function
1
PREVIEW
1
<class 'sphene.community.models.Group'>
------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/sphene/sphwiki/views.py", line 298, in editSnip
    return HttpResponse( unicode(WikiSnip(body = request.POST['body'], group = group).render() ))
  File "/usr/lib/python2.5/site-packages/sphene/sphwiki/models.py", line 64, in render
    'redirect': wikimacros.RedirectMacro( ),
  File "/usr/lib/python2.5/site-packages/sphene/community/templatetags/sph_extras.py", line 168, in sph_markdown
    ret = md.toString()
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1481, in __str__
    doc = self._transform()
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1068, in _transform
    self._processSection(self.top_element, buffer)
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1194, in _processSection
    self._processSection(parent_elem, theRest, inList)
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1194, in _processSection
    self._processSection(parent_elem, theRest, inList)
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1194, in _processSection
    self._processSection(parent_elem, theRest, inList)
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1173, in _processSection
    list = self._handleInlineWrapper2("\n".join(paragraph))
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1380, in _handleInlineWrapper2
    result = self._applyPattern(x, pattern)
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1461, in _applyPattern
    node = pattern.handleMatch(m, self.doc)
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/mdx_wikilink.py", line 97, in handleMatch
    wikilink = wikilink_utils.handle_wikilinks_match(m.groupdict())
  File "/usr/lib/python2.5/site-packages/sphene/sphwiki/wikilink_utils.py", line 34, in handle_wikilinks_match
    name = snipname, )
  File "/usr/lib/python2.5/site-packages/django/db/models/base.py", line 222, in __init__
    (field.name, field.rel.to, type(rel_obj)))
TypeError: Invalid value: 'group' should be a <class 'sphene.community.models.Group'> instance, not a <type 'NoneType'>

------------------------------------------------------------
[03/Jun/2008 08:52:00] "POST /software/1/wiki/edit/Start/ HTTP/1.1" 200 14



As you can see the group is gone - but first it is there...

Any help appreciated.

/Per
this is weird .. indeed.. the only idea i would have is.. that the thread local isn't set up correctly..

try the following:
from sphene.community.middleware import get_current_group
print get_current_group()

(i guess you have the ThreadLocals middleware in place ? otherwise nothing would work probably .. but anyway .. try the above code.. maybe something messed it up)
Hey, we have Signatures !!! Great, isn't it ? ;)
i guess i found the problem:

def edit_wiki_snip(request, project_id, snipName, versionId=None)

<-- try to add a 'group' parameter here:
def edit_wiki_snip(request, group, project_id, snipName, versionId=None)

and in your urlconf add a { 'groupName': None } as args to this view .. the MultiHostMiddleware and GroupMiddleware should do the rest than ..
Hey, we have Signatures !!! Great, isn't it ? ;)
[...]
from sphene.community.middleware import get_current_group
print get_current_group()
[...]
This does not work as I think it was intended. Perhaps my config of something is broken?

[...]
<-- try to add a 'group' parameter here:
def edit_wiki_snip(request, group, project_id, snipName, versionId=None)
[...]
add a { 'groupName': None }
[...]


Ok, but I had problems here, If I make it None I get problems later (I think that is because get_current_group() fails and again returns None). If I make it a default group I still get problems later since "default" does not "match regular expression '\\d+'".

I think the problem is in my config of some middleware? My hypothesis is that when a link is to be produced it needs the url and wants to look it up by using the group - the wrong group results in an exception inside som "build url" function (see traceback).

I also modified the editSnip to print slightly different feedback
svn diff
Index: views.py
===================================================================
--- views.py    (revision 629)
+++ views.py    (working copy)
@@ -286,7 +288,26 @@
 
     if request.method == 'POST':
         if 'type' in request.POST and request.POST['type'] == 'preview':
-            return HttpResponse( unicode(WikiSnip(body = request.POST['body']).render() ))
+
+            print "PREVIEW >> we are in the if-branch"
+
+            import traceback
+            print "PREVIEW >> imported traceback"
+
+
+            from sphene.community.middleware import get_current_group
+            print "PREVIEW >> the group variable: '%s'" % group
+            print "PREVIEW >> get_current_group:  '%s'" % get_current_group()
+            
+            try:
+                return HttpResponse( unicode(WikiSnip(body = request.POST['body'], group = group).render() ))
+            except:
+
+                print '-'*60
+                print traceback.format_exc()
+                print '-'*60
+
+                return HttpResponse('preview failed')
         changemessage = request.POST['message']
         form = SnipForm(request.POST)


My errormessage and traceback now looks like this:

PREVIEW >> we are in the if-branch
PREVIEW >> imported traceback
PREVIEW >> the group variable: '1'
PREVIEW >> get_current_group:  'default'
------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/sphene/sphwiki/views.py", line 303, in editSnip
    return HttpResponse( unicode(WikiSnip(body = request.POST['body'], group = group).render() ))
  File "/usr/lib/python2.5/site-packages/sphene/sphwiki/models.py", line 64, in render
    'redirect': wikimacros.RedirectMacro( ),
  File "/usr/lib/python2.5/site-packages/sphene/community/templatetags/sph_extras.py", line 168, in sph_markdown
    ret = md.toString()
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1481, in __str__
    doc = self._transform()
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1068, in _transform
    self._processSection(self.top_element, buffer)
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1194, in _processSection
    self._processSection(parent_elem, theRest, inList)
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1194, in _processSection
    self._processSection(parent_elem, theRest, inList)
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1194, in _processSection
    self._processSection(parent_elem, theRest, inList)
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1173, in _processSection
    list = self._handleInlineWrapper2("\n".join(paragraph))
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1380, in _handleInlineWrapper2
    result = self._applyPattern(x, pattern)
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/markdown.py", line 1461, in _applyPattern
    node = pattern.handleMatch(m, self.doc)
  File "/usr/lib/python2.5/site-packages/sphene/contrib/libs/markdown/mdx_wikilink.py", line 97, in handleMatch
    wikilink = wikilink_utils.handle_wikilinks_match(m.groupdict())
  File "/usr/lib/python2.5/site-packages/sphene/sphwiki/wikilink_utils.py", line 40, in handle_wikilinks_match
    href = snip.get_absolute_editurl()
  File "/usr/lib/python2.5/site-packages/sphene/community/sphpermalink.py", line 32, in inner
    return reverse(bits0, urlconf, *bits[1:3])
  File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 297, in reverse
    return iri_to_uri(u'/' + get_resolver(urlconf).reverse(viewname, *args, **kwargs))
  File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 283, in reverse
    return u''.join([reverse_helper(part.regex, *args, **kwargs) for part in self.reverse_dictlookup view])
  File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 88, in reverse_helper
    result = re.sub(r'\(([^)]+)\)', MatchChecker(args, kwargs), regex.pattern)
  File "/usr/lib/python2.5/re.py", line 150, in sub
    return _compile(pattern, 0).sub(repl, string, count)
  File "/usr/lib/python2.5/site-packages/django/core/urlresolvers.py", line 129, in __call__
    raise NoReverseMatch("Value %r didn't match regular expression %r" % (value, test_regex))
NoReverseMatch: Value u'default' didn't match regular expression '\\d+'

------------------------------------------------------------


What do you think? Should I just do some RTFM'ing about the details of the sphene middleware? If so: where?

Or can I manually set the correct group in thread locals somehow?

/Per
unfortunately .. there isn't much documentation about the middlewares (yet)

could you tell me your middleware setup ? which are active ?

and can you also print the type(get_current_group()) ? i'm a bit confused.. there is no way that get_current_group() can return a string.. every set_current_group(..) call passes a Group object..
Hey, we have Signatures !!! Great, isn't it ? ;)
parts of the settings file


[...]

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    'iportal.middleware.threadlocals.ThreadLocals',

    # sphene
    'sphene.community.middleware.ThreadLocals',
    'sphene.community.middleware.GroupMiddleware',    
)

# sphene
TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.request',
    'django.core.context_processors.media',
    'sphene.community.context_processors.navigation',
    )

[...]



If you see anything obvious please let me know.

You mentioned set_current_group, that helped me. If I in my view function set current group the preview works fine. So even though I am not sure why I had problems this fixes them for me :)

[...]
from sphene.community.middleware import set_current_group
[...]

def edit_wiki_snip(request, group, project_id, snipName, versionId=None):
    "Override sphwiki edit snip and insert: cancel-test + permissions test."

    project = get_object_or_404(models.Project, pk=project_id)
    [...]
    group = project.ensure_group()
    set_current_group(group)
    [...]


Thanks,
Per
[...]
i'm a bit confused.. there is no way that get_current_group() can return a string..
[...]
I missed this at first - I did not have None in my default dict - I had "default". The name of the default group, not the instance. I don't know why I tried it, but now I am not surprised it did not work either :)

/Per
Per Strandberg said @ 2008-06-04 06:45:46:
I missed this at first - I did not have None in my default dict - I had "default". The name of the default group, not the instance.
there are two different args .. 'groupName' should either contain None or the name of the group .. but.. None will only work if you are using the MultiHostMiddleware ..
so you should set it to 'default' .. IMHO

here the code snippet i think should do the job in the GroupMiddleware:

class GroupMiddleware(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        # ...
        group = None
        groupName = None
        if 'groupName' in view_kwargs:
            # ...
                groupName = view_kwargs['groupName']
                # ...
                if group == None:
                    group = get_object_or_404(Group, name = groupName )
                del view_kwargs['groupName']
                view_kwargs['group'] = group
                request.attributes['group'] = group

        set_current_group( group )


so .. IMHO .. it should load the group as mentioned in the view-args and set it into the thread local ..

maybe you could add some debugging to this middleware ? (e.g. line 106 right before set_current_group)

i guess using set_current_group( .. ) in your view should work fine for you for now .. but.. i wouldn't consider it a final solution :)
Hey, we have Signatures !!! Great, isn't it ? ;)

Please login to post a reply.



Powered by Sphene Community Tools