def renderWebItem(self, node, item, headerTemplate):
        i, webItemNode = item
        node.atts['class'] = i % 2 and 'altrow' or ''

        if isinstance(webItemNode, WebItemNode):
            webitem = webItemNode.webitem
            node = node.placeHolder
            node.checkbox.atts['name'] = str(webitem.id)
            node.itemDescription.content = unicode(webitem)
            if util.isFileURL(webitem.url):
                href = 'javascript:open_url(%s,"%s")' % (webitem.id, webitem.url)
                node.itemDescription.atts['href'] = href
            else:
                node.itemDescription.atts['href'] = webitem.url
            node.itemDescription.atts['title'] = '%s %s' % (webitem.modified, webitem.description)
            node.itemTag.tag.repeat(self.renderWebItemTag, webitem.tags)
            node.edit.atts['href'] %= webitem.id
            node.delete.atts['href'] = '%s/%s?method=delete' % (request.WEBLIB_URL, webitem.id)
#            if webitem.fetched:
#                node.cache.atts['href'] = '%s/%s/snapshotFrame' % (request.WEBLIB_URL, webitem.id)
#                node.cache.content = webitem.fetched
#            else:
#                node.cache.atts['href'] = '%s/%s/snapshot/get' % (request.WEBLIB_URL, webitem.id)
#                node.cache.content = 'download'
        else:
            tag = webItemNode.tag
            node.placeHolder = headerTemplate
            node.placeHolder.prefix.content = webItemNode.prefix
            node.placeHolder.itemHeader.content = unicode(tag) + webItemNode.suffix
            node.placeHolder.itemHeader.atts['href'] = request.tag_url([tag])
def doLaunchURL(wfile, req):
    wlib = store.getWeblib()
    item = wlib.webpages.getById(req.rid)
    if not item:
        wfile.write('404 not found\r\n\r\n%s not found' % req.rid)
        return

    if util.isFileURL(item.url):
        # TODO: HACK win32 only??
        # TODO: It is dangerous to launch anything could be executable or script
        from minds.weblib.win32 import ntfs_util
        ntfs_util.launch(item.url)
        wfile.write('content-type: text/html\r\n')
        wfile.write('Cache-control: no-cache\r\n')
        wfile.write('\r\n')
        wfile.write('''<html>
<head>
  <script>window.close();</script>
</head>
<body>
  File launched in separate window. Please close this window.
</body>
</html>
        ''')
    else:
        response.redirect(wfile, item.url)
def find_url(wlib, url):
    """
    @url - url to search for. String matching, no normalization.
    @return list of matched WebPages
    """
    if util.isFileURL(url):
        # use a for flexible match rule to account for file name variations.
        ### TODO: NT specific?
        ### TODO: need optimize?
        scheme, netloc, url_path, _, _, _ = urlparse.urlparse(url)
        pathname = util.nt_url2pathname(url_path)
        pathname = os.path.normcase(pathname)
        result = []
        for item in wlib.webpages:
            scheme, netloc, url_path, _, _, _ = urlparse.urlparse(item.url)
            if scheme != 'file':
                continue
            p1 = util.nt_url2pathname(url_path)
            p1 = os.path.normcase(p1)
            if pathname == p1:
                result.append(item)
        return result

    else:
        return [item for item in wlib.webpages if item.url == url]
    def render(self, node, bean, tags):

        item = bean.item
        wlib = store.getWeblib()

        if item.id == -1:
            node.form_title.content %= 'Add Entry'
            node.edit_header.omit()
        else:
            node.form_title.content %= 'Edit Entry'
            node.add_header.omit()

        form = node.form
        id = item.id < 0 and '_' or str(item.id)
        form.atts['action'] = request.rid_url(id)

        if bean.errors:
            escaped_errors = map(saxutils.escape, bean.errors)
            form.error.message.raw = '<br />'.join(escaped_errors)
        else:
            form.error.omit()

        if item:
            form.name       .atts['value'] = item.name
            form.created    .atts['value'] = item.created
            form.url        .atts['value'] = item.url
            form.description.content       = item.description
            form.tags       .atts['value'] = bean.item.tags_description
            form.nickname   .atts['value'] = item.nickname

            if weblib_util.isFileURL(item.url):
                scheme, netloc, url_path, _, _, _ = urlparse.urlparse(item.url)
                pathname = weblib_util.nt_url2pathname(url_path)
                form.url_link.atts['href']  = '/weblib/%s/url#%s' % (item.id, item.url)
                form.filename.content = pathname
            else:
                form.url_link.atts['href']  = item.url
                form.filename.omit()

