Permalink
Browse files

add RandomDate

  • Loading branch information...
1 parent 4cd6d82 commit 059494910ac4f8fc6fa91149f88c49761dcafa54 Simon Bächler committed Mar 28, 2013
Showing with 31 additions and 2 deletions.
  1. +7 −0 README.rst
  2. +2 −2 scaffolding/__init__.py
  3. +22 −0 scaffolding/tubes.py
View
@@ -118,6 +118,13 @@ You can set a ratio for true or false by specifying true or false:
e.g. ``false=3`` returns 3 times as many False than Trues.
+RandomDate
+----------
+
+Creates a random date between ``startdate`` and ``enddate``.
+``startdate`` and ``enddate`` have to be datetime.date instances.
+
+
Contrib
-------
View
@@ -8,14 +8,14 @@
from tubes import (Tube, Name, LoremIpsum, RandInt, Contrib, AlwaysTrue,
AlwaysFalse, StaticValue, RandomValue, EveryValue, RandomInternetImage,
- ForeignKey, FirstName, LastName, TrueOrFalse, BookTitle
+ ForeignKey, FirstName, LastName, TrueOrFalse, BookTitle, RandomDate
)
__all__ = ['Tube', 'Name', 'LoremIpsum', 'RandInt', 'Contrib', 'AlwaysTrue',
'AlwaysFalse', 'StaticValue', 'RandomValue', 'EveryValue',
'RandomInternetImage', 'FirstName', 'LastName',
- 'TrueOrFalse', 'BookTitle',
+ 'TrueOrFalse', 'BookTitle', 'RandomDate',
'ForeignKey', 'register', 'scaffold_for_model']
View
@@ -3,6 +3,7 @@
import os
import random
import urllib
+import datetime
from scaffolding.library import lorem_ipsum
from django.core.files import File
@@ -189,3 +190,24 @@ class ForeignKey(EveryValue):
"""
def __init__(self, queryset, chunksize=100, **kwargs):
super(ForeignKey, self).__init__(queryset[:chunksize])
+
+
+class RandomDate(Tube):
+ """ Creates a date between startdate and enddate """
+ def __init__(self, startdate, enddate, **kwargs):
+ super(RandomDate, self).__init__(**kwargs)
+ if not (isinstance(startdate, datetime.date) and
+ isinstance(enddate, datetime.date)):
+ raise AttributeError(
+ "startdate and enddate must be instances of datetime.date")
+ if enddate < startdate:
+ raise AttributeError(
+ "enddate must be after startdate"
+ )
+ self.startdate = startdate
+ self.enddate = enddate
+
+ def next(self):
+ delta = (self.enddate - self.startdate).days
+ return self.startdate + datetime.timedelta(random.randint(0, delta))
+

0 comments on commit 0594949

Please sign in to comment.