예제 #1
0
	def setUp(self):
		from columns.model import Upload, User
		tmp = User.from_dict(dict(
			id=1,
			name=u'test_user',
			open_id=None,
			fb_id=None,
			twitter_id=None,
			type=1,
			profile=None,
		))
		try:
			tmp.save()
		except:
			pass
		tmp = Upload(**dict(
			id=1,
			title=u'test',
			content=u'',
			filepath=u'',
			updated=datetime.datetime.utcnow(),
			published=datetime.datetime.utcnow(),
			author={'name':u'test_user'}
		))
		try:
			tmp.save()
		except Exception, ex:
			print ex
예제 #2
0
	def setUp(self):
		from columns.model import Page, User, Article, Upload
		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()
		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()
		tmp = Upload(**dict(
			id=1,
			title=u'test',
			content=u'',
			filepath=u'',
			updated=datetime.datetime.utcnow(),
			published=datetime.datetime.utcnow(),
			author={'name':u'test_user'}
		))
		tmp.save()
예제 #3
0
	def test_update_json(self):
		response = self.app.put(url('formatted_picture', id=1, format='json'), extra_environ=self.extra_environ, content_type='application/json', body=json.dumps(dict(
			title=u'blah',
			content=u'blah',
		)))
		from columns.model import Upload
		tmp = Upload.get_from_id(1)
		assert tmp.title == u'blah'
		assert tmp.content == u'blah'
예제 #4
0
	def test_update(self):
		response = self.app.put(url('picture', id=1), extra_environ=self.extra_environ, params=dict(
			title=u'blah',
			content=u'blah',
			tags=u'tag1,tag2',
		))
		from columns.model import Upload
		tmp = Upload.get_from_id(1)
		assert tmp.title == u'blah'
		assert tmp.content == u'blah'
예제 #5
0
	def test_update_browser_fakeout(self):
		response = self.app.post(url('picture', id=1), extra_environ=self.extra_environ, params=dict(
			_method='put',
			title=u'blah2',
			content=u'b2lah',
		))
		from columns.model import Upload
		tmp = Upload.get_from_id(1)
		assert tmp.title == u'blah2'
		assert tmp.content == u'b2lah'
예제 #6
0
파일: admin.py 프로젝트: yoshrote/Columns
	def quick_upload(self):
		import os.path
		ckedit_num = request.GET.get('CKEditorFuncNum')
		message = None
		filepath = 'null'
		try:
			item = Upload.quick(request.POST.mixed())
			item.add_author_or_contributor(user_from_session(session))
			item.save()
			static_web_path = config['static_web_path']
			filepath = os.path.join(static_web_path,item.filepath)
		except FileExistsError, ex:
			message = "Sorry. Could not upload file with that name."
			filepath = ex
예제 #7
0
	def _create(self, format, parent_id=None):
		#if format == 'json':
		#	from columns.lib import json
		#	params = self._validate(json.loads(request.body), CreateUpload, 'new')
		#elif format == 'atom':
		#	from lxml import etree
		#	params = Upload.parse_xml(etree.fromstring(request.body))
		if format == 'html':
			params = self._validate(request.POST.mixed(), CreateUpload, 'new')
		else:
			raise UnacceptedFormat(format)
		
		item = Upload.from_dict(params)
		item.add_author_or_contributor(user_from_session(session))
		item.save()
		return item
예제 #8
0
	def test_getowner(self):
		upload = Upload()
		upload.author_id = 1
		self.assertEquals(1,upload.owner())
예제 #9
0
	def _new(self, parent_id=None, with_defaults=True):
		item = Upload()
		if with_defaults is True:
			item.set_defaults()
		return item
예제 #10
0
	def test_delete_browser_fakeout(self):
		response = self.app.post(url('picture', id=1), extra_environ=self.extra_environ, params=dict(_method='delete'))
		from columns.model import Upload
		tmp = Upload.get_from_id(1)
		assert tmp == None
예제 #11
0
	def test_delete(self):
		response = self.app.delete(url('picture', id=1), extra_environ=self.extra_environ)
		from columns.model import Upload
		tmp = Upload.get_from_id(1)
		assert tmp == None
예제 #12
0
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)