Guest User

Untitled

a guest
Dec 30th, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.85 KB | None | 0 0
  1. ###############
  2. # Urls in /home/bux/Projets/djStock/stock/urls.py
  3. from django.conf.urls.defaults import *
  4.  
  5. urlpatterns = patterns('',
  6.   [...]
  7.   url(r'factory/view/(?P<factory_id>[0-9]+)$', 'stock.views.factory.view', name='factory_view'),
  8. )
  9.  
  10. ###############
  11. # View in /home/bux/Projets/djStock/stock/views/factory.py
  12. [...]
  13. from djStock.stock.models import ProductFactory
  14.  
  15. [...]
  16. def view(request, factory_id):
  17.   factory = ProductFactory.objects.get(pk=factory_id)
  18.   factory.products.all()
  19.  
  20. ###############
  21. # Models in /home/bux/Projets/djStock/stock/models/XxXxx.py
  22.  
  23. from stock.lib.ModelObject import ModelObject
  24. from django.db.models import Model
  25. from stock.models.Product import Product
  26. from django.db import models
  27. from stock.managers.models.filters.ProductFactoryFilter import ProductFactoryFilter as Filter
  28. from django.utils.translation import ugettext_lazy as _
  29.  
  30.  
  31. class ProductFactory(Model):
  32.  
  33.   objects = models.Manager()
  34.   filter = Filter()
  35.  
  36.   name = models.CharField(_(u"Name"), max_length=128)
  37.   products = models.ManyToManyField(Product, through='ProductFactoryProduct')
  38.   components = models.ManyToManyField(Product, through='ProductFactoryComponent')
  39.  
  40.   def produceds_string(self):
  41.     return ", ".join([product.name for product in self.products.all()])
  42.     #return ", ".join([product.quantity + ' ' + product.name for product in self.products])
  43.  
  44.   def components_string(self):
  45.     return ", ".join([product.name for product in self.components.all()])
  46.     #return ", ".join([product.quantity + ' ' + product.name for product in self.components])
  47.  
  48.   def __str__(self):
  49.     return self.name
  50.  
  51.   class Meta:
  52.     app_label = 'stock'
  53.  
  54.  
  55. # other file with model
  56. from stock.lib.ModelObject import ModelObject
  57. #from stock.models.ProductFactory import ProductFactory
  58. #from stock.models.Product import Product
  59. from django.db import models
  60. from django.utils.translation import ugettext_lazy as _
  61.  
  62.  
  63. class ProductFactoryProduct(ModelObject):
  64.  
  65.   factory = models.ForeignKey('ProductFactory')
  66.   product = models.ForeignKey('Product')
  67.   quantity = models.IntegerField(_(u"Quantity"), default=1)
  68.  
  69.   class Meta:
  70.     app_label = 'stock'
  71.  
  72. # other file with model
  73. from stock.lib.ModelObject import ModelObject
  74. #from stock.models.ProductFactory import ProductFactory
  75. #from stock.models.Product import Product
  76. from django.db import models
  77. from django.utils.translation import ugettext_lazy as _
  78.  
  79.  
  80. class ProductFactoryComponent(ModelObject):
  81.  
  82.   factory = models.ForeignKey('ProductFactory')
  83.   product = models.ForeignKey('Product')
  84.   quantity = models.IntegerField(_(u"Quantity"), default=1)
  85.  
  86.   class Meta:
  87.     app_label = 'stock'
  88.  
  89. # other file with model
  90. from django.db import models
  91. from stock.lib.ModelObject import ModelObject
  92. from django.utils.translation import ugettext_lazy as _
  93. from stock.tools.array import get_string_in_tuples_choices
  94. from stock.models import Article
  95. from django.db.models import Count
  96. from stock.managers.models.filters.ProductFilter import ProductFilter as Filter
  97. from stock.lib.exceptions import DeleteIntegrityException, ExistingWaitingPurchaseException
  98. from stock.managers.objects.ActiveObjects import ActiveObjects
  99. from Purchase import Purchase
  100. from stock.lib.forms.validators import internal_code_is_unique_all, barcode_is_unique_product
  101. from ProductReference import ProductReference
  102.  
  103.  
  104. class Product(ModelObject):
  105.  
  106.   CONSUMTION_DIRECT = 'd'
  107.   CONSUMTION_INDIRECT = 'i'
  108.  
  109.   CONSUMTION_TYPES = (
  110.     (CONSUMTION_DIRECT, _(u"Direct")),
  111.     (CONSUMTION_INDIRECT, _(u"Indirect"))
  112.   )
  113.  
  114.   WORK_UNIT_CM = 'cm'
  115.   WORK_UNIT_M = 'm'
  116.   WORK_UNIT_L = 'l'
  117.   WORK_UNIT_KG = 'kg'
  118.   WORK_UNIT_UNIT = 'un'
  119.   WORK_UNIT_FLASK = 'fl'
  120.   WORK_UNIT_BOX = 'bo'
  121.   WORK_UNIT_EMPTY = 'na'
  122.  
  123.   WORK_UNITS = (
  124.     (WORK_UNIT_CM, _(u"Centimeter")),
  125.     (WORK_UNIT_M, _(u"Meter")),
  126.     (WORK_UNIT_L, _(u"Liter")),
  127.     (WORK_UNIT_KG, _(u"Kilograme")),
  128.     (WORK_UNIT_UNIT, _(u"Unit")),
  129.     (WORK_UNIT_FLASK, _(u"Flacon")),
  130.     (WORK_UNIT_BOX, _(u"Boite")),
  131.     (WORK_UNIT_EMPTY, _(u"None")),
  132.   )
  133.  
  134.   name = models.CharField(_(u"Designation"), max_length=128)
  135.   packaging = models.IntegerField(_(u"Packaging"), default=1)
  136.   comsumtion_type = models.CharField(max_length=1,
  137.                                      choices=CONSUMTION_TYPES,
  138.                                      default=CONSUMTION_DIRECT)
  139.   work_unit = models.CharField(max_length=2,
  140.                                      choices=WORK_UNITS,
  141.                                      default=WORK_UNIT_EMPTY)
  142.   brand = models.CharField(_(u"Brand"), max_length=32, blank=True)
  143.   categorys = models.ManyToManyField('ProductCategory', db_table="stock_product_category_rel", blank=True, null=True)
  144.   providers = models.ManyToManyField('Provider', through='ProductReference')
  145.   quantity = models.IntegerField(_(u"Quantity"), default=0)
  146.   quantity_with_ampute = models.IntegerField(_(u"Quantity with consumeds"), default=0)
  147.   active = models.BooleanField(_(u"Active"), default=True)
  148.   stock_limit = models.IntegerField(_(u"Stock limit"), default=0)
  149.   purchase_quantity = models.IntegerField(_(u"Standart purchase quantity"), default=None, blank=True, null=True)
  150.   code = models.CharField(_(u"Internal code"), max_length=32, blank=True, null=True)
  151.   barcode = models.CharField(_(u"Barcode"), max_length=32, blank=True, null=True)
  152.  
  153.   objects = models.Manager()
  154.   active_objects = ActiveObjects()
  155.   filter = Filter()
  156.  
  157.   def delete(self):
  158.     if self.bypass_integrity:
  159.       super(Product, self).delete()
  160.     else:
  161.       if Purchase.objects.filter(complete=False, references__product=self).count():
  162.         raise ExistingWaitingPurchaseException(_("You can't delete this Product: Purchase not complete containing this product"))
  163.       self.active = False
  164.       self.save()
  165.  
  166.   def __unicode__(self):
  167.     return self.name
  168.  
  169.   def providers_string(self):
  170.     return ", ".join(self.get_providers_names())
  171.  
  172.   def get_providers_names(self):
  173.     providers_names = []
  174.     for product_reference in ProductReference.objects.filter(product=self):
  175.       providers_names.append(product_reference.provider.name)
  176.     return providers_names
  177.  
  178.   def is_direct_consumtion(self):
  179.     if self.comsumtion_type == self.CONSUMTION_DIRECT:
  180.       return True
  181.     return False
  182.  
  183.   def is_indirect_consumtion(self):
  184.     if self.comsumtion_type == self.CONSUMTION_INDIRECT:
  185.       return True
  186.     return False
  187.  
  188.   def consumtion_type_string(self):
  189.     return get_string_in_tuples_choices(self.CONSUMTION_TYPES, self.comsumtion_type)
  190.  
  191.   def work_unit_string(self):
  192.     return get_string_in_tuples_choices(self.WORK_UNITS, self.work_unit)
  193.  
  194.   def quantity_and_ampute(self):
  195.     if not self.is_direct_consumtion() and self.quantity != self.quantity_with_ampute:
  196.       return True
  197.     return False
  198.  
  199.   def quantity_string(self):
  200.     if self.quantity_and_ampute():
  201.       return "%d (%d)" % (self.quantity, self.quantity_with_ampute)
  202.     return self.quantity
  203.  
  204.   def get_articles_ampute(self):
  205.     return Article.objects.values('packaging')\
  206.                           .filter(product=self)\
  207.                           .exclude(packaging=self.packaging)\
  208.                           .annotate(pcount=Count('packaging'))\
  209.                           .order_by('-pcount')
  210.  
  211.   def stock_is_under_limit(self):
  212.     if self.quantity_and_ampute():
  213.       return True if self.quantity_with_ampute <= self.stock_limit else False
  214.     return True if self.quantity <= self.stock_limit else False
  215.  
  216.   def get_bundles_codes(self):
  217.     bundles = []
  218.     articles_records = Article.objects.values('bundle').filter(product=self).distinct()
  219.     for article_record in articles_records:
  220.       bundles.append(article_record['bundle'])
  221.     return bundles
  222.  
  223.   def get_bundles_codes_string(self):
  224.     return ", ".join([bundle for bundle in self.get_bundles_codes() if bundle])
  225.  
  226.   def get_bundle_quantitys(self, bundle):
  227.     return Article.objects.filter(product=self, bundle=bundle).count()
  228.  
  229.   def data(self):
  230.     return {
  231.       'name': self.name,
  232.       'packaging': self.packaging,
  233.       'work_unit': self.work_unit,
  234.       'brand': self.brand
  235.     }
  236.  
  237.   def validate_unique(self, *args, **kwargs):
  238.     super(Product, self).validate_unique(*args, **kwargs)
  239.     internal_code_is_unique_all(self)
  240.     barcode_is_unique_product(self)
  241.  
  242.   @staticmethod
  243.   def pre_delete(**kwargs):
  244.     if not kwargs.get('instance').bypass_integrity:
  245.       raise DeleteIntegrityException(kwargs.get('instance'))
  246.  
  247. #   @staticmethod
  248. #   def pre_save(**kwargs):
  249. #     # (raw) not applicable when fixture
  250. #     if not kwargs.get('raw'):
  251. #       if kwargs.get('instance').id:
  252. #         check_forbidden_update_fields(Product.objects.get(pk=kwargs.get('instance').id), kwargs.get('instance'),\
  253. #                                       ('packaging', 'comsumtion_type', 'work_unit', 'brand'))
  254.  
  255.   class Meta:
  256.     app_label = 'stock'
  257.     ordering = ('name',)
  258.  
  259. ############
  260. # Full trace
  261.  
  262. Internal Server Error: /stock/factory/view/1
  263. Traceback (most recent call last):
  264.   File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
  265.     response = callback(request, *callback_args, **callback_kwargs)
  266.   File "/home/bux/Projets/djStock/stock/views/factory.py", line 64, in view
  267.     factory.products.all()
  268.   File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/manager.py", line 128, in all
  269.     return self.get_query_set()
  270.   File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 615, in get_query_set
  271.     return super(ManyRelatedManager, self).get_query_set().using(db)._next_is_sticky().filter(**self.core_filters)
  272.   File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/query.py", line 669, in filter
  273.     return self._filter_or_exclude(False, *args, **kwargs)
  274.   File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/query.py", line 687, in _filter_or_exclude
  275.     clone.query.add_q(Q(*args, **kwargs))
  276.   File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1271, in add_q
  277.     can_reuse=used_aliases, force_having=force_having)
  278.   File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1139, in add_filter
  279.     process_extras=process_extras)
  280.   File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1337, in setup_joins
  281.     "Choices are: %s" % (name, ", ".join(names)))
  282. FieldError: Cannot resolve keyword 'productfactory' into field. Choices are: active, article, barcode, brand, categorys, code, comsumtion_type, id, name, packaging, productreference, provider, providers, purchase_quantity, quantity, quantity_with_ampute, stock_limit, stockmovement, stockmovementproduct, work_unit
Advertisement
Add Comment
Please, Sign In to add comment