def from_metaweblog(cls, struct, post_type='post', is_edit=False): """Receive metaWeblog RPC struct and initialize a Post. Used both by migrate_from_wordpress and when receiving a new or edited post from MarsEdit. """ title = struct.get('title', '') meta_description = struct.get('mt_excerpt', '') if len(meta_description) > 155: raise ValueError("Description is %d chars, max 155" % len(meta_description)) if 'mt_keywords' in struct: tags = [ tag.strip() for tag in struct['mt_keywords'].split(',') if tag.strip() ] else: tags = None slug = (slugify.slugify(struct['wp_slug']) if struct.get('wp_slug') else slugify.slugify(title)) description = struct.get('description', '') status = (struct.get('post_status') or struct.get('page_status') or 'publish') if 'date_modified_gmt' in struct: tup = struct['date_modified_gmt'].timetuple() mod = utc_tz.localize(datetime.datetime(*tup[0:6])) else: mod = datetime.datetime.utcnow() body = markup.markup(description) rv = cls( title=title, # Format for display body=body, plain=plain.plain(body), summary=summarize.summarize(body, 200), original=description, meta_description=meta_description, tags=tags, slug=slug, type=post_type, status=status, wordpress_id=struct.get('postid'), mod=mod) if not is_edit and 'date_created_gmt' in struct: # TODO: can fail if two posts created in same second, add random # suffix to ObjectId date_created = datetime.datetime.strptime( struct['date_created_gmt'].value, "%Y%m%dT%H:%M:%S") rv.id = ObjectId.from_datetime(date_created) return rv
def from_metaweblog( cls, struct, post_type='post', is_edit=False ): """Receive metaWeblog RPC struct and initialize a Post. Used both by migrate_from_wordpress and when receiving a new or edited post from MarsEdit. """ title = struct.get('title', '') # We expect MarsEdit to set categories with mt_setPostCategories() assert 'categories' not in struct if 'mt_keywords' in struct: tags = [ tag.strip() for tag in struct['mt_keywords'].split(',') if tag.strip() ] else: tags = None slug = ( slugify.slugify(struct['wp_slug']) if struct.get('wp_slug') else slugify.slugify(title)) description = struct.get('description', '') status = struct.get('post_status', 'publish') if 'date_modified_gmt' in struct: tup = struct['date_modified_gmt'].timetuple() mod = utc_tz.localize(datetime.datetime(*tup[0:6])) else: mod = datetime.datetime.utcnow() body = markup.markup(description) rv = cls( title=title, # Format for display body=body, summary=summarize.summarize(body, 200), original=description, tags=tags, slug=slug, type=post_type, status=status, wordpress_id=struct.get('postid'), mod=mod ) if not is_edit and 'date_created_gmt' in struct: # TODO: can fail if two posts created in same second, add random # suffix to ObjectId date_created = datetime.datetime.strptime( struct['date_created_gmt'].value, "%Y%m%dT%H:%M:%S") rv.id = ObjectId.from_datetime(date_created) return rv
def test_summarize_long(self): # Not truncated self.assertEqual("bar baz quux, fizzle, fazzle. hi!", summarize(sample_html, 100))
def test_summarize_short(self): # Truncated self.assertEqual("bar baz [...]".strip(), summarize(sample_html, 10))
def test_summarize_long(self): # Not truncated self.assertEqual( "bar baz quux, fizzle, fazzle. hi!", summarize(sample_html, 100))
def test_summarize_short(self): # Truncated self.assertEqual("bar baz [ ... ]".strip(), summarize(sample_html, 10))