|
| 1 | +from collections import OrderedDict |
| 2 | +from abc import ABCMeta, abstractmethod |
| 3 | +import unittest |
| 4 | + |
| 5 | + |
| 6 | +class ValidationError(Exception): |
| 7 | + pass |
| 8 | + |
| 9 | + |
| 10 | +class BaseField(metaclass=ABCMeta): |
| 11 | + def __init__(self, value=None): |
| 12 | + self.value = value |
| 13 | + |
| 14 | + def is_valid(self): |
| 15 | + return True |
| 16 | + |
| 17 | + @abstractmethod |
| 18 | + def __str__(self): |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +class Input(BaseField): |
| 23 | + def __str__(self): |
| 24 | + args = ['type="{}"'.format(self.__class__.type)] |
| 25 | + |
| 26 | + if self.value is not None: |
| 27 | + args.append('value="{}"'.format(self.value)) |
| 28 | + |
| 29 | + return '<input {} />'.format(' '.join(args)) |
| 30 | + |
| 31 | + |
| 32 | +class TextInput(Input): |
| 33 | + type = 'text' |
| 34 | + |
| 35 | + |
| 36 | +class SubmitInput(Input): |
| 37 | + type = 'submit' |
| 38 | + |
| 39 | + |
| 40 | +class PasswordInput(Input): |
| 41 | + type = 'password' |
| 42 | + |
| 43 | + |
| 44 | +class EmailInput(Input): |
| 45 | + type = 'email' |
| 46 | + |
| 47 | + def is_valid(self): |
| 48 | + if self. value == '[email protected]': |
| 49 | + raise ValidationError('No Pandas Allowed') |
| 50 | + |
| 51 | + |
| 52 | +class OrderedClass(type): |
| 53 | + @classmethod |
| 54 | + def __prepare__(metacls, name, bases, **kwds): |
| 55 | + return OrderedDict() |
| 56 | + |
| 57 | + def __new__(cls, name, bases, namespace, **kwds): |
| 58 | + result = type.__new__(cls, name, bases, dict(namespace)) |
| 59 | + result.members = tuple(namespace) |
| 60 | + return result |
| 61 | + |
| 62 | + |
| 63 | +class Form(metaclass=OrderedClass): |
| 64 | + def __init__(self, form_data=None): |
| 65 | + self.__dict__ = OrderedDict() |
| 66 | + self._form_data = form_data or {} |
| 67 | + |
| 68 | + cls_dict = vars(self.__class__) |
| 69 | + |
| 70 | + for key in self.__class__.members: |
| 71 | + if '__' not in key: |
| 72 | + value = cls_dict[key] |
| 73 | + |
| 74 | + if not callable(value): |
| 75 | + self.__dict__[key] = value |
| 76 | + # setattr(self, key, value) |
| 77 | + |
| 78 | + for key, value in self._form_data.items(): |
| 79 | + if key in self.__dict__: |
| 80 | + self.__dict__[key].value = value |
| 81 | + |
| 82 | + @property |
| 83 | + def fields(self): |
| 84 | + return [(name, field) for name, field |
| 85 | + in self.__dict__.items() |
| 86 | + if not name.startswith('_')] |
| 87 | + |
| 88 | + def is_valid(self): |
| 89 | + for _, field in self.fields: |
| 90 | + field.is_valid() |
| 91 | + |
| 92 | + cls_dict = vars(self.__class__) |
| 93 | + |
| 94 | + for name, field in self.fields: |
| 95 | + validator_name = 'validate_{}'.format(name) |
| 96 | + validator = cls_dict.get(validator_name, False) |
| 97 | + |
| 98 | + if validator and callable(validator): |
| 99 | + getattr(self, validator_name)(field) |
| 100 | + |
| 101 | + def __str__(self): |
| 102 | + tags = ['<form>'] |
| 103 | + tags += [str(field) for _, field in self.fields] |
| 104 | + |
| 105 | + tags.append(str(SubmitInput('Go!'))) |
| 106 | + tags.append('</form>') |
| 107 | + return "\n".join(tags) |
| 108 | + |
| 109 | + |
| 110 | +class LoginForm(Form): |
| 111 | + email = EmailInput() |
| 112 | + password = PasswordInput() |
| 113 | + |
| 114 | + def validate_password(self, pwd_field): |
| 115 | + print('I cant believe this works') |
| 116 | + print(pwd_field) |
| 117 | + |
| 118 | +f = LoginForm({ 'email': '[email protected]'}) |
| 119 | +print(f) |
| 120 | + |
| 121 | +f.is_valid() |
0 commit comments