def validate(converter, val):
     if isinstance(val, datetime): pass
     elif isinstance(val, basestring): val = str2datetime(val)
     else: throw(TypeError, "Attribute %r: expected type is 'datetime'. Got: %r" % (converter.attr, val))
     mcs = converter.round_microseconds_to_precision(val.microsecond, converter.precision)
     if mcs is not None: val = val.replace(microsecond=mcs)
     return val
 def validate(converter, val, obj=None):
     if isinstance(val, datetime): pass
     elif isinstance(val, basestring): val = str2datetime(val)
     else: throw(TypeError, "Attribute %r: expected type is 'datetime'. Got: %r" % (converter.attr, val))
     mcs = converter.round_microseconds_to_precision(val.microsecond, converter.precision)
     if mcs is not None: val = val.replace(microsecond=mcs)
     return val
 def validate(converter, val):
     if isinstance(val, datetime): pass
     elif isinstance(val, basestring): val = str2datetime(val)
     else: throw(TypeError, "Attribute %r: expected type is 'datetime'. Got: %r" % (converter.attr, val))
     p = converter.precision
     if not p: val = val.replace(microsecond=0)
     elif p == 6: pass
     else:
         rounding = 10 ** (6-p)
         microsecond = (val.microsecond // rounding) * rounding
         val = val.replace(microsecond=microsecond)
     return val
Example #4
0
def populate_database():
    c1 = Customer(email='*****@*****.**', password='******',
                  name='John Smith', country='USA', address='address 1')

    c2 = Customer(email='*****@*****.**', password='******',
                  name='Matthew Reed', country='USA', address='address 2')

    c3 = Customer(email='*****@*****.**', password='******',
                  name='Chuan Qin', country='China', address='address 3')

    c4 = Customer(email='*****@*****.**', password='******',
                  name='Rebecca Lawson', country='USA', address='address 4')

    c5 = Customer(email='*****@*****.**', password='******',
                  name='Oliver Blakey', country='UK', address='address 5')

    tablets = Category(name='Tablets')
    flash_drives = Category(name='USB Flash Drives')
    ssd = Category(name='Solid State Drives')
    storage = Category(name='Data Storage')

    p1 = Product(name='Kindle Fire HD', price=Decimal('284.00'), quantity=120,
                 description='Amazon tablet for web, movies, music, apps, '
                             'games, reading and more',
                 categories=[tablets])

    p2 = Product(name='Apple iPad with Retina Display MD513LL/A (16GB, Wi-Fi, White)',
                 price=Decimal('478.50'), quantity=180,
                 description='iPad with Retina display now features an A6X chip, '
                             'FaceTime HD camera, and faster Wi-Fi',
                 categories=[tablets])

    p3 = Product(name='SanDisk Cruzer 16 GB USB Flash Drive', price=Decimal('9.99'),
                 quantity=400, description='Take it all with you on reliable '
                                           'SanDisk USB flash drive',
                 categories=[flash_drives, storage])

    p4 = Product(name='Kingston Digital DataTraveler SE9 16GB USB 2.0',
                 price=Decimal('9.98'), quantity=350,
                 description='Convenient - small, capless and pocket-sized '
                             'for easy transportability',
                 categories=[flash_drives, storage])

    p5 = Product(name='Samsung 840 Series 2.5 inch 120GB SATA III SSD',
                 price=Decimal('98.95'), quantity=0,
                 description='Enables you to boot up your computer '
                             'in as little as 15 seconds',
                 categories=[ssd, storage])

    p6 = Product(name='Crucial m4 256GB 2.5-Inch SSD SATA 6Gb/s CT256M4SSD2',
                 price=Decimal('188.67'), quantity=60,
                 description='The award-winning SSD delivers '
                             'powerful performance gains for SATA 6Gb/s systems',
                 categories=[ssd, storage])

    CartItem(customer=c1, product=p1, quantity=1)
    CartItem(customer=c1, product=p2, quantity=1)
    CartItem(customer=c2, product=p5, quantity=2)

    o1 = Order(customer=c1, total_price=Decimal('292.00'), state=DELIVERED,
               date_created=str2datetime('2012-10-20 15:22:00'),
               date_shipped=str2datetime('2012-10-21 11:34:00'),
               date_delivered=str2datetime('2012-10-26 17:23:00'))

    OrderItem(order=o1, product=p1, price=Decimal('274.00'), quantity=1)
    OrderItem(order=o1, product=p4, price=Decimal('9.98'), quantity=2)

    o2 = Order(customer=c1, total_price=Decimal('478.50'), state=DELIVERED,
               date_created=str2datetime('2013-01-10 09:40:00'),
               date_shipped=str2datetime('2013-01-10 14:03:00'),
               date_delivered=str2datetime('2013-01-13 11:57:00'))

    OrderItem(order=o2, product=p2, price=Decimal('478.50'), quantity=1)

    o3 = Order(customer=c2, total_price=Decimal('680.50'), state=DELIVERED,
               date_created=str2datetime('2012-11-03 12:10:00'),
               date_shipped=str2datetime('2012-11-04 11:47:00'),
               date_delivered=str2datetime('2012-11-07 18:55:00'))

    OrderItem(order=o3, product=p2, price=Decimal('478.50'), quantity=1)
    OrderItem(order=o3, product=p4, price=Decimal('9.98'), quantity=2)
    OrderItem(order=o3, product=p6, price=Decimal('199.00'), quantity=1)

    o4 = Order(customer=c3, total_price=Decimal('99.80'), state=SHIPPED,
               date_created=str2datetime('2013-03-11 19:33:00'),
               date_shipped=str2datetime('2013-03-12 09:40:00'))

    OrderItem(order=o4, product=p4, price=Decimal('9.98'), quantity=10)

    o5 = Order(customer=c4, total_price=Decimal('722.00'), state=CREATED,
               date_created=str2datetime('2013-03-15 23:15:00'))

    OrderItem(order=o5, product=p1, price=Decimal('284.00'), quantity=1)
    OrderItem(order=o5, product=p2, price=Decimal('478.50'), quantity=1)
Example #5
0
def populate_database():
    c1 = Customer(email='*****@*****.**',
                  password='******',
                  name='John Smith',
                  country='USA',
                  address='address 1')

    c2 = Customer(email='*****@*****.**',
                  password='******',
                  name='Matthew Reed',
                  country='USA',
                  address='address 2')

    c3 = Customer(email='*****@*****.**',
                  password='******',
                  name='Chuan Qin',
                  country='China',
                  address='address 3')

    c4 = Customer(email='*****@*****.**',
                  password='******',
                  name='Rebecca Lawson',
                  country='USA',
                  address='address 4')

    c5 = Customer(email='*****@*****.**',
                  password='******',
                  name='Oliver Blakey',
                  country='UK',
                  address='address 5')

    tablets = Category(name='Tablets')
    flash_drives = Category(name='USB Flash Drives')
    ssd = Category(name='Solid State Drives')
    storage = Category(name='Data Storage')

    p1 = Product(name='Kindle Fire HD',
                 price=Decimal('284.00'),
                 quantity=120,
                 description='Amazon tablet for web, movies, music, apps, '
                 'games, reading and more',
                 categories=[tablets])

    p2 = Product(
        name='Apple iPad with Retina Display MD513LL/A (16GB, Wi-Fi, White)',
        price=Decimal('478.50'),
        quantity=180,
        description='iPad with Retina display now features an A6X chip, '
        'FaceTime HD camera, and faster Wi-Fi',
        categories=[tablets])

    p3 = Product(name='SanDisk Cruzer 16 GB USB Flash Drive',
                 price=Decimal('9.99'),
                 quantity=400,
                 description='Take it all with you on reliable '
                 'SanDisk USB flash drive',
                 categories=[flash_drives, storage])

    p4 = Product(name='Kingston Digital DataTraveler SE9 16GB USB 2.0',
                 price=Decimal('9.98'),
                 quantity=350,
                 description='Convenient - small, capless and pocket-sized '
                 'for easy transportability',
                 categories=[flash_drives, storage])

    p5 = Product(name='Samsung 840 Series 2.5 inch 120GB SATA III SSD',
                 price=Decimal('98.95'),
                 quantity=0,
                 description='Enables you to boot up your computer '
                 'in as little as 15 seconds',
                 categories=[ssd, storage])

    p6 = Product(name='Crucial m4 256GB 2.5-Inch SSD SATA 6Gb/s CT256M4SSD2',
                 price=Decimal('188.67'),
                 quantity=60,
                 description='The award-winning SSD delivers '
                 'powerful performance gains for SATA 6Gb/s systems',
                 categories=[ssd, storage])

    CartItem(customer=c1, product=p1, quantity=1)
    CartItem(customer=c1, product=p2, quantity=1)
    CartItem(customer=c2, product=p5, quantity=2)

    o1 = Order(customer=c1,
               total_price=Decimal('292.00'),
               state=DELIVERED,
               date_created=str2datetime('2012-10-20 15:22:00'),
               date_shipped=str2datetime('2012-10-21 11:34:00'),
               date_delivered=str2datetime('2012-10-26 17:23:00'))

    OrderItem(order=o1, product=p1, price=Decimal('274.00'), quantity=1)
    OrderItem(order=o1, product=p4, price=Decimal('9.98'), quantity=2)

    o2 = Order(customer=c1,
               total_price=Decimal('478.50'),
               state=DELIVERED,
               date_created=str2datetime('2013-01-10 09:40:00'),
               date_shipped=str2datetime('2013-01-10 14:03:00'),
               date_delivered=str2datetime('2013-01-13 11:57:00'))

    OrderItem(order=o2, product=p2, price=Decimal('478.50'), quantity=1)

    o3 = Order(customer=c2,
               total_price=Decimal('680.50'),
               state=DELIVERED,
               date_created=str2datetime('2012-11-03 12:10:00'),
               date_shipped=str2datetime('2012-11-04 11:47:00'),
               date_delivered=str2datetime('2012-11-07 18:55:00'))

    OrderItem(order=o3, product=p2, price=Decimal('478.50'), quantity=1)
    OrderItem(order=o3, product=p4, price=Decimal('9.98'), quantity=2)
    OrderItem(order=o3, product=p6, price=Decimal('199.00'), quantity=1)

    o4 = Order(customer=c3,
               total_price=Decimal('99.80'),
               state=SHIPPED,
               date_created=str2datetime('2013-03-11 19:33:00'),
               date_shipped=str2datetime('2013-03-12 09:40:00'))

    OrderItem(order=o4, product=p4, price=Decimal('9.98'), quantity=10)

    o5 = Order(customer=c4,
               total_price=Decimal('722.00'),
               state=CREATED,
               date_created=str2datetime('2013-03-15 23:15:00'))

    OrderItem(order=o5, product=p1, price=Decimal('284.00'), quantity=1)
    OrderItem(order=o5, product=p2, price=Decimal('478.50'), quantity=1)
Example #6
0
    "range": lambda attribute, value: "%(attribute)s >= %(v0)s and (attribute)s <= %(v1)s"
    % {"attribute": attribute, "v0": value[0], "v1": value[1]},
    # TODO: implement
    # 'week_day', 'regex','isearch', 'search', 'iregex', 'regex',
}

