This repository has been archived by the owner on May 2, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
737 additions
and
115 deletions.
There are no files selected for viewing
11 changes: 0 additions & 11 deletions
11
work/036-pyramid-intro/billtracker/billtracker/__init__.py
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
62 changes: 0 additions & 62 deletions
62
work/036-pyramid-intro/billtracker/billtracker/templates/base.pt
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
work/036-pyramid-intro/billtracker/billtracker/templates/home.pt
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
work/036-pyramid-intro/billtracker/billtracker/views/home.py
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions
25
work/037-pyramid-intro/billtracker/billtracker/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# -*- conding: utf-8 -*- | ||
|
||
"""App entry point.""" | ||
|
||
import os | ||
|
||
from pyramid.config import Configurator | ||
|
||
from billtracker.bin.load_base_data import load_starter_data | ||
from billtracker.data.db_session import DbSession | ||
|
||
|
||
def main(global_config, **settings): | ||
"""This function returns a Pyramid WSGI application.""" | ||
with Configurator(settings=settings) as config: | ||
config.include("pyramid_chameleon") | ||
config.include(".routes") | ||
config.include(".security") | ||
config.scan() | ||
|
||
current_dir = os.path.dirname(os.path.abspath(__file__)) | ||
db_file = os.path.join(current_dir, "db/billtracker.sqlite3") | ||
DbSession.global_init(db_file) | ||
load_starter_data() | ||
return config.make_wsgi_app() |
File renamed without changes.
65 changes: 65 additions & 0 deletions
65
work/037-pyramid-intro/billtracker/billtracker/bin/load_base_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import json | ||
import os | ||
import random | ||
from typing import List | ||
|
||
import dateutil.parser | ||
from sqlalchemy.orm import Session | ||
|
||
from billtracker.data.db_session import DbSession | ||
from billtracker.data.models.bill import Bill | ||
from billtracker.data.models.users import User | ||
|
||
|
||
def load_starter_data(): | ||
print("Loading starter data...") | ||
session = DbSession.create_session() | ||
if session.query(Bill).count() > 0: | ||
session.close() | ||
print("Data already loaded...") | ||
return | ||
|
||
session.expire_on_commit = False | ||
|
||
users = add_users(session) | ||
add_bills(users) | ||
|
||
session.commit() | ||
session.close() | ||
|
||
|
||
def add_users(session: Session) -> List[User]: | ||
users = [] | ||
data_file = os.path.join(DbSession.db_folder, 'MOCK_USERS.json') | ||
with open(data_file, 'r', encoding='utf-8') as fin: | ||
data = json.load(fin) | ||
|
||
for u in data: | ||
user = User() | ||
users.append(user) | ||
user.email = u.get('email') | ||
user.name = u.get('name') | ||
user.created_date = dateutil.parser.parse(u.get('created_date')) | ||
user.last_login = dateutil.parser.parse(u.get('last_login')) | ||
user.last_login = dateutil.parser.parse(u.get('last_login')) | ||
user.hashed_password = u.get('hashed_password') | ||
session.add(user) | ||
|
||
return users | ||
|
||
|
||
def add_bills(users: List[User]): | ||
data_file = os.path.join(DbSession.db_folder, 'MOCK_PAYMENTS.json') | ||
with open(data_file, 'r', encoding='utf-8') as fin: | ||
data = json.load(fin) | ||
|
||
for p in data: | ||
user = random.choice(users) | ||
|
||
bill = Bill() | ||
bill.created_date = dateutil.parser.parse(p.get('created_date')) | ||
bill.description = p.get('description') | ||
bill.total = int(p.get('total')) | ||
bill.paid = min(bill.total, int(p.get('paid'))) | ||
|
||
user.bills.append(bill) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...illtracker/billtracker/db/MOCK_USERS.json → ...illtracker/billtracker/db/MOCK_USERS.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def includeme(config): | ||
config.add_static_view('static', 'static', cache_max_age=3600) | ||
config.add_route('login', '/login') | ||
config.add_route('logout', '/logout') | ||
config.add_route('home', '/') | ||
config.add_route('welcome', '/welcome') | ||
config.add_route('bill_detail', '/bill/{bill_id}') |
39 changes: 39 additions & 0 deletions
39
work/037-pyramid-intro/billtracker/billtracker/security.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
"""Defines the security functionality for the app.""" | ||
|
||
from pyramid.authentication import AuthTktAuthenticationPolicy | ||
from pyramid.authorization import ACLAuthorizationPolicy | ||
|
||
|
||
from billtracker.data import repository | ||
|
||
|
||
class MyAuthenticationPolicy(AuthTktAuthenticationPolicy): | ||
"""Defines the auth policy for the app.""" | ||
|
||
def authenticated_user(self, request): | ||
"""Return id of authenticated user from request.""" | ||
user = request.user | ||
if user is not None: | ||
return user.id | ||
|
||
|
||
def get_user(request): | ||
"""Return User object from database with given id.""" | ||
user_id = request.unauthenticated_userid | ||
if user_id is not None: | ||
user = repository.get_user_by_id(user_id) | ||
return user | ||
|
||
|
||
def includeme(config): | ||
settings = config.get_settings() | ||
authn_policy = MyAuthenticationPolicy( | ||
settings["auth.secret"], | ||
hashalg="sha512", | ||
) | ||
config.set_authentication_policy(authn_policy) | ||
config.set_authorization_policy(ACLAuthorizationPolicy()) | ||
config.add_request_method(get_user, "user", reify=True) | ||
|
Oops, something went wrong.