Exemple #1
0
 def execute(self):
     # TODO: better abstract this using the formatter
     html = []
     if exists(self.request, self.page.page_name, self.attachment):
         html.append('<img class="pagelogo" src="')
         html.append(
             getAttachUrl(self.page.page_name, self.attachment,
                          self.request))
         html.append('" >')
     return self.formatter.rawHTML('\n'.join(html))
Exemple #2
0
    def pageListWithContext(self, request, formatter, info=1, context=180,
                            maxlines=1, paging=True, hitsFrom=0, hitsInfo=0,
                            highlight_titles=True, highlight_pages=True):
        """ Format a list of found pages with context

        @param request: current request
        @param formatter: formatter to use
        @param info: show match info near the page link
        @param context: how many characters to show around each match.
        @param maxlines: how many contexts lines to show.
        @param paging: toggle paging
        @param hitsFrom: current position in the hits
        @param hitsInfo: toggle hits info line
        @param highlight_titles: perform highlighting in page list
        @param highlight_pages: add highlight parameter to page URLs
        @rtype: unicode
        @return formatted page list with context
        """
        ngowikiutil = NgoWikiUtil(request)
        ngowikiutil.open_database()

        self._reset(request, formatter)
        f = formatter
        write = self.buffer.write
        _ = request.getText

        if paging and len(self.hits) <= request.cfg.search_results_per_page:
            paging = False

        # Add pages formatted as definition list
        hitsTo = hitsFrom + request.cfg.search_results_per_page + 1
        hitDiff = 0
        if self.hits:
            write(f.definition_list(1))

            displayHits = []
            hitIdx = 0
            for hit in self.hits:
                result = ngowikiutil.select_page_by_path(hit.page_name)
                if result == None:
                    hitDiff = hitDiff + 1
                    continue
                if len(ngowikiutil.select_page_tags_by_id(result["id"])) == 0:
                    hitDiff = hitDiff + 1
                    continue
                hitIdx = hitIdx + 1
                if paging:
                    if hitsTo <= hitIdx - 1:
                        break
                    if hitsFrom <= hitIdx - 1:
                        displayHits.append(hit)
                else:
                    displayHits.append(hit)
            if len(displayHits) <= request.cfg.search_results_per_page and hitsFrom == 0:
                paging = False
            if len(displayHits) > request.cfg.search_results_per_page:
                displayHits = displayHits[0:request.cfg.search_results_per_page]

            if len(displayHits) == 0:
                write(u'没有找到相关内容,请调整搜索条件重新搜索')

            template = '''
                <table class="listitem_with_logosummary">
                    <tr>
                        <!--
                        <td class="logo">
                            %(logo)s
                        </td>
                        -->
                        <td>
                           <div class="title">
                              <a href="%(link)s">%(title)s</a>
                           </div>
                           <div class="meta">
                               <span>%(lastmodified)s</span>
                               <span>%(tags)s</span>
                               <span><span class="metaitem">%(likecount)s<span></span>
                               <span><span class="metaitem">%(commentcount)s<span></span>
                               <span><span class="metaitem">%(hitcount)s<span></span>
                           </div>
                           <div class="summary">%(summary)s</div>
                        </td>
                     </tr>
                 </table>
                '''
            for page in displayHits:
                result = ngowikiutil.select_page_by_path(page.page_name)
                if result != None:
                    if len(ngowikiutil.select_page_tags_by_id(result["id"])) > 0:
                        page = Page(request, result["path"]) 
                        logo = '<div class="logo defaultLogo">&nbsp;</div>'
                        if len(result["logo"]) > 0 and exists(request, result["path"], result["logo"]):
                            logo = '<img class="logo" src="' + getAttachUrl(result["path"], result["logo"], request) + '">'
                        link = page.url(request)
                        title = result["title"]
                        lastmodified = page.mtime_printable(request)
                        summary = result["summary"].replace(u"'''", "").replace(u"【请在此插入图片】", "").replace(u"【请在此插入图片,最多可插入9张】", "")

                        tags = (", ".join(
                                  map(lambda x: '<a href=\'javascript:add_filter_by_tag(' + json.dumps(x["tag"]) + ')\' >' + x["tag"] + '</a>', 
                                      filter(lambda x: x["type"] == 1, ngowikiutil.select_page_tags_by_id(result["id"]))
                                  )))

                        if len(tags) > 0:
                            tags = '<span class="metaitem">' + tags + '</span>'

                        write(template % {"logo":logo, "title": title, "link": link, "lastmodified": lastmodified, "tags": tags, "summary": summary, "likecount": u'\u559c\u6b22\uff1a' + str(result["likecount"]), "commentcount": u'\u8bc4\u8bba\u6570\uff1a' + str(result["commentcount"]), "hitcount": u'\u8bbf\u95ee\u91cf\uff1a' + str(result["hitcount"])})
            write(f.definition_list(0))
            if paging:
                write(self.formatPageLinks(hitsFrom=hitsFrom,
                    hitsPerPage=request.cfg.search_results_per_page,
                    hitsNum=len(self.hits) - hitDiff))

        return self.getvalue()
