Implement comprehensive project time logging feature

Add complete project management system with role-based access control:

**Core Features:**
- Project creation and management for Admins/Supervisors
- Time tracking with optional project selection and notes
- Project-based filtering and reporting in history
- Enhanced export functionality with project data
- Team-specific project assignments

**Database Changes:**
- New Project model with full relationships
- Enhanced TimeEntry model with project_id and notes
- Updated migration scripts with rollback support
- Sample project creation for testing

**User Interface:**
- Project management templates (create, edit, list)
- Enhanced time tracking with project dropdown
- Project filtering in history page
- Updated navigation for role-based access
- Modern styling with hover effects and responsive design

**API Enhancements:**
- Project validation and access control
- Updated arrive endpoint with project support
- Enhanced export functions with project data
- Role-based route protection

**Migration Support:**
- Comprehensive migration scripts (migrate_projects.py)
- Updated main migration script (migrate_db.py)
- Detailed migration documentation
- Rollback functionality for safe deployment

**Role-Based Access:**
- Admins: Full project CRUD operations
- Supervisors: Project creation and management
- Team Leaders: View team hours with projects
- Team Members: Select projects when tracking time

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jens Luedicke
2025-06-29 17:18:10 +02:00
parent 77d26a6063
commit be111a4bed
12 changed files with 1393 additions and 14 deletions

View File

@@ -0,0 +1,127 @@
{% extends "layout.html" %}
{% block content %}
<div class="timetrack-container">
<div class="admin-header">
<h2>Project Management</h2>
<a href="{{ url_for('create_project') }}" class="btn">Create New Project</a>
</div>
{% if projects %}
<div class="projects-table">
<table class="time-history">
<thead>
<tr>
<th>Code</th>
<th>Name</th>
<th>Team</th>
<th>Status</th>
<th>Start Date</th>
<th>End Date</th>
<th>Created By</th>
<th>Time Entries</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for project in projects %}
<tr class="{% if not project.is_active %}inactive-project{% endif %}">
<td><strong>{{ project.code }}</strong></td>
<td>{{ project.name }}</td>
<td>
{% if project.team %}
{{ project.team.name }}
{% else %}
<em>All Teams</em>
{% endif %}
</td>
<td>
<span class="status-badge {% if project.is_active %}active{% else %}inactive{% endif %}">
{{ 'Active' if project.is_active else 'Inactive' }}
</span>
</td>
<td>{{ project.start_date.strftime('%Y-%m-%d') if project.start_date else '-' }}</td>
<td>{{ project.end_date.strftime('%Y-%m-%d') if project.end_date else '-' }}</td>
<td>{{ project.created_by.username }}</td>
<td>{{ project.time_entries|length }}</td>
<td class="actions">
<a href="{{ url_for('edit_project', project_id=project.id) }}" class="btn btn-small">Edit</a>
{% if g.user.role.name == 'ADMIN' and project.time_entries|length == 0 %}
<form method="POST" action="{{ url_for('delete_project', project_id=project.id) }}" style="display: inline;"
onsubmit="return confirm('Are you sure you want to delete this project?')">
<button type="submit" class="btn btn-small btn-danger">Delete</button>
</form>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
<div class="no-data">
<p>No projects found. <a href="{{ url_for('create_project') }}">Create your first project</a>.</p>
</div>
{% endif %}
</div>
<style>
.admin-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 2rem;
}
.projects-table {
overflow-x: auto;
}
.inactive-project {
opacity: 0.6;
}
.status-badge {
padding: 0.25rem 0.5rem;
border-radius: 4px;
font-size: 0.875rem;
font-weight: 500;
}
.status-badge.active {
background-color: #d4edda;
color: #155724;
}
.status-badge.inactive {
background-color: #f8d7da;
color: #721c24;
}
.actions {
white-space: nowrap;
}
.btn-small {
padding: 0.375rem 0.75rem;
font-size: 0.875rem;
margin-right: 0.5rem;
}
.btn-danger {
background-color: #dc3545;
border-color: #dc3545;
}
.btn-danger:hover {
background-color: #c82333;
border-color: #bd2130;
}
.no-data {
text-align: center;
padding: 3rem;
color: #666;
}
</style>
{% endblock %}

