Code

Ticket #1414: lazydate-attributes.diff

File lazydate-attributes.diff, 789 bytes (added by akaihola, 8 years ago)

serve missing LazyDate attributes from datetime computed at runtime

Line 
1Index: magic-removal/django/db/models/__init__.py
2===================================================================
3--- magic-removal/django/db/models/__init__.py  (revision 2433)
4+++ magic-removal/django/db/models/__init__.py  (working copy)
5@@ -27,11 +27,15 @@
6     def __init__(self, **kwargs):
7         self.delta = datetime.timedelta(**kwargs)
8 
9-    def __str__(self):
10-        return str(self.__get_value__())
11-
12     def __repr__(self):
13         return "<LazyDate: %s>" % self.delta
14 
15     def __get_value__(self):
16         return datetime.datetime.now() + self.delta
17+
18+    def __getattr__(self, attr):
19+        """
20+        Catch missing attributes and serve them from the datetime
21+        calculated at run time.
22+        """
23+        return getattr(self.__get_value__(), attr)