Exemple #1
0
def showSnip(request, group, snipName):
    snip_rendered_body = None
    try:
        snip = WikiSnip.objects.get(group=group, name__exact=snipName)
    except WikiSnip.DoesNotExist:
        snip = WikiSnip(name=snipName, group=group)

    if not snip.has_view_permission():
        raise PermissionDenied()

    res = None
    if 'type' in request.GET:
        if request.GET['type'] == 'src':
            res = HttpResponse(
                snip.body,
                mimetype='text/plain',
            )
        if request.GET['type'] == 'full':
            res = HttpResponse(
                snip.render(),
                mimetype='text/html',
            )

    if not res:
        sphdata = get_current_sphdata()
        snip_rendered_body = False
        redirects = ()
        while not snip_rendered_body or 'sphwiki_redirect_to_snip' in sphdata:
            if snip_rendered_body:
                if snip in redirects:
                    if request.user.is_authenticated():
                        request.user.message_set.create(
                            message=ugettext("Detected redirect loop."))
                    break
                redirects += (snip, )
                snip = sphdata['sphwiki_redirect_to_snip']
                del sphdata['sphwiki_redirect_to_snip']
            snip_rendered_body = snip.render()

        if sphdata != None: sphdata['subtitle'] = snip.title or snip.name

        res = render_to_response('sphene/sphwiki/showSnip.html', {
            'snip':
            snip,
            'snipName':
            snipName,
            'snip_rendered_body':
            snip_rendered_body,
            'redirects':
            redirects,
            'commentstemplate':
            'sphene.sphcomments' in settings.INSTALLED_APPS
            and 'sphene/sphwiki/wikicomments.html'
            or 'sphene/sphwiki/wikicomments_unavailable.html',
        },
                                 context_instance=RequestContext(request))

    res.sph_lastmodified = snip.changed
    return res
def showSnip(request, group, snipName):
    snip_rendered_body = None
    status = None
    try:
        snip = WikiSnip.objects.get( group = group,
                                     name__exact = snipName )
    except WikiSnip.DoesNotExist:
        snip = WikiSnip( name = snipName, group = group )
        status = 404

    if not snip.has_view_permission():
        raise PermissionDenied()

    res = None
    if 'type' in request.GET:
        if request.GET['type'] == 'src':
            res =  HttpResponse( snip.body, mimetype = 'text/plain', )
        if request.GET['type'] == 'full':
            res = HttpResponse( snip.render(), mimetype = 'text/html', )

    if not res:
        sphdata = get_current_sphdata()
        snip_rendered_body = False
        redirects = ()
        while not snip_rendered_body or 'sphwiki_redirect_to_snip' in sphdata:
            if snip_rendered_body:
                if snip in redirects:
                    if request.user.is_authenticated():
                        request.user.message_set.create( message = ugettext("Detected redirect loop.") )
                    break
                redirects += (snip,)
                snip = sphdata['sphwiki_redirect_to_snip']
                del sphdata['sphwiki_redirect_to_snip']
            snip_rendered_body = snip.render()
            
        if sphdata != None: sphdata['subtitle'] = snip.title or snip.name
    
        res = render_to_response( 'sphene/sphwiki/showSnip.html',
                                  { 'snip': snip,
                                    'snipName' : snipName,
                                    'snip_rendered_body': snip_rendered_body,
                                    'redirects': redirects,
                                    'wiki_sidebar_html': rendersidebar('W'),
                                    'commentstemplate': 'sphene.sphcomments' in settings.INSTALLED_APPS and 'sphene/sphwiki/wikicomments.html' or 'sphene/sphwiki/wikicomments_unavailable.html',
                                    },
                                  context_instance = RequestContext(request) )

    if status is not None:
        res.status_code = status
    res.sph_lastmodified = snip.changed
    return res
