This repository was archived by the owner on Aug 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathsqlalchemy_fields.py
More file actions
69 lines (51 loc) 路 1.77 KB
/
Copy pathsqlalchemy_fields.py
File metadata and controls
69 lines (51 loc) 路 1.77 KB
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
import ipaddress
import uuid
import sqlalchemy
class GUID(sqlalchemy.TypeDecorator):
"""
Platform-independent GUID type.
Uses PostgreSQL's UUID type, otherwise uses
CHAR(32), storing as stringified hex values.
"""
impl = sqlalchemy.CHAR
cache_ok = True
def load_dialect_impl(self, dialect):
if dialect.name == "postgresql":
return dialect.type_descriptor(sqlalchemy.dialects.postgresql.UUID())
else:
return dialect.type_descriptor(sqlalchemy.CHAR(32))
def process_bind_param(self, value, dialect):
if value is None:
return value
if dialect.name == "postgresql":
return str(value)
else:
return value.hex
def process_result_value(self, value, dialect):
if value is None:
return value
if not isinstance(value, uuid.UUID):
value = uuid.UUID(value)
return value
class GenericIP(sqlalchemy.TypeDecorator):
"""
Platform-independent IP Address type.
Uses PostgreSQL's INET type, otherwise uses
CHAR(45), storing as stringified values.
"""
impl = sqlalchemy.CHAR
cache_ok = True
def load_dialect_impl(self, dialect):
if dialect.name == "postgresql":
return dialect.type_descriptor(sqlalchemy.dialects.postgresql.INET())
else:
return dialect.type_descriptor(sqlalchemy.CHAR(45))
def process_bind_param(self, value, dialect):
if value is not None:
return str(value)
def process_result_value(self, value, dialect):
if value is None:
return value
if not isinstance(value, (ipaddress.IPv4Address, ipaddress.IPv6Address)):
value = ipaddress.ip_address(value)
return value