Files
thrilltrack-explorer/django/apps/entities/services/company_submission.py
pacnpal 2884bc23ce Implement entity submission services for ThrillWiki
- Added BaseEntitySubmissionService as an abstract base for entity submissions.
- Created specific submission services for entities: Park, Ride, Company, RideModel.
- Implemented create, update, and delete functionalities with moderation workflow.
- Enhanced logging and validation for required fields.
- Addressed foreign key handling and special field processing for each entity type.
- Noted existing issues with JSONField usage in Company submissions.
2025-11-08 22:23:41 -05:00

87 lines
3.0 KiB
Python

"""
Company submission service for ThrillWiki.
Handles Company entity creation and updates through the Sacred Pipeline.
"""
import logging
from django.core.exceptions import ValidationError
from apps.entities.models import Company
from apps.entities.services import BaseEntitySubmissionService
logger = logging.getLogger(__name__)
class CompanySubmissionService(BaseEntitySubmissionService):
"""
Service for creating Company submissions through the Sacred Pipeline.
Companies represent manufacturers, operators, designers, and other entities
in the amusement industry.
Required fields:
- name: Company name
Known Issue:
- company_types is currently a JSONField but should be M2M relationship
TODO: Convert company_types from JSONField to Many-to-Many relationship
This violates the project rule: "NEVER use JSON/JSONB in SQL"
Example:
from apps.entities.services.company_submission import CompanySubmissionService
submission, company = CompanySubmissionService.create_entity_submission(
user=request.user,
data={
'name': 'Bolliger & Mabillard',
'company_types': ['manufacturer', 'designer'],
'description': 'Swiss roller coaster manufacturer...',
'website': 'https://www.bolliger-mabillard.com',
},
source='api'
)
"""
entity_model = Company
entity_type_name = 'Company'
required_fields = ['name']
@classmethod
def create_entity_submission(cls, user, data, **kwargs):
"""
Create a Company submission.
Note: The company_types field currently uses JSONField which violates
project standards. This should be converted to a proper M2M relationship.
Args:
user: User creating the company
data: Company field data (must include name)
**kwargs: Additional metadata (source, ip_address, user_agent)
Returns:
tuple: (ContentSubmission, Company or None)
"""
# TODO: Remove this warning once company_types is converted to M2M
if 'company_types' in data:
logger.warning(
"Company.company_types uses JSONField which violates project rules. "
"This should be converted to Many-to-Many relationship."
)
# Validate and normalize location FK if provided
location = data.get('location')
if location and isinstance(location, str):
try:
from apps.core.models import Locality
location = Locality.objects.get(id=location)
data['location'] = location
except:
raise ValidationError(f"Location not found: {location}")
# Create submission through base class
submission, company = super().create_entity_submission(user, data, **kwargs)
return submission, company