Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
pip install -r requirements.txt
- name: Analysing the code with pylint
run: |
pylint app --disable=C0116,C0114,C0115,C0411,E0401,W0611,W0622,W0719,C0103,W1514,R0903,R1732
pylint app --disable=C0116,C0114,C0115,C0411,E0401,W0611,W0622,W0719,C0103,W1514,R0903,R1732,R0914
- name: Analysing the code with pycodestyle
run: |
pycodestyle app
2 changes: 1 addition & 1 deletion app/api/web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def api_get_user_permissions():
@web_api.route("/api/users", methods=["POST"])
def api_add_user():
user_data = request.get_json()
user = User(user_data["name"], user_data["key"])
user = User(user_data["name"], user_data["key"], user_data["email"])

number_of_new_user_added = user.save()

Expand Down
21 changes: 12 additions & 9 deletions app/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@


class User:
def __init__(self, name, key, slack_id=None):
def __init__(self, name, key, email=None):
self.name = name
self.key = key
self.slack_id = slack_id
self.email = email

@classmethod
def get_by_key(cls, key):
Expand Down Expand Up @@ -34,14 +34,14 @@ def save(self):
if existing_user:
# Update existing user data
cursor.execute(
"UPDATE users SET name = ?, slack_id = ? WHERE key = ?",
(self.name, self.slack_id, self.key),
"UPDATE users SET name = ?, key = ? WHERE email = ?",
(self.name, self.key, self.email if self.email else None),
)
else:
# Create new user
cursor.execute(
"INSERT INTO users (name, key, slack_id) VALUES (?, ?, ?)",
(self.name, self.key, self.slack_id),
"INSERT INTO users (name, key, email) VALUES (?, ?, ?)",
(self.name, self.key, self.email if self.email else None),
)
number_of_new_user = 1
conn.commit()
Expand Down Expand Up @@ -79,7 +79,7 @@ def get_permissions(cls, user_key=None):
if user_key:
cursor.execute(
"""
SELECT users.name, users.key,
SELECT users.name, users.key, users.email,
(SELECT operation_time
FROM event_logs
WHERE user_key = users.key
Expand All @@ -93,7 +93,7 @@ def get_permissions(cls, user_key=None):
else:
cursor.execute(
"""
SELECT users.name, users.key,
SELECT users.name, users.key, users.email,
(SELECT operation_time
FROM event_logs
WHERE user_key = users.key
Expand All @@ -106,7 +106,8 @@ def get_permissions(cls, user_key=None):
for row in cursor.fetchall():
user_name = row[0]
user_key = row[1]
latest_activity = row[2]
user_email = row[2] if row[2] else None # Handle email being None
latest_activity = row[3]

# Get device permissions for the current user
device_permissions = []
Expand Down Expand Up @@ -138,6 +139,7 @@ def get_permissions(cls, user_key=None):
user_record = {
"user_name": user_name,
"user_key": user_key,
"user_email": user_email,
"permissions": device_permissions,
"latest_activity": latest_activity,
}
Expand All @@ -146,6 +148,7 @@ def get_permissions(cls, user_key=None):
# Close the database connection
conn.close()

print(f"User data fetched: {user_data}")
return user_data

def has_permission_for_device(self, device_id):
Expand Down
2 changes: 1 addition & 1 deletion app/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CREATE TABLE IF NOT EXISTS users
(
name TEXT NOT NULL,
key TEXT NOT NULL,
slack_id TEXT DEFAULT NULL
email TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS permissions
(
Expand Down
39 changes: 27 additions & 12 deletions app/templates/prismo/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<div class="card-body">
<h5 class="card-title">Add last used RFID card as new user: <span id="used-key-at"></span></h5>
<div class="input-group mb-3">
<button class="btn btn-primary" type="submit" onclick="addUser(document.getElementById('user_name').value)" ><i class="bi bi-person-add"></i> Add User</button>
<button class="btn btn-primary" type="submit" onclick="addUser(document.getElementById('user_name').value, document.getElementById('user_email').value)" ><i class="bi bi-person-add"></i> Add User</button>
<input type="text" class="form-control" id="user_name" placeholder="User Name">
<input type="email" class="form-control ms-2" id="user_email" placeholder="Email">
</div>
</div>
</div>
Expand Down Expand Up @@ -46,8 +47,9 @@ <h5 class="card-title">Add last used RFID card as new user: <span id="used-key-a
return null; // Or throw an error
}
}

async function addUser(name) {
// Example curl request to add a new user:
// curl -X POST -H "Content-Type: application/json" -d '{"name":"John Doe","email":"john@example.com","key":"RFID_KEY"}' http://localhost:5000/api/users
async function addUser(name, email) {
try {
const latestKey = await getLatestKey(); // Wait for API call

Expand All @@ -58,7 +60,7 @@ <h5 class="card-title">Add last used RFID card as new user: <span id="used-key-a
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ name, key })
body: JSON.stringify({ name, email, key })
});

if (!response.ok) {
Expand Down Expand Up @@ -107,6 +109,7 @@ <h5 class="card-title">Add last used RFID card as new user: <span id="used-key-a
return acc
}, {}),
latestActivity: user.latest_activity,
email: user.user_email,
operation: '<button class="btn btn-sm btn-danger" onclick="deleteUser(\'' + user.user_key + '\')">Delete</button>'
}
})
Expand Down Expand Up @@ -135,14 +138,26 @@ <h5 class="card-title">Add last used RFID card as new user: <span id="used-key-a
}

const getColumns = (data) => {
return Object.keys(data[0]).map(key => ({
data: (row) => {
const value = row[key]
return typeof value === 'string' ? value : `<input type="checkbox" ${value?.allowed && "checked"} onchange="updatePermissions('${row.key}', '${value?.device_id}', ${value?.allowed})">`
},
visible: key !== 'key',
title: key.charAt(0).toUpperCase() + key.slice(1)
}))
return Object.keys(data[0]).map(key => {
// Force the email column to be visible and render as plain text
if (key === 'email') {
return {
data: row => row.email || '',
visible: true,
title: 'Email'
}
}
return {
data: (row) => {
const value = row[key]
return typeof value === 'string'
? value
: `<input type="checkbox" ${value?.allowed ? "checked" : ""} onchange="updatePermissions('${row.key}', '${value?.device_id}', ${value?.allowed})">`
},
visible: key !== 'key',
title: key.charAt(0).toUpperCase() + key.slice(1)
}
})
}

async function initializeDataTable() {
Expand Down
Loading