View File

@@ -0,0 +1,136 @@
{% extends "layout.html" %}
{% block content %}
<div class="timetrack-container">
<h2>Create New Project</h2>
<form method="POST" action="{{ url_for('create_project') }}" class="project-form">
<div class="form-row">
<div class="form-group">
<label for="name">Project Name *</label>
<input type="text" id="name" name="name" required
value="{{ request.form.name if request.form.name else '' }}"
placeholder="Enter project name">
</div>
<div class="form-group">
<label for="code">Project Code *</label>
<input type="text" id="code" name="code" required
value="{{ request.form.code if request.form.code else '' }}"
placeholder="e.g., PRJ001" maxlength="20" style="text-transform: uppercase;">
</div>
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea id="description" name="description" rows="3"
placeholder="Enter project description">{{ request.form.description if request.form.description else '' }}</textarea>
</div>
<div class="form-row">
<div class="form-group">
<label for="team_id">Assigned Team</label>
<select id="team_id" name="team_id">
<option value="">All Teams</option>
{% for team in teams %}
<option value="{{ team.id }}"
{% if request.form.team_id and request.form.team_id|int == team.id %}selected{% endif %}>
{{ team.name }}
</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-row">
<div class="form-group">
<label for="start_date">Start Date</label>
<input type="date" id="start_date" name="start_date"
value="{{ request.form.start_date if request.form.start_date else '' }}">
</div>
<div class="form-group">
<label for="end_date">End Date</label>
<input type="date" id="end_date" name="end_date"
value="{{ request.form.end_date if request.form.end_date else '' }}">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn">Create Project</button>
<a href="{{ url_for('admin_projects') }}" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
<style>
.project-form {
max-width: 600px;
margin: 0 auto;
}
.form-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
}
.form-group {
margin-bottom: 1.5rem;
}
.form-group label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
color: #333;
}
.form-group input,
.form-group select,
.form-group textarea {
width: 100%;
padding: 0.75rem;
border: 2px solid #e9ecef;
border-radius: 6px;
font-size: 1rem;
transition: border-color 0.2s ease;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
outline: none;
border-color: #4CAF50;
box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.1);
}
.form-actions {
display: flex;
gap: 1rem;
justify-content: flex-end;
margin-top: 2rem;
}
.btn-secondary {
background-color: #6c757d;
border-color: #6c757d;
}
.btn-secondary:hover {
background-color: #5a6268;
border-color: #545b62;
}
#code {
text-transform: uppercase;
}
</style>
<script>
// Auto-uppercase project code
document.getElementById('code').addEventListener('input', function(e) {
e.target.value = e.target.value.toUpperCase();
});
</script>
{% endblock %}

198
templates/edit_project.html Normal file
View File

