|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | from __future__ import absolute_import
|
3 | 3 |
|
| 4 | +import binascii |
4 | 5 | import datetime
|
5 |
| -import graphene |
6 | 6 |
|
7 |
| -from graphene import Scalar |
| 7 | +import graphene |
8 | 8 | from graphene.utils.str_converters import to_camel_case
|
9 | 9 | from graphql.language import ast
|
10 | 10 |
|
|
17 | 17 | )
|
18 | 18 |
|
19 | 19 |
|
20 |
| -class Date(Scalar): |
21 |
| - ''' |
22 |
| - The `Date` scalar type represents a Date |
23 |
| - value as specified by |
24 |
| - [iso8601](https://en.wikipedia.org/wiki/ISO_8601). |
25 |
| - ''' |
26 |
| - epoch_time = '00:00:00' |
27 |
| - |
28 |
| - @staticmethod |
29 |
| - def serialize(date): |
30 |
| - if isinstance(date, datetime.datetime): |
31 |
| - date = date.date() |
32 |
| - |
33 |
| - assert isinstance(date, datetime.date), ( |
34 |
| - 'Received not compatible date "{}"'.format(repr(date)) |
35 |
| - ) |
36 |
| - return date.isoformat() |
37 |
| - |
38 |
| - @classmethod |
39 |
| - def parse_literal(cls, node): |
40 |
| - if isinstance(node, ast.StringValue): |
41 |
| - return cls.parse_value(node.value) |
42 |
| - |
43 |
| - @classmethod |
44 |
| - def parse_value1(cls, value): |
45 |
| - dt = iso8601.parse_date('{}T{}'.format(value, cls.epoch_time)) |
46 |
| - return datetime.date(dt.year, dt.month, dt.day) |
47 |
| - |
48 |
| - @staticmethod |
49 |
| - def parse_value(value): |
50 |
| - return iso8601.parse_date(value).date() |
51 |
| - |
52 |
| - |
53 | 20 | def object_type_factory(_type, new_model, new_name=None, new_only_fields=(), new_exclude_fields=(),
|
54 | 21 | new_filter_fields=None, new_registry=None, new_skip_registry=False):
|
55 | 22 |
|
@@ -144,3 +111,116 @@ class GenericForeignKeyInputType(graphene.InputObjectType):
|
144 | 111 |
|
145 | 112 | class Meta:
|
146 | 113 | description = ' Auto generated InputType for a model\'s GenericForeignKey field '
|
| 114 | + |
| 115 | + |
| 116 | +# ************************************************ # |
| 117 | +# ************** CUSTOM BASE TYPES *************** # |
| 118 | +# ************************************************ # |
| 119 | +class CustomDate(object): |
| 120 | + |
| 121 | + def __init__(self, date): |
| 122 | + self.date_str = date |
| 123 | + |
| 124 | + |
| 125 | +class Binary(graphene.Scalar): |
| 126 | + """ |
| 127 | + BinaryArray is used to convert a Django BinaryField to the string form |
| 128 | + """ |
| 129 | + @staticmethod |
| 130 | + def binary_to_string(value): |
| 131 | + return binascii.hexlify(value).decode("utf-8") |
| 132 | + |
| 133 | + serialize = binary_to_string |
| 134 | + parse_value = binary_to_string |
| 135 | + |
| 136 | + @classmethod |
| 137 | + def parse_literal(cls, node): |
| 138 | + if isinstance(node, ast.StringValue): |
| 139 | + return cls.binary_to_string(node.value) |
| 140 | + |
| 141 | + |
| 142 | +class Time(graphene.Scalar): |
| 143 | + """ |
| 144 | + The `Time` scalar type represents a Time value as |
| 145 | + specified by |
| 146 | + [iso8601](https://en.wikipedia.org/wiki/ISO_8601). |
| 147 | + """ |
| 148 | + epoch_date = '1970-01-01' |
| 149 | + |
| 150 | + @staticmethod |
| 151 | + def serialize(time): |
| 152 | + if isinstance(time, CustomDate): |
| 153 | + return time.date_str |
| 154 | + |
| 155 | + assert isinstance(time, datetime.time), ( |
| 156 | + 'Received not compatible time "{}"'.format(repr(time)) |
| 157 | + ) |
| 158 | + return time.isoformat() |
| 159 | + |
| 160 | + @classmethod |
| 161 | + def parse_literal(cls, node): |
| 162 | + if isinstance(node, ast.StringValue): |
| 163 | + return cls.parse_value(node.value) |
| 164 | + |
| 165 | + @classmethod |
| 166 | + def parse_value(cls, value): |
| 167 | + dt = iso8601.parse_date('{}T{}'.format(cls.epoch_date, value)) |
| 168 | + return datetime.time(dt.hour, dt.minute, dt.second, dt.microsecond, dt.tzinfo) |
| 169 | + |
| 170 | + |
| 171 | +class Date(graphene.Scalar): |
| 172 | + """ |
| 173 | + The `Date` scalar type represents a Date |
| 174 | + value as specified by |
| 175 | + [iso8601](https://en.wikipedia.org/wiki/ISO_8601). |
| 176 | + """ |
| 177 | + epoch_time = '00:00:00' |
| 178 | + |
| 179 | + @staticmethod |
| 180 | + def serialize(date): |
| 181 | + if isinstance(date, CustomDate): |
| 182 | + return date.date_str |
| 183 | + |
| 184 | + if isinstance(date, datetime.datetime): |
| 185 | + date = date.date() |
| 186 | + |
| 187 | + assert isinstance(date, datetime.date), ( |
| 188 | + 'Received not compatible date "{}"'.format(repr(date)) |
| 189 | + ) |
| 190 | + return date.isoformat() |
| 191 | + |
| 192 | + @classmethod |
| 193 | + def parse_literal(cls, node): |
| 194 | + if isinstance(node, ast.StringValue): |
| 195 | + return cls.parse_value(node.value) |
| 196 | + |
| 197 | + @staticmethod |
| 198 | + def parse_value(value): |
| 199 | + return iso8601.parse_date(value).date() |
| 200 | + |
| 201 | + |
| 202 | +class DateTime(graphene.Scalar): |
| 203 | + """ |
| 204 | + The `DateTime` scalar type represents a DateTime |
| 205 | + value as specified by |
| 206 | + [iso8601](https://en.wikipedia.org/wiki/ISO_8601). |
| 207 | + """ |
| 208 | + |
| 209 | + @staticmethod |
| 210 | + def serialize(dt): |
| 211 | + if isinstance(dt, CustomDate): |
| 212 | + return dt.date_str |
| 213 | + |
| 214 | + assert isinstance(dt, (datetime.datetime, datetime.date)), ( |
| 215 | + 'Received not compatible datetime "{}"'.format(repr(dt)) |
| 216 | + ) |
| 217 | + return dt.isoformat() |
| 218 | + |
| 219 | + @classmethod |
| 220 | + def parse_literal(cls, node): |
| 221 | + if isinstance(node, ast.StringValue): |
| 222 | + return cls.parse_value(node.value) |
| 223 | + |
| 224 | + @staticmethod |
| 225 | + def parse_value(value): |
| 226 | + return iso8601.parse_date(value) |
0 commit comments