forked from alexaorrico/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
executable file
·53 lines (46 loc) · 1.64 KB
/
user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python3
""" holds class User"""
import hashlib
import models
from models.base_model import BaseModel, Base
from sqlalchemy import Column, String
from sqlalchemy.orm import relationship
class User(BaseModel, Base):
"""Representation of a user """
if models.storage_t == 'db':
__tablename__ = 'users'
email = Column(String(128), nullable=False)
_password = Column("password", String(128), nullable=False)
first_name = Column(String(128), nullable=True)
last_name = Column(String(128), nullable=True)
places = relationship("Place", backref="user")
reviews = relationship("Review", backref="user")
else:
email = ""
_password = ""
first_name = ""
last_name = ""
def __init__(self, *args, **kwargs):
"""initializes user"""
if 'password' in kwargs:
self.password = kwargs['password']
del kwargs['password']
super().__init__(*args, **kwargs)
@property
def password(self):
"""Getter for password"""
return self._password
@password.setter
def password(self, value):
"""Setter for password"""
self._password = self.hash_password(value)
@staticmethod
def hash_password(password):
"""Hashes a password using MD5"""
return hashlib.md5(password.encode()).hexdigest()
def to_dict(self, include_password=False):
"""Returns a dictionary representation of a User instance"""
new_dict = super().to_dict(include_password=include_password)
if include_password:
new_dict["password"] = self._password
return new_dict