-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor of models.py; integration tests #9
Open
nicowe
wants to merge
3
commits into
master
Choose a base branch
from
refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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 |
---|---|---|
|
@@ -4,38 +4,15 @@ | |
from flask.ext.mongoengine.wtf import model_form | ||
|
||
from taarifa_backend import db | ||
from taarifa_backend.utils import get_mongoengine_class, fieldmap | ||
|
||
fieldmap = { | ||
'BinaryField': db.BinaryField, | ||
'BooleanField': db.BooleanField, | ||
'ComplexDateTimeField': db.ComplexDateTimeField, | ||
'DateTimeField': db.DateTimeField, | ||
'DecimalField': db.DecimalField, | ||
'DictField': db.DictField, | ||
'DynamicField': db.DynamicField, | ||
'EmailField': db.EmailField, | ||
'EmbeddedDocumentField': db.EmbeddedDocumentField, | ||
'FileField': db.FileField, | ||
'FloatField': db.FloatField, | ||
'GenericEmbeddedDocumentField': db.GenericEmbeddedDocumentField, | ||
'GenericReferenceField': db.GenericReferenceField, | ||
'GeoPointField': db.GeoPointField, | ||
'ImageField': db.ImageField, | ||
'IntField': db.IntField, | ||
'ListField': db.ListField, | ||
'MapField': db.MapField, | ||
'ObjectIdField': db.ObjectIdField, | ||
'ReferenceField': db.ReferenceField, | ||
'SequenceField': db.SequenceField, | ||
'SortedListField': db.SortedListField, | ||
'StringField': db.StringField, | ||
'URLField': db.URLField, | ||
'UUIDField': db.UUIDField, | ||
} | ||
|
||
|
||
class Field(db.EmbeddedDocument): | ||
"""Field in a :class:`Service`.""" | ||
|
||
class ReportField(db.EmbeddedDocument): | ||
"""Description of a field in a report""" | ||
# TODO: must be a python field name, check or always correct this | ||
name = db.StringField(required=True) | ||
fieldtype = db.StringField(required=True, choices=fieldmap.keys()) | ||
# mongoengine properties of a Field | ||
db_field = db.StringField(default=None) | ||
required = db.BooleanField(default=False) | ||
default = db.DynamicField(default=None) | ||
|
@@ -48,49 +25,39 @@ class Field(db.EmbeddedDocument): | |
|
||
|
||
class Service(db.Document): | ||
"""A service schema served by the API.""" | ||
name = db.StringField(required=True) | ||
fields = db.DictField(required=True) | ||
"""A service schema served by the API. | ||
|
||
Describes the fields and validations of a certain type of report | ||
""" | ||
# Must be a valid python class name | ||
# TODO: Have to check or correct this | ||
classname = db.StringField(required=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
service_name = db.StringField(required=True) | ||
service_code = db.StringField(required=True, unique=True) | ||
fields = db.ListField(field=db.EmbeddedDocumentField(ReportField)) | ||
description = db.StringField() | ||
group = db.StringField() | ||
keywords = db.ListField(db.StringField()) | ||
protocol_type = db.StringField() | ||
service_name = db.StringField(required=True) | ||
service_code = db.StringField(required=True, unique=True) | ||
|
||
|
||
def build_schema(service): | ||
build_field = lambda d: fieldmap[d.pop('type')](**d) | ||
return type(str(service.name), (Report,), | ||
dict(description=service.description, | ||
group=service.group, | ||
keywords=service.keywords, | ||
protocol_type=service.protocol_type, | ||
service_name=service.service_name, | ||
service_code=service.service_code, | ||
meta={'allow_inheritance': True}, | ||
**dict((k, build_field(v)) for k, v in service.fields.items())) | ||
) | ||
|
||
|
||
class Metadata(object): | ||
def create_db_field(field): | ||
"""creates a mongoengine field from the description contained in a :class:`ReportField`""" | ||
_class = get_mongoengine_class(field.fieldtype) | ||
return _class(db_field=field.db_field, required=field.required, default=field.default, | ||
unique=field.unique, unique_with=field.unique_with, | ||
primary_key=field.primary_key, choices=field.choices, | ||
help_text=field.help_text, verbose_name=field.verbose_name) | ||
|
||
""" | ||
Description of a service | ||
""" | ||
|
||
def __init__(self, service_code, service_name, description, group=None): | ||
self.service_code = service_code | ||
self.service_name = service_name | ||
self.description = description | ||
self.group = group | ||
|
||
def __repr__(self): | ||
args = [self.service_code, self.service_name, self.description, self.group] | ||
return 'Metadata(%s)' % ', '.join(map(str, args)) | ||
def build_schema(service): | ||
"""dynamically creates the :class:`Report` for the given :class:`Service`""" | ||
fields = dict([(f.name, create_db_field(f)) for f in service.fields]) | ||
return type(str(service.classname), (Report, ), fields) | ||
|
||
|
||
class Report(db.Document): | ||
"""base class used for all created Reports""" | ||
created_at = db.DateTimeField(default=datetime.datetime.now, required=True) | ||
|
||
latitude = db.FloatField(required=True) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is part of the admin infrastructure and used by flask-security.