@@ -0,0 +1,198 @@
{% extends "layout.html" %}
{% block content %}
<div class="timetrack-container">
<h2>Edit Project: {{ project.name }}</h2>
<form method="POST" action="{{ url_for('edit_project', project_id=project.id) }}" class="project-form">
<div class="form-row">
<div class="form-group">
<label for="name">Project Name *</label>
<input type="text" id="name" name="name" required
value="{{ request.form.name if request.form.name else project.name }}"
placeholder="Enter project name">
</div>
<div class="form-group">
<label for="code">Project Code *</label>
<input type="text" id="code" name="code" required
value="{{ request.form.code if request.form.code else project.code }}"
placeholder="e.g., PRJ001" maxlength="20" style="text-transform: uppercase;">
</div>
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea id="description" name="description" rows="3"
placeholder="Enter project description">{{ request.form.description if request.form.description else (project.description or '') }}</textarea>
</div>
<div class="form-row">
<div class="form-group">
<label for="team_id">Assigned Team</label>
<select id="team_id" name="team_id">
<option value="">All Teams</option>
{% for team in teams %}
<option value="{{ team.id }}"
{% if (request.form.team_id and request.form.team_id|int == team.id) or (not request.form.team_id and project.team_id == team.id) %}selected{% endif %}>
{{ team.name }}
</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label class="checkbox-label">
<input type="checkbox" name="is_active"
{% if (request.form.is_active and request.form.is_active == 'on') or (not request.form and project.is_active) %}checked{% endif %}>
Active Project
</label>
</div>
</div>
<div class="form-row">
<div class="form-group">
<label for="start_date">Start Date</label>
<input type="date" id="start_date" name="start_date"
value="{{ request.form.start_date if request.form.start_date else (project.start_date.strftime('%Y-%m-%d') if project.start_date else '') }}">
</div>
<div class="form-group">
<label for="end_date">End Date</label>
<input type="date" id="end_date" name="end_date"
value="{{ request.form.end_date if request.form.end_date else (project.end_date.strftime('%Y-%m-%d') if project.end_date else '') }}">
</div>
</div>
<div class="project-info">
<div class="info-row">
<span class="info-label">Created by:</span>
<span class="info-value">{{ project.created_by.username }}</span>
</div>
<div class="info-row">
<span class="info-label">Created on:</span>
<span class="info-value">{{ project.created_at.strftime('%Y-%m-%d %H:%M') }}</span>
</div>
<div class="info-row">
<span class="info-label">Time entries:</span>
<span class="info-value">{{ project.time_entries|length }}</span>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn">Update Project</button>
<a href="{{ url_for('admin_projects') }}" class="btn btn-secondary">Cancel</a>
</div>
</form>
</div>
<style>
.project-form {
max-width: 600px;
margin: 0 auto;
}
.form-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
}
.form-group {
margin-bottom: 1.5rem;
}
.form-group label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
color: #333;
}
.checkbox-label {
display: flex;
align-items: center;
gap: 0.5rem;
cursor: pointer;
margin-top: 1.5rem;
}
.checkbox-label input[type="checkbox"] {
width: auto;
margin: 0;
}
.form-group input,
.form-group select,
.form-group textarea {
width: 100%;
padding: 0.75rem;
border: 2px solid #e9ecef;
border-radius: 6px;
font-size: 1rem;
transition: border-color 0.2s ease;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
outline: none;
border-color: #4CAF50;
box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.1);
}
.project-info {
background: #f8f9fa;
padding: 1rem;
border-radius: 6px;
margin: 1.5rem 0;
}
.info-row {
display: flex;
justify-content: space-between;
margin-bottom: 0.5rem;
}
.info-row:last-child {
margin-bottom: 0;
}
.info-label {
font-weight: 500;
color: #666;
}
.info-value {
color: #333;
}
.form-actions {
display: flex;
gap: 1rem;
justify-content: flex-end;
margin-top: 2rem;
}
.btn-secondary {
background-color: #6c757d;
border-color: #6c757d;
}
.btn-secondary:hover {
background-color: #5a6268;
border-color: #545b62;
}
#code {
text-transform: uppercase;
}
</style>
<script>
// Auto-uppercase project code
document.getElementById('code').addEventListener('input', function(e) {
e.target.value = e.target.value.toUpperCase();
});
</script>
{% endblock %}

View File

