예제 #1
0
    def handle(self, *args, **options):
        ix = create_in(settings.HAYSTACK_WHOOSH_PATH, PAGES_WHOOSH_SCHEMA, "ZORNA_PAGES")
        writer = ix.writer()
        root = settings.PROJECT_PATH + \
                os.sep + settings.ZORNA_CONTENT + os.sep
        for dirName, subdirList, fileList in os.walk(root):
            for file_name in fileList:
                try:
                    path_file = os.path.join(dirName, file_name)
                    context, text = get_context_text(path_file)
                    pars = HTMLParser.HTMLParser()
                    text = pars.unescape(unicode(text,'utf-8'))
                    text = ''.join( BeautifulSoup( text ).findAll( text = True ))
                    url = path_file.replace(root, '')
                    title = os.path.splitext(os.path.split(path_file)[1])[0]
                    if context:
                        import yaml
                        context = yaml.load(context)
                        if context['title']:
                            title = context['title']
                    print "Added %s ---- %s" % (url, title)

                    writer.add_document(title=unicode(title), content=text,
                                    url=unicode(url))
                except Exception as e:
                    print e
                    pass
        writer.commit()
예제 #2
0
    def handle(self, *args, **options):
        ix = create_in(settings.HAYSTACK_WHOOSH_PATH, PAGES_WHOOSH_SCHEMA,
                       "ZORNA_PAGES")
        writer = ix.writer()
        root = settings.PROJECT_PATH + \
                os.sep + settings.ZORNA_CONTENT + os.sep
        for dirName, subdirList, fileList in os.walk(root):
            for file_name in fileList:
                try:
                    path_file = os.path.join(dirName, file_name)
                    context, text = get_context_text(path_file)
                    pars = HTMLParser.HTMLParser()
                    text = pars.unescape(unicode(text, 'utf-8'))
                    text = ''.join(BeautifulSoup(text).findAll(text=True))
                    url = path_file.replace(root, '')
                    title = os.path.splitext(os.path.split(path_file)[1])[0]
                    if context:
                        import yaml
                        context = yaml.load(context)
                        if context['title']:
                            title = context['title']
                    print "Added %s ---- %s" % (url, title)

                    writer.add_document(title=unicode(title),
                                        content=text,
                                        url=unicode(url))
                except Exception as e:
                    print e
                    pass
        writer.commit()
예제 #3
0
파일: views.py 프로젝트: somair/zorna
def edit_page(request):
    b_pages_manager, b_templates_manager = get_pages_access(request.user)
    if b_pages_manager:
        from zorna.utils import get_context_text
        page = request.REQUEST.get('file', '')
        path_tf = os.path.join(
            settings.PROJECT_PATH, settings.ZORNA_CONTENT, page)
        header, text = get_context_text(path_tf)
        blocks = get_blocks(request, text)
        import yaml
        context_yaml = yaml.load(header)
        if request.method == 'POST':
            document = ''
            try:
                import codecs
                lflr = '\r\n'
                fd = codecs.open(path_tf, "r+", "utf-8")
                text = fd.read()
                to_add = []
                for key, value in request.POST.iteritems():
                    if key[0:2] == '__':
                        document = document + ' ' + value
                        repl = "%s block %s %s\n%s\n%s endblock %s" % (
                            '{%', key, '%}', value, '{%', '%}')
                        pre = re.compile(r'(%s\s*block\s*%s\s*%s)(.*?)(%s\s*endblock.*?\s*%s)' % (re.escape(
                            '{%'), key, re.escape('%}'), re.escape('{%'), re.escape('%}')), re.M | re.DOTALL)
                        if pre.search(text):
                            text = pre.sub(repl, text)
                        else:
                            to_add.append(repl + lflr)

                description = request.POST.get(
                    "description", '').replace('\n', '')
                tab_ctx = {'title': request.POST.get("title", ''), 'description': description, 'keywords': request.POST.get(
                    "keywords", ''), 'created': str(datetime.datetime.now()), 'author': str(request.user.pk)}
                if not header:
                    context_yaml = tab_ctx
                else:
                    context_yaml.update(tab_ctx)
                result = ''
                for k, v in context_yaml.iteritems():
                    if k in request.POST:
                        v = request.POST.get(k, '').replace('\n', '')
                    result = result + k + ": '%s'%s" % (
                        v.replace("'", "''"), lflr)
                ctx = "%s zorna %s%s%s%s" % ('{%', lflr, result, lflr, '%}')
                pre = re.compile(r'(%s\s*zorna)(.*?)(\s*%s)' % (
                    re.escape('{%'), re.escape('%}')), re.M | re.DOTALL)
                if pre.search(text):
                    text = pre.sub(ctx, text)
                else:
                    text = text + lflr + ctx

                what = request.REQUEST.get('what', 'save')
                if what == 'save':
                    fd.seek(0)
                    fd.truncate()
                    fd.write(text)
                    fd.close()
                    zorna_page_save.send(
                        None, created=False, content=document, title=request.POST.get("title", page), url=page)
                else:
                    # create temporary file
                    head, tail = os.path.split(page)
                    if head:
                        head = head + '/'
                    temp_page = head + 'temp-%s' % tail
                    path_tempf = os.path.join(
                        settings.PROJECT_PATH, settings.ZORNA_CONTENT, temp_page)
                    fd = open(path_tempf, 'w+')
                    fd.write(text.encode('UTF-8'))
                    fd.close()
                    return HttpResponseRedirect(reverse('preview_page', args=[os.path.splitext(temp_page)[0]]))
            except Exception as e:
                ret = {'status': 'error', 'message': 'Error: %s' % str(e)}
                return HttpResponse(simplejson.dumps(ret))

            ret = {'status': 'success', 'message':
                   'Your changes have been saved successfully.'}
            return HttpResponse(simplejson.dumps(ret))

        form = PageEditFileForm(extra=blocks, request=request)
        if header:
            initial_data = {}
            initial_data['title'] = context_yaml[
                'title'] if 'title' in context_yaml else ''
            initial_data['description'] = context_yaml[
                'description'] if 'description' in context_yaml else ''
            initial_data['keywords'] = context_yaml[
                'keywords'] if 'keywords' in context_yaml else ''
            for e in ['author', 'created', 'title', 'keywords', 'description']:
                if e in context_yaml:
                    del context_yaml[e]
            form_context = PageEditFileContextForm(
                initial=initial_data, extra=context_yaml)
        else:
            form_context = None
        extra_context = {'form_context': form_context, 'form':
                         form, 'cdir_components': format_components(page), 'template_file': page}
        context = RequestContext(request)
        return render_to_response('pages/fm_edit_file.html', extra_context, context_instance=context)
    else:
        return HttpResponse('')