def handle_wikilinks_match(matchgroups):
    if matchgroups.get('urls'):
        # We matched a HTML link .. simply return the whole html code.
        return {'label': matchgroups.get('urls')}
    if matchgroups.get('escape'):
        # CamelCase expresion was escaped ...
        return {'label': matchgroups.get('wholeexpression')}
    snipname = matchgroups.get('camelcase') or matchgroups.get('snipname')
    label = matchgroups.get('sniplabel') or snipname.replace('_', ' ')

    cssclass = 'sph_wikilink'

    try:
        snip = WikiSnip.objects.get(
            group=get_current_group(),
            name=snipname,
        )
        href = snip.get_absolute_url()
    except WikiSnip.DoesNotExist:

        try:
            snip = WikiSnip(
                group=get_current_group(),
                name=snipname,
            )
        except TypeError:
            log.error('No group found when getting wikilinks. Ignoring.')
            return {'label': label}

        if not snip.has_edit_permission() \
                and get_sph_setting('wiki_links_nonexistent_show_only_privileged'):
            return {
                'label': label,
            }

        href = snip.get_absolute_editurl()
        cssclass += ' sph_nonexistent'
        if get_sph_setting('wiki_links_nonexistent_prefix'):
            label = "create:" + label

    return {
        'href': href,
        'label': label,
        'class': cssclass,
    }
def handle_wikilinks_match(matchgroups):
    if matchgroups.get('urls'):
        # We matched a HTML link .. simply return the whole html code.
        return { 'label': matchgroups.get('urls') }
    if matchgroups.get('escape'):
        # CamelCase expresion was escaped ...
        return { 'label': matchgroups.get('wholeexpression') }
    snipname = matchgroups.get('camelcase') or matchgroups.get('snipname')
    label = matchgroups.get('sniplabel') or snipname.replace('_', ' ')

    cssclass = 'sph_wikilink'

    try:
        snip = WikiSnip.objects.get( group = get_current_group(),
                                     name = snipname, )
        href = snip.get_absolute_url()
    except WikiSnip.DoesNotExist:

        try:
            snip = WikiSnip( group = get_current_group(),
                            name = snipname, )
        except TypeError:
            log.error('No group found when getting wikilinks. Ignoring.')
            return { 'label' : label}

        if not snip.has_edit_permission() \
                and get_sph_setting('wiki_links_nonexistent_show_only_privileged'):
            return { 'label': label, }

        href = snip.get_absolute_editurl()
        cssclass += ' sph_nonexistent'
        if get_sph_setting( 'wiki_links_nonexistent_prefix' ):
            label = "create:"+label
    
    return { 'href': href,
             'label': label,
             'class': cssclass,
             }
Exemple #5
0
def editSnip(request, group, snipName, versionId = None):
    version = None
    data = request.method == 'POST' and request.POST or None
    try:
        snip = WikiSnip.objects.get( group = group,
                                     name__exact = snipName )
        if versionId is not None:
            version = WikiSnipChange.objects.get( pk = versionId )
            if not version or version.snip != snip:
                # TODO: raise a 404 instead
                raise PermissionDenied()
            snip.body = version.body

        #SnipForm = forms.models.form_for_instance(snip, form = CaptchaEditBaseForm)
        form = WikiSnipForm(data, instance = snip)

    except WikiSnip.DoesNotExist:
        #SnipForm = forms.models.form_for_model(WikiSnip, form = CaptchaEditBaseForm)
        form = WikiSnipForm(data)
        snip = WikiSnip( name = snipName, group = group )
        #snip = None

    if not snip.has_edit_permission():
        raise PermissionDenied()

    #SnipForm.base_fields['body'].widget.attrs['cols'] = 80
    #SnipForm.base_fields['body'].widget.attrs['rows'] = 30

    changemessage = ""

    if request.method == 'POST':
        if 'type' in request.POST and request.POST['type'] == 'preview':
            return HttpResponse( unicode(WikiSnip(body = request.POST['body']).render() ))
        changemessage = request.POST['message']
        #form = SnipForm(request.POST)

        if form.is_valid():
            old_title = None
            old_body = None
            change_type = 0
            if snip.id:
                old_title = snip.title
                old_body = snip.body

            snip = form.save(commit=False)
            snip.group = group
            snip.name = snipName
            if request.user.is_authenticated():
                snip.editor = request.user
            else:
                snip.editor = None
            snip.save()

            if old_body is not None:
                if snip.body != old_body:
                    change_type |= 1

                if snip.title != old_title:
                    change_type |= 2

            else:
                # if a snip is new, everything has changed ..
                change_type = 1 | 2 | 4

            data = form.cleaned_data

            if tag_set_labels( snip, data['tags'] ):
                change_type |= 4

            change = WikiSnipChange( snip = snip,
                                     #editor = request.user,
                                     editor = snip.editor,
                                     edited = datetime.today(),
                                     message = request.POST['message'],
                                     title = snip.title,
                                     body = snip.body,
                                     change_type = change_type,
                                     )
            change.save()

            tag_set_labels( change, data['tags'] )
            
            return HttpResponseRedirect( snip.get_absolute_url() )

    else:
        #form = SnipForm()

        if snip is not None:
            form.fields['tags'].initial = tag_get_labels(snip)
        pass

    if version:
        from sphene.community.templatetags.sph_extras import sph_date
        changemessage = ugettext('Reverted to revision of %(editdate)s') % \
            { 'editdate': sph_date( version.edited ) }

    t = loader.get_template( 'sphene/sphwiki/editSnip.html' )
    return HttpResponse( t.render( RequestContext( request, 
                                                   { 'form': form,
                                                     'snip': snip,
                                                     'version': version,
                                                     'changemessage': changemessage } ) ) )
