Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ###############
- # Urls in /home/bux/Projets/djStock/stock/urls.py
- from django.conf.urls.defaults import *
- urlpatterns = patterns('',
- [...]
- url(r'factory/view/(?P<factory_id>[0-9]+)$', 'stock.views.factory.view', name='factory_view'),
- )
- ###############
- # View in /home/bux/Projets/djStock/stock/views/factory.py
- [...]
- from djStock.stock.models import ProductFactory
- [...]
- def view(request, factory_id):
- factory = ProductFactory.objects.get(pk=factory_id)
- factory.products.all()
- ###############
- # Models in /home/bux/Projets/djStock/stock/models/XxXxx.py
- from stock.lib.ModelObject import ModelObject
- from django.db.models import Model
- from stock.models.Product import Product
- from django.db import models
- from stock.managers.models.filters.ProductFactoryFilter import ProductFactoryFilter as Filter
- from django.utils.translation import ugettext_lazy as _
- class ProductFactory(Model):
- objects = models.Manager()
- filter = Filter()
- name = models.CharField(_(u"Name"), max_length=128)
- products = models.ManyToManyField(Product, through='ProductFactoryProduct')
- components = models.ManyToManyField(Product, through='ProductFactoryComponent')
- def produceds_string(self):
- return ", ".join([product.name for product in self.products.all()])
- #return ", ".join([product.quantity + ' ' + product.name for product in self.products])
- def components_string(self):
- return ", ".join([product.name for product in self.components.all()])
- #return ", ".join([product.quantity + ' ' + product.name for product in self.components])
- def __str__(self):
- return self.name
- class Meta:
- app_label = 'stock'
- # other file with model
- from stock.lib.ModelObject import ModelObject
- #from stock.models.ProductFactory import ProductFactory
- #from stock.models.Product import Product
- from django.db import models
- from django.utils.translation import ugettext_lazy as _
- class ProductFactoryProduct(ModelObject):
- factory = models.ForeignKey('ProductFactory')
- product = models.ForeignKey('Product')
- quantity = models.IntegerField(_(u"Quantity"), default=1)
- class Meta:
- app_label = 'stock'
- # other file with model
- from stock.lib.ModelObject import ModelObject
- #from stock.models.ProductFactory import ProductFactory
- #from stock.models.Product import Product
- from django.db import models
- from django.utils.translation import ugettext_lazy as _
- class ProductFactoryComponent(ModelObject):
- factory = models.ForeignKey('ProductFactory')
- product = models.ForeignKey('Product')
- quantity = models.IntegerField(_(u"Quantity"), default=1)
- class Meta:
- app_label = 'stock'
- # other file with model
- from django.db import models
- from stock.lib.ModelObject import ModelObject
- from django.utils.translation import ugettext_lazy as _
- from stock.tools.array import get_string_in_tuples_choices
- from stock.models import Article
- from django.db.models import Count
- from stock.managers.models.filters.ProductFilter import ProductFilter as Filter
- from stock.lib.exceptions import DeleteIntegrityException, ExistingWaitingPurchaseException
- from stock.managers.objects.ActiveObjects import ActiveObjects
- from Purchase import Purchase
- from stock.lib.forms.validators import internal_code_is_unique_all, barcode_is_unique_product
- from ProductReference import ProductReference
- class Product(ModelObject):
- CONSUMTION_DIRECT = 'd'
- CONSUMTION_INDIRECT = 'i'
- CONSUMTION_TYPES = (
- (CONSUMTION_DIRECT, _(u"Direct")),
- (CONSUMTION_INDIRECT, _(u"Indirect"))
- )
- WORK_UNIT_CM = 'cm'
- WORK_UNIT_M = 'm'
- WORK_UNIT_L = 'l'
- WORK_UNIT_KG = 'kg'
- WORK_UNIT_UNIT = 'un'
- WORK_UNIT_FLASK = 'fl'
- WORK_UNIT_BOX = 'bo'
- WORK_UNIT_EMPTY = 'na'
- WORK_UNITS = (
- (WORK_UNIT_CM, _(u"Centimeter")),
- (WORK_UNIT_M, _(u"Meter")),
- (WORK_UNIT_L, _(u"Liter")),
- (WORK_UNIT_KG, _(u"Kilograme")),
- (WORK_UNIT_UNIT, _(u"Unit")),
- (WORK_UNIT_FLASK, _(u"Flacon")),
- (WORK_UNIT_BOX, _(u"Boite")),
- (WORK_UNIT_EMPTY, _(u"None")),
- )
- name = models.CharField(_(u"Designation"), max_length=128)
- packaging = models.IntegerField(_(u"Packaging"), default=1)
- comsumtion_type = models.CharField(max_length=1,
- choices=CONSUMTION_TYPES,
- default=CONSUMTION_DIRECT)
- work_unit = models.CharField(max_length=2,
- choices=WORK_UNITS,
- default=WORK_UNIT_EMPTY)
- brand = models.CharField(_(u"Brand"), max_length=32, blank=True)
- categorys = models.ManyToManyField('ProductCategory', db_table="stock_product_category_rel", blank=True, null=True)
- providers = models.ManyToManyField('Provider', through='ProductReference')
- quantity = models.IntegerField(_(u"Quantity"), default=0)
- quantity_with_ampute = models.IntegerField(_(u"Quantity with consumeds"), default=0)
- active = models.BooleanField(_(u"Active"), default=True)
- stock_limit = models.IntegerField(_(u"Stock limit"), default=0)
- purchase_quantity = models.IntegerField(_(u"Standart purchase quantity"), default=None, blank=True, null=True)
- code = models.CharField(_(u"Internal code"), max_length=32, blank=True, null=True)
- barcode = models.CharField(_(u"Barcode"), max_length=32, blank=True, null=True)
- objects = models.Manager()
- active_objects = ActiveObjects()
- filter = Filter()
- def delete(self):
- if self.bypass_integrity:
- super(Product, self).delete()
- else:
- if Purchase.objects.filter(complete=False, references__product=self).count():
- raise ExistingWaitingPurchaseException(_("You can't delete this Product: Purchase not complete containing this product"))
- self.active = False
- self.save()
- def __unicode__(self):
- return self.name
- def providers_string(self):
- return ", ".join(self.get_providers_names())
- def get_providers_names(self):
- providers_names = []
- for product_reference in ProductReference.objects.filter(product=self):
- providers_names.append(product_reference.provider.name)
- return providers_names
- def is_direct_consumtion(self):
- if self.comsumtion_type == self.CONSUMTION_DIRECT:
- return True
- return False
- def is_indirect_consumtion(self):
- if self.comsumtion_type == self.CONSUMTION_INDIRECT:
- return True
- return False
- def consumtion_type_string(self):
- return get_string_in_tuples_choices(self.CONSUMTION_TYPES, self.comsumtion_type)
- def work_unit_string(self):
- return get_string_in_tuples_choices(self.WORK_UNITS, self.work_unit)
- def quantity_and_ampute(self):
- if not self.is_direct_consumtion() and self.quantity != self.quantity_with_ampute:
- return True
- return False
- def quantity_string(self):
- if self.quantity_and_ampute():
- return "%d (%d)" % (self.quantity, self.quantity_with_ampute)
- return self.quantity
- def get_articles_ampute(self):
- return Article.objects.values('packaging')\
- .filter(product=self)\
- .exclude(packaging=self.packaging)\
- .annotate(pcount=Count('packaging'))\
- .order_by('-pcount')
- def stock_is_under_limit(self):
- if self.quantity_and_ampute():
- return True if self.quantity_with_ampute <= self.stock_limit else False
- return True if self.quantity <= self.stock_limit else False
- def get_bundles_codes(self):
- bundles = []
- articles_records = Article.objects.values('bundle').filter(product=self).distinct()
- for article_record in articles_records:
- bundles.append(article_record['bundle'])
- return bundles
- def get_bundles_codes_string(self):
- return ", ".join([bundle for bundle in self.get_bundles_codes() if bundle])
- def get_bundle_quantitys(self, bundle):
- return Article.objects.filter(product=self, bundle=bundle).count()
- def data(self):
- return {
- 'name': self.name,
- 'packaging': self.packaging,
- 'work_unit': self.work_unit,
- 'brand': self.brand
- }
- def validate_unique(self, *args, **kwargs):
- super(Product, self).validate_unique(*args, **kwargs)
- internal_code_is_unique_all(self)
- barcode_is_unique_product(self)
- @staticmethod
- def pre_delete(**kwargs):
- if not kwargs.get('instance').bypass_integrity:
- raise DeleteIntegrityException(kwargs.get('instance'))
- # @staticmethod
- # def pre_save(**kwargs):
- # # (raw) not applicable when fixture
- # if not kwargs.get('raw'):
- # if kwargs.get('instance').id:
- # check_forbidden_update_fields(Product.objects.get(pk=kwargs.get('instance').id), kwargs.get('instance'),\
- # ('packaging', 'comsumtion_type', 'work_unit', 'brand'))
- class Meta:
- app_label = 'stock'
- ordering = ('name',)
- ############
- # Full trace
- Internal Server Error: /stock/factory/view/1
- Traceback (most recent call last):
- File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
- response = callback(request, *callback_args, **callback_kwargs)
- File "/home/bux/Projets/djStock/stock/views/factory.py", line 64, in view
- factory.products.all()
- File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/manager.py", line 128, in all
- return self.get_query_set()
- File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 615, in get_query_set
- return super(ManyRelatedManager, self).get_query_set().using(db)._next_is_sticky().filter(**self.core_filters)
- File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/query.py", line 669, in filter
- return self._filter_or_exclude(False, *args, **kwargs)
- File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/query.py", line 687, in _filter_or_exclude
- clone.query.add_q(Q(*args, **kwargs))
- File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1271, in add_q
- can_reuse=used_aliases, force_having=force_having)
- File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1139, in add_filter
- process_extras=process_extras)
- File "/home/bux/.virtualenvs/djstock/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1337, in setup_joins
- "Choices are: %s" % (name, ", ".join(names)))
- 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