NONE_REPR = set(["None", "none", "Null", "null"])
TRUE_REPR = set(["true", "True", "yes", "Yes"])
FALSE_REPR = set(["false", "False", "no", "No"])

QUERY_TERM_CONVERT_VALUE = {
    bool: lambda x: None if x in NONE_REPR else True if x in TRUE_REPR else False if x in FALSE_REPR else x,
    int: lambda x: None if x in NONE_REPR else int(x),
    long: lambda x: None if x in NONE_REPR else long(x),
    datetime.date: lambda x: None if x in NONE_REPR else str2date(x),
    datetime.datetime: lambda x: None if x in NONE_REPR else str2datetime(x),
    datetime.time: lambda x: None if x in NONE_REPR else str2time(x),
    datetime.timedelta: lambda x: None if x in NONE_REPR else datetime.timedelta(0, x),
    str: lambda x: None if x in NONE_REPR else x.encode("utf-8"),
    unicode: lambda x: None if x in NONE_REPR else x,
    decimal.Decimal: lambda x: None if x in NONE_REPR else decimal.Decimal(x),
    float: lambda x: None if x in NONE_REPR else float(x),
}


class DjonyDeclarativeMetaclass(DeclarativeMetaclass):
    def __new__(cls, name, bases, attrs):
        new_class = super(DjonyDeclarativeMetaclass, cls).__new__(cls, name, bases, attrs)
        include_fields = getattr(new_class._meta, "fields", [])
        excludes = getattr(new_class._meta, "excludes", [])
        object_model = getattr(new_class._meta, "object_model", None)
