forked from alexaorrico/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
executable file
·45 lines (40 loc) · 1.35 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
#!/usr/bin/python3
""" holds class User"""
import hashlib
import models
from models.base_model import BaseModel, Base
from os import getenv, environ
import sqlalchemy
from sqlalchemy import Column, String
from sqlalchemy.orm import relationship
STORAGE_TYPE = environ.get('HBNB_TYPE_STORAGE')
class User(BaseModel, Base):
"""Representation of a user """
if STORAGE_TYPE == 'db':
__tablename__ = 'users'
email = Column(String(128), nullable=False)
password = Column(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 kwargs:
pwd = kwargs.pop('password', None)
if pwd:
User.__set_password(self, pwd)
super().__init__(*args, **kwargs)
def __set_password(self, pwd):
"""
custom setter: encrypts password to MD5
"""
secure = hashlib.md5()
secure.update(pwd.encode("utf-8"))
secure_password = secure.hexdigest()
setattr(self, "password", secure_password)