|
| 1 | +""" |
| 2 | +Deis API custom fields for representing data in Django forms. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import unicode_literals |
| 6 | +from uuid import uuid4 |
| 7 | + |
| 8 | +from django import forms |
| 9 | +from django.db import models |
| 10 | +from json_field import JSONField |
| 11 | +from yamlfield.fields import YAMLField |
| 12 | + |
| 13 | + |
| 14 | +class UuidField(models.CharField): |
| 15 | + |
| 16 | + """A univerally unique ID field.""" |
| 17 | + # pylint: disable=R0904 |
| 18 | + |
| 19 | + description = __doc__ |
| 20 | + |
| 21 | + def __init__(self, *args, **kwargs): |
| 22 | + kwargs.setdefault('auto_created', True) |
| 23 | + kwargs.setdefault('editable', False) |
| 24 | + kwargs.setdefault('max_length', 32) |
| 25 | + kwargs.setdefault('unique', True) |
| 26 | + super(UuidField, self).__init__(*args, **kwargs) |
| 27 | + |
| 28 | + def db_type(self, connection=None): |
| 29 | + """Return the database type for a UuidField.""" |
| 30 | + db_type = None |
| 31 | + if connection and 'postgres' in connection.vendor: |
| 32 | + db_type = 'uuid' |
| 33 | + else: |
| 34 | + db_type = 'char({0})'.format(self.max_length) |
| 35 | + return db_type |
| 36 | + |
| 37 | + def pre_save(self, model_instance, add): |
| 38 | + """Initialize an empty field with a new UUID before it is saved.""" |
| 39 | + value = getattr(model_instance, self.get_attname(), None) |
| 40 | + if not value and add: |
| 41 | + uuid = str(uuid4()) |
| 42 | + setattr(model_instance, self.get_attname(), uuid) |
| 43 | + return uuid |
| 44 | + else: |
| 45 | + return super(UuidField, self).pre_save(model_instance, add) |
| 46 | + |
| 47 | + def formfield(self, **kwargs): |
| 48 | + """Tell forms how to represent this UuidField.""" |
| 49 | + kwargs.update({ |
| 50 | + 'form_class': forms.CharField, |
| 51 | + 'max_length': self.max_length, |
| 52 | + }) |
| 53 | + return super(UuidField, self).formfield(**kwargs) |
| 54 | + |
| 55 | + |
| 56 | +class EnvVarsField(JSONField): |
| 57 | + |
| 58 | + """ |
| 59 | + A text field that accepts a JSON object, coercing its keys to uppercase. |
| 60 | + """ |
| 61 | + pass |
| 62 | + |
| 63 | + |
| 64 | +class DataBagField(JSONField): |
| 65 | + """ |
| 66 | + A text field that accepts a JSON object, used for storing Chef data bags. |
| 67 | + """ |
| 68 | + pass |
| 69 | + |
| 70 | + |
| 71 | +class ProcfileField(JSONField): |
| 72 | + """ |
| 73 | + A text field that accepts a JSON object, used for Procfile data. |
| 74 | + """ |
| 75 | + pass |
| 76 | + |
| 77 | + |
| 78 | +class CredentialsField(JSONField): |
| 79 | + """ |
| 80 | + A text field that accepts a JSON object, used for storing provider |
| 81 | + API Credentials. |
| 82 | + """ |
| 83 | + pass |
| 84 | + |
| 85 | + |
| 86 | +class ParamsField(JSONField): |
| 87 | + """ |
| 88 | + A text field that accepts a JSON object, used for storing provider |
| 89 | + API Parameters. |
| 90 | + """ |
| 91 | + |
| 92 | +class CloudInitField(YAMLField): |
| 93 | + """ |
| 94 | + A text field that accepts a YAML object, used for storing cloud-init |
| 95 | + boostrapping scripts. |
| 96 | + """ |
| 97 | + pass |
| 98 | + |
| 99 | + |
| 100 | +class NodeStatusField(JSONField): |
| 101 | + """ |
| 102 | + A text field that accepts a YAML object, used for storing cloud-init |
| 103 | + boostrapping scripts. |
| 104 | + """ |
| 105 | + pass |
| 106 | + |
| 107 | + |
| 108 | +try: |
| 109 | + from south.modelsinspector import add_introspection_rules |
| 110 | + # Tell the South schema migration tool to handle a UuidField. |
| 111 | + add_introspection_rules([], [r'^api\.fields\.UuidField']) |
| 112 | + add_introspection_rules([], [r'^api\.fields\.EnvVarsField']) |
| 113 | + add_introspection_rules([], [r'^api\.fields\.DataBagField']) |
| 114 | + add_introspection_rules([], [r'^api\.fields\.ProcfileField']) |
| 115 | + add_introspection_rules([], [r'^api\.fields\.CredentialsField']) |
| 116 | + add_introspection_rules([], [r'^api\.fields\.ParamsField']) |
| 117 | + add_introspection_rules([], [r'^api\.fields\.CloudInitField']) |
| 118 | + add_introspection_rules([], [r'^api\.fields\.NodeStatusField']) |
| 119 | +except ImportError: |
| 120 | + pass |
0 commit comments