Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Advertising

Untitled
Sunday, July 1st, 2012 at 1:37:30pm MDT 

  1. class ItemQuerySet(models.query.QuerySet):
  2.     def in_stock(self):
  3.         return self.filter(qty__gt=0).annotate(qtydue=Sum('line_items__qtydue')).filter(qtydue__lt=F('qty'))
  4.  
  5.     def distinct_in_stock(self):
  6.         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'))
  7.  
  8.  
  9. class ItemManager(models.Manager):
  10.     def get_query_set(self):
  11.         return ItemQuerySet(self.model)
  12.  
  13.     def __getattr__(self, attr, *args):
  14.         try:
  15.             return getattr(self.__class__, attr, *args)
  16.         except AttributeError:
  17.             return getattr(self.get_query_set(), attr, *args)
  18.  
  19. class Item(models.Model):
  20.     id = models.BigIntegerField(primary_key=True)
  21.     version = models.IntegerField()
  22.     description = models.CharField(max_length=255, blank=True)
  23.     copy = models.CharField(max_length=255, blank=True)
  24.     qty = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
  25.     price = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
  26.     attr1 = models.CharField(max_length=255, blank=True)
  27.     attr2 = models.CharField(max_length=255, blank=True)
  28.     attr3 = models.CharField(max_length=255, blank=True)
  29.     attr4 = models.CharField(max_length=255, blank=True)
  30.     attr5 = models.CharField(max_length=255, blank=True)
  31.     attr6 = models.CharField(max_length=255, blank=True)
  32.     deposit = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
  33.     cost = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
  34.     minmargin = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
  35.     costlast = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
  36.     costnext = models.DecimalField(null=True, max_digits=19, decimal_places=2, blank=True)
  37.     deptcode = models.CharField(max_length=255, blank=True)
  38.     dept_id = models.BigIntegerField(null=True, blank=True)
  39.     firstrecd = models.DateField(null=True, blank=True)
  40.     lastrecd = models.DateField(null=True, blank=True)
  41.     firstsold = models.DateField(null=True, blank=True)
  42.     lastsold = models.DateField(null=True, blank=True)
  43.     saleunit = models.CharField(max_length=255, blank=True)
  44.     unitscase = models.IntegerField(null=True, blank=True)
  45.     upccode = models.CharField(max_length=255, blank=True)
  46.     vendor_id = models.BigIntegerField(null=True, blank=True)
  47.     vendorname = models.CharField(max_length=255, blank=True)
  48.     vendorreorderid = models.CharField(max_length=255, blank=True)
  49.     misc = models.BooleanField()
  50.     active = models.NullBooleanField(blank=True)
  51.  
  52.     keywords = models.ManyToManyField('Keyword', db_table='item_keywords')
  53.  
  54.     def __unicode__(self):
  55.         return u'{0}: {1}'.format(self.id, self.description)
  56.  
  57.     class Meta:
  58.         db_table = u'item'
  59.  
  60.     @property
  61.     def feedback(self):
  62.         from pumpernickel.feedback.models import ItemFeedback
  63.         return ItemFeedback.objects.filter(item_id=self.id)
  64.  
  65.     @property
  66.     def slug(self):
  67.         if not hasattr(self, '_slug'):
  68.             self._slug = pretty_slugify(self.description)
  69.  
  70.         return self._slug
  71.  
  72.     def get_absolute_url(self):
  73.         return reverse('shop-item', kwargs={'slug': self.slug, 'item_pk':self.id})
  74.  
  75.     @property
  76.     def stock(self):
  77.         cursor = connections['bread'].cursor()
  78.         cursor.execute('SELECT SUM(qtydue) FROM solineitem WHERE item_id=%s', [self.id])
  79.         row = cursor.fetchone()
  80.         try:
  81.             due = int(row[0])
  82.         except:
  83.             due = 0
  84.  
  85.         return max(0, self.qty - due)
  86.  
  87.     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.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



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.

worth-right
fantasy-obligation
fantasy-obligation