Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
TJTS5901 K23 Template
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Startuplab
Courses
TJTS5901 Continuous Software Engineering
TJTS5901 K23 Template
Commits
f3cbbeeb
Commit
f3cbbeeb
authored
2 years ago
by
Teemu Autto
Browse files
Options
Downloads
Plain Diff
Merge branch 'rest-api-simple' into 'main'
Rest api simple See merge request
!71
parents
081fec6f
fd10b6ed
No related branches found
Branches containing commit
No related tags found
1 merge request
!71
Rest api simple
Pipeline
#15814
failed
2 years ago
Stage: build
Stage: test
Stage: staging
Stage: smoketest
Stage: deploy
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/tjts5901/app.py
+1
-0
1 addition, 0 deletions
src/tjts5901/app.py
src/tjts5901/items.py
+95
-1
95 additions, 1 deletion
src/tjts5901/items.py
with
96 additions
and
1 deletion
src/tjts5901/app.py
+
1
−
0
View file @
f3cbbeeb
...
...
@@ -81,6 +81,7 @@ def create_app(config: Optional[Dict] = None) -> Flask:
from
.
import
items
flask_app
.
register_blueprint
(
items
.
bp
)
flask_app
.
register_blueprint
(
items
.
api
)
flask_app
.
add_url_rule
(
'
/
'
,
endpoint
=
'
index
'
)
return
flask_app
...
...
This diff is collapsed.
Click to expand it.
src/tjts5901/items.py
+
95
−
1
View file @
f3cbbeeb
...
...
@@ -2,7 +2,7 @@ from datetime import datetime, timedelta
import
logging
from
typing
import
Optional
from
flask
import
(
Blueprint
,
flash
,
redirect
,
render_template
,
request
,
url_for
Blueprint
,
flash
,
redirect
,
render_template
,
request
,
url_for
,
jsonify
)
from
flask_babel
import
_
,
get_locale
from
werkzeug.exceptions
import
abort
...
...
@@ -21,6 +21,7 @@ from .currency import (
)
bp
=
Blueprint
(
'
items
'
,
__name__
)
api
=
Blueprint
(
'
api_items
'
,
__name__
,
url_prefix
=
'
/api/items
'
)
logger
=
logging
.
getLogger
(
__name__
)
...
...
@@ -276,3 +277,96 @@ def bid(id):
flash
(
_
(
"
Bid placed successfully!
"
))
return
redirect
(
url_for
(
'
items.view
'
,
id
=
id
))
@api.route
(
'
<id>/bids
'
,
methods
=
(
'
GET
'
,))
@login_required
def
api_item_bids
(
id
):
"""
Get the bids for an item.
:param id: The id of the item to get bids for.
:return: A JSON response containing the bids.
"""
item
=
Item
.
objects
.
get_or_404
(
id
=
id
)
bids
=
[]
for
bid
in
Bid
.
objects
(
item
=
item
).
order_by
(
'
-amount
'
):
bids
.
append
(
bid
.
to_json
())
return
jsonify
({
'
success
'
:
True
,
'
bids
'
:
bids
})
@api.route
(
'
<id>/bids
'
,
methods
=
(
'
POST
'
,))
@login_required
def
api_item_place_bid
(
id
):
"""
Place a bid on an item.
If the bid is valid, create a new bid and return the bid.
Otherwise, return an error message.
Only accepts `REF_CURRENCY` bids.
:param id: The id of the item to bid on.
:return: A JSON response containing the bid.
"""
item
=
Item
.
objects
.
get_or_404
(
id
=
id
)
min_amount
=
get_item_price
(
item
)
try
:
amount
=
int
(
request
.
form
[
'
amount
'
])
except
KeyError
:
return
jsonify
({
'
success
'
:
False
,
'
error
'
:
_
(
"
Missing required argument %(argname)s
"
,
argname
=
'
amount
'
)
})
except
ValueError
:
return
jsonify
({
'
success
'
:
False
,
'
error
'
:
_
(
"
Invalid value for argument %(argname)s
"
,
argname
=
'
amount
'
)
})
except
Exception
as
exc
:
return
jsonify
({
'
success
'
:
False
,
'
error
'
:
_
(
"
Error parsing argument %(argname)s: %(exc)s
"
,
argname
=
'
amount
'
,
exc
=
exc
)
})
if
amount
<
min_amount
:
return
jsonify
({
'
success
'
:
False
,
'
error
'
:
_
(
"
Bid must be at least %(min_amount)s
"
,
min_amount
=
min_amount
)
})
if
item
.
closes_at
<
datetime
.
utcnow
():
return
jsonify
({
'
success
'
:
False
,
'
error
'
:
_
(
"
This item is no longer on sale.
"
)
})
try
:
bid
=
Bid
(
item
=
item
,
bidder
=
current_user
,
amount
=
amount
,
)
bid
.
save
()
except
Exception
as
exc
:
logger
.
error
(
"
Error placing bid: %s
"
,
exc
,
exc_info
=
True
,
extra
=
{
'
item_id
'
:
item
.
id
,
'
bidder_id
'
:
current_user
.
id
,
'
amount
'
:
amount
,
})
return
jsonify
({
'
success
'
:
False
,
'
error
'
:
_
(
"
Error placing bid: %(exc)s
"
,
exc
=
exc
)
})
return
jsonify
({
'
success
'
:
True
,
'
bid
'
:
bid
.
to_mongo
().
to_dict
()
})
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