Example #7
0
 def validate(converter, val):
     if isinstance(val, datetime): return val
     if isinstance(val, basestring): return str2datetime(val)
     throw(
         TypeError, "Attribute %r: expected type is 'datetime'. Got: %r" %
         (converter.attr, val))
Example #8
0
    'in':lambda attribute,value:'%(attribute)s in (%(value)s)' % { 'attribute':attribute,'value':','.join(v for v in value)},
    'range':lambda attribute,value:'%(attribute)s >= %(v0)s and (attribute)s <= %(v1)s' % { 'attribute':attribute,'v0':value[0],'v1':value[1]},
    # TODO: implement
    # 'week_day', 'regex','isearch', 'search', 'iregex', 'regex',
}

NONE_REPR = set(['None','none','Null','null'])
TRUE_REPR = set(['true','True','yes','Yes'])
FALSE_REPR = set(['false','False','no','No'])

QUERY_TERM_CONVERT_VALUE = {
    bool:lambda x: None if x in NONE_REPR else True if x in TRUE_REPR else False if x in FALSE_REPR else x,
    int:lambda x:  None if x in NONE_REPR else int(x),
    long:lambda x:  None if x in NONE_REPR else long(x),
    datetime.date:lambda x: None if x in NONE_REPR else str2date(x),
    datetime.datetime:lambda x: None if x in NONE_REPR else str2datetime(x),
    datetime.time:lambda x: None if x in NONE_REPR else str2time(x),
    datetime.timedelta:lambda x: None if x in NONE_REPR else datetime.timedelta(0,x),
    str:lambda x:  None if x in NONE_REPR else x.encode('utf-8'),
    unicode:lambda x:  None if x in NONE_REPR else x,
    decimal.Decimal: lambda x:  None if x in NONE_REPR else decimal.Decimal(x),
    float: lambda x:  None if x in NONE_REPR else float(x),
}

