Test coverage report: auction.models.bases

# auction.models.bases: 125 total statements, 97.2% covered

Generated: Mon 2013-04-29 18:13 CST

Source file: /home/slam/workspace/django-auction/auction/models/bases.py

Stats: 105 executed, 3 missed, 17 excluded, 67 ignored

1. `from decimal import Decimal`
2. `from django.db import models`
3. `from polymorphic.polymorphic_model import PolymorphicModel`
4. `from django.utils.translation import ugettext_lazy as _`
5. `from django.contrib.auth.models import User`
6. `from django.contrib.contenttypes.models import ContentType`
7. `from django.contrib.contenttypes import generic`
8. `from auction.utils.loader import get_model_string`
9. `from south.modelsinspector import add_introspection_rules`
10. `from django.conf import settings`
11. ``
12. `add_introspection_rules([], ["^auction\.models\.bases\.CurrencyField"])`
13. ``
14. `class CurrencyField(models.DecimalField):`
15. `__metaclass__ = models.SubfieldBase`
16. ``
17. `def to_python(self, value):`
18. `try:`
19. `return super(CurrencyField, self).to_python(value=value).quantize(Decimal("0.01"))`
20. `except AttributeError:`
21. `return None`
22. ``
23. `class BaseAuction(PolymorphicModel):`
24. `name = models.CharField(max_length=255, verbose_name=_('Auction name'))`
25. `slug = models.SlugField(unique=True, verbose_name=_('Slug'))`
26. `start_date = models.DateTimeField(verbose_name=_('Start date'))`
27. `end_date = models.DateTimeField(verbose_name=_('End date'))`
28. `active = models.BooleanField(default=False, verbose_name=_('Active'))`
29. `total_bids = models.IntegerField(default=0, verbose_name=_('Total bids'))`
30. `date_added = models.DateTimeField(auto_now_add=True, verbose_name=_('Date added'))`
31. `last_modified = models.DateTimeField(auto_now=True, verbose_name=_('Last modified'))`
32. ``
33. `class Meta:`
34. `abstract = True`
35. `app_label = 'auction'`
36. `verbose_name = _('Auction')`
37. `verbose_name_plural = _('Auctions')`
38. ``
39. `def __unicode__(self):`
40. `return self.name`
41. ``
42. `class BaseBidBasket(models.Model):`
43. `"""`
44. `This models functions similarly to a shopping cart, except it expects a logged in user.`
45. `"""`
46. `user = models.OneToOneField(User, related_name="%(app_label)s_%(class)s_related", verbose_name=_('User'))`
47. `date_added = models.DateTimeField(auto_now_add=True, verbose_name=_('Date added'))`
48. `last_modified = models.DateTimeField(auto_now=True, verbose_name=_('Last modified'))`
49. ``
50. `class Meta:`
51. `abstract = True`
52. `app_label = 'auction'`
53. `verbose_name = _('Bid basket')`
54. `verbose_name_plural = _('Bid baskets')`
55. ``
56. `def add_bid(self, lot, amount):`
57. `from auction.models import BidItem`
58. `self.save()`
59. ``
60. `if not lot.is_biddable:`
61. `return False`
62. ``
63. `try:`
64. `amount = Decimal(amount)`
65. `except Exception, e:`
66. `amount = Decimal('0')`
67. ``
68. `from auction.models.lot import Lot`
69. `item,created = BidItem.objects.get_or_create(bid_basket=self,`
70. `content_type=ContentType.objects.get_for_model(Lot),`
71. `lot_id=lot.pk)`
72. `if item:`
73. `item.amount=amount`
74. `item.save()`
75. `return item`
76. ``
77. `def update_bid(self, bid_basket_item_id, amount):`
78. `"""`
79. `Update amount of bid. Delete bid if amount is 0.`
80. `"""`
81. ``
82. `try:`
83. `amount = Decimal(amount)`
84. `except Exception, e:`
85. `amount = Decimal('0')`
86. ``
87. `bid_basket_item = self.bids.get(pk=bid_basket_item_id)`
88. `if not bid_basket_item.is_locked():`
89. `if amount == 0:`
90. `bid_basket_item.delete()`
91. `else:`
92. `bid_basket_item.amount = amount`
93. `bid_basket_item.save()`
94. `self.save()`
95. `return bid_basket_item`
96. ``
97. `def delete_bid(self, bid_basket_item_id):`
98. `"""`
99. `Delete a single item from bid basket.`
100. `"""`
101. `bid_basket_item = self.bids.get(pk=bid_basket_item_id)`
102. `if not bid_basket_item.is_locked():`
103. `bid_basket_item.delete()`
104. `return bid_basket_item`
105. ``
106. `def empty(self):`
107. `"""`
108. `Remove all bids from bid basket.`
109. `"""`
110. `if self.pk:`
111. `bids = self.bids.all()`
112. `for bid in bids:`
113. `if not bid.is_locked():`
114. `bid.delete()`
115. ``
116. `@property`
117. `def bids(self):`
118. `"""`
119. `Used as accessor for abstract related (BaseBidItem.bid_items).`
120. ``
121. `If you override BaseBidItem and use a label other than "auction"`
122. `you will also need to set AUCTION_BIDBASKET_BIDS_RELATED_NAME.`
123. `Example: foo_biditem_related`
124. `(where your label is "foo" and your model is "BidItem")`
125. `"""`
126. `bids = getattr(settings, 'AUCTION_BIDBASKET_BIDS_RELATED_NAME',`
127. `'auction_biditem_related')`
128. `return getattr(self, bids)`
129. ``
130. `@property`
131. `def total_bids(self):`
132. `"""`
133. `Returns total bids in basket.`
134. `"""`
135. `return len(self.bids.all())`
136. ``
137. `class BaseAuctionLot(PolymorphicModel):`
138. `name = models.CharField(max_length=255, verbose_name=_('Lot name'))`
139. `slug = models.SlugField(verbose_name=_('Slug'))`
140. `active = models.BooleanField(default=False, verbose_name=_('Active'))`
141. `is_biddable = models.BooleanField(default=False, verbose_name=_('Is biddable?'))`
142. `content_type = models.ForeignKey(ContentType, verbose_name=_('Content type'))`
143. `object_id = models.PositiveIntegerField(verbose_name=_('Object ID'))`
144. `content_object = generic.GenericForeignKey('content_type', 'object_id')`
145. `date_added = models.DateTimeField(auto_now_add=True, verbose_name=_('Date added'))`
146. `last_modified = models.DateTimeField(auto_now=True, verbose_name=_('Last modified'))`
147. ``
148. `class Meta:`
149. `abstract = True`
150. `app_label = 'auction'`
151. `verbose_name = _('Auction lot')`
152. `verbose_name_plural = _('Auction lots')`
153. ``
154. `def __unicode__(self):`
155. `return self.name`
156. ``
157. `@property`
158. `def is_locked(self):`
159. `"""`
160. `This property is meant to be overwritten with your own logic. Bid baskets`
161. `check this method to find out if a bid can be manipulated.`
162. `"""`
163. `import auction.utils.generic`
164. `now = auction.utils.generic.get_current_time()`
165. `if self.content_object.end_date <= now:`
166. `return True`
167. `return False`
168. ``
169. `class BaseBidItem(models.Model):`
170. `"""`
171. `This is a holder for total number of bids and a pointer to`
172. `item being bid on.`
173. `"""`
174. ``
175. `bid_basket = models.ForeignKey(get_model_string("BidBasket"), related_name="%(app_label)s_%(class)s_related", verbose_name=_('Bid basket'))`
176. `content_type = models.ForeignKey(ContentType, related_name="%(app_label)s_%(class)s_related", verbose_name=_('Content type'))`
177. `lot_id = models.PositiveIntegerField(verbose_name=_('Lot ID'))`
178. `lot_object = generic.GenericForeignKey('content_type', 'lot_id')`
179. `amount = CurrencyField(max_digits=100, decimal_places=2, null=True, blank=True, verbose_name=_('Amount'))`
180. ``
181. `class Meta:`
182. `abstract = True`
183. `app_label = 'auction'`
184. `verbose_name = _('Bid item')`
185. `verbose_name_plural = _('Bid items')`
186. ``
187. `def is_locked(self):`
188. `return self.lot.is_locked`
189. ``
190. `@property`
191. `def lot(self):`
192. `return self.lot_object`