forked from projectcaluma/caluma
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement flexible format validators
This commit adds flexible format validators. There is one new query: `all_format_validators` that enables querying all available format validators. A text- or textarea-question can have one or multiple format_validators assigned. When querying a question, there is a new `FormatValidatorsConnection`, containing all FormatValidator-objects assigned on the question. For this there is a new `QuestionValidator` that ensures, referenced format_validators exist. `translate_value()` has been moved to `core.utils`, as this is used in the `data_source`- and `form`-app. There is a new extension-point `format_validators.py`. When adding the tests for this, it became obvious, that there was something wrong with the parameters for `DataSource`. As I was touching those tests anyway, I fixed this as well. Added commented out extension volume to docker-compose.yml. Also added forgotten data_sources extension volume. Additionally this commit also cleans up the snapshots for document and question as there were quite a lot old unused snapshots. Closes projectcaluma#360
- Loading branch information
1 parent
e1331e3
commit 885b962
Showing
20 changed files
with
1,406 additions
and
782 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.conf import settings | ||
from django.utils import translation | ||
|
||
|
||
def translate_value(value): | ||
"""Translate a string. | ||
:param value: dict or string | ||
:return: translated value or original string | ||
""" | ||
lang = translation.get_language() | ||
if lang in value: | ||
return value[lang] | ||
if settings.LANGUAGE_CODE in value: | ||
return value[settings.LANGUAGE_CODE] | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# To be overwritten for format validator extensions point |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import re | ||
from collections import namedtuple | ||
|
||
from django.conf import settings | ||
from django.utils.module_loading import import_string | ||
from rest_framework.exceptions import ValidationError | ||
|
||
from caluma.core.utils import translate_value | ||
|
||
|
||
class BaseFormatValidator: | ||
r"""Basic format validator class to be extended by any format validator implementation. | ||
A custom format validator class could look like this: | ||
``` | ||
>>> from caluma.form.format_validators import BaseFormatValidator | ||
... | ||
... | ||
... class CustomFormatValidator(BaseFormatValidator): | ||
... slug = "email" | ||
... name = {"en": "E-mail", "de": "E-Mail"} | ||
... regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)" | ||
... error_msg = {"en": "Not an e-mail address", "de": "Keine E-Mail adresse"} | ||
``` | ||
""" | ||
|
||
def __init__(self): | ||
if not all( | ||
[self.slug, self.regex, self.name, self.error_msg] | ||
): # pragma: no cover | ||
raise NotImplementedError("Missing properties!") | ||
|
||
def validate(self, value, document): | ||
if not re.match(self.regex, value): | ||
raise ValidationError(translate_value(self.error_msg)) | ||
|
||
|
||
class EMailFormatValidator(BaseFormatValidator): | ||
slug = "email" | ||
name = {"en": "E-mail", "de": "E-Mail"} | ||
regex = r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)" | ||
error_msg = {"en": "Not an e-mail address", "de": "Keine E-Mail adresse"} | ||
|
||
|
||
base_format_validators = [EMailFormatValidator] | ||
|
||
|
||
FormatValidator = namedtuple("FormatValidator", ["slug", "name", "regex", "error_msg"]) | ||
|
||
|
||
def get_format_validators(include=None, dic=False): | ||
"""Get all FormatValidators. | ||
:param include: List of FormatValidators to include | ||
:param dic: Should return a dict | ||
:return: List of FormatValidator-objects if dic False otherwise dict | ||
""" | ||
|
||
format_validator_classes = [ | ||
import_string(cls) for cls in settings.FORMAT_VALIDATOR_CLASSES | ||
] + base_format_validators | ||
if include is not None: | ||
format_validator_classes = [ | ||
fvc for fvc in format_validator_classes if fvc.slug in include | ||
] | ||
if dic: | ||
return {ds.slug: ds for ds in format_validator_classes} | ||
return [ | ||
FormatValidator( | ||
slug=ds.slug, | ||
name=translate_value(ds.name), | ||
regex=ds.regex, | ||
error_msg=translate_value(ds.error_msg), | ||
) | ||
for ds in format_validator_classes | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.20 on 2019-05-06 06:55 | ||
from __future__ import unicode_literals | ||
|
||
import django.contrib.postgres.fields | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [("form", "0014_auto_20190429_0910")] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="question", | ||
name="format_validators", | ||
field=django.contrib.postgres.fields.ArrayField( | ||
base_field=models.CharField(max_length=255), | ||
blank=True, | ||
default=list, | ||
size=None, | ||
), | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.