Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Joramas committed Apr 4, 2024
2 parents 4374c03 + 603b68e commit 1c6512d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 35 deletions.
53 changes: 42 additions & 11 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
from roles.models import get_all_roles
from sup_view import inspect_supervisor
from werkzeug.utils import secure_filename

from flask_sslify import SSLify

app = Flask(__name__)
# sslify = SSLify(app)

# Enforce SSL
sslify = SSLify(app)


# Configuration
app.config["DEBUG"] = True
app.secret_key = 'testing'

app.config['APPLICATION_ROOT'] = '/'
Expand Down Expand Up @@ -74,10 +75,15 @@ def initialize_database():
def login_required(f):
@wraps(f)
def wrap(*args, **kwargs):
log(f)
log(args)
log(kwargs)
if 'logged_in' in session:
log("redirecting to the called site")
return f(*args, **kwargs)
else:
return redirect('/dashboard')
log("redirecting to login")
return redirect('/')
return wrap

def admin_required(f):
Expand Down Expand Up @@ -177,6 +183,7 @@ def get_pending(role, user):
@app.route("/")
def home():
if 'logged_in' in session:
print(f"Session Data: {session.__dict__}")
return redirect('/dashboard')
# Render the home page with the login form
return render_template('home.html', register=False)
Expand Down Expand Up @@ -412,13 +419,16 @@ def dashboard(
alert=None
):

log("1")
if alert == None:
alert = session['messages'] if 'messages' in session else None
session['messages'] = None

if 'providerId' in session['user']:
session['user']['providerId'] = int(session['user']['providerId'])

log("2")

# Detect the role of the loged user to determine the permissions
if 'role' in session['user'] and session['user']['role'] != None:
role = session['user']['role']
Expand All @@ -427,10 +437,10 @@ def dashboard(

users = db.users.find()
users = sorted(users, key=lambda d: (d['role'], d['name']))
print(year, month)
log(year, month)
entries, total_hours, supervised_time, ids, meetings, min_year, supervisors, observed_with_client, face_to_face = get_entries(
role, year, month, session['user'])
print(face_to_face)
log(face_to_face)
if role in get_supervisors():
us = inspect_supervisor(
db=db, year=year, month=month, pid=session['user']['providerId'])
Expand All @@ -441,7 +451,7 @@ def dashboard(
users = nus

pending = get_pending(role, session['user'])

log("3")
for entry in entries:
name = db.users.find_one({"ProviderId": int(entry['Supervisor'])})
if name:
Expand All @@ -454,8 +464,8 @@ def dashboard(
# log(name)
if name:
entry['Supervisor'] = name['first_name']
# log(observed_with_client)

log(observed_with_client)
missing = []
user = session['user']

Expand All @@ -465,7 +475,13 @@ def dashboard(
continue
missing.append(i)
exp = supervised_time >= 5/100*total_hours and observed_with_client >= 1 and face_to_face >= 2
return render_template('dashboard.html', user=user, face_to_face=face_to_face, role=role, entries=entries, providerIds=ids, supervisors=supervisors, session=session, total_hours=round_half_up(total_hours, 2), minimum_supervised=round(5/100*total_hours, 2), supervised_hours=supervised_time, meeting_group=meetings, year=year, min_year=min_year, month=month, users=users, pending=pending, id=str(session['user']['_id']), alert=alert, report=not (role in get_admins()), observed_with_client=observed_with_client,exp = exp, missing=missing)
try:
template = render_template('dashboard.html', user=user, face_to_face=face_to_face, role=role, entries=entries, providerIds=ids, supervisors=supervisors, session=session, total_hours=round_half_up(total_hours, 2), minimum_supervised=round(5/100*total_hours, 2), supervised_hours=supervised_time, meeting_group=meetings, year=year, min_year=min_year, month=month, users=users, pending=pending, id=str(session['user']['_id']), alert=alert, report=not (role in get_admins()), observed_with_client=observed_with_client, exp=exp, missing=missing)
log("render")
return template
except Exception as e:
log(f"Error rendering dashboard template: {str(e)}")
return "An error occurred while trying to render the dashboard. Please try again later."

# Only admins will see this page and it will let edit users and provider ids

Expand Down Expand Up @@ -810,7 +826,22 @@ def signup():

@ app.route('/user/login', methods=['POST'])
def login():
return User().login(db)
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
user = db.users.find_one({"email": email})

if user and pbkdf2_sha256.verify(password, user['password']):
session['logged_in'] = True
session['user'] = user
log(f"User {email} logged in successfully.")
# return redirect(url_for('dashboard'))
else:
log(f"Failed login attempt.")
flash('Invalid login credentials', 'danger')

usr = User().login(db)
return dashboard()


@ app.route('/user/signout')
Expand Down
45 changes: 23 additions & 22 deletions static/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,31 @@ $("form[name=signup_form]").submit(function(e){
e.preventDefault();
});

$("form[name=login_form]").submit(function(e){
// $("form[name=login_form]").submit(function(e){

var $form = $(this);
var $error = $form.find(".error");
var data = $form.serialize();
// var $form = $(this);
// var $error = $form.find(".error");
// var data = $form.serialize();

$.ajax({
url: "https://rbt.americanbehavioralsolutions.com/user/login",
type: "POST",
data: data,
dataType: "json",
success: function(resp){
console.log(resp);
$error.text(" ").addClass("error--hidden");
window.location.href = "https://rbt.americanbehavioralsolutions.com/dashboard"
},
error:function(resp){
console.log(resp);
console.log(resp.responseJSON.error)
$error.text(resp.responseJSON.error).removeClass("error--hidden");
},
});
e.preventDefault();
});
// $.ajax({
// url: "https://rbt.americanbehavioralsolutions.com/user/login", // Change to your domain with https
// type: "POST",
// data: data,
// dataType: "json",
// success: function(resp){
// console.log(resp);
// $error.text("").addClass("error--hidden"); // Changed " " to "" for clarity
// window.location.href = "https://rbt.americanbehavioralsolutions.com/dashboard" // Change to your domain with https
// },
// error:
// function(resp){
// console.log(resp);
// console.log(resp.responseJSON.error)
// $error.text(resp.responseJSON.error).removeClass("error--hidden");
// },
// });
// e.preventDefault();
// });

// $("form[name=edit_form]").submit(function(e){

Expand Down
2 changes: 1 addition & 1 deletion templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h1 class="text-center">Login</h1>
</div>
<div class="card-body">
<p class="text-center mb-4">Welcome to AmericanBehavioralSolutions</p>
<form name="login_form" method="POST">
<form name="login_form" method="POST", action="/user/login">
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="text" name="email" class="form-control" required>
Expand Down
4 changes: 3 additions & 1 deletion user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from termcolor import colored
import datetime_format
# from app import db

from logger import log

class User:

Expand All @@ -19,6 +19,8 @@ def start_session(self, user):
user['_id'] = str(user['_id'])
session['logged_in'] = True
session['user'] = user
log(user)
log(session)
return jsonify(user), 200

# create new user
Expand Down

0 comments on commit 1c6512d

Please sign in to comment.