Skip to content

Commit

Permalink
Backend+Frontend: Add About me to user profile (#52)
Browse files Browse the repository at this point in the history
- Change user model.
 - Frontend and API relevant additions.
 - Test fixes.
 - Use exceptions instead of returning False when authenticating.
  • Loading branch information
asaf-kali authored Jul 8, 2020
1 parent c8f4814 commit 507d755
Show file tree
Hide file tree
Showing 15 changed files with 374 additions and 321 deletions.
4 changes: 3 additions & 1 deletion backend/contrib/load_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ def main():
user_data = dict(username=USERNAME,
password=PASSWORD,
email='[email protected]',
full_name='IsraelCities')
full_name='IsraelCities',
about_me="I am a test user")
response = requests.post(API_SERVER + '/auth/users', json=user_data)
token_response = requests.post(
API_SERVER + '/auth/token',
data={
'grant_type': 'password',
'username': user_data['username'],
'password': user_data['password'],
'about_me': user_data['about_me']
},
headers={'content-type': "application/x-www-form-urlencoded"})

Expand Down
3 changes: 2 additions & 1 deletion backend/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def user_data():
return dict(username='zulu',
password='uluz',
email='[email protected]',
full_name='Zulu Uluz')
full_name='Zulu Uluz',
about_me="I'm a test user")


@pytest.fixture
Expand Down
2 changes: 2 additions & 0 deletions backend/tests/routes/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_user_me(client, some_user, user_data):
'grant_type': 'password',
'username': user_data['username'],
'password': user_data['password'],
'about_me': user_data['about_me']
},
headers={'content-type': "application/x-www-form-urlencoded"})

Expand All @@ -31,3 +32,4 @@ def test_user_me(client, some_user, user_data):
response.raise_for_status()
user = response.json()
assert user['username'] == user_data['username']
assert user['about_me'] == user_data['about_me']
24 changes: 18 additions & 6 deletions backend/zulu/auth_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Modified from: https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/
from datetime import datetime, timedelta
from typing import Union, Optional

import jwt
from dynaconf import settings
Expand Down Expand Up @@ -30,18 +31,30 @@ def get_password_hash(password):
return pwd_context.hash(password)


def get_user(db, username: str):
def get_user(db, username: str) -> Optional[UserInDB]:
user = db.find_one({'username': username})
if user:
return UserInDB(**user)


def authenticate_user(db, username: str, password: str):
class AuthenticationError(Exception):
pass


class NonExistingUserNameError(AuthenticationError):
pass


class WrongPasswordError(AuthenticationError):
pass


def authenticate_user(db, username: str, password: str) -> UserInDB:
user = get_user(db, username)
if not user:
return False
raise NonExistingUserNameError()
if not verify_password(password, user.hashed_password):
return False
raise WrongPasswordError()
return user


Expand Down Expand Up @@ -94,8 +107,7 @@ async def get_current_user(security_scopes: SecurityScopes,
return user


async def get_current_active_user(
current_user: User = Security(get_current_user)):
async def get_current_active_user(current_user: User = Security(get_current_user)):
if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user
2 changes: 2 additions & 0 deletions backend/zulu/db_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ def db():
client = MongoClient(settings.MONGO_URI).geo_data
yield client


def comments_db():
client = MongoClient(settings.MONGO_URI).geo_data
collection = client.comments
yield collection


def points_db():
client = MongoClient(settings.MONGO_URI).geo_data
collection = client.points
Expand Down
15 changes: 11 additions & 4 deletions backend/zulu/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,21 @@ class UserLocationResponse(BaseModel):
geometry: GeoJSONPoint
story: UserStory
user_id: str


class CommentPostResponse(BaseModel):
story_id: str
comment: str =None
comment: str = None
user_name: str = None
insertion_date:datetime.date=None
is_wiki:bool=False
insertion_date: datetime.date = None
is_wiki: bool = False


class CommentModel(BaseModel):
story_id: str
content: str
is_wiki:bool =False
is_wiki: bool = False


class ImageId(BaseModel):
id: str
Expand All @@ -62,9 +66,11 @@ class Token(BaseModel):
access_token: str
token_type: str


class StoryId(BaseModel):
id: str


class TokenData(BaseModel):
username: str = None
scopes: List[str] = []
Expand All @@ -76,6 +82,7 @@ class User(BaseModel):
full_name: str = None
disabled: bool = None
super_user: bool = False
about_me: str = None


class UserInDB(User):
Expand Down
18 changes: 10 additions & 8 deletions backend/zulu/routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@

from zulu.auth_utils import (authenticate_user, create_access_token,
get_current_active_user, get_password_hash,
get_user)
get_user, AuthenticationError)
from zulu.db_tools import users_db
from zulu.models import Token, User

api = APIRouter()


@api.post("/token", response_model=Token)
async def login_for_access_token(
form_data: OAuth2PasswordRequestForm = Depends(),
db=Depends(users_db)):
user = authenticate_user(db, form_data.username, form_data.password)
if not user:
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(),
db=Depends(users_db)):
try:
user = authenticate_user(db, form_data.username, form_data.password)
except AuthenticationError:
raise HTTPException(status_code=400,
detail="Incorrect username or password")
access_token_expires = timedelta(
Expand All @@ -45,8 +45,9 @@ async def read_users_me(current_user: User = Depends(get_current_active_user)):
async def create_user(db=Depends(users_db),
password: str = Body(...),
email: EmailStr = Body(...),
full_name: str = Body(None),
username: str = Body(...)):
full_name: str = Body(default=None),
username: str = Body(...),
about_me: str = Body(default="")):
user = get_user(db=db, username=username)

if user:
Expand All @@ -58,6 +59,7 @@ async def create_user(db=Depends(users_db),
'hashed_password': get_password_hash(password),
'full_name': full_name,
'username': username,
'about_me': about_me,
'disabled': False,
'super_user': False,
})
Expand Down
65 changes: 34 additions & 31 deletions frontend/zulu/src/App.css
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
.container-fluid.mt-3 {
height: 100%;
width: 100%;
height: 100%;
width: 100%;
}
.container-fluid{
background-color:white;

.container-fluid {
background-color: white;
}

label {
display: block;
margin-top: 10px;
display: block;
margin-top: 10px;
}

.card-container.card {
max-width: 350px !important;
padding: 40px 40px;
max-width: 350px !important;
padding: 40px 40px;
}

.buttons-container {
Expand All @@ -22,31 +24,32 @@ label {
}

.card {
background-color: #f7f7f7;
padding: 20px 25px 30px;
margin: 0 auto 25px;
margin-top: 50px;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
border-radius: 2px;
-moz-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
-webkit-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
background-color: #f7f7f7;
padding: 20px 25px 30px;
margin: 0 auto 25px;
margin-top: 50px;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
border-radius: 2px;
-moz-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
-webkit-box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
}


.profile-img-card {
width: 96px;
height: 96px;
margin: 0 auto 10px;
display: block;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
}
#about .modal-content{
width: 100% !important;
text-align: left !important;

padding: 30px !important;
width: 96px;
height: 96px;
margin: 0 auto 10px;
display: block;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
}

#about .modal-content {
width: 100% !important;
text-align: left !important;

padding: 30px !important;
}
Loading

0 comments on commit 507d755

Please sign in to comment.