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
6bb0e5e3
Commit
6bb0e5e3
authored
Sep 01, 2019
by
Ahmad Anvari
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finalize user model class
parent
887fbe1c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
14 deletions
+45
-14
user.py
db/user.py
+45
-14
No files found.
db/user.py
View file @
6bb0e5e3
from
abc
import
ABC
from
bson.objectid
import
ObjectId
from
.mongo
import
Model
from
pymongo.errors
import
DuplicateKeyError
import
hashlib
class
User
(
Model
):
@
staticmethod
def
dict_to_object
(
dictionary
):
u
=
User
(
first_name
=
dictionary
[
"first_name"
],
last_name
=
dictionary
[
"last_name"
],
email
=
dictionary
[
"email"
],
hashed_password
=
dictionary
[
"hashed_password"
]
)
u
.
__id
=
str
(
dictionary
[
"_id"
])
return
u
def
to_dict
(
self
):
res
=
{}
if
self
.
__id
is
not
None
:
...
...
@@ -13,29 +25,48 @@ class User(Model):
res
[
"first_name"
]
=
self
.
__first_name
if
self
.
__last_name
is
not
None
:
res
[
"last_name"
]
=
self
.
__last_name
if
self
.
__email
is
not
None
:
res
[
"email"
]
=
self
.
__email
if
self
.
__hashed_password
is
not
None
:
res
[
"hashed_password"
]
=
self
.
__hashed_password
return
res
def
__init__
(
self
,
first_name
,
last_name
,
email
,
hashed_password
):
self
.
__id
=
""
self
.
__id
=
None
self
.
__first_name
=
first_name
self
.
__last_name
=
last_name
self
.
__email
=
email
self
.
__hashed_password
=
hashed_password
def
get_collection
(
self
):
return
self
.
get_db
()[
"users"
]
@
staticmethod
def
get_collection
():
return
Model
.
get_db
()[
"users"
]
def
get_id
(
self
):
return
self
.
__id
def
store
(
self
):
add_document
=
self
.
get_collection
()
.
insert_one
({
"first_name"
:
self
.
__first_name
,
"last_name"
:
self
.
__last_name
,
"email"
:
self
.
__email
,
"hashed_password"
:
self
.
__hashed_password
,
})
self
.
__id
=
str
(
add_document
.
inserted_id
)
try
:
add_document
=
User
.
get_collection
()
.
insert_one
(
self
.
to_dict
())
self
.
__id
=
str
(
add_document
.
inserted_id
)
except
DuplicateKeyError
:
print
(
"Error occurred"
)
@
staticmethod
def
find_by_username
(
email
):
u
=
User
.
get_collection
()
.
find_one
({
"email"
:
email
})
if
u
is
not
None
:
return
User
.
dict_to_object
(
u
)
return
u
@
staticmethod
def
find_by_username
(
self
,
email
):
return
self
.
get_collection
()
.
find_one
({
"email"
:
email
})
def
find_by_id
(
object_id
):
u
=
User
.
get_collection
()
.
find_one
({
"_id"
:
ObjectId
(
object_id
)})
if
u
is
not
None
:
return
User
.
dict_to_object
(
u
)
return
u
def
compare_password
(
self
,
new_password
):
new_hashed_password
=
hashlib
.
md5
(
new_password
.
encode
())
.
hexdigest
()
return
self
.
__hashed_password
==
new_hashed_password
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