Skip to content

Commit

Permalink
Fixes Create Research Questions title and name validations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavle Jonoski committed Jan 3, 2020
1 parent 8da07df commit 5221e40
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,22 @@ If you specify `--model=all`, all indexes will be rebuilt (same as not specifyin
pip install -U setuptools # optional, only if there is an error about PEP 517 "BackendUnavailable"
pip install -U spacy
python -m spacy download en_core_web_sm
```

# User Intents

User intents are extracted from the user queries in a batch process that is run
periodically.

The following command updates the latest user intents and should be run at least
once a day:

```bash
knowledgehub -c /etc/ckan/default/production.ini intents update
```

The crontab should look something like this:

```cron
0 0 * * * knowledgehub -c /etc/ckan/default/development.ini intents update >/dev/null 2>&1
```
2 changes: 1 addition & 1 deletion ckanext/knowledgehub/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def sub_theme_update():
def research_question_schema():
return {
'name': [not_empty,
name_validator,
validators.long_name_validator(),
validators.research_question_name_validator,
unicode],
'theme': [ignore_missing, unicode],
Expand Down
52 changes: 43 additions & 9 deletions ckanext/knowledgehub/logic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,7 @@ def research_question_title_characters_validator(key, data, errors, context):
p.toolkit._('Must be at least %s '
'characters long') % 2)

if len(data[key]) > 160:
errors[key].append(
p.toolkit._('Research question must be a '
'maximum of %i characters long') % 160)

if not title_match.match(data[key]):
errors[key].append(
p.toolkit._('Must be purely lowercase alphanumeric '
'(ascii) characters and these symbols: -_.?'))
# Title should not be limited to max size and may contain other characters


def check_sub_theme_parent(key, data, errors, context):
Expand Down Expand Up @@ -223,3 +215,45 @@ def user_query_result_query_id(key, data, errors, context):
errors[key].append(
p.toolkit._('This query_id already exists. '
'Choose another one.'))


def long_name_validator(max_length=500):
'''Returns a validator function for validating names with given max size.
'''

def _long_name_validator(value, context):
'''Return the given value if it's a valid name, otherwise raise Invalid.
If it's a valid name, the given value will be returned unmodified.
This function applies general validation rules for names of packages,
groups, users, etc.
This validator is different from CKAN's own name_validator in that this
validator allows for custom max length of the name.
:raises ckan.lib.navl.dictization_functions.Invalid: if ``value`` is not
a valid name
'''
name_match = re.compile('[a-z0-9_\-]*$')
if not isinstance(value, string_types):
raise Invalid(_('Names must be strings'))

# check basic textual rules
if value in ['new', 'edit', 'search']:
raise Invalid(_('That name cannot be used'))

if len(value) < 2:
raise Invalid(_('Must be at least %s characters long') % 2)
if max_length is not None:
if len(value) > max_length:
raise Invalid(_('Name must be a maximum of '
'%i characters long') % \
max_length)
if not name_match.match(value):
raise Invalid(_('Must be purely lowercase alphanumeric '
'(ascii) characters and these symbols: -_'))
return value

return _long_name_validator

0 comments on commit 5221e40

Please sign in to comment.