diff options
| author | Tom Christie | 2014-09-02 17:41:23 +0100 |
|---|---|---|
| committer | Tom Christie | 2014-09-02 17:41:23 +0100 |
| commit | f2852811f93863f2eed04d51eeb7ef27716b2409 (patch) | |
| tree | 45799932849f81d45d77edc53cb00269465ba0f1 /rest_framework/fields.py | |
| parent | ec096a1caceff6a4f5c75a152dd1c7bea9ed281d (diff) | |
| download | django-rest-framework-f2852811f93863f2eed04d51eeb7ef27716b2409.tar.bz2 | |
Getting tests passing
Diffstat (limited to 'rest_framework/fields.py')
| -rw-r--r-- | rest_framework/fields.py | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 3e0f7ca4..838aa3b0 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1,4 +1,5 @@ from rest_framework.utils import html +import inspect class empty: @@ -11,6 +12,22 @@ class empty: pass +def is_simple_callable(obj): + """ + True if the object is a callable that takes no arguments. + """ + function = inspect.isfunction(obj) + method = inspect.ismethod(obj) + + if not (function or method): + return False + + args, _, _, defaults = inspect.getargspec(obj) + len_args = len(args) if function else len(args) - 1 + len_defaults = len(defaults) if defaults else 0 + return len_args <= len_defaults + + def get_attribute(instance, attrs): """ Similar to Python's built in `getattr(instance, attr)`, @@ -98,6 +115,7 @@ class Field(object): self.field_name = field_name self.parent = parent self.root = root + self.context = parent.context # `self.label` should deafult to being based on the field name. if self.label is None: @@ -297,25 +315,55 @@ class IntegerField(Field): self.fail('invalid_integer') return data + def to_primative(self, value): + if value is None: + return None + return int(value) + class EmailField(CharField): pass # TODO +class URLField(CharField): + pass # TODO + + class RegexField(CharField): def __init__(self, **kwargs): self.regex = kwargs.pop('regex') super(CharField, self).__init__(**kwargs) +class DateField(CharField): + def __init__(self, **kwargs): + self.input_formats = kwargs.pop('input_formats', None) + super(DateField, self).__init__(**kwargs) + + +class TimeField(CharField): + def __init__(self, **kwargs): + self.input_formats = kwargs.pop('input_formats', None) + super(TimeField, self).__init__(**kwargs) + + class DateTimeField(CharField): - pass # TODO + def __init__(self, **kwargs): + self.input_formats = kwargs.pop('input_formats', None) + super(DateTimeField, self).__init__(**kwargs) class FileField(Field): pass # TODO +class ReadOnlyField(Field): + def to_primative(self, value): + if is_simple_callable(value): + return value() + return value + + class MethodField(Field): def __init__(self, **kwargs): kwargs['source'] = '*' |