@@ -7,16 +7,40 @@
<a href="{{ url_for('export') }}" class="btn">Export Data</a>
</div>
<!-- Project Filter -->
<div class="filter-section">
<form method="GET" action="{{ url_for('history') }}" class="filter-form">
<div class="form-group">
<label for="project-filter">Filter by Project:</label>
<select id="project-filter" name="project_id" onchange="this.form.submit()">
<option value="">All Projects</option>
{% for project in available_projects %}
<option value="{{ project.id }}"
{% if request.args.get('project_id') and request.args.get('project_id')|int == project.id %}selected{% endif %}>
{{ project.code }} - {{ project.name }}
</option>
{% endfor %}
<option value="none"
{% if request.args.get('project_id') == 'none' %}selected{% endif %}>
No Project Assigned
</option>
</select>
</div>
</form>
</div>
<div class="history-section">
{% if entries %}
<table class="time-history">
<thead>
<tr>
<th>Date</th>
<th>Project</th>
<th>Arrival</th>
<th>Departure</th>
<th>Work Duration</th>
<th>Break Duration</th>
<th>Notes</th>
<th>Actions</th>
</tr>
</thead>
@@ -24,10 +48,25 @@
{% for entry in entries %}
<tr data-entry-id="{{ entry.id }}">
<td>{{ entry.arrival_time.strftime('%Y-%m-%d') }}</td>
<td>
{% if entry.project %}
<span class="project-tag">{{ entry.project.code }}</span>
<small>{{ entry.project.name }}</small>
{% else %}
<em>No project</em>
{% endif %}
</td>
<td>{{ entry.arrival_time.strftime('%H:%M:%S') }}</td>
<td>{{ entry.departure_time.strftime('%H:%M:%S') if entry.departure_time else 'Active' }}</td>
<td>{{ '%d:%02d:%02d'|format(entry.duration//3600, (entry.duration%3600)//60, entry.duration%60) if entry.duration is not none else 'In progress' }}</td>
<td>{{ '%d:%02d:%02d'|format(entry.total_break_duration//3600, (entry.total_break_duration%3600)//60, entry.total_break_duration%60) if entry.total_break_duration is not none else '00:00:00' }}</td>
<td>
{% if entry.notes %}
<span class="notes-preview" title="{{ entry.notes }}">{{ entry.notes[:50] }}{% if entry.notes|length > 50 %}...{% endif %}</span>
{% else %}
<em>-</em>
{% endif %}
</td>
<td>
<button class="edit-entry-btn" data-id="{{ entry.id }}">Edit</button>
<button class="delete-entry-btn" data-id="{{ entry.id }}">Delete</button>
@@ -234,4 +273,70 @@
});
});
</script>
<style>
.filter-section {
background: #f8f9fa;
padding: 1rem;
border-radius: 8px;
margin-bottom: 2rem;
}
.filter-form .form-group {
margin: 0;
}
.filter-form label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
color: #333;
}
.filter-form select {
width: 100%;
max-width: 300px;
padding: 0.5rem;
border: 2px solid #e9ecef;
border-radius: 6px;
font-size: 1rem;
transition: border-color 0.2s ease;
}
.filter-form select:focus {
outline: none;
border-color: #4CAF50;
box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.1);
}
.project-tag {
background: #4CAF50;
color: white;
padding: 0.25rem 0.5rem;
border-radius: 4px;
font-size: 0.8rem;
font-weight: 500;
margin-right: 0.5rem;
}
.project-tag + small {
color: #666;
font-size: 0.85rem;
}
.notes-preview {
color: #666;
font-size: 0.9rem;
cursor: help;
}
.time-history td {
vertical-align: middle;
}
.time-history .project-tag + small {
display: block;
margin-top: 0.25rem;
}
</style>
{% endblock %}

View File

