Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
pymongo
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
docker101
pymongo
Commits
78c9d3f4
Commit
78c9d3f4
authored
Sep 01, 2019
by
Ahmad Anvari
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new features
parent
3a642411
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
105 additions
and
0 deletions
+105
-0
order.py
db/order.py
+20
-0
product.py
db/product.py
+57
-0
order.py
routes/order.py
+0
-0
product.py
routes/product.py
+28
-0
No files found.
db/order.py
0 → 100644
View file @
78c9d3f4
from
db.mongo
import
Model
class
Order
(
Model
):
def
__init__
(
self
):
pass
def
store
(
self
):
pass
@
staticmethod
def
get_collection
():
pass
@
staticmethod
def
dict_to_object
(
dictionary
):
pass
def
to_dict
(
self
):
pass
db/product.py
0 → 100644
View file @
78c9d3f4
from
db.mongo
import
Model
class
Product
(
Model
):
def
__init__
(
self
,
name
,
price
,
available
,
tags
):
self
.
__id
=
None
self
.
__name
=
name
self
.
__price
=
price
self
.
__available
=
available
self
.
__tags
=
tags
@
staticmethod
def
get_collection
():
return
Model
.
get_db
()
.
get_collection
(
'products'
)
@
staticmethod
def
dict_to_object
(
dictionary
):
pass
def
to_dict
(
self
):
dict
=
{}
if
self
.
__id
is
not
None
:
dict
[
"_id"
]
=
self
.
__id
if
self
.
__name
is
not
None
:
dict
[
"name"
]
=
self
.
__name
if
self
.
__price
is
not
None
:
dict
[
"price"
]
=
self
.
__price
if
self
.
__available
is
not
None
:
dict
[
"available"
]
=
self
.
__available
if
self
.
__tags
is
not
None
:
dict
[
"tags"
]
=
self
.
__tags
return
dict
def
store
(
self
):
add_document
=
Product
.
get_collection
()
.
insert_one
(
self
.
to_dict
())
self
.
__id
=
str
(
add_document
.
inserted_id
)
def
search_by_tags
(
self
):
pass
@
staticmethod
def
search
(
count
,
tags
):
print
(
len
(
tags
))
print
(
tags
)
if
len
(
tags
)
==
0
:
objects
=
Product
.
get_collection
()
.
find
()
.
limit
(
count
)
else
:
objects
=
Product
.
get_collection
()
.
find
({
"tags"
:
{
"$all"
:
tags
}})
.
sort
([(
"price"
,
-
1
)])
.
limit
(
count
)
result
=
[]
for
obj
in
objects
:
obj
[
"_id"
]
=
str
(
obj
[
"_id"
])
result
.
append
(
obj
)
return
result
def
get_id
(
self
):
return
self
.
__id
\ No newline at end of file
routes/order.py
0 → 100644
View file @
78c9d3f4
routes/product.py
0 → 100644
View file @
78c9d3f4
from
app
import
app
from
flask
import
request
,
jsonify
from
db.product
import
Product
@
app
.
route
(
'/products'
,
methods
=
[
"GET"
])
def
search_products
():
tags
=
request
.
args
.
get
(
'tags'
)
if
tags
is
None
:
tags
=
[]
else
:
tags
=
tags
.
split
(
','
)
count
=
request
.
args
.
get
(
'count'
)
if
count
is
None
:
count
=
5
else
:
count
=
int
(
count
)
r
=
Product
.
search
(
count
=
count
,
tags
=
tags
)
return
jsonify
(
r
)
@
app
.
route
(
'/products'
,
methods
=
[
"POST"
])
def
add_product
():
p1
=
Product
(
name
=
"Lenovo ThinkPad TH230"
,
price
=
193000000
,
tags
=
[
"laptop"
,
"pc"
,
"computer"
,
"lenovo"
,
"thinkpad"
],
available
=
int
(
50
))
p2
=
Product
(
name
=
"MSI 127"
,
price
=
308000000
,
tags
=
[
"laptop"
,
"pc"
,
"computer"
,
"msi"
,
"gaming"
],
available
=
int
(
20
))
p1
.
store
()
p2
.
store
()
return
jsonify
({
"id"
:
[
p1
.
get_id
(),
p2
.
get_id
()]})
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