Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Odevzdávací Systém MO
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Mareš
Odevzdávací Systém MO
Commits
0ae74998
Commit
0ae74998
authored
3 years ago
by
Martin Mareš
Browse files
Options
Downloads
Patches
Plain Diff
API pro JS na zadávání škol
parent
3f1c9534
No related branches found
No related tags found
1 merge request
!86
Registrace
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
mo/web/__init__.py
+1
-0
1 addition, 0 deletions
mo/web/__init__.py
mo/web/api.py
+78
-0
78 additions, 0 deletions
mo/web/api.py
with
79 additions
and
0 deletions
mo/web/__init__.py
+
1
−
0
View file @
0ae74998
...
...
@@ -215,6 +215,7 @@ except ImportError:
# Většina webu je v samostatných modulech
import
mo.web.api
import
mo.web.auth
import
mo.web.jinja
import
mo.web.menu
...
...
This diff is collapsed.
Click to expand it.
mo/web/api.py
0 → 100644
+
78
−
0
View file @
0ae74998
from
flask
import
request
from
flask.json
import
jsonify
from
sqlalchemy.orm
import
joinedload
import
werkzeug.exceptions
import
mo.db
as
db
from
mo.util_format
import
inflect_with_number
from
mo.web
import
app
@app.route
(
'
/api/
'
)
def
api_root
():
"""
Slouží jako prefix pro konstrukci URL v JavaScriptu.
"""
raise
werkzeug
.
exceptions
.
NotFound
()
@app.route
(
'
/api/find-town
'
)
def
api_find_town
():
query
=
request
.
args
.
get
(
'
q
'
)
if
query
is
None
or
len
(
query
)
<
2
:
return
jsonify
(
error
=
'
Zadejte alespoň 2 znaky jména obce.
'
)
elif
'
%
'
in
query
:
return
jsonify
(
error
=
'
Nepovolené znaky ve jménu obce.
'
)
else
:
# FIXME: Hledání bez akcentů
# FIXME: Hodil by se index...
max_places
=
50
places
=
(
db
.
get_session
().
query
(
db
.
Place
)
.
filter_by
(
level
=
3
)
.
filter
(
db
.
Place
.
name
.
ilike
(
query
+
'
%
'
))
.
options
(
joinedload
(
db
.
Place
.
parent_place
))
.
order_by
(
db
.
Place
.
name
,
db
.
Place
.
place_id
)
.
limit
(
max_places
)
.
all
())
if
not
places
:
return
jsonify
(
error
=
'
Nenalezena žádná obec.
'
)
# XXX: Nemůže se stát, že nastane přesná shoda a k tomu příliš mnoho nepřesných?
if
len
(
places
)
>=
max_places
:
return
jsonify
(
error
=
'
Nalezeno příliš mnoho obcí. Zadejte prosím více znaků jména.
'
)
res
=
[]
for
p
in
places
:
name
=
p
.
name
if
p
.
name
!=
p
.
parent_place
.
name
:
name
+=
f
'
(okres
{
p
.
parent_place
.
name
}
)
'
res
.
append
([
p
.
place_id
,
name
])
msg
=
inflect_with_number
(
len
(
res
),
'
Nalezena %s obec.
'
,
'
Nalezeny %s obce.
'
,
'
Nalezeno %s obcí.
'
)
return
jsonify
(
found
=
res
,
msg
=
msg
)
@app.route
(
'
/api/get-schools
'
)
def
api_get_schools
():
town
=
request
.
args
.
get
(
'
town
'
)
if
town
is
None
or
not
town
.
isnumeric
():
raise
werkzeug
.
exceptions
.
BadRequest
()
town_id
=
int
(
town
)
places
=
(
db
.
get_session
().
query
(
db
.
Place
)
.
filter_by
(
level
=
4
,
type
=
db
.
PlaceType
.
school
,
parent
=
town_id
)
.
options
(
joinedload
(
db
.
Place
.
school
))
.
order_by
(
db
.
Place
.
name
)
.
all
())
zs
=
[]
ss
=
[]
for
p
in
places
:
s
=
{
'
id
'
:
p
.
place_id
,
'
name
'
:
p
.
name
,
}
if
p
.
school
.
is_zs
:
zs
.
append
(
s
)
if
p
.
school
.
is_ss
:
ss
.
append
(
s
)
return
jsonify
(
zs
=
zs
,
ss
=
ss
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment