Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
where
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
SRCT
where
Commits
df973667
Commit
df973667
authored
Feb 24, 2020
by
Zach Perkins
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'rewrite2' of git.gmu.edu:srct/where into rewrite2
parents
871c0afb
1bb04b45
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
117 additions
and
114 deletions
+117
-114
where/app.py
where/app.py
+2
-0
where/model/__init__.py
where/model/__init__.py
+2
-114
where/model/models.py
where/model/models.py
+113
-0
No files found.
where/app.py
View file @
df973667
...
...
@@ -247,6 +247,8 @@ def search_resource(schema, data):
:param data: A dictionary containing search parameters
:return: a Flask Response object
'''
# TODO: returns 404 when accessing children - i think it should just return an empty array
query
=
g
.
db_session
.
query
(
schema
.
Meta
.
model
).
filter_by
(
**
data
)
resp
=
(
None
,
404
)
if
query
.
first
()
is
None
else
\
(
schema
.
dump
(
query
.
all
(),
many
=
True
),
200
)
...
...
where/model/__init__.py
View file @
df973667
from
contextlib
import
contextmanager
from
sqlalchemy
import
Column
,
Integer
,
String
,
Float
,
JSON
,
ForeignKey
,
Enum
from
sqlalchemy.ext.declarative
import
as_declarative
from
sqlalchemy.orm
import
relationship
,
validates
from
.meta
import
Session
from
.field_types
import
FieldType
from
.meta
import
Session
,
engine
@
as_declarative
()
class
Base
(
object
):
pass
class
Point
(
Base
):
"""
Represents actual instances of any and all points on the map.
"""
__tablename__
=
'point'
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
name
=
Column
(
String
,
nullable
=
True
)
lat
=
Column
(
Float
,
nullable
=
False
)
lon
=
Column
(
Float
,
nullable
=
False
)
attributes
=
Column
(
JSON
,
nullable
=
False
)
# Relationships
category_id
=
Column
(
Integer
,
ForeignKey
(
'category.id'
),
nullable
=
False
)
category
=
relationship
(
'Category'
)
parent_id
=
Column
(
Integer
,
ForeignKey
(
'point.id'
),
nullable
=
True
)
parent
=
relationship
(
'Point'
,
remote_side
=
[
id
])
children
=
relationship
(
'Point'
)
def
__init__
(
self
,
**
kwargs
):
# Need to load category first or else attribute validation will fail
if
'category'
in
kwargs
:
self
.
category
=
kwargs
.
pop
(
'category'
)
super
(
Point
,
self
).
__init__
(
**
kwargs
)
@
validates
(
'attributes'
)
def
validate_data
(
self
,
_
,
data
):
if
data
is
None
:
return
fields
=
self
.
category
.
fields
for
key
in
data
:
# Find Field object that corresponds to this key
for
field
in
fields
:
if
field
.
slug
==
key
:
break
else
:
raise
ValueError
(
f'extra data "
{
key
}
" must be a registered field'
)
field
.
validate_data
(
data
[
key
])
return
data
def
as_json
(
self
,
children
=
True
):
if
children
:
children
=
[
child
.
as_json
(
children
=
False
)
for
child
in
self
.
children
]
return
{
"name"
:
self
.
name
,
"lat"
:
self
.
lat
,
"lon"
:
self
.
lon
,
"category"
:
self
.
category
.
id
,
"attributes"
:
self
.
attributes
,
"children"
:
children
}
class
Category
(
Base
):
"""
Represent a schema for a single category of objects (e.g. water fountain or bathroom)
"""
__tablename__
=
'category'
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
name
=
Column
(
String
,
nullable
=
False
,
unique
=
True
)
icon
=
Column
(
String
,
nullable
=
True
)
fields
=
relationship
(
"Field"
)
def
as_json
(
self
):
return
{
"id"
:
self
.
id
,
"name"
:
self
.
name
,
"icon"
:
self
.
icon
,
"attributes"
:
{
attr
.
slug
:
attr
.
as_json
()
for
attr
in
self
.
fields
}
}
class
Field
(
Base
):
"""
Represents a single field in the Category schema.
"""
__tablename__
=
'field'
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
slug
=
Column
(
String
,
nullable
=
False
)
name
=
Column
(
String
,
nullable
=
False
)
type
=
Column
(
Enum
(
FieldType
),
nullable
=
False
)
# Relationship
category_id
=
Column
(
Integer
,
ForeignKey
(
'category.id'
))
def
validate_data
(
self
,
data
):
"""
Verify that data is the correct type for this Field.
"""
self
.
type
.
validate
(
data
)
def
as_json
(
self
):
return
{
"slug"
:
self
.
slug
,
"name"
:
self
.
name
,
"type"
:
self
.
type
.
name
}
from
.models
import
Base
,
Point
,
Category
,
Field
\ No newline at end of file
where/model/models.py
0 → 100644
View file @
df973667
from
sqlalchemy.ext.declarative
import
as_declarative
from
sqlalchemy
import
Column
,
Integer
,
String
,
Float
,
JSON
,
ForeignKey
,
Enum
from
sqlalchemy.orm
import
relationship
,
validates
from
.
import
FieldType
@
as_declarative
()
class
Base
(
object
):
pass
class
Point
(
Base
):
"""
Represents actual instances of any and all points on the map.
"""
__tablename__
=
'point'
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
name
=
Column
(
String
,
nullable
=
True
)
lat
=
Column
(
Float
,
nullable
=
False
)
lon
=
Column
(
Float
,
nullable
=
False
)
attributes
=
Column
(
JSON
,
nullable
=
False
)
# Relationships
category_id
=
Column
(
Integer
,
ForeignKey
(
'category.id'
),
nullable
=
False
)
category
=
relationship
(
'Category'
)
parent_id
=
Column
(
Integer
,
ForeignKey
(
'point.id'
),
nullable
=
True
)
parent
=
relationship
(
'Point'
,
remote_side
=
[
id
])
children
=
relationship
(
'Point'
)
def
__init__
(
self
,
**
kwargs
):
# Need to load category first or else attribute validation will fail
if
'category'
in
kwargs
:
self
.
category
=
kwargs
.
pop
(
'category'
)
super
(
Point
,
self
).
__init__
(
**
kwargs
)
@
validates
(
'attributes'
)
def
validate_data
(
self
,
_
,
data
):
if
data
is
None
:
return
fields
=
self
.
category
.
fields
for
key
in
data
:
# Find Field object that corresponds to this key
for
field
in
fields
:
if
field
.
slug
==
key
:
break
else
:
raise
ValueError
(
f'extra data "
{
key
}
" must be a registered field'
)
field
.
validate_data
(
data
[
key
])
return
data
def
as_json
(
self
,
children
=
True
):
if
children
:
children
=
[
child
.
as_json
(
children
=
False
)
for
child
in
self
.
children
]
return
{
"name"
:
self
.
name
,
"lat"
:
self
.
lat
,
"lon"
:
self
.
lon
,
"category"
:
self
.
category
.
id
,
"attributes"
:
self
.
attributes
,
"children"
:
children
}
class
Category
(
Base
):
"""
Represent a schema for a single category of objects (e.g. water fountain or bathroom)
"""
__tablename__
=
'category'
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
name
=
Column
(
String
,
nullable
=
False
,
unique
=
True
)
icon
=
Column
(
String
,
nullable
=
True
)
fields
=
relationship
(
"Field"
)
def
as_json
(
self
):
return
{
"id"
:
self
.
id
,
"name"
:
self
.
name
,
"icon"
:
self
.
icon
,
"attributes"
:
{
attr
.
slug
:
attr
.
as_json
()
for
attr
in
self
.
fields
}
}
class
Field
(
Base
):
"""
Represents a single field in the Category schema.
"""
__tablename__
=
'field'
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
slug
=
Column
(
String
,
nullable
=
False
)
name
=
Column
(
String
,
nullable
=
False
)
type
=
Column
(
Enum
(
FieldType
),
nullable
=
False
)
# Relationship
category_id
=
Column
(
Integer
,
ForeignKey
(
'category.id'
))
def
validate_data
(
self
,
data
):
"""
Verify that data is the correct type for this Field.
"""
self
.
type
.
validate
(
data
)
def
as_json
(
self
):
return
{
"slug"
:
self
.
slug
,
"name"
:
self
.
name
,
"type"
:
self
.
type
.
name
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment