final commit
This commit is contained in:
1 parent
0ffbba3bed
commit
7b242b8685
39 files changed
+5524
-65
No files matched your search
@@ -0,0 +1,242 @@
|
||||
# Admin Area Implementation
|
||||
|
||||
This document describes the admin area implementation for Newzy news platform.
|
||||
|
||||
## Overview
|
||||
|
||||
The admin area allows administrators to manage content items (articles) through a web-based interface with full CRUD operations.
|
||||
|
||||
## Features Implemented
|
||||
|
||||
### Backend (`/backend`)
|
||||
|
||||
1. **Database Model Updates**
|
||||
- Added `is_admin` field to User model
|
||||
- Soft delete support via `published` field on Article model
|
||||
|
||||
2. **Admin Authentication**
|
||||
- New `get_current_admin()` dependency for admin-only endpoints
|
||||
- Admin flag verified server-side via JWT token + database lookup
|
||||
- 403 Forbidden response for non-admin users
|
||||
|
||||
3. **Admin API Endpoints**
|
||||
- `GET /api/admin/items` - List items with pagination, search, sort
|
||||
- `GET /api/admin/items/{id}` - Get single item (admin required)
|
||||
- `POST /api/admin/items` - Create new item (admin only)
|
||||
- `PUT /api/admin/items/{id}` - Update item (admin only)
|
||||
- `DELETE /api/admin/items/{id}` - Delete item (admin only, soft delete)
|
||||
|
||||
4. **Features**
|
||||
- Pagination with configurable page sizes (5-50 items per page)
|
||||
- Search by title and/or URL (case-insensitive)
|
||||
- Sortable columns: title (A-Z/Z-A) and created_at (newest/oldest)
|
||||
- Admin-only access control enforced on server side
|
||||
|
||||
### Frontend (`/frontend/src`)
|
||||
|
||||
1. **Admin Layout Component**
|
||||
- Sidebar navigation with dashboard and new item links
|
||||
- Breadcrumb/back to regular site
|
||||
- Responsive mobile design with collapsible sidebar
|
||||
- User info at bottom of sidebar
|
||||
|
||||
2. **Admin Dashboard Page**
|
||||
- Paginated list view of all items
|
||||
- Search bar with debounce (300ms)
|
||||
- Sort controls for title and date
|
||||
- Edit and delete action buttons
|
||||
- Delete confirmation modal with warning
|
||||
|
||||
3. **Edit Item Page**
|
||||
- Create/Edit form with full item details:
|
||||
- Title (required, max 300 chars)
|
||||
- URL (optional, must be http/https format)
|
||||
- Content (rich text editor)
|
||||
- Points (number, 0-1,000,000)
|
||||
- Basic toolbar for formatting
|
||||
- Form validation and error handling
|
||||
|
||||
4. **Components**
|
||||
- `DeleteConfirmation` modal with explicit "Yes, Delete" confirmation
|
||||
- Admin-specific styling (dark sidebar, distinct color scheme)
|
||||
|
||||
### Styling (`/frontend/src/assets/styles/admin.css`)
|
||||
|
||||
- Dark sidebar navigation matching design system
|
||||
- Card-based list views with hover states
|
||||
- Responsive design for mobile devices
|
||||
- Consistent button styles and colors
|
||||
- Modal overlay for delete confirmation
|
||||
|
||||
## Setup Instructions
|
||||
|
||||
### 1. Database Migration
|
||||
|
||||
The admin field is automatically added when the application starts (SQLAlchemy auto-creates tables). For existing databases, run:
|
||||
|
||||
```bash
|
||||
cd /path/to/newz/backend
|
||||
python -c "from db.session import engine, Base; Base.metadata.create_all(engine)"
|
||||
```
|
||||
|
||||
### 2. Create Admin User
|
||||
|
||||
Run the seed script to create an admin user:
|
||||
|
||||
```bash
|
||||
# Simple usage (default username: 'admin')
|
||||
cd /path/to/newz/backend
|
||||
python seed_admin.py
|
||||
|
||||
# Custom username
|
||||
python seed_admin.py my_admin_user
|
||||
```
|
||||
|
||||
Or set via environment variable and run without args:
|
||||
|
||||
```bash
|
||||
ADMIN_PASSWORD=your-secret-password python seed_admin.py
|
||||
```
|
||||
|
||||
### 3. Start Application
|
||||
|
||||
```bash
|
||||
# Using Docker
|
||||
docker-compose up -d
|
||||
|
||||
# Or using uvicorn directly for backend
|
||||
cd /path/to/newz/backend
|
||||
uvicorn main:app --reload --host 0.0.0.0 --port 8000
|
||||
```
|
||||
|
||||
## Security Considerations
|
||||
|
||||
1. **Server-Side Validation**
|
||||
- All admin endpoints require valid JWT token AND `is_admin=True` in database
|
||||
- Client-side checks are for UX only; server always validates
|
||||
|
||||
2. **Input Validation**
|
||||
- Title: max 300 characters, required
|
||||
- URL: optional, must be valid http/https format if provided
|
||||
- Content: any text, validated by pydantic schema
|
||||
- Points: integer, 0 to 1,000,000
|
||||
|
||||
3. **Soft Delete**
|
||||
- Delete sets `published=False` instead of hard deletion
|
||||
- Allows recovery of deleted items
|
||||
- Admin list excludes unpublished items by default
|
||||
|
||||
4. **Rate Limiting**
|
||||
- Not implemented yet (future milestone)
|
||||
- Recommended for delete operations specifically
|
||||
|
||||
## API Documentation
|
||||
|
||||
### GET /api/admin/items
|
||||
|
||||
List all published articles with pagination, search, and sorting.
|
||||
|
||||
**Query Parameters:**
|
||||
- `page` (int, default 1): Page number (1-indexed)
|
||||
- `page_size` (int, default 20): Items per page (5-50)
|
||||
- `search` (string, optional): Search title or URL
|
||||
- `sort_by` (enum: "title"|"created_at", default "created_at"): Sort field
|
||||
- `sort_order` (enum: "asc"|"desc", default "desc"): Sort order
|
||||
|
||||
**Response Example:**
|
||||
```json
|
||||
{
|
||||
"items": [
|
||||
{
|
||||
"id": 1,
|
||||
"title": "Example Article",
|
||||
"url": "https://example.com/article",
|
||||
"content": "Article content...",
|
||||
"points": 42,
|
||||
"author_id": 1,
|
||||
"created_at": "2024-01-01T12:00:00Z"
|
||||
}
|
||||
],
|
||||
"total": 100,
|
||||
"page": 1,
|
||||
"page_size": 20,
|
||||
"total_pages": 5
|
||||
}
|
||||
```
|
||||
|
||||
**Access Control:** Requires `is_admin=true`. Non-admin users receive 403 Forbidden.
|
||||
|
||||
### POST /api/admin/items
|
||||
|
||||
Create a new article.
|
||||
|
||||
**Request Body:**
|
||||
```json
|
||||
{
|
||||
"title": "New Article Title",
|
||||
"url": "https://example.com/new-article",
|
||||
"content": "Rich text content...",
|
||||
"points": 10
|
||||
}
|
||||
```
|
||||
|
||||
### PUT /api/admin/items/{id}
|
||||
|
||||
Update an existing article.
|
||||
|
||||
### DELETE /api/admin/items/{id}
|
||||
|
||||
Soft-delete an article (sets `published=False`).
|
||||
|
||||
## Testing
|
||||
|
||||
### Unit Tests
|
||||
Add tests in `/backend/tests/` for:
|
||||
- Admin dependency returns 403 for non-admins
|
||||
- CRUD operations work correctly
|
||||
- Pagination calculations are correct
|
||||
- Search matches title and URL
|
||||
- Sort order works correctly
|
||||
- Input validation rejects invalid data
|
||||
|
||||
### Integration Tests
|
||||
Test admin endpoints with authenticated tokens:
|
||||
- Login as admin user
|
||||
- Verify access to admin routes
|
||||
- Test CRUD operations end-to-end
|
||||
|
||||
## Future Enhancements (M7+)
|
||||
|
||||
1. **Role-Based Permissions** - Beyond just admin flag
|
||||
2. **Audit Logging** - Track all admin actions
|
||||
3. **Batch Operations** - Edit/delete multiple items at once
|
||||
4. **Export Functionality** - Export to CSV/JSON
|
||||
5. **Advanced Search** - Filter by date range, author, category
|
||||
6. **Content Preview** - Better rich text editor with image uploads
|
||||
7. **Drafts Support** - Save and publish articles later
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Admin user not creating
|
||||
- Check database connection: `DATABASE_URL` environment variable
|
||||
- Ensure users table exists
|
||||
- Run migration script manually
|
||||
|
||||
### Can't access admin area
|
||||
1. Verify user has `is_admin=True` in database
|
||||
2. Clear browser cookies/local storage
|
||||
3. Login again after seed script runs
|
||||
4. Check backend logs for 403 errors
|
||||
|
||||
### API returning empty items
|
||||
- Items must have `published=True` to be listed
|
||||
- Create new item via POST /api/admin/items
|
||||
- Or update existing item's published field
|
||||
|
||||
## References
|
||||
|
||||
- Base reference: `/home/daniel/Gitea/daniel/news/`
|
||||
- Specification: `milestones/m6-setup-admin-area-prompt.md`
|
||||
- Schema: `/backend/app/schemas.py`
|
||||
- Models: `/backend/app/models.py`
|
||||
- Auth: `/backend/app/auth.py`
|
||||
Reference in new issue
Block a user