Example #1
0
	def _new(self, parent_id=None, with_defaults=True):
		item = Comment()
		if parent_id is not None:
			parent_attr = self._get_parent_attr()
			setattr(item,parent_attr,parent_id)
		if with_defaults is True:
			item.set_defaults()
		return item
Example #2
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
Example #3
0
	def setUp(self):
		from columns.model import Page, User, Article, Comment
		ptmp = Page.from_dict(dict(
			id=1,
			title=u'Main',slug=u'main',content=u'',template=u'/blog/stream',
			stream_comment_style=u'summary',story_comment_style=u'list',
			visible=True,can_post=True,in_main=True,in_menu=False,
		))
		ptmp.save()
		utmp = User.from_dict(dict(
			id=1,
			name=u'test_user',
			open_id=None,
			fb_id=None,
			twitter_id=None,
			type=1,
			profile=None,
		))
		utmp.save()
		u2tmp = User.from_dict(dict(
			id=2,
			name=None,
			open_id=None,
			fb_id=None,
			twitter_id=None,
			type=1,
			profile=None,
		))
		u2tmp.save()
		atmp = Article(**dict(
			id=1,
			created=dt,
			updated=dt,
			atom_id=u'-'.join([dt.strftime("%Y-%m-%d"),u'test']),
			title=u'test',
			content=u'',
			summary=u'',
			published=dt,
			links=[],
			author_id=utmp.id,
			author={
				'name':u'test_user',
			},
			contributors=[],
			metatags={},
			metacontent=u'',
			permalink=u'-'.join([dt.strftime("%Y-%m-%d"),u'test']),
			sticky=False,
			can_comment=True,
			page_id=ptmp.id,
		))
		atmp.save()
		ctmp = Comment(**dict(
			id=1,
			updated=dt,
			atom_id=u'-'.join([dt.strftime("%Y-%m-%d"),u'test']),
			title=u'testcomment',
			content=u'this is a comment',
			published=dt,
			author_id=utmp.id,
			author={
				'name':u'test_user',
			},
			article_id=atmp.id,
		))
		ctmp.save()
Example #4
0
	def test_delete_browser_fakeout(self):
		response = self.app.post(url('comment', parent_id=1, id=1), extra_environ=self.extra_environ, params=dict(_method='delete'))
		from columns.model import Comment
		tmp = Comment.get_from_id(1)
		assert tmp is None
Example #5
0
	def test_delete(self):
		response = self.app.delete(url('comment', parent_id=1, id=1), extra_environ=self.extra_environ)
		from columns.model import Comment
		tmp = Comment.get_from_id(1)
		assert tmp is None
Example #6
0
	def test_getowner(self):
		comment = Comment()
		comment.author_id = 1
		self.assertEquals(1,comment.owner())
Example #7
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)