Spaces:
Running
Running
File size: 4,927 Bytes
3ca9355 3f85e49 3ca9355 9588aa4 770e172 3ca9355 9588aa4 770e172 3ca9355 0368899 fa9cf94 0368899 fa9cf94 3ca9355 16bcbad 0368899 3ca9355 fa9cf94 0368899 3ca9355 16bcbad 0368899 3ca9355 fa9cf94 0368899 3ca9355 16bcbad 0368899 3ca9355 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
"""Data models for Mi Pesca RD application."""
from dataclasses import dataclass, field
from datetime import datetime
from typing import List, Optional, Literal
@dataclass
class ClosedSeason:
"""Closed season information for a species."""
start: str # MM-DD format
end: str # MM-DD format
description: str
@dataclass
class Species:
"""Species data model."""
id: str
commonName: str
scientificName: str
category: Literal['Peces', 'Crustáceos', 'Moluscos', 'Fondo', 'Pelágico', 'Invertebrado']
imageUrl: str
protected: bool
closedSeason: Optional[ClosedSeason] = None
def to_dict(self):
"""Convert to dictionary for JSON serialization."""
data = {
'id': self.id,
'commonName': self.commonName,
'scientificName': self.scientificName,
'category': self.category,
'imageUrl': self.imageUrl,
'protected': self.protected
}
if self.closedSeason:
data['closedSeason'] = {
'start': self.closedSeason.start,
'end': self.closedSeason.end,
'description': self.closedSeason.description
}
return data
@dataclass
class CaptureItem:
"""Individual capture item (species with quantity)."""
speciesId: str
quantity: float
unit: Literal['lbs', 'units']
customName: Optional[str] = None
commonName: Optional[str] = None
category: Optional[str] = None
def to_dict(self):
"""Convert to dictionary for JSON serialization."""
return {
'speciesId': self.speciesId,
'quantity': self.quantity,
'unit': self.unit,
'customName': self.customName,
'commonName': self.commonName,
'category': self.category
}
@dataclass
class Capture:
"""Capture data model."""
id: str
timestamp: datetime
latitude: float
longitude: float
species: List[str]
items: List[CaptureItem]
fishingMethod: str
depth: float
synced: bool
# Simplified fields
port: Optional[str] = None
phone: Optional[str] = None
userId: Optional[str] = None
placeName: Optional[str] = None
totalWeight: float = 0
captureSource: str = 'device-gps'
syncedAt: Optional[datetime] = None
temperature: Optional[float] = None
seaState: Optional[str] = None
def to_dict(self):
"""Convert to dictionary for JSON serialization."""
return {
'id': self.id,
'timestamp': self.timestamp.isoformat() if isinstance(self.timestamp, datetime) else self.timestamp,
'latitude': self.latitude,
'longitude': self.longitude,
'species': self.species,
'items': [item.to_dict() if hasattr(item, 'to_dict') else item for item in self.items],
'fishingMethod': self.fishingMethod,
'depth': self.depth,
'synced': self.synced,
'port': self.port,
'phone': self.phone,
'userId': self.userId,
'placeName': self.placeName,
'totalWeight': self.totalWeight,
'captureSource': self.captureSource,
'syncedAt': self.syncedAt.isoformat() if self.syncedAt and isinstance(self.syncedAt, datetime) else self.syncedAt,
'temperature': self.temperature,
'seaState': self.seaState
}
@classmethod
def from_dict(cls, data: dict):
"""Create Capture instance from dictionary."""
items = []
for item in data.get('items', []):
if isinstance(item, dict):
items.append(CaptureItem(**item))
else:
items.append(item)
timestamp = data.get('timestamp')
if isinstance(timestamp, str):
timestamp = datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
syncedAt = data.get('syncedAt')
if syncedAt and isinstance(syncedAt, str):
syncedAt = datetime.fromisoformat(syncedAt.replace('Z', '+00:00'))
return cls(
id=data.get('id'),
timestamp=timestamp,
latitude=data.get('latitude'),
longitude=data.get('longitude'),
species=data.get('species', []),
items=items,
fishingMethod=data.get('fishingMethod'),
depth=data.get('depth'),
synced=data.get('synced', False),
port=data.get('port'),
phone=data.get('phone'),
userId=data.get('userId'),
placeName=data.get('placeName'),
totalWeight=data.get('totalWeight', 0),
captureSource=data.get('captureSource', 'device-gps'),
syncedAt=syncedAt,
temperature=data.get('temperature'),
seaState=data.get('seaState')
)
|