コード例 #1
0
ファイル: __init__.py プロジェクト: yoshrote/Columns
	def update_from_dict(self, dt):
		#self.atom_id = dt.get('atom_id',None)
		self.title = dt.get('title',None)
		self.content = dt.get('content',None)
		
		self.tags.clear()
		for label in dt.get('tags',[]):
			tag = Tag.get_from_id(slugify(label)) or Tag(label=label)
			self.tags.add(tag)
		self.updated = rfc3339.now().replace(tzinfo=None)
コード例 #2
0
ファイル: __init__.py プロジェクト: yoshrote/Columns
	def update_from_dict(self, dt):
		self.title = dt['title']
		self.slug = slugify(self.title)
		self.content = dt.get('content',None)
		self.template = dt.get('template',None)
		#settings, should be moved to a module with a value of Page.slug
		self.stream_comment_style = dt['stream_comment_style']
		self.story_comment_style = dt['story_comment_style']
		self.visible = dt['visible']
		self.can_post = dt['can_post']
		self.in_main = dt['in_main']
		self.in_menu = dt['in_menu']
コード例 #3
0
ファイル: __init__.py プロジェクト: yoshrote/Columns
	def from_dict(cls, dt):
		item = cls()
		item.title = dt['title']
		item.slug = slugify(item.title)
		item.content = dt.get('content',None)
		item.template = dt.get('template',None)
		item.stream_comment_style = dt['stream_comment_style']
		item.story_comment_style = dt['story_comment_style']
		item.visible = dt['visible']
		item.can_post = dt['can_post']
		item.in_main = dt['in_main']
		item.in_menu = dt['in_menu']
		return item
コード例 #4
0
ファイル: __init__.py プロジェクト: yoshrote/Columns
	def from_dict(cls, dt):
		item = cls()
		item.id = dt.get('id',None)
		#item.atom_id = dt.get('atom_id',None)
		item.title = dt.get('title',None)
		item.content = dt.get('content',None)
		
		for label in dt.get('tags',[]):
			tag = Tag.get_from_id(slugify(label)) or Tag(label=label)
			item.tags.add(tag)
		item.updated = item.published = rfc3339.now().replace(tzinfo=None)
		basename = item._upload_file(dt)
		item.title = basename if item.title is None else item.title
		#upload_url = url_gen('story',permalink=item.permalink)
		#item.atom_id = get_tag_uri(upload_url,item.published,basename)
		return item
コード例 #5
0
ファイル: __init__.py プロジェクト: yoshrote/Columns
	def update_from_dict(self, dt, default_thumb=None):
		self.title = dt.get('title',None)
		self.published = dt.get('published',None)
		self.content = dt.get('content',None)
		self.sticky = dt.get('sticky',None)
		self.can_comment = dt.get('can_comment',None)
		self.page_id = dt.get('page_id',None)
		
		self.metacontent = html.striphtml(self.content)
		self.summary = html.stripobjects(self.content)
		media_data = html.get_metamedia_data(self.content, default_thumb)
		self.links = media_data.get('link',{})
		self.metatags = media_data.get('meta',{})
		
		self.tags.clear()
		for label in dt.get('tags',[]):
			tag = Tag.get_from_id(slugify(label)) or Tag(label=label)
			self.tags.add(tag)
		
		self.updated = rfc3339.now().replace(tzinfo=None)
コード例 #6
0
ファイル: comments.py プロジェクト: yoshrote/Columns
	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
コード例 #7
0
ファイル: __init__.py プロジェクト: yoshrote/Columns
	def from_dict(cls, dt, default_thumb=None):
		item = cls()
		item.title = dt.get('title',None)
		item.published = dt.get('published',None)
		item.content = dt.get('content',None)
		item.sticky = dt.get('sticky',None)
		item.can_comment = dt.get('can_comment',None)
		item.page_id = dt.get('page_id',None)
		
		item.contributors = []
		item.metacontent = html.striphtml(item.content)
		item.summary = html.stripobjects(item.content)
		media_data = html.get_metamedia_data(item.content, default_thumb)
		item.links = media_data.get('link',{})
		item.metatags = media_data.get('meta',{})
		
		for label in dt.get('tags',[]):
			tag = Tag.get_from_id(slugify(label)) or Tag(label=label)
			item.tags.add(tag)
		item.updated = rfc3339.now().replace(tzinfo=None)
		if item.created is None:
			item.created = item.updated
		return item
コード例 #8
0
ファイル: test_atom.py プロジェクト: yoshrote/Columns
	def test_slugify(self):
		test = atom.slugify("Yes're this is it!1_")
		self.assertEquals(test,"yesre-this-is-it1")
