mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-22 22:31:09 -05:00
Add autocomplete functionality for parks: implement BaseAutocomplete class and integrate with forms
This commit is contained in:
63
memory-bank/features/autocomplete/base.md
Normal file
63
memory-bank/features/autocomplete/base.md
Normal file
@@ -0,0 +1,63 @@
|
||||
# Base Autocomplete Implementation
|
||||
|
||||
The project uses `django-htmx-autocomplete` with a custom base implementation to ensure consistent behavior across all autocomplete widgets.
|
||||
|
||||
## BaseAutocomplete Class
|
||||
|
||||
Located in `core/forms.py`, the `BaseAutocomplete` class provides project-wide defaults and standardization:
|
||||
|
||||
```python
|
||||
from core.forms import BaseAutocomplete
|
||||
|
||||
class MyModelAutocomplete(BaseAutocomplete):
|
||||
model = MyModel
|
||||
search_attrs = ['name', 'description']
|
||||
```
|
||||
|
||||
### Features
|
||||
|
||||
- **Authentication Enforcement**: Requires user authentication by default
|
||||
- Controlled via `AUTOCOMPLETE_BLOCK_UNAUTHENTICATED` setting
|
||||
- Override `auth_check()` for custom auth logic
|
||||
|
||||
- **Search Configuration**
|
||||
- `minimum_search_length = 2` - More responsive than default 3
|
||||
- `max_results = 10` - Optimized for performance
|
||||
|
||||
- **Internationalization**
|
||||
- All text strings use Django's translation system
|
||||
- Customizable messages through class attributes
|
||||
|
||||
### Usage Guidelines
|
||||
|
||||
1. Always extend `BaseAutocomplete` instead of using `autocomplete.Autocomplete` directly
|
||||
2. Configure search_attrs based on your model's indexed fields
|
||||
3. Use the AutocompleteWidget with proper options:
|
||||
|
||||
```python
|
||||
class MyForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = MyModel
|
||||
fields = ['related_field']
|
||||
widgets = {
|
||||
'related_field': AutocompleteWidget(
|
||||
ac_class=MyModelAutocomplete,
|
||||
options={
|
||||
"multiselect": True, # For M2M fields
|
||||
"placeholder": "Custom placeholder..." # Optional
|
||||
}
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
### Performance Considerations
|
||||
|
||||
- Keep `search_attrs` minimal and indexed
|
||||
- Use `select_related`/`prefetch_related` in custom querysets
|
||||
- Consider caching for frequently used results
|
||||
|
||||
### Security Notes
|
||||
|
||||
- Authentication required by default
|
||||
- Implements proper CSRF protection via HTMX
|
||||
- Rate limiting should be implemented at the web server level
|
||||
105
memory-bank/features/parks/search.md
Normal file
105
memory-bank/features/parks/search.md
Normal file
@@ -0,0 +1,105 @@
|
||||
# Park Search Implementation
|
||||
|
||||
## Architecture
|
||||
|
||||
The park search functionality uses a combination of:
|
||||
- BaseAutocomplete for search suggestions
|
||||
- django-htmx for async updates
|
||||
- Django filters for advanced filtering
|
||||
|
||||
### Components
|
||||
|
||||
1. **Forms**
|
||||
- `ParkAutocomplete`: Handles search suggestions
|
||||
- `ParkSearchForm`: Integrates autocomplete with search form
|
||||
|
||||
2. **Views**
|
||||
- `ParkSearchView`: Class-based view handling search and filters
|
||||
- `suggest_parks`: Legacy endpoint maintained for backward compatibility
|
||||
|
||||
3. **Templates**
|
||||
- Simplified search UI using autocomplete widget
|
||||
- Integrated loading indicators
|
||||
- Filter form for additional search criteria
|
||||
|
||||
## Implementation Details
|
||||
|
||||
### Search Form
|
||||
```python
|
||||
class ParkSearchForm(forms.Form):
|
||||
park = forms.ModelChoiceField(
|
||||
queryset=Park.objects.all(),
|
||||
required=False,
|
||||
widget=AutocompleteWidget(
|
||||
ac_class=ParkAutocomplete,
|
||||
attrs={
|
||||
'class': 'w-full border-gray-300 rounded-lg form-input dark:border-gray-600 dark:bg-gray-700 dark:text-white',
|
||||
'placeholder': 'Search parks...'
|
||||
}
|
||||
)
|
||||
)
|
||||
```
|
||||
|
||||
### Autocomplete
|
||||
```python
|
||||
class ParkAutocomplete(BaseAutocomplete):
|
||||
model = Park
|
||||
search_attrs = ['name']
|
||||
|
||||
def get_search_results(self, search):
|
||||
return (get_base_park_queryset()
|
||||
.filter(name__icontains=search)
|
||||
.select_related('owner')
|
||||
.order_by('name'))
|
||||
```
|
||||
|
||||
### View Integration
|
||||
```python
|
||||
class ParkSearchView(TemplateView):
|
||||
template_name = "parks/park_list.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['search_form'] = ParkSearchForm(self.request.GET)
|
||||
# ... filter handling ...
|
||||
return context
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
1. **Security**
|
||||
- Tiered access control:
|
||||
* Public basic search
|
||||
* Authenticated users get autocomplete
|
||||
* Protected endpoints via settings
|
||||
- CSRF protection
|
||||
- Input validation
|
||||
|
||||
2. **Real-time Search**
|
||||
- Debounced input handling
|
||||
- Instant results display
|
||||
- Loading indicators
|
||||
|
||||
3. **Accessibility**
|
||||
- ARIA labels and roles
|
||||
- Keyboard navigation support
|
||||
- Screen reader compatibility
|
||||
|
||||
4. **Integration**
|
||||
- Works with existing filter system
|
||||
- Maintains view mode selection
|
||||
- Preserves URL state
|
||||
|
||||
## Performance Considerations
|
||||
|
||||
- Prefetch related owner data
|
||||
- Uses base queryset optimizations
|
||||
- Debounced search requests
|
||||
- Proper index usage on name field
|
||||
|
||||
## Future Improvements
|
||||
|
||||
- Consider adding full-text search
|
||||
- Implement result caching
|
||||
- Add geographic search capabilities
|
||||
- Enhance filter integration
|
||||
Reference in New Issue
Block a user