RectiFlex / BlockFix

0 stars 0 forks source link

Features #1

Closed RectiFlex closed 1 week ago

RectiFlex commented 1 week ago

What Hey @autopilot implement the following features Work Order Management Automatic work order generation from maintenance requests. Status tracking (Pending, In Progress, Completed). Task Scheduling Calendar view and scheduling for tasks with deadlines. Task assignment to personnel. Task Tracking Real-time status updates with progress indicators. Role-Based User Permissions Custom access for Admins, Maintenance Managers, Supervisors, and Subcontractors.

codeautopilot[bot] commented 1 week ago

Potential solution

To implement the requested features, we need to enhance the existing system to support work order management, task scheduling, task tracking, and role-based user permissions. This involves updating the backend logic, database schema, and frontend templates to accommodate these new functionalities. The solution will involve creating new functions, updating models, generating migration scripts, and modifying HTML templates to provide a comprehensive user interface for managing tasks and work orders.

How to implement

  1. Backend Logic (utils.py):

    • Enhance the generate_work_order function to handle automatic work order generation based on maintenance requests.
    • Implement a schedule_task function to assign tasks to personnel and set deadlines.
    • Create an update_status function to handle real-time status updates and notifications.
    def schedule_task(task_id, user_id, deadline):
       # Logic to assign a task to a user and set a deadline
       pass
    
    def update_status(entity_id, new_status):
       # Logic to update the status of a work order or task
       # Send a notification about the status change
       pass
  2. Database Schema (migrations/versions/):

    • Update models in models.py to include fields for status tracking, task scheduling, and role-based permissions.
    • Use Alembic to generate migration scripts to update the database schema.
    alembic revision --autogenerate -m "Add fields for status tracking, task scheduling, and role-based permissions"
    alembic upgrade head
  3. Frontend Templates (templates/):

    • dashboard.html: Add charts for work order and task statuses using Chart.js and integrate a calendar view for task scheduling using FullCalendar.
    • workorders.html: Display work order statuses, allow task assignments, and integrate a calendar view.
    • tasks.html: Display task statuses, progress indicators, and allow task assignments to personnel.
    <div class="progress mb-3">
       <div class="progress-bar" role="progressbar" style="width: {{ task.progress }}%;" aria-valuenow="{{ task.progress }}" aria-valuemin="0" aria-valuemax="100">{{ task.progress }}%</div>
    </div>
  4. Routes (routes.py):

    • Ensure existing routes for work order creation and task status updates are robust.
    • Add a new route for task scheduling.
    • Implement role-based access control using decorators to restrict access based on user roles.
    @app.route('/tasks/schedule', methods=['POST'])
    @login_required
    def schedule_task():
       # Logic to schedule a task
       pass
    
    def role_required(role):
       def wrapper(fn):
           @wraps(fn)
           def decorated_view(*args, **kwargs):
               if current_user.role != role:
                   flash('You do not have permission to access this page.', 'danger')
                   return redirect(url_for('index'))
               return fn(*args, **kwargs)
           return decorated_view
       return wrapper
  5. Models (models.py):

    • Update User, WorkOrder, and Task models to include necessary fields for role-based permissions, status tracking, and task scheduling.
    class User(UserMixin, db.Model):
       role = db.Column(Enum('Admin', 'Maintenance Manager', 'Supervisor', 'Subcontractor', 'User'), default='User')
    
    class WorkOrder(db.Model):
       status = db.Column(Enum('Pending', 'In Progress', 'Completed'), default='Pending')
    
    class Task(db.Model):
       status = db.Column(Enum('Not Started', 'In Progress', 'Completed'), default='Not Started')
       assigned_to = db.Column(db.Integer, db.ForeignKey('user.id'))

By following these steps, the application will be enhanced to support the requested features, providing a comprehensive system for managing work orders, tasks, and user permissions.

Click here to create a Pull Request with the proposed solution

Files used for this task:

Changes on utils.py To address the task for the `utils.py` file, you need to implement functions that handle automatic work order generation, task scheduling, and sending real-time status updates. Here's a practical proposal for each requirement: 1. **Automatic Work Order Generation**: - The existing `generate_work_order` function already creates a work order based on a maintenance log. Ensure this function is robust and handles various scenarios, such as different priority levels and user assignments. 2. **Task Scheduling**: - Implement a function to schedule tasks. This could involve setting deadlines and assigning tasks to personnel. You might need to interact with a calendar API or library to manage scheduling. - Example function signature: ```python def schedule_task(task_id, user_id, deadline): # Logic to assign a task to a user and set a deadline pass ``` 3. **Real-Time Status Updates**: - Utilize the existing `send_notification` function to send real-time updates about the status of work orders and tasks. Ensure that updates are sent whenever there is a change in status. - Consider adding a function to update the status of a task or work order and trigger a notification: ```python def update_status(entity_id, new_status): # Logic to update the status of a work order or task # Send a notification about the status change pass ``` By implementing these functions, you will enhance the utility module to support the new features requested in the issue. Make sure to test these functions thoroughly to ensure they integrate well with the rest of the application.
Changes on migrations/versions/ To address the task for the `migrations/versions/` directory, you need to generate new migration scripts that will update the database schema to include the necessary fields for status tracking, task scheduling, and role-based permissions. Here's a practical proposal to achieve this: 1. **Define the Changes in Models**: Ensure that the models in `models.py` have been updated to include the new fields for work order status, task scheduling, and user roles. This is crucial as the migration scripts will be generated based on these model definitions. 2. **Generate Migration Scripts**: - Use a migration tool like Alembic (commonly used with SQLAlchemy in Python) to generate the migration scripts. - Run the command to autogenerate the migration script. For Alembic, this would typically be: ```bash alembic revision --autogenerate -m "Add fields for status tracking, task scheduling, and role-based permissions" ``` - This command will create a new migration script in the `migrations/versions/` directory with the necessary changes to the database schema. 3. **Review and Edit the Migration Script**: - Open the newly generated migration script and review the changes to ensure they accurately reflect the updates made in the models. - Make any necessary adjustments to the script to handle complex changes or data migrations that might not be automatically detected. 4. **Apply the Migration**: - Once the migration script is ready, apply it to the database to update the schema. For Alembic, this would be done using: ```bash alembic upgrade head ``` By following these steps, you will create and apply the necessary migration scripts to support the new features related to work order management, task scheduling, and role-based permissions.
Changes on templates/dashboard.html To update the `dashboard.html` template to include charts or views for work order and task statuses, and integrate a calendar view for task scheduling, follow these steps: 1. **Add Charts for Work Order and Task Statuses:** - Ensure that the data for task and work order statuses is passed to the template from the backend. - Use a JavaScript charting library like Chart.js to render the charts. The existing placeholders (`taskChart` and `workOrderChart`) can be utilized for this purpose. - Update the JavaScript in `dashboard.js` to fetch and display the status distribution data in these charts. 2. **Integrate a Calendar View for Task Scheduling:** - Use a library like FullCalendar to integrate a calendar view. - Add a new section in the HTML to display the calendar. This can be done by adding a new card similar to the existing ones. - Ensure that the calendar is initialized and populated with task scheduling data using JavaScript. 3. **Modify the HTML Structure:** - Add a new card for the calendar view within the existing layout. For example: ```html
Task Scheduling Calendar
``` 4. **Update JavaScript:** - In `dashboard.js`, initialize the FullCalendar instance and configure it to display tasks with their deadlines. - Ensure that the calendar is interactive, allowing users to view task details and deadlines. 5. **Ensure Data Availability:** - Verify that the necessary data for charts and calendar is available in the backend and correctly passed to the template. By following these steps, the dashboard will be enhanced to provide a comprehensive view of work order and task statuses, along with a calendar for task scheduling.
Changes on routes.py To implement the requested features in the `routes.py` file, you will need to add or modify several routes to handle automatic work order generation, task scheduling, status updates, and role-based access control. Here's a practical proposal for each feature: 1. **Automatic Work Order Generation**: - Ensure the existing route for creating work orders (`/workorders/create`) is capable of handling automatic generation. This might involve integrating logic from `utils.py` to automatically populate fields based on maintenance requests. 2. **Task Scheduling**: - Add a new route to handle task scheduling, which could involve creating a new task or updating an existing one with a scheduled date and time. This might look like: ```python @app.route('/tasks/schedule', methods=['POST']) @login_required def schedule_task(): # Logic to schedule a task pass ``` 3. **Status Updates**: - You already have an API endpoint for updating task status (`/api/update_task_status`). Ensure this is robust and can handle real-time updates, possibly by integrating WebSocket or similar technology for real-time communication. 4. **Role-Based User Permissions**: - Modify existing routes to check user roles before allowing access to certain actions. For example, you can use a decorator to enforce role-based access: ```python def role_required(role): def wrapper(fn): @wraps(fn) def decorated_view(*args, **kwargs): if current_user.role != role: flash('You do not have permission to access this page.', 'danger') return redirect(url_for('index')) return fn(*args, **kwargs) return decorated_view return wrapper ``` - Apply this decorator to routes that require specific roles: ```python @app.route('/admin/dashboard') @login_required @role_required('admin') def admin_dashboard(): # Admin-specific logic pass ``` By implementing these changes, you will enhance the functionality of the application to support the requested features. Make sure to test each route thoroughly to ensure they work as expected and handle edge cases appropriately.
Changes on templates/workorders.html To modify the `workorders.html` template to meet the requirements outlined in the task, you should focus on the following updates: 1. **Display Work Order Statuses:** - Ensure that the current implementation already displays the status of each work order. This is done with the line `

Status: {{ order.status }}

`. No changes are needed here unless you want to enhance the display (e.g., using badges or color coding). 2. **Allow Task Assignments:** - Add a dropdown or a form element within each work order card to assign tasks to personnel. This could be a select input populated with user names or roles. - Example: ```html
``` 3. **Integrate a Calendar View for Scheduling:** - Integrate a calendar library (e.g., FullCalendar) to display and manage task schedules. This will likely require additional JavaScript and CSS. - You can add a calendar section below the work orders list: ```html
``` - Ensure to include the necessary scripts and styles for the calendar in the `scripts` block: ```html {% block scripts %} {% endblock %} ``` By implementing these changes, the `workorders.html` template will be enhanced to support task assignments and scheduling, providing a more comprehensive view and management of work orders.
Changes on templates/tasks.html To modify the `templates/tasks.html` file to display task statuses, progress indicators, and allow task assignments to personnel, you can follow these steps: 1. **Display Task Statuses and Progress Indicators:** - Ensure that each task card displays the current status of the task. This is already implemented with the `

Status: {{ task.status }}

` line. - Add a progress indicator to visually represent the task's progress. You can use a progress bar for this purpose. 2. **Allow Task Assignments to Personnel:** - Add a dropdown or a selection input within each task card to assign tasks to personnel. This will require a list of personnel to be passed to the template. Here's how you can update the template: ```html {% extends "base.html" %} {% block title %}Tasks and Assigned Work Orders - Rectiflex Maintenance{% endblock %} {% block content %}

Tasks and Assigned Work Orders

Tasks

{% for task in tasks %}
{{ task.title }}

{{ task.description }}

Status: {{ task.status }}

Due Date: {{ task.due_date.strftime('%Y-%m-%d') }}

{{ task.progress }}%
{% endfor %}

Assigned Work Orders

{% for work_order in assigned_work_orders %}
{{ work_order.title }}

{{ work_order.description }}

Status: {{ work_order.status }}

Priority: {{ work_order.priority }}

Due Date: {{ work_order.due_date.strftime('%Y-%m-%d') }}

View Details
{% endfor %}
{% endblock %} {% block scripts %} {% endblock %} ``` ### Additional Considerations: - Ensure that the `tasks` and `personnel` data are passed to the template from the backend. - Implement JavaScript in `tasks.js` to handle the status change and task assignment actions. - Update the backend to handle task assignment and status updates when these actions are performed on the frontend.
Changes on models.py To address the task of adding or updating models for `WorkOrder`, `Task`, and `User` to include fields for status tracking, task scheduling, and role-based permissions, you can make the following changes to the `models.py` file: 1. **Enhance the `User` Model for Role-Based Permissions:** - Update the `role` field to include more specific roles such as 'Admin', 'Maintenance Manager', 'Supervisor', and 'Subcontractor'. - Consider using an Enum for role management to ensure consistency. 2. **Update the `WorkOrder` Model:** - Ensure the `status` field is comprehensive enough to cover all necessary states (e.g., 'Pending', 'In Progress', 'Completed'). - Add any additional fields needed for task scheduling, such as `scheduled_date` if different from `due_date`. 3. **Update the `Task` Model:** - Ensure the `status` field is comprehensive and aligns with the workflow (e.g., 'Not Started', 'In Progress', 'Completed'). - Add fields for task scheduling, such as `assigned_to` for task assignment to personnel. Here is a proposed update to the `models.py` file: ```python from app import db from flask_login import UserMixin from werkzeug.security import generate_password_hash, check_password_hash from sqlalchemy import Enum class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password_hash = db.Column(db.String(256)) role = db.Column(Enum('Admin', 'Maintenance Manager', 'Supervisor', 'Subcontractor', 'User'), default='User') def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) class MaintenanceLog(db.Model): id = db.Column(db.Integer, primary_key=True) date = db.Column(db.DateTime, default=db.func.current_timestamp()) lot = db.Column(db.String(64)) details = db.Column(db.Text) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) class WorkOrder(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(120), nullable=False) description = db.Column(db.Text) status = db.Column(Enum('Pending', 'In Progress', 'Completed'), default='Pending') priority = db.Column(db.String(20), default='Normal') created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) due_date = db.Column(db.DateTime, nullable=True) assigned_to = db.Column(db.Integer, db.ForeignKey('user.id')) created_by = db.Column(db.Integer, db.ForeignKey('user.id')) maintenance_log_id = db.Column(db.Integer, db.ForeignKey('maintenance_log.id')) task = db.Column(db.Text) user = db.relationship('User', foreign_keys=[assigned_to], backref='assigned_work_orders') creator = db.relationship('User', foreign_keys=[created_by], backref='created_work_orders') maintenance_log = db.relationship('MaintenanceLog', backref='work_orders') class Task(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(120)) description = db.Column(db.Text) status = db.Column(Enum('Not Started', 'In Progress', 'Completed'), default='Not Started') due_date = db.Column(db.DateTime) created_at = db.Column(db.DateTime, default=db.func.current_timestamp()) updated_at = db.Column(db.DateTime, default=db.func.current_timestamp(), onupdate=db.func.current_timestamp()) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) work_order_id = db.Column(db.Integer, db.ForeignKey('work_order.id')) assigned_to = db.Column(db.Integer, db.ForeignKey('user.id')) work_order = db.relationship('WorkOrder', backref='tasks') assignee = db.relationship('User', foreign_keys=[assigned_to], backref='assigned_tasks') ``` These changes will help in implementing the requested features by ensuring that the models support status tracking, task scheduling, and role-based permissions.