Exemple #6
0
def editSnip(request, group, snipName, versionId=None):
    version = None
    data = request.method == 'POST' and request.POST or None
    try:
        snip = WikiSnip.objects.get(group=group, name__exact=snipName)
        if versionId is not None:
            version = WikiSnipChange.objects.get(pk=versionId)
            if not version or version.snip != snip:
                # TODO: raise a 404 instead
                raise PermissionDenied()
            snip.body = version.body

        #SnipForm = forms.models.form_for_instance(snip, form = CaptchaEditBaseForm)
        form = WikiSnipForm(data, instance=snip)

    except WikiSnip.DoesNotExist:
        #SnipForm = forms.models.form_for_model(WikiSnip, form = CaptchaEditBaseForm)
        form = WikiSnipForm(data)
        snip = WikiSnip(name=snipName, group=group)
        #snip = None

    if not snip.has_edit_permission():
        raise PermissionDenied()

    #SnipForm.base_fields['body'].widget.attrs['cols'] = 80
    #SnipForm.base_fields['body'].widget.attrs['rows'] = 30

    changemessage = ""

    if request.method == 'POST':
        if 'type' in request.POST and request.POST['type'] == 'preview':
            return HttpResponse(
                unicode(WikiSnip(body=request.POST['body']).render()))
        changemessage = request.POST['message']
        #form = SnipForm(request.POST)

        if form.is_valid():
            old_title = None
            old_body = None
            change_type = 0
            if snip.id:
                old_title = snip.title
                old_body = snip.body

            snip = form.save(commit=False)
            snip.group = group
            snip.name = snipName
            if request.user.is_authenticated():
                snip.editor = request.user
            else:
                snip.editor = None
            snip.save()

            if old_body is not None:
                if snip.body != old_body:
                    change_type |= 1

                if snip.title != old_title:
                    change_type |= 2

            else:
                # if a snip is new, everything has changed ..
                change_type = 1 | 2 | 4

            data = form.cleaned_data

            if tag_set_labels(snip, data['tags']):
                change_type |= 4

            change = WikiSnipChange(
                snip=snip,
                #editor = request.user,
                editor=snip.editor,
                edited=datetime.today(),
                message=request.POST['message'],
                title=snip.title,
                body=snip.body,
                change_type=change_type,
            )
            change.save()

            tag_set_labels(change, data['tags'])

            return HttpResponseRedirect(snip.get_absolute_url())

    else:
        #form = SnipForm()

        if snip is not None:
            form.fields['tags'].initial = tag_get_labels(snip)
        pass

    if version:
        from sphene.community.templatetags.sph_extras import sph_date
        changemessage = ugettext('Reverted to revision of %(editdate)s') % \
            { 'editdate': sph_date( version.edited ) }

    t = loader.get_template('sphene/sphwiki/editSnip.html')
    return HttpResponse(
        t.render(
            RequestContext(
                request, {
                    'form': form,
                    'snip': snip,
                    'version': version,
                    'changemessage': changemessage
                })))