def set_auto_slug(self): full_slug = gen_slug(self.name) if not full_slug: # if slug generation failed we select the highest category # id as base for slug generation. tag = Tag.query.autoflush(False).order_by(Tag.id.desc()).first() full_slug = unicode(tag and tag.id or u'1') if full_slug != self.slug: while Tag.query.autoflush(False) \ .filter_by(slug=full_slug).limit(1).count(): full_slug = increment_string(full_slug) self.slug = full_slug
def set_auto_slug(self): """Generate a slug for this category.""" full_slug = gen_slug(self.name) if not full_slug: # if slug generation failed we select the highest category # id as base for slug generation. category = Category.query.autoflush(False) \ .order_by(Category.id.desc()).first() full_slug = unicode(category and category.id or u'1') if full_slug != self.slug: while Category.query.autoflush(False) \ .filter_by(slug=full_slug).limit(1).count(): full_slug = increment_string(full_slug) self.slug = full_slug
def set_auto_slug(self): """Generate a slug for this post.""" #cfg = get_application().cfg slug = gen_slug(self.title) if not slug: slug = to_blog_timezone(self.pub_date).strftime('%H%M') full_slug = gen_timestamped_slug(slug, self.content_type, self.pub_date) if full_slug != self.slug: while Post.query.autoflush(False).filter_by(slug=full_slug) \ .limit(1).count(): full_slug = increment_string(full_slug) self.slug = full_slug
def _perform_import(app, blog, d): # import models here because they have the same names as our # importer objects this module exports from rezine.models import User, Tag, Category, Post, Comment author_mapping = {} tag_mapping = {} category_mapping = {} def prepare_author(author): """Adds an author to the author mapping and returns it.""" if author.id not in author_mapping: author_rewrite = d['authors'][author.id] if author_rewrite != '__rezine_create_user': user = User.query.get(int(author_rewrite)) else: query = User.query.filter_by(username=author.username) user = query.first() if user is None: user = User(author.username, None, author.email, author.real_name, author.description, author.www, author.is_author) if author.pw_hash: user.pw_hash = author.pw_hash user.own_privileges.update(author.privileges) author_mapping[author.id] = user return author_mapping[author.id] def prepare_tag(tag): """Get a tag for a tag.""" t = tag_mapping.get(tag.slug) if t is not None: return t t = Tag.query.filter_by(slug=tag.slug).first() if t is not None: tag_mapping[tag.slug] = t return t t = Tag.query.filter_by(name=tag.name).first() if t is not None: tag_mapping[tag.slug] = t return t t = tag_mapping[tag.id] = Tag(tag.name, tag.slug) return t def prepare_category(category): """Get a category for a category.""" c = category_mapping.get(category.slug) if c is not None: return c c = Category.query.filter_by(slug=category.slug).first() if c is not None: category_mapping[category.slug] = c return c c = Category.query.filter_by(name=category.name).first() if c is not None: category_mapping[category.slug] = c return c c = category_mapping[category.id] = Category(category.name, category.description, category.slug) return c # start debug output yield u'<ul>' # update blog configuration if user wants that if d['title']: app.cfg.change_single('blog_title', blog.title) yield u'<li>%s</li>\n' % _('set blog title from dump') if d['description']: app.cfg.change_single('blog_tagline', blog.description) yield u'<li>%s</li>\n' % _('set blog tagline from dump') # convert the posts now for old_post in blog.posts: # in theory that will never happen because there are no # checkboxes for already imported posts on the form, but # who knows what users manage to do and also skip posts # we don't want converted if old_post.already_imported or not d['posts'][old_post.id]: continue slug = old_post.slug while Post.query.autoflush(False).filter_by(slug=slug) \ .limit(1).count(): slug = increment_string(slug) post = Post(old_post.title, prepare_author(old_post.author), old_post.text, slug, old_post.pub_date, old_post.updated, old_post.comments_enabled, old_post.pings_enabled, parser=old_post.parser, uid=old_post.uid, content_type=old_post.content_type, status=old_post.status, extra=old_post.extra) if old_post.parser_data is not None: post.parser_data.clear() post.parser_data.update(old_post.parser_data) yield u'<li><strong>%s</strong>' % escape(post.title) for tag in old_post.tags: post.tags.append(prepare_tag(tag)) yield u'.' for category in old_post.categories: post.categories.append(prepare_category(category)) yield u'.' # now the comments if user wants them. if d['comments'][old_post.id]: to_create = set(old_post.comments) created = {} def _create_comment(comment): parent = None if comment.parent is not None: if comment.parent in created: parent = created[comment.parent] else: parent = _create_comment(comment.parent) to_create.discard(comment.parent) if isinstance(comment.author, Author): author = prepare_author(comment.author) else: author = comment.author rv = Comment(post, author, comment.body, comment.author_email, comment.author_url, parent, comment.pub_date, comment.remote_addr, comment.parser, comment.is_pingback, comment.status) if comment.blocked_msg: rv.blocked_msg = comment.blocked_msg created[comment] = rv return rv while to_create: _create_comment(to_create.pop()) yield u'.' yield u' <em>%s</em></li>\n' % _('done') # send to the database yield u'<li>%s' % _('Committing transaction...') db.commit() # write config if we have if d['load_config']: yield u'<li>%s' % _('Updating configuration...') t = app.cfg.edit() for key, value in blog.config.iteritems(): if key in t and key not in ignored_config_keys: t.set_from_string(key, value) t.commit() yield u' <em>%s</em></li></ul>' % _('done')