# HAVEN GRAZURI INVESTMENT LIMITED- Media & Static Files Fix

## Problem Description

The HAVEN GRAZURI INVESTMENT LIMITEDsystem was experiencing issues with:

1. **Images not loading** in production environment (when `DEBUG = False`)
2. **Client names overlapping** in tables

These issues were occurring because:

- Static files were not being properly served in production mode
- The `.htaccess` file had incorrect rewrite rules for static and media files
- CSS was missing to properly handle client names in tables
- The Django settings were not properly configured for production

## Solution Implemented

We created several scripts to fix these issues:

### 1. `fix_production_images.py`

This is the main fix script that addresses all issues by:

- Temporarily setting `DEBUG = True` in `settings_production.py` for troubleshooting
- Updating `STATIC_ROOT` to use the `staticfiles` directory
- Adding `STATICFILES_DIRS` to include the `static` directory
- Improving `.htaccess` rewrite rules for static and media files
- Adding CSS and JavaScript fixes for client names and images
- Setting proper file permissions
- Collecting static files

### 2. `verify_fix.py`

This script verifies that all fixes have been properly applied by checking:

- Django settings configuration
- File structure and existence
- `.htaccess` configuration
- Template configuration

### 3. `deploy_media_fix.py`

This script is designed for deployment to production and includes:

- Backup of critical files
- Running the fix script
- Running the verification script
- Updating templates
- Creating deployment instructions

## How to Use

### For Local Development

1. Run the fix script:
   ```
   python fix_production_images.py
   ```

2. Verify the fixes:
   ```
   python verify_fix.py
   ```

3. Test the website with `DEBUG = True`

4. Once everything is working, set `DEBUG = False` in `settings_production.py`

### For Production Deployment

1. Upload the fix scripts to the production server

2. Run the deployment script:
   ```
   python deploy_media_fix.py
   ```

3. Restart the web server

4. Test the website

## Technical Details

### Key Changes Made

1. **Settings Configuration**:
   - Changed `STATIC_ROOT` from `static` to `staticfiles`
   - Added `STATICFILES_DIRS = [BASE_DIR / 'static']`
   - Temporarily set `DEBUG = True` for troubleshooting

2. **URL Configuration**:
   - Modified `urls.py` to conditionally serve static files only in development
   - Ensured media files are always served by Django

3. **.htaccess Configuration**:
   - Added improved rewrite rules for static and media files
   - Added fallback mechanism to check both `staticfiles` and `static` directories
   - Added security headers, compression, and caching settings

4. **CSS and JavaScript Fixes**:
   - Added CSS to prevent client names from overlapping
   - Added CSS to ensure images display correctly
   - Added JavaScript to enhance table layout

### File Permissions

The scripts set the following permissions:

- Directories: `755` (rwxr-xr-x)
- Files: `644` (rw-r--r--)

## Troubleshooting

If issues persist after applying the fixes:

1. Check server error logs

2. Verify file permissions:
   ```
   chmod -R 755 media/ static/ staticfiles/
   find media/ static/ staticfiles/ -type f -exec chmod 644 {} \;
   ```

3. Ensure `.htaccess` is being read by Apache:
   - Check that `AllowOverride All` is set in the Apache configuration
   - Add a test rule to `.htaccess` and see if it takes effect

4. Keep `DEBUG = True` temporarily for troubleshooting

5. Run the verification script to check for any issues:
   ```
   python verify_fix.py
   ```

## Rollback Plan

If the fixes cause any issues, you can restore the backup files from the `backups` directory:

```bash
cp backups/settings_production.py.bak branch_system/settings_production.py
cp backups/urls.py.bak branch_system/urls.py
cp backups/wsgi.py.bak branch_system/wsgi.py
cp backups/.htaccess.bak .htaccess
```

Then restart the web server.