Skip to content

Commit

Permalink
Add team number to user and fix things
Browse files Browse the repository at this point in the history
  • Loading branch information
cherriae committed Dec 19, 2024
1 parent de55d4a commit bf58d14
Show file tree
Hide file tree
Showing 7 changed files with 536 additions and 130 deletions.
3 changes: 1 addition & 2 deletions app/auth/auth_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def ensure_connected(self):
@with_mongodb_retry(retries=3, delay=2)
async def create_user(
self, email, username, password,
role="user"):
):
"""Create a new user with retry mechanism"""
self.ensure_connected()
try:
Expand All @@ -111,7 +111,6 @@ async def create_user(
"username": username,
"team_number": None,
"password_hash": generate_password_hash(password),
"role": role,
"created_at": datetime.now(timezone.utc),
"last_login": None,
}
Expand Down
18 changes: 10 additions & 8 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def __init__(self, data):
self.email = data.get("email")
self.teamNumber = data.get("teamNumber")
self.password_hash = data.get("password_hash")
self.role = data.get("role", "user")
self.last_login = data.get("last_login")
self.created_at = data.get("created_at")

Expand Down Expand Up @@ -52,11 +51,15 @@ def to_dict(self):
"username": self.username,
"teamNumber": self.teamNumber,
"password_hash": self.password_hash,
"role": self.role,
"last_login": self.last_login,
"created_at": self.created_at,
}

def update_team_number(self, team_number):
"""Update the user's team number"""
self.teamNumber = team_number
return self


class TeamData:
def __init__(self, data):
Expand All @@ -78,7 +81,6 @@ def __init__(self, data):
"username": scouter_data.get("username", "Unknown"),
"team_number": scouter_data.get("team_number"),
"email": scouter_data.get("email"),
"role": scouter_data.get("role", "user"),
}

@property
Expand Down Expand Up @@ -206,13 +208,13 @@ def to_dict(self):
"logo_id": str(self.logo_id) if self.logo_id else None,
}

def is_admin(self, user_id: str) -> bool:
"""Check if a user is an admin or owner of the team"""
return str(user_id) in self.admins or self.is_owner(user_id)

def is_owner(self, user_id: str) -> bool:
"""Check if a user is the owner of the team"""
return str(self.owner_id) == user_id

def is_admin(self, user_id: str) -> bool:
"""Check if a user is an admin of the team"""
return user_id in self.admins or self.is_owner(user_id)
return str(self.owner_id) == str(user_id)

@property
def id(self):
Expand Down
58 changes: 23 additions & 35 deletions app/static/js/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
// Add this at the top of your file
const offlineStorage = {
getPendingRequests() {
const requests = localStorage.getItem('pendingRequests');
return requests ? JSON.parse(requests) : [];
},

savePendingRequest(request) {
const requests = this.getPendingRequests();
request.id = Date.now().toString();
requests.push(request);
localStorage.setItem('pendingRequests', JSON.stringify(requests));
return request.id;
},

removePendingRequest(requestId) {
const requests = this.getPendingRequests();
const filtered = requests.filter(r => r.id !== requestId);
localStorage.setItem('pendingRequests', JSON.stringify(filtered));
}
};

// Register service worker
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
Expand All @@ -12,25 +34,6 @@ if ('serviceWorker' in navigator) {
}


// Handle offline status
let syncTimeout;
function updateOfflineStatus() {
const offlineAlert = document.getElementById('offlineAlert');
const syncAlert = document.getElementById('syncAlert');

if (!navigator.onLine) {
offlineAlert.classList.remove('hidden');
syncAlert.classList.add('hidden');
} else {
offlineAlert.classList.add('hidden');
// Debounce the sync call
clearTimeout(syncTimeout);
syncTimeout = setTimeout(() => {
syncPendingScoutingData();
}, 1000); // Wait 1 second before syncing
}
}


// Initialize
document.addEventListener('DOMContentLoaded', () => {
Expand All @@ -42,19 +45,4 @@ document.addEventListener('DOMContentLoaded', () => {
window.addEventListener('online', updateOfflineStatus);
window.addEventListener('offline', updateOfflineStatus);
updateOfflineStatus();
});


// In the syncPendingScoutingData function, update the fetch response handling:
if (response.ok) {
const result = await response.json();
await offlineStorage.removePendingRequest(request.id);
console.log(`Successfully synced request ${request.id}`);
syncSuccessful = true;

// If the server provides a redirect URL, use it
if (result.redirect) {
window.location.href = result.redirect;
return; // Stop processing other requests
}
}
});
Loading

0 comments on commit bf58d14

Please sign in to comment.