Advertising
- Untitled
- Sunday, July 1st, 2012 at 1:37:30pm MDT
- class ItemQuerySet(models.query.QuerySet):
- def in_stock(self):
- return self.filter(qty__gt=0).annotate(qtydue=Sum('line_items__qtydue')).filter(qtydue__lt=F('qty'))
- def distinct_in_stock(self):
- return self.values('description').annotate(qtydue=Sum('line_items__qtydue'), id=Min('id'), max_price=Max('price'), min_price=Min('price'), qtysum=Sum('qty')).filter(qtydue__lt=F('qtysum'))
- class ItemManager(models.Manager):
- def get_query_set(self):
- return ItemQuerySet(self.model)
- def __getattr__(self, attr, *args):
- try:
- return getattr(self.__class__, attr, *args)
- except AttributeError:
- return getattr(self.get_query_set(), attr, *args)
- class Item(models.Model):
- id = models.BigIntegerField(primary_key=True)
- version = models.IntegerField()
- description = models.CharField(max_length=255, blank=True)
- copy = models.CharField(max_length=255, blank=True)
- qty = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
- price = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
- attr1 = models.CharField(max_length=255, blank=True)
- attr2 = models.CharField(max_length=255, blank=True)
- attr3 = models.CharField(max_length=255, blank=True)
- attr4 = models.CharField(max_length=255, blank=True)
- attr5 = models.CharField(max_length=255, blank=True)
- attr6 = models.CharField(max_length=255, blank=True)
- deposit = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
- cost = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
- minmargin = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
- costlast = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
- costnext = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
- deptcode = models.CharField(max_length=255, blank=True)
- dept_id = models.BigIntegerField(null=True, blank=True)
- firstrecd = models.DateField(null=True, blank=True)
- lastrecd = models.DateField(null=True, blank=True)
- firstsold = models.DateField(null=True, blank=True)
- lastsold = models.DateField(null=True, blank=True)
- saleunit = models.CharField(max_length=255, blank=True)
- unitscase = models.IntegerField(null=True, blank=True)
- upccode = models.CharField(max_length=255, blank=True)
- vendor_id = models.BigIntegerField(null=True, blank=True)
- vendorname = models.CharField(max_length=255, blank=True)
- vendorreorderid = models.CharField(max_length=255, blank=True)
- misc = models.BooleanField()
- active = models.NullBooleanField(blank=True)
- keywords = models.ManyToManyField('Keyword', db_table='item_keywords')
- def __unicode__(self):
- return u'{0}: {1}'.format(self.id, self.description)
- class Meta:
- db_table = u'item'
- @property
- def feedback(self):
- from pumpernickel.feedback.models import ItemFeedback
- return ItemFeedback.objects.filter(item_id=self.id)
- @property
- def slug(self):
- if not hasattr(self, '_slug'):
- self._slug = pretty_slugify(self.description)
- return self._slug
- def get_absolute_url(self):
- return reverse('shop-item', kwargs={'slug': self.slug, 'item_pk':self.id})
- @property
- def stock(self):
- cursor = connections['bread'].cursor()
- cursor.execute('SELECT SUM(qtydue) FROM solineitem WHERE item_id=%s', [self.id])
- row = cursor.fetchone()
- try:
- due = int(row[0])
- except:
- due = 0
- return max(0, self.qty - due)
- objects = ItemManager()
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.