Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
suggest_name method now handles non-ascii better. This changed
Browse files Browse the repository at this point in the history
  the dependencies from 'slugify' to 'awesome-slugify'.
  • Loading branch information
robinharms committed Mar 27, 2015
1 parent e4c2f4f commit 9788381
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 35 deletions.
9 changes: 5 additions & 4 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@

0.3dev - (unreleased)
---------------------
0.3b - 2015-03-27
-----------------

(nothing yet)
- suggest_name method now handles non-ascii better. This changed
the dependencies from 'slugify' to 'awesome-slugify'.

0.2b - 2014-02-11
-----------------
Expand All @@ -19,7 +20,7 @@
existing view. Before it was possible to name objects the same way as views. [robinharms]

0.1a3 - 2012-02-14
-------------------
------------------

- Typo on code caused description to be stored under the wrong attribute of
the schema when using createSchema. [robinharms]
Expand Down
31 changes: 15 additions & 16 deletions betahaus/pyracont/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import inspect

import pytz
from slugify import slugify
from slugify import UniqueSlugify
from zope.component.event import objectEventNotify
from zope.interface import implementer
from zope.interface import providedBy
Expand Down Expand Up @@ -173,28 +173,27 @@ def check_unique_name(context, request, name):
return False
return True

def get_context_view_names(context, request):
provides = [IViewClassifier] + map(
providedBy,
(request, context)
)
return [x for (x, y) in request.registry.adapters.lookupAll(provides, IView)]

def generate_slug(parent, text, limit=20):
""" Suggest a name for content that will be added.
text is a title or similar to be used.
"""
text = unicodify(text)
suggestion = slugify(text[:limit])
used_names = set(parent.keys())
request = get_current_request()
used_names.update(get_context_view_names(parent, request))
sluggo = UniqueSlugify(to_lower = True,
stop_words = ['a', 'an', 'the'],
max_length = 80,
uids = used_names)
suggestion = sluggo(text)
if not len(suggestion):
raise ValueError("When text was made URL-friendly, nothing remained.")
request = get_current_request()

#Is the suggested ID already unique?
if check_unique_name(parent, request, suggestion):
return suggestion

#ID isn't unique, let's try to generate a unique one.
RETRY = 100
i = 1
while i <= RETRY:
new_s = "%s-%s" % (suggestion, str(i))
if check_unique_name(parent, request, new_s):
return new_s
i += 1
#If no id was found, don't just continue
raise KeyError("No unique id could be found")
2 changes: 1 addition & 1 deletion betahaus/pyracont/fields/tests/test_versioning_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class VersioningFieldTests(TestCase):
def setUp(self):
self.config = testing.setUp()
self.config = testing.setUp(request = testing.DummyRequest())

def tearDown(self):
testing.tearDown()
Expand Down
3 changes: 1 addition & 2 deletions betahaus/pyracont/fields/versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from BTrees.LOBTree import LOBTree
from pyramid.threadlocal import get_current_request
from pyramid.security import authenticated_userid
from zope.interface import implementer

from betahaus.pyracont.fields.base import BaseField
Expand Down Expand Up @@ -55,7 +54,7 @@ def add(self, value, author=None, created=None):
assert isinstance(created, datetime)
if author is None:
request = get_current_request()
author = authenticated_userid(request)
author = request.authenticated_userid
#author might still be None, if this is run by a script or by an unauthenticated user
id = self.get_current_rev_id()+1
self._revision_values[id] = value
Expand Down
9 changes: 0 additions & 9 deletions betahaus/pyracont/tests/test_base_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,6 @@ def test_suggest_name_occupied_namespace(self):
obj = self._cut(title="a")
self.assertEqual(obj.suggest_name(parent), 'a-3')

def test_suggest_name_full_namespace(self):
parent = {}
parent['a'] = object()
for i in range(101):
k = "a-%s" % i
parent[k] = object()
obj = self._cut(title='a')
self.assertRaises(KeyError, obj.suggest_name, parent)

def test_mark_modified(self):
obj = self._cut()
modified = obj.modified
Expand Down
3 changes: 2 additions & 1 deletion buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ parts =
develop = .

eggs =
PasteScript
betahaus.pyracont
coverage

Expand Down Expand Up @@ -53,3 +52,5 @@ defaults =


[versions]
pyramid = 1.5.4

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

requires = ('pyramid',
'repoze.folder',
'slugify',
'awesome-slugify',
'pytz',
'ZODB3',
'colander',
Expand All @@ -17,7 +17,7 @@
)

setup(name='betahaus.pyracont',
version='0.3dev',
version='0.3b',
description='betahaus.pyracont',
long_description=README + '\n\n' + CHANGES,
classifiers=[
Expand Down

0 comments on commit 9788381

Please sign in to comment.