-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from alphagov/move_validation_to_utils
Move validation to utils
- Loading branch information
Showing
8 changed files
with
1,213 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '0.15.0' | ||
__version__ = '0.16.0' |
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,73 @@ | ||
import yaml | ||
import inflection | ||
import re | ||
import os | ||
|
||
|
||
class ContentLoader(object): | ||
|
||
def __init__(self, manifest, content_directory): | ||
|
||
with open(manifest, "r") as file: | ||
section_order = yaml.load(file) | ||
|
||
self._directory = content_directory | ||
self._question_cache = {} | ||
self.sections = [ | ||
self.__populate_section__(s) for s in section_order | ||
] | ||
|
||
def get_section(self, requested_section): | ||
|
||
for section in self.sections: | ||
if section["id"] == requested_section: | ||
return section | ||
|
||
def get_question(self, question): | ||
|
||
if question not in self._question_cache: | ||
|
||
question_file = self._directory + question + ".yml" | ||
|
||
if not os.path.isfile(question_file): | ||
self._question_cache[question] = {} | ||
return {} | ||
|
||
with open(question_file, "r") as file: | ||
question_content = yaml.load(file) | ||
|
||
question_content["id"] = question | ||
|
||
# wrong way to do it? question should be shown by default. | ||
question_content["depends_on_lots"] = ( | ||
self.__get_dependent_lots__(question_content["dependsOnLots"]) | ||
) if "dependsOnLots" in question_content else ( | ||
["saas", "paas", "iaas", "scs"] | ||
) | ||
|
||
self._question_cache[question] = question_content | ||
|
||
return self._question_cache[question] | ||
|
||
def __populate_section__(self, section): | ||
section["questions"] = [ | ||
self.get_question(q) for q in section["questions"] | ||
] | ||
all_dependencies = [ | ||
q["depends_on_lots"] for q in section["questions"] | ||
] | ||
section["depends_on_lots"] = [ | ||
y for x in all_dependencies for y in x # flatten array | ||
] | ||
section["id"] = self.__make_id__(section["name"]) | ||
return section | ||
|
||
def __make_id__(self, name): | ||
return inflection.underscore( | ||
re.sub("\s", "_", name) | ||
) | ||
|
||
def __get_dependent_lots__(self, dependent_lots_as_string): | ||
return [ | ||
x.strip() for x in dependent_lots_as_string.lower().split(",") | ||
] |
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,30 @@ | ||
import re | ||
|
||
|
||
class Presenters(object): | ||
|
||
def __init__(self): | ||
return None | ||
|
||
def present(self, value, question_content): | ||
if "type" in question_content: | ||
field_type = question_content["type"] | ||
else: | ||
return value | ||
|
||
if hasattr(self, "_" + field_type): | ||
return getattr(self, "_" + field_type)(value) | ||
else: | ||
return value | ||
|
||
def _service_id(self, value): | ||
if re.findall("[a-zA-Z]", str(value)): | ||
return [value] | ||
else: | ||
return re.findall("....", str(value)) | ||
|
||
def _upload(self, value): | ||
return { | ||
"url": value or "", | ||
"filename": value.split("/")[-1] or "" | ||
} |
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,48 @@ | ||
import os | ||
import boto | ||
import boto.exception | ||
import datetime | ||
import mimetypes | ||
|
||
from boto.exception import S3ResponseError # noqa | ||
|
||
|
||
class S3(object): | ||
def __init__(self, bucket_name=None, host='s3-eu-west-1.amazonaws.com'): | ||
conn = boto.connect_s3(host=host) | ||
|
||
self.bucket_name = bucket_name | ||
self.bucket = conn.get_bucket(bucket_name) | ||
|
||
def save(self, path, file, acl='public-read', move_prefix=None): | ||
path = path.lstrip('/') | ||
|
||
self._move_existing(path, move_prefix) | ||
|
||
key = self.bucket.new_key(path) | ||
key.set_contents_from_file( | ||
file, | ||
headers={'Content-Type': self._get_mimetype(key.name)} | ||
) | ||
key.set_acl(acl) | ||
return key | ||
|
||
def _move_existing(self, existing_path, move_prefix=None): | ||
if move_prefix is None: | ||
move_prefix = default_move_prefix() | ||
|
||
if self.bucket.get_key(existing_path): | ||
path, name = os.path.split(existing_path) | ||
self.bucket.copy_key( | ||
os.path.join(path, '{}-{}'.format(move_prefix, name)), | ||
self.bucket_name, | ||
existing_path | ||
) | ||
|
||
def _get_mimetype(self, filename): | ||
mimetype, _ = mimetypes.guess_type(filename) | ||
return mimetype | ||
|
||
|
||
def default_move_prefix(): | ||
return datetime.datetime.utcnow().isoformat() |
Oops, something went wrong.