@@ -20,6 +20,9 @@ Please <a href="{{ url_for('login') }}">login</a> or <a href="{{ url_for('regist
<div class="active-timer">
<h3>Currently Working</h3>
<p>Started at: {{ active_entry.arrival_time.strftime('%Y-%m-%d %H:%M:%S') }}</p>
{% if active_entry.project %}
<p class="project-info">Project: <strong>{{ active_entry.project.code }} - {{ active_entry.project.name }}</strong></p>
{% endif %}
<div id="timer"
data-start="{{ active_entry.arrival_time.timestamp() }}"
data-breaks="{{ active_entry.total_break_duration }}"
@@ -43,7 +46,22 @@ Please <a href="{{ url_for('login') }}">login</a> or <a href="{{ url_for('regist
{% else %}
<div class="inactive-timer">
<h3>Not Currently Working</h3>
<button id="arrive-btn" class="arrive-btn">Arrive</button>
<div class="start-work-form">
<div class="form-group">
<label for="project-select">Select Project (Optional):</label>
<select id="project-select" name="project_id">
<option value="">No specific project</option>
{% for project in available_projects %}
<option value="{{ project.id }}">{{ project.code }} - {{ project.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="work-notes">Notes (Optional):</label>
<textarea id="work-notes" name="notes" rows="2" placeholder="What are you working on?"></textarea>
</div>
<button id="arrive-btn" class="arrive-btn">Arrive</button>
</div>
</div>
{% endif %}
</div>
@@ -55,6 +73,7 @@ Please <a href="{{ url_for('login') }}">login</a> or <a href="{{ url_for('regist
<thead>
<tr>
<th>Date</th>
<th>Project</th>
<th>Arrival</th>
<th>Departure</th>
<th>Work Duration</th>
@@ -66,6 +85,14 @@ Please <a href="{{ url_for('login') }}">login</a> or <a href="{{ url_for('regist
{% for entry in history %}
<tr data-entry-id="{{ entry.id }}">
<td>{{ entry.arrival_time.strftime('%Y-%m-%d') }}</td>
<td>
{% if entry.project %}
<span class="project-tag">{{ entry.project.code }}</span>
<small>{{ entry.project.name }}</small>
{% else %}
<em>No project</em>
{% endif %}
</td>
<td>{{ entry.arrival_time.strftime('%H:%M:%S') }}</td>
<td>{{ entry.departure_time.strftime('%H:%M:%S') if entry.departure_time else 'Active' }}</td>
<td>{{ '%d:%02d:%02d'|format(entry.duration//3600, (entry.duration%3600)//60, entry.duration%60) if entry.duration is not none else 'In progress' }}</td>
@@ -310,4 +337,71 @@ Please <a href="{{ url_for('login') }}">login</a> or <a href="{{ url_for('regist
});
</script>
<style>
.start-work-form {
background: #f8f9fa;
padding: 1.5rem;
border-radius: 8px;
margin-bottom: 1rem;
}
.start-work-form .form-group {
margin-bottom: 1rem;
}
.start-work-form label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
color: #333;
}
.start-work-form select,
.start-work-form textarea {
width: 100%;
padding: 0.75rem;
border: 2px solid #e9ecef;
border-radius: 6px;
font-size: 1rem;
transition: border-color 0.2s ease;
}
.start-work-form select:focus,
.start-work-form textarea:focus {
outline: none;
border-color: #4CAF50;
box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.1);
}
.project-info {
color: #4CAF50;
font-size: 0.9rem;
margin-top: 0.5rem;
}
.project-tag {
background: #4CAF50;
color: white;
padding: 0.25rem 0.5rem;
border-radius: 4px;
font-size: 0.8rem;
font-weight: 500;
margin-right: 0.5rem;
}
.project-tag + small {
color: #666;
font-size: 0.85rem;
}
.time-history td {
vertical-align: middle;
}
.time-history .project-tag + small {
display: block;
margin-top: 0.25rem;
}
</style>
{% endblock %}

View File

@@ -23,6 +23,14 @@
<li><a href="{{ url_for('profile') }}">Profile</a></li>
<li><a href="{{ url_for('config') }}">Config</a></li>
<li><a href="{{ url_for('dashboard') }}">Dashboard</a></li>
<li><a href="{{ url_for('admin_dashboard') }}">Admin Dashboard</a></li>
<li><a href="{{ url_for('admin_users') }}">Manage Users</a></li>
<li><a href="{{ url_for('admin_teams') }}">Manage Teams</a></li>
<li><a href="{{ url_for('admin_projects') }}">Manage Projects</a></li>
<li><a href="{{ url_for('admin_settings') }}">System Settings</a></li>
{% if g.user.team_id %}
<li><a href="{{ url_for('team_hours') }}">Team Hours</a></li>
{% endif %}
<li><a href="{{ url_for('logout') }}">Logout</a></li>
</ul>
</li>
@@ -34,6 +42,9 @@
<li><a href="{{ url_for('profile') }}">Profile</a></li>
<li><a href="{{ url_for('config') }}">Config</a></li>
<li><a href="{{ url_for('dashboard') }}">Dashboard</a></li>
{% if g.user.role == Role.SUPERVISOR %}
<li><a href="{{ url_for('admin_projects') }}">Manage Projects</a></li>
{% endif %}
{% if g.user.team_id %}
<li><a href="{{ url_for('team_hours') }}">Team Hours</a></li>
{% endif %}