class DjonyDeclarativeMetaclass(DeclarativeMetaclass):
    def __new__(cls, name, bases, attrs):
        new_class = super(DjonyDeclarativeMetaclass, cls).__new__(cls, name, bases, attrs)
        include_fields = getattr(new_class._meta, 'fields', [])
        excludes = getattr(new_class._meta, 'excludes', [])
        object_model = getattr(new_class._meta, 'object_model', None)
        if Resource in bases:
Example #9
0
def populate_database():
    c1 = Customer(
        email="*****@*****.**",
        password="******",
        name="John Smith",
        country="USA",
        address="address 1",
    )

    c2 = Customer(
        email="*****@*****.**",
        password="******",
        name="Matthew Reed",
        country="USA",
        address="address 2",
    )

    c3 = Customer(
        email="*****@*****.**",
        password="******",
        name="Chuan Qin",
        country="China",
        address="address 3",
    )

    c4 = Customer(
        email="*****@*****.**",
        password="******",
        name="Rebecca Lawson",
        country="USA",
        address="address 4",
    )

    c5 = Customer(
        email="*****@*****.**",
        password="******",
        name="Oliver Blakey",
        country="UK",
        address="address 5",
    )

    tablets = Category(name="Tablets")
    flash_drives = Category(name="USB Flash Drives")
    ssd = Category(name="Solid State Drives")
    storage = Category(name="Data Storage")

    p1 = Product(
        name="Kindle Fire HD",
        price=Decimal("284.00"),
        quantity=120,
        description="Amazon tablet for web, movies, music, apps, "
        "games, reading and more",
        categories=[tablets],
    )

    p2 = Product(
        name="Apple iPad with Retina Display MD513LL/A (16GB, Wi-Fi, White)",
        price=Decimal("478.50"),
        quantity=180,
        description="iPad with Retina display now features an A6X chip, "
        "FaceTime HD camera, and faster Wi-Fi",
        categories=[tablets],
    )

    p3 = Product(
        name="SanDisk Cruzer 16 GB USB Flash Drive",
        price=Decimal("9.99"),
        quantity=400,
        description="Take it all with you on reliable " "SanDisk USB flash drive",
        categories=[flash_drives, storage],
    )

    p4 = Product(
        name="Kingston Digital DataTraveler SE9 16GB USB 2.0",
        price=Decimal("9.98"),
        quantity=350,
        description="Convenient - small, capless and pocket-sized "
        "for easy transportability",
        categories=[flash_drives, storage],
    )

    p5 = Product(
        name="Samsung 840 Series 2.5 inch 120GB SATA III SSD",
        price=Decimal("98.95"),
        quantity=0,
        description="Enables you to boot up your computer "
        "in as little as 15 seconds",
        categories=[ssd, storage],
    )

    p6 = Product(
        name="Crucial m4 256GB 2.5-Inch SSD SATA 6Gb/s CT256M4SSD2",
        price=Decimal("188.67"),
        quantity=60,
        description="The award-winning SSD delivers "
        "powerful performance gains for SATA 6Gb/s systems",
        categories=[ssd, storage],
    )

    CartItem(customer=c1, product=p1, quantity=1)
    CartItem(customer=c1, product=p2, quantity=1)
    CartItem(customer=c2, product=p5, quantity=2)

    o1 = Order(
        customer=c1,
        total_price=Decimal("292.00"),
        state=DELIVERED,
        date_created=str2datetime("2012-10-20 15:22:00"),
        date_shipped=str2datetime("2012-10-21 11:34:00"),
        date_delivered=str2datetime("2012-10-26 17:23:00"),
    )

    OrderItem(order=o1, product=p1, price=Decimal("274.00"), quantity=1)
    OrderItem(order=o1, product=p4, price=Decimal("9.98"), quantity=2)

    o2 = Order(
        customer=c1,
        total_price=Decimal("478.50"),
        state=DELIVERED,
        date_created=str2datetime("2013-01-10 09:40:00"),
        date_shipped=str2datetime("2013-01-10 14:03:00"),
        date_delivered=str2datetime("2013-01-13 11:57:00"),
    )

    OrderItem(order=o2, product=p2, price=Decimal("478.50"), quantity=1)

    o3 = Order(
        customer=c2,
        total_price=Decimal("680.50"),
        state=DELIVERED,
        date_created=str2datetime("2012-11-03 12:10:00"),
        date_shipped=str2datetime("2012-11-04 11:47:00"),
        date_delivered=str2datetime("2012-11-07 18:55:00"),
    )

    OrderItem(order=o3, product=p2, price=Decimal("478.50"), quantity=1)
    OrderItem(order=o3, product=p4, price=Decimal("9.98"), quantity=2)
    OrderItem(order=o3, product=p6, price=Decimal("199.00"), quantity=1)

    o4 = Order(
        customer=c3,
        total_price=Decimal("99.80"),
        state=SHIPPED,
        date_created=str2datetime("2013-03-11 19:33:00"),
        date_shipped=str2datetime("2013-03-12 09:40:00"),
    )

    OrderItem(order=o4, product=p4, price=Decimal("9.98"), quantity=10)

    o5 = Order(
        customer=c4,
        total_price=Decimal("722.00"),
        state=CREATED,
        date_created=str2datetime("2013-03-15 23:15:00"),
    )

    OrderItem(order=o5, product=p1, price=Decimal("284.00"), quantity=1)
    OrderItem(order=o5, product=p2, price=Decimal("478.50"), quantity=1)