def testSlugify(): capitals = slugify('CAPITALS') MyStrinG = slugify('MyStrinG') spaces = slugify('string with spaces in') assert capitals == 'capitals', 'slugify() should return string with all capitals changed to lowercase' assert MyStrinG == 'mystring', 'slugify() should return string with mix of capitals and lowercase changed to all lowercase' assert spaces == 'string-with-spaces-in', 'slugify() should return string where any spaces have been replaced with -'
def populate_db(database, host='localhost', port=27017): """ :param database: :param host: :param port: """ db = MongoClient(host=host, port=port)[database] for i in xrange(randint(5, 15)): cat_id = db.categories.insert({ 'name': 'Cat_%d' % i, 'name_slug': slugify('Cat_%d' % i) }) for j in xrange(randint(25, 100)): price = random() * randint(1, 100) db.products.insert({ 'name': 'Prod_%d_%d' % (i, j), 'name_slug': slugify('Prod_%d_%d' % (i, j)), 'price': float('{0:.2f}'.format(price)), 'promo_price': float('{0:.2f}'.format(price * 0.75)), 'cat_id': cat_id }) promo_codes = [{ 'code': 'hydecode', 'bonus': '20%' }, { 'code': 'testcode', 'bonus': '100' }, { 'code': 'free', 'bonus': '100%' }] db.promo_codes.insert(promo_codes)
def create_post(): if request.method == 'GET': return render_template('create_post.html', title="Create Post") else: title = request.form['title'] body = request.form['body'] slug = slugify(title).lower() author = g.user post = Post(title=title, body=body, slug=slug, author=author) db.session.add(post) db.session.commit() return redirect(url_for('posts'))
def edit(slug, body=None): link = db.session.query(Post).filter_by(slug=slug).first() if request.method == 'GET': return render_template("edit.html", postq=link, post=link) update = db.session.query(Post).filter_by(slug=slug).first() update.title = request.form['title'] update.body = request.form['body'] update.slug = slugify(request.form['title']) db.session.add(update) db.session.commit() flash('Your post has been updated.') return redirect(url_for('index'))
def __init__(self, title, body, slug ): self.title = title self.body = body self.slug = slugify(title)
def test_return_only_ascii_letters_and_numbers(self): self.assertEqual(app.slugify('qwerty12345!@#$%^&*()"><?:}{|-'), 'qwerty12345-')
def test_remove_unnecessary_spaces(self): self.assertEqual(app.slugify(' Hyde Code '), 'hyde-code')
def test_replace_space_with_dash(self): self.assertEqual(app.slugify('Hyde Code'), 'hyde-code')
def test_return_lowercase_text(self): self.assertEqual(app.slugify('HYDECODE'), 'hydecode')
def __init__(self, title, body, slug, author): self.title = title self.body = body self.slug = slugify(title).lower() self.author = author