aboutsummaryrefslogtreecommitdiff
path: root/src/cerberus/core/models.py
blob: ecdcb3c71029dd5ca0a583a36faad0647330a003 (plain)
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
from dataclasses import dataclass, field
from datetime import datetime
from typing import Optional, List, Dict, Any
import json

@dataclass
class PasswordEntry:
    """Represents a single password entry in the password manager."""
    id: str
    website: str
    username: str
    password: str
    url: str = ""
    notes: str = ""
    tags: List[str] = field(default_factory=list)
    created_at: datetime = field(default_factory=datetime.utcnow)
    updated_at: datetime = field(default_factory=datetime.utcnow)
    last_used: Optional[datetime] = None
    password_strength: Optional[float] = None
    compromised: bool = False
    custom_fields: Dict[str, Any] = field(default_factory=dict)
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert the password entry to a dictionary for serialization."""
        return {
            'id': self.id,
            'website': self.website,
            'username': self.username,
            'password': self.password,
            'url': self.url,
            'notes': self.notes,
            'tags': self.tags,
            'created_at': self.created_at.isoformat(),
            'updated_at': self.updated_at.isoformat(),
            'last_used': self.last_used.isoformat() if self.last_used else None,
            'password_strength': self.password_strength,
            'compromised': self.compromised,
            'custom_fields': self.custom_fields
        }
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'PasswordEntry':
        """Create a PasswordEntry from a dictionary."""
        return cls(
            id=data.get('id', ''),
            website=data['website'],
            username=data['username'],
            password=data['password'],
            url=data.get('url', ''),
            notes=data.get('notes', ''),
            tags=data.get('tags', []),
            created_at=datetime.fromisoformat(data['created_at']),
            updated_at=datetime.fromisoformat(data['updated_at']),
            last_used=datetime.fromisoformat(data['last_used']) if data.get('last_used') else None,
            password_strength=data.get('password_strength'),
            compromised=data.get('compromised', False),
            custom_fields=data.get('custom_fields', {})
        )