Exemple #3
0
    def execute(self):
        ngowikiutil = NgoWikiUtil(self.request)
        ngowikiutil.open_database()
        try:
            offset = 0
            length = 10
            sortby = "lastmodified"
            order = "DESC"
            filterByTags = self.filterByTags
            favorite = None

            form = self.request.values
            if 'from' in form:
                offset = int(form['from'])
            if 'length' in form:
                length = int(form['length'])
            if 'sortby' in form:
                sortby = form['sortby']
                if sortby == 'title':
                    order = "ASC"
            if 'order' in form:
                order = form['order']
            if 'favorite' in form and form[
                    'favorite'] != "false" and self.user != None and self.user.valid:
                favorite = self.user.id
            if 'filterByTags' in form:
                filterByTags = form['filterByTags'].split(",")

            for tag in filterByTags:
                ngowikiutil.update_tag_hitcount(tag)

            results = ngowikiutil.select_pages_by_tag(filterByTags, favorite,
                                                      sortby, order, offset,
                                                      length)
            total = ngowikiutil.count_pages_by_tag(filterByTags, favorite)

            buffer = []
            buffer.append(
                '''
                <script language="javascript">window.__ListPagesByTag_filterByTag = %(filterByTags)s;window.__ListPagesByTag_filterByTag_default = %(filterByTagsDefault)s;</script>
                <div id="listpagesbytag_sorter"></div>
				<div id="listpagesbytag_favorite"></div>
                <div id="listpagesbytag_filter"></div>
            ''' % {
                    "filterByTagsDefault": json.dumps(self.filterByTags),
                    "filterByTags": json.dumps(",".join(filterByTags))
                })

            template = '''
                <table class="listitem_with_logosummary">
                    <tr>
                        <!--
                        <td class="logo">
                            %(logo)s
                        </td>
                        -->
                        <td>
                           <div class="title">
                              <a href="%(link)s">%(title)s</a>
                           </div>
                           <div class="meta">
                               <span>%(lastmodified)s</span>
                               <span>%(tags)s</span>
                               <span><span class="metaitem">%(likecount)s<span></span>
                               <span><span class="metaitem">%(commentcount)s<span></span>
                               <span><span class="metaitem">%(hitcount)s<span></span>
                           </div>
                           <div class="summary">%(summary)s</div>
                        </td>
                     </tr>
                 </table>
                '''
            for result in results:
                page = Page(self.request, result["path"])
                logo = '<div class="logo defaultLogo">&nbsp;</div>'
                if len(result["logo"]) > 0 and exists(
                        self.request, result["path"], result["logo"]):
                    logo = '<img class="logo" src="' + getAttachUrl(
                        result["path"], result["logo"], self.request) + '">'
                link = page.url(self.request)
                title = result["title"]
                lastmodified = page.mtime_printable(self.request)
                summary = result["summary"].replace("'''", "").replace(
                    u"【请在此插入图片】", "").replace(u"【请在此插入图片,最多可插入9张】", "")

                tags = (", ".join(
                    map(
                        lambda x: '<a href=\'javascript:add_filter_by_tag(' +
                        json.dumps(x["tag"]) + ')\' >' + x["tag"] + '</a>',
                        filter(
                            lambda x: x["type"] == 1 or x["type"] == 2,
                            ngowikiutil.select_page_tags_by_id(
                                result["id"])))))

                if len(tags) > 0:
                    tags = '<span class="metaitem">' + tags + '</span>'

                buffer.append(
                    template % {
                        "logo":
                        logo,
                        "title":
                        title,
                        "link":
                        link,
                        "lastmodified":
                        lastmodified,
                        "tags":
                        tags,
                        "summary":
                        summary,
                        "likecount":
                        u'\u559c\u6b22\uff1a' + str(result["likecount"]),
                        "commentcount":
                        u'\u8bc4\u8bba\u6570\uff1a' +
                        str(result["commentcount"]),
                        "hitcount":
                        u'\u8bbf\u95ee\u91cf\uff1a' + str(result["hitcount"])
                    })

            buffer.append("<script language='javascript'>render_pagingbar(" +
                          str(total) + ', ' + str(length) + ');</script>')
            ret = ''.join(buffer)
            return ret
        finally:
            ngowikiutil.close_database(True)
    def execute(self):

        if FrontpageMacro.lastupdated == None or long(time.time()) - FrontpageMacro.lastupdated > 3600L:
            ngowikiutil = NgoWikiUtil(self.request)
            try:
                ngowikiutil.open_database()
                FrontpageMacro.totalcount_activities = ngowikiutil.count_pages_by_tag([u'服务产品类']) + ngowikiutil.count_pages_by_tag([u'视听产品类']) + ngowikiutil.count_pages_by_tag([u'实体产品类'])
                FrontpageMacro.totalcount_ngos = ngowikiutil.count_pages_by_tag([u'公益机构类'])
                FrontpageMacro.totalcount_enterprises = ngowikiutil.count_pages_by_tag([u'企业志愿组织类'])
                FrontpageMacro.featured_activities = ngowikiutil.select_pages_with_one_of_tags([u'服务产品类', u'视听产品类', u'实体产品类'], 'featured', 'DESC', 0, 20)
                for record in FrontpageMacro.featured_activities[0:2]:
                    record["summary"] = record["summary"].replace("'''", "")
                    record["summary"] = filter_summary(record["summary"], 50);
                    pagename = record["path"]
                    page = Page(self.request, pagename)
                    record["link"] = page.url(self.request)
                    if len(record["logo"]) > 0 and exists(self.request, record["path"], record["logo"]):
                        record["logo_link"] = getAttachUrl(record["path"], record["logo"], self.request)
                    else:
                        findLogo = False
                        text = page.getPageText()
                        match = re.search('\\{\\{attachment:([^\\|]+)\\|\\|.*}}', text)
                        if match != None:
                            logo = match.group(1)
                            if exists(self.request, record["path"], logo):
                                record["logo_link"] = getAttachUrl(record["path"], logo, self.request)
                                findLogo = True
                        if not findLogo:
                            record["logo_link"] = self.request.cfg.url_prefix_static + "/ngowiki/img/no-logo.png"
                FrontpageMacro.featured_ngos = ngowikiutil.select_pages_by_tag([u'公益机构类'], 'featured', 'DESC', 0, 2)
                for record in FrontpageMacro.featured_ngos:
                    pagename = record["path"]
                    record["summary"] = filter_summary(record["summary"], 50)
                    page = Page(self.request, pagename)
                    record["link"] = page.url(self.request)
                    if len(record["logo"]) > 0 and exists(self.request, record["path"], record["logo"]):
                        record["logo_link"] = getAttachUrl(record["path"], record["logo"], self.request)
                    else:
                        record["logo_link"] = self.request.cfg.url_prefix_static + "/ngowiki/img/no-logo.png"
                FrontpageMacro.recently_added = ngowikiutil.select_latest_created_pages([u'服务产品类', u'视听产品类', u'实体产品类', u'公益机构类', u'企业志愿组织类'], 0, 5)
                for record in FrontpageMacro.recently_added:
                    pagename = record["path"]
                    page = Page(self.request, pagename)
                    record["link"] = page.url(self.request)
                    if len(record["logo"]) > 0 and exists(self.request, record["path"], record["logo"]):
                        record["logo_link"] = getAttachUrl(record["path"], record["logo"], self.request)
                    else:
                        record["logo_link"] = self.request.cfg.url_prefix_static + "/ngowiki/img/no-logo.png"
                    if u'服务产品类' in ngowikiutil.parse_page(page)["categories"] or u'视听产品类' in ngowikiutil.parse_page(page)["categories"] or u'实体产品类' in ngowikiutil.parse_page(page)["categories"]:
                        record["recently_added_type"] = "activity"
                    elif u'公益机构类' in ngowikiutil.parse_page(page)["categories"]:
                        record["recently_added_type"] = "ngo"
                    else:
                        record["recently_added_type"] = "enterprise"
                FrontpageMacro.news_items = ngowikiutil.select_pages_by_tag([u'新闻动态类'], 'featured', 'DESC', 0, 100)
                for record in FrontpageMacro.news_items:
                    pagename = record["path"]
                    page = Page(self.request, pagename)
                    record["link"] = page.url(self.request)
                    if len(record["logo"]) > 0 and exists(self.request, record["path"], record["logo"]):
                        record["logo_link"] = getAttachUrl(record["path"], record["logo"], self.request)
                    else:
                        record["logo_link"] = self.request.cfg.url_prefix_static + "/ngowiki/img/no-logo.png"
            finally:
                ngowikiutil.close_database(True)
            FrontpageMacro.lastupdated = long(time.time())

        context = {
            'totalcount_activities': FrontpageMacro.totalcount_activities,
            'totalcount_ngos': FrontpageMacro.totalcount_ngos,
            'totalcount_enterprises': FrontpageMacro.totalcount_enterprises,
            'featured_activities': FrontpageMacro.featured_activities,
            'featured_ngos': FrontpageMacro.featured_ngos,
            'recently_added': FrontpageMacro.recently_added,
            'news_items': FrontpageMacro.news_items,
            'logo_url': self.request.cfg.url_prefix_static + "/ngowiki/img/sitelogo.png",
            'slogan_url': self.request.cfg.url_prefix_static + "/ngowiki/img/slogan2.png"
        }

        engine = tenjin.Engine(path=[os.path.dirname(__file__) + '/views'])
        html = engine.render('Frontpage.pyhtml', context)

        return self.formatter.rawHTML(html)