コード例 #9
0
ファイル: __init__.py プロジェクト: yoshrote/Columns
	def update_from_dict(self, dt):
		target = meta.Session.query(self.__class__).get(slugify(dt.get('label')))
		if target is None:
			target = self.from_dict(dt)
			target.save()
		merge_tags(self, target)
コード例 #10
0
ファイル: __init__.py プロジェクト: yoshrote/Columns
	def from_dict(cls, dt):
		item = cls()
		item.label = dt.get('label')
		item.id = slugify(item.label)
		return item
コード例 #11
0
ファイル: __init__.py プロジェクト: yoshrote/Columns
	def __init__(self,*args,**kwargs):
		Base.__init__(self,*args,**kwargs)
		if self.label is not None:
			self.label = self.label.strip()
			self.id = slugify(self.label)
コード例 #12
0
ファイル: wordpress.py プロジェクト: yoshrote/Columns
def main(config, wp_file, static_path, base_wp_url, base_col_url):
	from columns.lib.app_globals import Globals	
	db_url =  config.get("app:main","sqlalchemy.url")
	engine = create_engine(db_url)
	init_model(engine)
	errors = []
	with open(wp_file) as f:
		xmlstr = f.read()
	dom = ElementTree.fromstring(xmlstr)
	
	
	#import tags
	for x in dom.findall('channel/{http://wordpress.org/export/1.0/}tag'):
		tk = x.findtext('{http://wordpress.org/export/1.0/}tag_slug')
		tv = x.findtext('{http://wordpress.org/export/1.0/}tag_name')
		try:
			meta.Session.merge(Tag(id=unicode(tk), name=unicode(tv)))
		except:
			pass
	meta.Session.flush()
	
	#import users
	authors = set([])
	for x in dom.findall('channel/item/{http://purl.org/dc/elements/1.1/}creator'):
		authors.add(x.text.lower())
	for x in authors:
		if meta.Session.query(User).filter(User.name==unicode(x)).count() == 0:
			meta.Session.add(User(name=unicode(x), type=3))
	meta.Session.flush()
	author_to_id = dict(meta.Session.query(User.name,User.id).all())
	
	#create 'main' page if it doesn't exist
	try:
		main_page = meta.Session.query(Page).filter(Page.slug==u'main').one()
	except orm.exc.NoResultFound:
		main_page = meta.Session.merge(
			Page(
				title = u'Main',
				slug = u'main',
				stream_comment_style = u'summary',
				story_comment_style = u'list',
				visible = True,
				can_post = True,
				tweet = True,
				content = None
			)
		)
		meta.Session.flush()
	
	#import pages
	for x in dom.findall('channel/item'):
		if x.findtext('{http://wordpress.org/export/1.0/}post_type') != 'page':
			continue
		title = unicode(x.findtext('title')).strip()
		slug = unicode(slugify(title))
		if slug == u'main':
			continue
		if meta.Session.query(Page).filter(Page.slug==slug).count() == 0:
			can_post = len(x.findall('{http://wordpress.org/export/1.0/}comment')) > 0
			soup = BeautifulSoup(x.findtext('{http://purl.org/rss/1.0/modules/content/}encoded'))
			t_page = meta.Session.merge(
				Page(
					title = title,
					slug = slug,
					stream_comment_style = u'summary',
					story_comment_style = u'list',
					visible = x.findtext('{http://wordpress.org/export/1.0/}status') == "publish",
					can_post = can_post,
					tweet = False,
					content = unicode(soup),
				)
			)	
		#add comments
		dummy_post = False
		t_post = None
		for comment in x.findall('{http://wordpress.org/export/1.0/}comment'):
			if dummy_post is False:
				t_post = Article(
					id=int(x.findtext('{http://wordpress.org/export/1.0/}post_id')),
					user_id=author_to_id.get(x.findtext('{http://purl.org/dc/elements/1.1/}creator').lower()),
					page_id=t_page.id,
					subject=unicode(t_page.title),
					date=datetime.datetime.strptime('2009-11-27 17:35:23',WORDPRESS_DT_FORMAT),
					published=True,
					permalink=None,
					can_comment=True,
					content=None,
					sticky=False
				)
				dummy_post = True
			author_name = comment.findtext('{http://wordpress.org/export/1.0/}comment_author')
			author_email = comment.findtext('{http://wordpress.org/export/1.0/}comment_author_email')
			author_url = comment.findtext('{http://wordpress.org/export/1.0/}comment_author_url')
			if author_name is None and author_email is None and author_url is None:
				continue
			try:
				userid = author_to_id.get(author_name.lower(), None)
				if userid is not None:
					user_t = meta.Session.get(userid)
					author_name = user_t.name
					author_url = user_t.profile
			except:
				pass
			soup = BeautifulSoup(comment.findtext('{http://wordpress.org/export/1.0/}comment_content'))
			try:
				t_post.comments.append(
					Comment(
						author_name = unicode(author_name) if author_name is not None else None,
						author_email = unicode(author_email) if author_email is not None else None,
						author_url = unicode(author_url) if author_url is not None else None,
						parent_comment = None,
						subject = u'',
						date = datetime.datetime.strptime(comment.findtext('{http://wordpress.org/export/1.0/}comment_date'),WORDPRESS_DT_FORMAT),
						content = unicode(soup),
					)
				)
			except:
				pass
		if t_post is not None:
			t_page.posts.append(t_post)
	meta.Session.flush()
	
	static_file_path = os.path.join(static_path,'uploaded')
	
	#import uploads
	upload_old_to_new = {}
	for x in dom.findall('channel/item'):
		if x.findtext('{http://wordpress.org/export/1.0/}post_type') != 'attachment':
			continue
		src = x.findtext('{http://wordpress.org/export/1.0/}attachment_url')
		re_match = re.match(r'^(?P<basepath>.*\/uploads)\/(?P<year>\d+)\/(?P<month>\d+)\/(?P<file>.*)$',src)
		
		item = Upload()
		item.alt_text = unicode(x.findtext('{http://wordpress.org/export/1.0/}post_name'))
		item.description = None
		item.date = datetime.datetime(year=int(re_match.group('year')),month=int(re_match.group('month')),day=1)
		
		item.filepath = unicode(src.replace(re_match.group('basepath'),static_file_path))
		meta.Session.add(item)
	meta.Session.flush()
	
	caption_regex = re.compile(ur'\[caption .*? caption=\"(.*?)\"\](.*)\[\/caption\]')
	replace_str = ur'<div class=\"img-block\">\2<span class=\"img-caption\">\1</span></div>'
	#import posts
	for x in dom.findall('channel/item'):
		if x.findtext('{http://wordpress.org/export/1.0/}post_type') != 'post':
			continue
		user_fk = author_to_id.get(x.findtext('{http://purl.org/dc/elements/1.1/}creator').lower())
		page_fk = main_page.id
		post_pk = int(x.findtext('{http://wordpress.org/export/1.0/}post_id'))
		subject = x.findtext('title')
		published = x.findtext('{http://wordpress.org/export/1.0/}status') != "draft"
		date = None if not published else datetime.datetime.strptime(x.findtext('{http://wordpress.org/export/1.0/}post_date'),WORDPRESS_DT_FORMAT)
		permalink = None if not published else unicode(slugify('-'.join([date.date().strftime("%Y-%m-%d"),subject])))
		can_comment = True
		content = x.findtext('{http://purl.org/rss/1.0/modules/content/}encoded')
		content = content.replace(u'%s/wp-content/uploads/'%base_wp_url,u'%s/uploaded/'%base_col_url)
		soup = BeautifulSoup(content)
		soup = caption_regex.sub(replace_str ,unicode(soup))
		t_post = Article(
			id=post_pk,
			user_id=user_id,
			page_id=page_id,
			subject=unicode(subject),
			date=date,
			published=published,
			permalink=permalink,
			can_comment=can_comment,
			content=soup,
			sticky=False
		)
		for tag in x.findall('category'):
			if tag.attrib.get('domain','') == 'tag' and tag.attrib.get('nicename',None) is not None:
				t_post.tags.append(meta.Session.query(Tag).get(unicode(tag.attrib['nicename'])))
		#add comments
		for comment in x.findall('{http://wordpress.org/export/1.0/}comment'):
			author_name = comment.findtext('{http://wordpress.org/export/1.0/}comment_author')
			author_email = comment.findtext('{http://wordpress.org/export/1.0/}comment_author_email')
			author_url = comment.findtext('{http://wordpress.org/export/1.0/}comment_author_url')
			if author_name is None and author_email is None and author_url is None:
				continue
			try:
				userid = author_to_id.get(author_name.lower(), None)
				if userid is not None:
					user_t = meta.Session.get(userid)
					author_name = user_t.name
					author_url = user_t.profile
			except:
				pass
			soup = BeautifulSoup(comment.findtext('{http://wordpress.org/export/1.0/}comment_content'))
			t_post.comments.append(
				Comment(
					author_name = unicode(author_name) if author_name is not None else None,
					author_email = unicode(author_email) if author_email is not None else None,
					author_url = unicode(author_url) if author_url is not None else None,
					parent_comment = None,
					subject = u'',
					date = datetime.datetime.strptime(comment.findtext('{http://wordpress.org/export/1.0/}comment_date'),WORDPRESS_DT_FORMAT),
					content = unicode(soup),
				)
			)
		meta.Session.add(t_post)
	meta.Session.flush()
	return '\n'.join(errors)