Beispiel #1
0
	def _create(self, format, parent_id=None):
		if format == 'json':
			params = self._validate(json.loads(request.body), CreateComment, 'new')
		#elif format == 'atom':
		#	from lxml import etree
		#	params = Comment.parse_xml(etree.fromstring(request.body))
		elif format == 'html':
			params = self._validate(request.POST.mixed(), CreateComment, 'new')
		else:
			raise UnacceptedFormat(format)
		
		params['parent'] = parent_id
		item = Comment.from_dict(params)
		item.add_author_or_contributor(user_from_session(session))
		if item.published is not None:
			user_id = item.author['name'] or str(session['user_id'])
			permalink = atom.slugify('-'.join([item.published.strftime("%Y-%m-%dT%H-%M-%S"),user_id]))
			story_permalink = meta.Session.query(Article).get(int(parent_id)).permalink
			story_url = url('story',permalink=story_permalink)
			item.atom_id = atom.get_tag_uri(story_url,item.published,user_id)
		item.save()
		app_globals.clear_count_comments()
		return item
Beispiel #2
0
    def pingback_ping(self, sourceURI, targetURI):
        try:
            doc = urlopen(sourceURI)
        except (HTTPError, URLError):
            return Fault(16, "The source URI does not exist.")

            # does the source refer to the target?
        soup = BeautifulSoup(doc.read())
        mylink = soup.find("a", attrs={"href": targetURI})
        if not mylink:
            return Fault(
                17, "The source URI does not contain a link to the target URI, and so cannot be used as a source."
            )

            # grab the title of the pingback source
        title = soup.find("title")
        if title:
            title = html.striphtml(unicode(title))
        else:
            title = "Unknown title"

            # extract the text around the incoming link
        content = unicode(mylink.findParent())
        i = content.index(unicode(mylink))
        content = html.striphtml(content)
        max_length = config.get("PINGBACK_RESPONSE_LENGTH", 200)
        if len(content) > max_length:
            start = i - max_length / 2
            if start < 0:
                start = 0
            end = i + len(unicode(mylink)) + max_length / 2
            if end > len(content):
                end = len(content)
            content = content[start:end]

        scheme, server, path, query, fragment = urlsplit(targetURI)

        # check if the target is valid target
        if request.headers["SERVER_NAME"] not in [server, server.split(":")[0]]:
            return Fault(
                33,
                "The specified target URI cannot be used as a target. It either doesn't exist, or it is not a pingback-enabled resource.",
            )

        route = config["routes.map"].match(path)
        try:
            article = meta.Session.query(Article).filter(Article.permalink == path).one()
        except:
            article = None
        if route is None or article is None:
            return Fault(32, "The specified target URI does not exist.")

            # Check if view accept pingbacks
        if route["controller"] not in ["blog"]:
            return Fault(
                33,
                "The specified target URI cannot be used as a target. It either doesn't exist, or it is not a pingback-enabled resource.",
            )

        pingbacks = (
            meta.Session.query(Comment)
            .filter(Comment.article_id == article.id)
            .filter(Comment.is_pingback == True)
            .all()
        )
        if any([x.author["uri"] == sourceURI for x in pingbacks]):
            return Fault(48, "The pingback has already been registered.")

        pb = Comment.from_dict(
            {"title": title.encode("utf-8"), "content": content.encode("utf-8"), "parent": article.id}
        )
        pb.author = {"uri": sourceURI, "name": "", "email": None}
        pb.save()
        return "pingback from %s to %s saved" % (sourceURI, targetURI)