-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields.py
122 lines (95 loc) · 3.31 KB
/
fields.py
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
"""
Deis API custom fields for representing data in Django forms.
"""
from __future__ import unicode_literals
from uuid import uuid4
from django import forms
from django.db import models
from json_field import JSONField
from yamlfield.fields import YAMLField
class UuidField(models.CharField):
"""A univerally unique ID field."""
# pylint: disable=R0904
description = __doc__
def __init__(self, *args, **kwargs):
kwargs.setdefault('auto_created', True)
kwargs.setdefault('editable', False)
kwargs.setdefault('max_length', 32)
kwargs.setdefault('unique', True)
super(UuidField, self).__init__(*args, **kwargs)
def db_type(self, connection=None):
"""Return the database type for a UuidField."""
db_type = None
if connection and 'postgres' in connection.vendor:
db_type = 'uuid'
else:
db_type = "char({})".format(self.max_length)
return db_type
def pre_save(self, model_instance, add):
"""Initialize an empty field with a new UUID before it is saved."""
value = getattr(model_instance, self.get_attname(), None)
if not value and add:
uuid = str(uuid4())
setattr(model_instance, self.get_attname(), uuid)
return uuid
else:
return super(UuidField, self).pre_save(model_instance, add)
def formfield(self, **kwargs):
"""Tell forms how to represent this UuidField."""
kwargs.update({
'form_class': forms.CharField,
'max_length': self.max_length,
})
return super(UuidField, self).formfield(**kwargs)
class EnvVarsField(JSONField):
"""
A text field that accepts a JSON object, coercing its keys to uppercase.
"""
pass
class DataBagField(JSONField):
"""
A text field that accepts a JSON object, used for storing Chef data bags.
"""
pass
class ProcfileField(JSONField):
"""
A text field that accepts a JSON object, used for Procfile data.
"""
pass
class CredentialsField(JSONField):
"""
A text field that accepts a JSON object, used for storing provider
API Credentials.
"""
pass
class ParamsField(JSONField):
"""
A text field that accepts a JSON object, used for storing provider
API Parameters.
"""
pass
class CloudInitField(YAMLField):
"""
A text field that accepts a YAML object, used for storing cloud-init
boostrapping scripts.
"""
pass
class NodeStatusField(JSONField):
"""
A text field that accepts a YAML object, used for storing cloud-init
boostrapping scripts.
"""
pass
try:
from south.modelsinspector import add_introspection_rules
# Tell the South schema migration tool to handle our custom fields.
add_introspection_rules([], [r'^api\.fields\.UuidField'])
add_introspection_rules([], [r'^api\.fields\.EnvVarsField'])
add_introspection_rules([], [r'^api\.fields\.DataBagField'])
add_introspection_rules([], [r'^api\.fields\.ProcfileField'])
add_introspection_rules([], [r'^api\.fields\.CredentialsField'])
add_introspection_rules([], [r'^api\.fields\.ParamsField'])
add_introspection_rules([], [r'^api\.fields\.CloudInitField'])
add_introspection_rules([], [r'^api\.fields\.NodeStatusField'])
except ImportError:
pass