#            if item.modified:
#                form.modified_txt.content = item.modified
#            if item.fetched:
#                form.snapshot_txt.content = item.fetched

        tags_strings = [u'        "%s"' % response.jsEscapeString(unicode(tag)) for tag in tags]
        node.form.tags_array.raw = node.form.tags_array.raw % ',\n'.join(tags_strings)

        new_tags = bean.newTags and u', '.join(bean.newTags) or ''
        encoded_tags = response.jsEscapeString(new_tags)
        node.form.new_tags_js_var.raw = node.form.new_tags_js_var.raw % encoded_tags

# weblibForm get invoked from CGI weblib.py

#if __name__ == "__main__":
#    main(sys.stdin, sys.stdout, os.environ)
Esempio n. 5
0
    def writeWebPage(self, webpage, flush=True):
        """
        The webpage can be a new or an existing item.
        If webpage.id is less than 0, an new id would be assigned.

        @param webpage - the webpage to be written. By design this item
            would be invalidated after this call. Use the new instance
            returned instead if necessary.

        @return an new instance of the webpage written.
        """
        self.lock.acquire()
        try:
            op = "U"
            webpage.timestamp = _getTimeStamp()
            # TODO: this relies on call has the version of existing item.
            # On the other hand caller are suppose to create a new item
            # instead of updating the object in memory. Probably the only
            # right way to do it is call original item's __copy__().
            # Expect too high of discipline?
            webpage.version += 1
            if webpage.id < 0:
                op = "C"
                webpage.id = self.wlib.webpages.acquireId()
            line = self._serialize_webpage(op, webpage)
            newItem = self._interpretRecord(line)
            self._conv_tagid(self.wlib.webpages.getById(newItem.id))
            self._log(line, flush)

            # shred the input webpage
            webpage.__dict__.clear()  # TODO: this would only raise AttributeError for caller. Make better error message?

            # 2005-12-20 TODO Review if this is the best place for this
            # If it is a file URL, try to save the meta data with the file also (if the OS support).
            if util.isFileURL(newItem.url):
                updateFileMetaData(newItem)

            return newItem

        finally:
            self.lock.release()
    def _parse_GET(self, req):
        """
        Parse submission from via bookmarklet or links
          method: GET
          parameters: url, title, description
        """
        wlib = store.getWeblib()

        # Three possiblities:
        #
        # 1. rid is an existing webpage
        #    The edit link from main page or
        #    user enter URL directly or
        #    request of case 3 redirected to an existing rid
        #
        # 2. rid is -1 and URL is not found in weblib
        #    Submit new page via bookmarklet
        #
        # 3. rid is -1 and URL is found in weblib
        #    Submit existing page via bookmarklet

        if self.oldItem:
            # Case 1. make a copy of the existing item
            item = self.oldItem.__copy__()
            # overwritten with request parameters (only if defined)
            # usually only defined if it is redirected from case 3 request.
#            if req.param('title')      : item.name        = req.param('title')
#            if req.param('url')        : item.url         = req.param('url')
#            if req.param('description'): item.description = req.param('description')
        else:
            url = req.param('url')
            matches = query_wlib.find_url(wlib, url)
            if not matches:
                # Case 2. this is a new webpage
                today = datetime.date.today().isoformat()
                if weblib_util.isFileURL(url):
                    item, tags = ntfs_util.makeWebPage(url)
                else:
                    item = weblib.WebPage(
                        name        = req.param('title'),
                        url         = url,
                        description = req.param('description'),
                        created     = today,
                        modified    = today,
                        lastused    = today,
                    )

                if wlib.getDefaultTag():
                    item.tags = [wlib.getDefaultTag()]
            else:
                # Case 3. use existing webpage
                self.oldItem = matches[0]
                item = self.oldItem.__copy__()
                # however override with possibly new title and description
                item.name        = req.param('title')
                item.description = req.param('description')
                # actually the item is not very important because we
                # are going to redirect the request to the proper rid.

        self.item = item
        # construct tags_description for editing
        self.item.tags_description  = ', '.join([l.name for l in item.tags])