Reindent stock app

This commit is contained in:
Skia 2017-04-25 08:57:07 +02:00
parent 20deda8a8e
commit df20bf6dde
3 changed files with 375 additions and 375 deletions

View File

@ -7,80 +7,80 @@ from django.conf import settings
from counter.models import Counter, ProductType from counter.models import Counter, ProductType
class Stock(models.Model): class Stock(models.Model):
""" """
The Stock class, this one is used to know how many products are left for a specific counter The Stock class, this one is used to know how many products are left for a specific counter
""" """
name = models.CharField(_('name'), max_length=64) name = models.CharField(_('name'), max_length=64)
counter = models.OneToOneField(Counter, verbose_name=_('counter'), related_name='stock') counter = models.OneToOneField(Counter, verbose_name=_('counter'), related_name='stock')
def __str__(self): def __str__(self):
return "%s (%s)" % (self.name, self.counter) return "%s (%s)" % (self.name, self.counter)
def get_absolute_url(self): def get_absolute_url(self):
return reverse('stock:list') return reverse('stock:list')
def can_be_viewed_by(self, user): def can_be_viewed_by(self, user):
return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID) return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID)
class StockItem(models.Model): class StockItem(models.Model):
""" """
The StockItem class, element of the stock The StockItem class, element of the stock
""" """
name = models.CharField(_('name'), max_length=64) name = models.CharField(_('name'), max_length=64)
unit_quantity = models.IntegerField(_('unit quantity'), default=0, help_text=_('number of element in one box')) unit_quantity = models.IntegerField(_('unit quantity'), default=0, help_text=_('number of element in one box'))
effective_quantity = models.IntegerField(_('effective quantity'), default=0, help_text=_('number of box')) effective_quantity = models.IntegerField(_('effective quantity'), default=0, help_text=_('number of box'))
minimal_quantity = models.IntegerField(_('minimal quantity'), default=1, minimal_quantity = models.IntegerField(_('minimal quantity'), default=1,
help_text=_('if the effective quantity is less than the minimal, item is added to the shopping list')) help_text=_('if the effective quantity is less than the minimal, item is added to the shopping list'))
type = models.ForeignKey(ProductType, related_name="stock_items", verbose_name=_("type"), null=True, blank=True, type = models.ForeignKey(ProductType, related_name="stock_items", verbose_name=_("type"), null=True, blank=True,
on_delete=models.SET_NULL) on_delete=models.SET_NULL)
stock_owner = models.ForeignKey(Stock, related_name="items") stock_owner = models.ForeignKey(Stock, related_name="items")
def __str__(self): def __str__(self):
return "%s" % (self.name) return "%s" % (self.name)
def get_absolute_url(self): def get_absolute_url(self):
return reverse('stock:items_list', kwargs={'stock_id':self.stock_owner.id}) return reverse('stock:items_list', kwargs={'stock_id':self.stock_owner.id})
def can_be_viewed_by(self, user): def can_be_viewed_by(self, user):
return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID) return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID)
class ShoppingList(models.Model): class ShoppingList(models.Model):
""" """
The ShoppingList class, used to make an history of the shopping lists The ShoppingList class, used to make an history of the shopping lists
""" """
date = models.DateTimeField(_('date')) date = models.DateTimeField(_('date'))
name = models.CharField(_('name'), max_length=64) name = models.CharField(_('name'), max_length=64)
todo = models.BooleanField(_('todo')) todo = models.BooleanField(_('todo'))
comment = models.TextField(_('comment'), null=True, blank=True) comment = models.TextField(_('comment'), null=True, blank=True)
stock_owner = models.ForeignKey(Stock, null=True, related_name="shopping_lists") stock_owner = models.ForeignKey(Stock, null=True, related_name="shopping_lists")
def __str__(self): def __str__(self):
return "%s (%s)" % (self.name, self.date) return "%s (%s)" % (self.name, self.date)
def get_absolute_url(self): def get_absolute_url(self):
return reverse('stock:shoppinglist_list') return reverse('stock:shoppinglist_list')
def can_be_viewed_by(self, user): def can_be_viewed_by(self, user):
return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID) return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID)
class ShoppingListItem(models.Model): class ShoppingListItem(models.Model):
""" """
""" """
shopping_lists = models.ManyToManyField(ShoppingList, verbose_name=_("shopping lists"), related_name="shopping_items_to_buy") shopping_lists = models.ManyToManyField(ShoppingList, verbose_name=_("shopping lists"), related_name="shopping_items_to_buy")
stockitem_owner = models.ForeignKey(StockItem, related_name="shopping_item", null=True) stockitem_owner = models.ForeignKey(StockItem, related_name="shopping_item", null=True)
name = models.CharField(_('name'), max_length=64) name = models.CharField(_('name'), max_length=64)
type = models.ForeignKey(ProductType, related_name="shoppinglist_items", verbose_name=_("type"), null=True, blank=True, type = models.ForeignKey(ProductType, related_name="shoppinglist_items", verbose_name=_("type"), null=True, blank=True,
on_delete=models.SET_NULL) on_delete=models.SET_NULL)
tobuy_quantity = models.IntegerField(_('quantity to buy'), default=6, help_text=_("quantity to buy during the next shopping session")) tobuy_quantity = models.IntegerField(_('quantity to buy'), default=6, help_text=_("quantity to buy during the next shopping session"))
bought_quantity = models.IntegerField(_('quantity bought'), default=0, help_text=_("quantity bought during the last shopping session")) bought_quantity = models.IntegerField(_('quantity bought'), default=0, help_text=_("quantity bought during the last shopping session"))
def __str__(self): def __str__(self):
return "%s - %s" % (self.name, self.shopping_lists.first()) return "%s - %s" % (self.name, self.shopping_lists.first())
def can_be_viewed_by(self, user): def can_be_viewed_by(self, user):
return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID) return user.is_in_group(settings.SITH_GROUP_COUNTER_ADMIN_ID)
def get_absolute_url(self):
return reverse('stock:shoppinglist_list')
def get_absolute_url(self):
return reverse('stock:shoppinglist_list')

View File

@ -3,28 +3,28 @@ from django.conf.urls import include, url
from stock.views import * from stock.views import *
urlpatterns = [ urlpatterns = [
#Stock urls #Stock urls
url(r'^new/counter/(?P<counter_id>[0-9]+)$', StockCreateView.as_view(), name='new'), url(r'^new/counter/(?P<counter_id>[0-9]+)$', StockCreateView.as_view(), name='new'),
url(r'^edit/(?P<stock_id>[0-9]+)$', StockEditView.as_view(), name='edit'), url(r'^edit/(?P<stock_id>[0-9]+)$', StockEditView.as_view(), name='edit'),
url(r'^list$', StockListView.as_view(), name='list'), url(r'^list$', StockListView.as_view(), name='list'),
# StockItem urls # StockItem urls
url(r'^(?P<stock_id>[0-9]+)$', StockItemList.as_view(), name='items_list'), url(r'^(?P<stock_id>[0-9]+)$', StockItemList.as_view(), name='items_list'),
url(r'^(?P<stock_id>[0-9]+)/stockItem/newItem$', StockItemCreateView.as_view(), name='new_item'), url(r'^(?P<stock_id>[0-9]+)/stockItem/newItem$', StockItemCreateView.as_view(), name='new_item'),
url(r'^stockItem/(?P<item_id>[0-9]+)/edit$', StockItemEditView.as_view(), name='edit_item'), url(r'^stockItem/(?P<item_id>[0-9]+)/edit$', StockItemEditView.as_view(), name='edit_item'),
url(r'^(?P<stock_id>[0-9]+)/stockItem/takeItems$', StockTakeItemsBaseFormView.as_view(), name='take_items'), url(r'^(?P<stock_id>[0-9]+)/stockItem/takeItems$', StockTakeItemsBaseFormView.as_view(), name='take_items'),
# ShoppingList urls # ShoppingList urls
url(r'^(?P<stock_id>[0-9]+)/shoppingList/list$', StockShoppingListView.as_view(), name='shoppinglist_list'), url(r'^(?P<stock_id>[0-9]+)/shoppingList/list$', StockShoppingListView.as_view(), name='shoppinglist_list'),
url(r'^(?P<stock_id>[0-9]+)/shoppingList/create$', StockItemQuantityBaseFormView.as_view(), name='shoppinglist_create'), url(r'^(?P<stock_id>[0-9]+)/shoppingList/create$', StockItemQuantityBaseFormView.as_view(), name='shoppinglist_create'),
url(r'^(?P<stock_id>[0-9]+)/shoppingList/(?P<shoppinglist_id>[0-9]+)/items$', StockShoppingListItemListView.as_view(), url(r'^(?P<stock_id>[0-9]+)/shoppingList/(?P<shoppinglist_id>[0-9]+)/items$', StockShoppingListItemListView.as_view(),
name='shoppinglist_items'), name='shoppinglist_items'),
url(r'^(?P<stock_id>[0-9]+)/shoppingList/(?P<shoppinglist_id>[0-9]+)/delete$', StockShoppingListDeleteView.as_view(), url(r'^(?P<stock_id>[0-9]+)/shoppingList/(?P<shoppinglist_id>[0-9]+)/delete$', StockShoppingListDeleteView.as_view(),
name='shoppinglist_delete'), name='shoppinglist_delete'),
url(r'^(?P<stock_id>[0-9]+)/shoppingList/(?P<shoppinglist_id>[0-9]+)/setDone$', StockShopppingListSetDone.as_view(), url(r'^(?P<stock_id>[0-9]+)/shoppingList/(?P<shoppinglist_id>[0-9]+)/setDone$', StockShopppingListSetDone.as_view(),
name='shoppinglist_set_done'), name='shoppinglist_set_done'),
url(r'^(?P<stock_id>[0-9]+)/shoppingList/(?P<shoppinglist_id>[0-9]+)/setTodo$', StockShopppingListSetTodo.as_view(), url(r'^(?P<stock_id>[0-9]+)/shoppingList/(?P<shoppinglist_id>[0-9]+)/setTodo$', StockShopppingListSetTodo.as_view(),
name='shoppinglist_set_todo'), name='shoppinglist_set_todo'),
url(r'^(?P<stock_id>[0-9]+)/shoppingList/(?P<shoppinglist_id>[0-9]+)/updateStock$', StockUpdateAfterShopppingBaseFormView.as_view(), url(r'^(?P<stock_id>[0-9]+)/shoppingList/(?P<shoppinglist_id>[0-9]+)/updateStock$', StockUpdateAfterShopppingBaseFormView.as_view(),
name='update_after_shopping'), name='update_after_shopping'),
] ]

View File

@ -19,211 +19,211 @@ from stock.models import Stock, StockItem, ShoppingList, ShoppingListItem
class StockItemList(CounterAdminTabsMixin, CanCreateMixin, ListView): class StockItemList(CounterAdminTabsMixin, CanCreateMixin, ListView):
""" """
The stockitems list view for the counter owner The stockitems list view for the counter owner
""" """
model = Stock model = Stock
template_name = 'stock/stock_item_list.jinja' template_name = 'stock/stock_item_list.jinja'
pk_url_kwarg = "stock_id" pk_url_kwarg = "stock_id"
current_tab = "stocks" current_tab = "stocks"
def get_context_data(self):
ret = super(StockItemList, self).get_context_data()
if 'stock_id' in self.kwargs.keys():
ret['stock'] = Stock.objects.filter(id=self.kwargs['stock_id']).first();
return ret
def get_context_data(self):
ret = super(StockItemList, self).get_context_data()
if 'stock_id' in self.kwargs.keys():
ret['stock'] = Stock.objects.filter(id=self.kwargs['stock_id']).first();
return ret
class StockListView(CounterAdminTabsMixin, CanViewMixin, ListView): class StockListView(CounterAdminTabsMixin, CanViewMixin, ListView):
""" """
A list view for the admins A list view for the admins
""" """
model = Stock model = Stock
template_name = 'stock/stock_list.jinja' template_name = 'stock/stock_list.jinja'
current_tab = "stocks" current_tab = "stocks"
class StockEditForm(forms.ModelForm): class StockEditForm(forms.ModelForm):
""" """
A form to change stock's characteristics A form to change stock's characteristics
""" """
class Meta: class Meta:
model = Stock model = Stock
fields = ['name', 'counter'] fields = ['name', 'counter']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(StockEditForm, self).__init__(*args, **kwargs) super(StockEditForm, self).__init__(*args, **kwargs)
def save(self, *args, **kwargs):
return super(StockEditForm, self).save(*args, **kwargs)
def save(self, *args, **kwargs):
return super(StockEditForm, self).save(*args, **kwargs)
class StockEditView(CounterAdminTabsMixin, CanEditPropMixin, UpdateView): class StockEditView(CounterAdminTabsMixin, CanEditPropMixin, UpdateView):
""" """
An edit view for the stock An edit view for the stock
""" """
model = Stock model = Stock
form_class = modelform_factory(Stock, fields=['name', 'counter']) form_class = modelform_factory(Stock, fields=['name', 'counter'])
pk_url_kwarg = "stock_id" pk_url_kwarg = "stock_id"
template_name = 'core/edit.jinja' template_name = 'core/edit.jinja'
current_tab = "stocks" current_tab = "stocks"
class StockItemEditView(CounterAdminTabsMixin, CanEditPropMixin, UpdateView): class StockItemEditView(CounterAdminTabsMixin, CanEditPropMixin, UpdateView):
""" """
An edit view for a stock item An edit view for a stock item
""" """
model = StockItem model = StockItem
form_class = modelform_factory(StockItem, fields=['name', 'unit_quantity', 'effective_quantity', 'minimal_quantity', 'type', 'stock_owner']) form_class = modelform_factory(StockItem, fields=['name', 'unit_quantity', 'effective_quantity', 'minimal_quantity', 'type', 'stock_owner'])
pk_url_kwarg = "item_id" pk_url_kwarg = "item_id"
template_name = 'core/edit.jinja' template_name = 'core/edit.jinja'
current_tab = "stocks" current_tab = "stocks"
class StockCreateView(CounterAdminTabsMixin, CanCreateMixin, CreateView): class StockCreateView(CounterAdminTabsMixin, CanCreateMixin, CreateView):
""" """
A create view for a new Stock A create view for a new Stock
""" """
model = Stock model = Stock
form_class = modelform_factory(Stock, fields=['name', 'counter']) form_class = modelform_factory(Stock, fields=['name', 'counter'])
template_name = 'core/create.jinja' template_name = 'core/create.jinja'
pk_url_kwarg = "counter_id" pk_url_kwarg = "counter_id"
current_tab = "stocks" current_tab = "stocks"
success_url = reverse_lazy('stock:list') success_url = reverse_lazy('stock:list')
def get_initial(self):
ret = super(StockCreateView, self).get_initial()
if 'counter_id' in self.kwargs.keys():
ret['counter'] = self.kwargs['counter_id']
return ret
def get_initial(self):
ret = super(StockCreateView, self).get_initial()
if 'counter_id' in self.kwargs.keys():
ret['counter'] = self.kwargs['counter_id']
return ret
class StockItemCreateView(CounterAdminTabsMixin, CanCreateMixin, CreateView): class StockItemCreateView(CounterAdminTabsMixin, CanCreateMixin, CreateView):
""" """
A create view for a new StockItem A create view for a new StockItem
""" """
model = StockItem model = StockItem
form_class = modelform_factory(StockItem, fields=['name', 'unit_quantity', 'effective_quantity', 'minimal_quantity', 'type', 'stock_owner']) form_class = modelform_factory(StockItem, fields=['name', 'unit_quantity', 'effective_quantity', 'minimal_quantity', 'type', 'stock_owner'])
template_name = 'core/create.jinja' template_name = 'core/create.jinja'
pk_url_kwarg = "stock_id" pk_url_kwarg = "stock_id"
current_tab = "stocks" current_tab = "stocks"
def get_initial(self): def get_initial(self):
ret = super(StockItemCreateView, self).get_initial() ret = super(StockItemCreateView, self).get_initial()
if 'stock_id' in self.kwargs.keys(): if 'stock_id' in self.kwargs.keys():
ret['stock_owner'] = self.kwargs['stock_id'] ret['stock_owner'] = self.kwargs['stock_id']
return ret return ret
def get_success_url(self): def get_success_url(self):
return reverse_lazy('stock:items_list', kwargs={'stock_id':self.object.stock_owner.id}) return reverse_lazy('stock:items_list', kwargs={'stock_id':self.object.stock_owner.id})
class StockShoppingListView(CounterAdminTabsMixin, CanViewMixin, ListView): class StockShoppingListView(CounterAdminTabsMixin, CanViewMixin, ListView):
""" """
A list view for the people to know the item to buy A list view for the people to know the item to buy
""" """
model = Stock model = Stock
template_name = "stock/stock_shopping_list.jinja" template_name = "stock/stock_shopping_list.jinja"
pk_url_kwarg = "stock_id" pk_url_kwarg = "stock_id"
current_tab = "stocks" current_tab = "stocks"
def get_context_data(self): def get_context_data(self):
ret = super(StockShoppingListView, self).get_context_data() ret = super(StockShoppingListView, self).get_context_data()
if 'stock_id' in self.kwargs.keys(): if 'stock_id' in self.kwargs.keys():
ret['stock'] = Stock.objects.filter(id=self.kwargs['stock_id']).first(); ret['stock'] = Stock.objects.filter(id=self.kwargs['stock_id']).first();
return ret return ret
class StockItemQuantityForm(forms.BaseForm): class StockItemQuantityForm(forms.BaseForm):
def clean(self): def clean(self):
with transaction.atomic(): with transaction.atomic():
self.stock = Stock.objects.filter(id=self.stock_id).first() self.stock = Stock.objects.filter(id=self.stock_id).first()
shopping_list = ShoppingList(name="Courses "+self.stock.counter.name, date=timezone.now(), todo=True) shopping_list = ShoppingList(name="Courses "+self.stock.counter.name, date=timezone.now(), todo=True)
shopping_list.save() shopping_list.save()
shopping_list.stock_owner = self.stock shopping_list.stock_owner = self.stock
shopping_list.save() shopping_list.save()
for k,t in self.cleaned_data.items(): for k,t in self.cleaned_data.items():
if k == 'name': if k == 'name':
shopping_list.name = t shopping_list.name = t
shopping_list.save() shopping_list.save()
elif k == "comment": elif k == "comment":
shopping_list.comment = t shopping_list.comment = t
shopping_list.save() shopping_list.save()
else: else:
if t > 0 : if t > 0 :
item_id = int(k[5:]) item_id = int(k[5:])
item = StockItem.objects.filter(id=item_id).first() item = StockItem.objects.filter(id=item_id).first()
shoppinglist_item = ShoppingListItem(stockitem_owner=item, name=item.name, type=item.type, tobuy_quantity=t) shoppinglist_item = ShoppingListItem(stockitem_owner=item, name=item.name, type=item.type, tobuy_quantity=t)
shoppinglist_item.save() shoppinglist_item.save()
shoppinglist_item.shopping_lists.add(shopping_list) shoppinglist_item.shopping_lists.add(shopping_list)
shoppinglist_item.save() shoppinglist_item.save()
return self.cleaned_data return self.cleaned_data
class StockItemQuantityBaseFormView(CounterAdminTabsMixin, CanEditMixin, DetailView, BaseFormView): class StockItemQuantityBaseFormView(CounterAdminTabsMixin, CanEditMixin, DetailView, BaseFormView):
""" """
docstring for StockItemOutList docstring for StockItemOutList
""" """
model = StockItem model = StockItem
template_name = "stock/shopping_list_quantity.jinja" template_name = "stock/shopping_list_quantity.jinja"
pk_url_kwarg = "stock_id" pk_url_kwarg = "stock_id"
current_tab = "stocks" current_tab = "stocks"
def get_form_class(self): def get_form_class(self):
fields = OrderedDict() fields = OrderedDict()
kwargs = {} kwargs = {}
fields['name'] = forms.CharField(max_length=30, required=True, label=_('Shopping list name')) fields['name'] = forms.CharField(max_length=30, required=True, label=_('Shopping list name'))
for t in ProductType.objects.order_by('name').all(): for t in ProductType.objects.order_by('name').all():
for i in self.stock.items.filter(type=t).order_by('name').all(): for i in self.stock.items.filter(type=t).order_by('name').all():
if i.effective_quantity <= i.minimal_quantity: if i.effective_quantity <= i.minimal_quantity:
field_name = "item-%s" % (str(i.id)) field_name = "item-%s" % (str(i.id))
fields[field_name] = forms.IntegerField(required=True, label=str(i), initial=0, fields[field_name] = forms.IntegerField(required=True, label=str(i), initial=0,
help_text=_(str(i.effective_quantity)+" left")) help_text=_(str(i.effective_quantity)+" left"))
fields['comment'] = forms.CharField(widget=forms.Textarea(attrs={"placeholder":_("Add here, items to buy that are not reference as a stock item (example : sponge, knife, mugs ...)")}), fields['comment'] = forms.CharField(widget=forms.Textarea(attrs={"placeholder":_("Add here, items to buy that are not reference as a stock item (example : sponge, knife, mugs ...)")}),
required=False, label=_("Comments")) required=False, label=_("Comments"))
kwargs['stock_id'] = self.stock.id kwargs['stock_id'] = self.stock.id
kwargs['base_fields'] = fields kwargs['base_fields'] = fields
return type('StockItemQuantityForm', (StockItemQuantityForm,), kwargs) return type('StockItemQuantityForm', (StockItemQuantityForm,), kwargs)
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
""" """
Simple get view Simple get view
""" """
self.stock = Stock.objects.filter(id=self.kwargs['stock_id']).first() self.stock = Stock.objects.filter(id=self.kwargs['stock_id']).first()
return super(StockItemQuantityBaseFormView, self).get(request, *args, **kwargs) return super(StockItemQuantityBaseFormView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
"""
Handle the many possibilities of the post request
"""
self.object = self.get_object()
self.stock = Stock.objects.filter(id=self.kwargs['stock_id']).first()
return super(StockItemQuantityBaseFormView, self).post(request, *args, **kwargs)
def form_valid(self, form): def post(self, request, *args, **kwargs):
return super(StockItemQuantityBaseFormView, self).form_valid(form) """
Handle the many possibilities of the post request
"""
self.object = self.get_object()
self.stock = Stock.objects.filter(id=self.kwargs['stock_id']).first()
return super(StockItemQuantityBaseFormView, self).post(request, *args, **kwargs)
def get_context_data(self, **kwargs): def form_valid(self, form):
kwargs = super(StockItemQuantityBaseFormView, self).get_context_data(**kwargs) return super(StockItemQuantityBaseFormView, self).form_valid(form)
if 'form' not in kwargs.keys():
kwargs['form'] = self.get_form()
kwargs['stock'] = self.stock
return kwargs
def get_success_url(self): def get_context_data(self, **kwargs):
return reverse_lazy('stock:shoppinglist_list', args=self.args, kwargs=self.kwargs) kwargs = super(StockItemQuantityBaseFormView, self).get_context_data(**kwargs)
if 'form' not in kwargs.keys():
kwargs['form'] = self.get_form()
kwargs['stock'] = self.stock
return kwargs
def get_success_url(self):
return reverse_lazy('stock:shoppinglist_list', args=self.args, kwargs=self.kwargs)
class StockShoppingListItemListView(CounterAdminTabsMixin, CanViewMixin, ListView): class StockShoppingListItemListView(CounterAdminTabsMixin, CanViewMixin, ListView):
"""docstring for StockShoppingListItemListView""" """docstring for StockShoppingListItemListView"""
model = ShoppingList model = ShoppingList
template_name = "stock/shopping_list_items.jinja" template_name = "stock/shopping_list_items.jinja"
pk_url_kwarg = "shoppinglist_id" pk_url_kwarg = "shoppinglist_id"
current_tab = "stocks" current_tab = "stocks"
def get_context_data(self): def get_context_data(self):
ret = super(StockShoppingListItemListView, self).get_context_data() ret = super(StockShoppingListItemListView, self).get_context_data()
if 'shoppinglist_id' in self.kwargs.keys(): if 'shoppinglist_id' in self.kwargs.keys():
ret['shoppinglist'] = ShoppingList.objects.filter(id=self.kwargs['shoppinglist_id']).first(); ret['shoppinglist'] = ShoppingList.objects.filter(id=self.kwargs['shoppinglist_id']).first();
return ret return ret
class StockShoppingListDeleteView(CounterAdminTabsMixin, CanEditMixin, DeleteView): class StockShoppingListDeleteView(CounterAdminTabsMixin, CanEditMixin, DeleteView):
""" """
@ -235,7 +235,7 @@ class StockShoppingListDeleteView(CounterAdminTabsMixin, CanEditMixin, DeleteVie
current_tab = "stocks" current_tab = "stocks"
def get_success_url(self): def get_success_url(self):
return reverse_lazy('stock:shoppinglist_list', kwargs={'stock_id':self.object.stock_owner.id}) return reverse_lazy('stock:shoppinglist_list', kwargs={'stock_id':self.object.stock_owner.id})
class StockShopppingListSetDone(CanEditMixin, DetailView): class StockShopppingListSetDone(CanEditMixin, DetailView):
@ -275,143 +275,143 @@ class StockShopppingListSetTodo(CanEditMixin, DetailView):
class StockUpdateAfterShopppingForm(forms.BaseForm): class StockUpdateAfterShopppingForm(forms.BaseForm):
def clean(self): def clean(self):
with transaction.atomic(): with transaction.atomic():
self.shoppinglist = ShoppingList.objects.filter(id=self.shoppinglist_id).first() self.shoppinglist = ShoppingList.objects.filter(id=self.shoppinglist_id).first()
for k,t in self.cleaned_data.items(): for k,t in self.cleaned_data.items():
shoppinglist_item_id = int(k[5:]) shoppinglist_item_id = int(k[5:])
if int(t) > 0 : if int(t) > 0 :
shoppinglist_item = ShoppingListItem.objects.filter(id=shoppinglist_item_id).first() shoppinglist_item = ShoppingListItem.objects.filter(id=shoppinglist_item_id).first()
shoppinglist_item.bought_quantity = int(t) shoppinglist_item.bought_quantity = int(t)
shoppinglist_item.save() shoppinglist_item.save()
shoppinglist_item.stockitem_owner.effective_quantity += int(t) shoppinglist_item.stockitem_owner.effective_quantity += int(t)
shoppinglist_item.stockitem_owner.save() shoppinglist_item.stockitem_owner.save()
self.shoppinglist.todo = False self.shoppinglist.todo = False
self.shoppinglist.save() self.shoppinglist.save()
return self.cleaned_data return self.cleaned_data
class StockUpdateAfterShopppingBaseFormView(CounterAdminTabsMixin, CanEditMixin, DetailView, BaseFormView): class StockUpdateAfterShopppingBaseFormView(CounterAdminTabsMixin, CanEditMixin, DetailView, BaseFormView):
""" """
docstring for StockUpdateAfterShopppingBaseFormView docstring for StockUpdateAfterShopppingBaseFormView
""" """
model = ShoppingList model = ShoppingList
template_name = "stock/update_after_shopping.jinja" template_name = "stock/update_after_shopping.jinja"
pk_url_kwarg = "shoppinglist_id" pk_url_kwarg = "shoppinglist_id"
current_tab = "stocks" current_tab = "stocks"
def get_form_class(self): def get_form_class(self):
fields = OrderedDict() fields = OrderedDict()
kwargs = {} kwargs = {}
for t in ProductType.objects.order_by('name').all(): for t in ProductType.objects.order_by('name').all():
for i in self.shoppinglist.shopping_items_to_buy.filter(type=t).order_by('name').all(): for i in self.shoppinglist.shopping_items_to_buy.filter(type=t).order_by('name').all():
field_name = "item-%s" % (str(i.id)) field_name = "item-%s" % (str(i.id))
fields[field_name] = forms.CharField(max_length=30, required=True, label=str(i), fields[field_name] = forms.CharField(max_length=30, required=True, label=str(i),
help_text=_(str(i.tobuy_quantity) + " asked")) help_text=_(str(i.tobuy_quantity) + " asked"))
kwargs['shoppinglist_id'] = self.shoppinglist.id kwargs['shoppinglist_id'] = self.shoppinglist.id
kwargs['base_fields'] = fields kwargs['base_fields'] = fields
return type('StockUpdateAfterShopppingForm', (StockUpdateAfterShopppingForm,), kwargs) return type('StockUpdateAfterShopppingForm', (StockUpdateAfterShopppingForm,), kwargs)
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
self.shoppinglist = ShoppingList.objects.filter(id=self.kwargs['shoppinglist_id']).first() self.shoppinglist = ShoppingList.objects.filter(id=self.kwargs['shoppinglist_id']).first()
return super(StockUpdateAfterShopppingBaseFormView, self).get(request, *args, **kwargs) return super(StockUpdateAfterShopppingBaseFormView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
"""
Handle the many possibilities of the post request
"""
self.object = self.get_object()
self.shoppinglist = ShoppingList.objects.filter(id=self.kwargs['shoppinglist_id']).first()
return super(StockUpdateAfterShopppingBaseFormView, self).post(request, *args, **kwargs)
def form_valid(self, form): def post(self, request, *args, **kwargs):
""" """
We handle here the redirection Handle the many possibilities of the post request
""" """
return super(StockUpdateAfterShopppingBaseFormView, self).form_valid(form) self.object = self.get_object()
self.shoppinglist = ShoppingList.objects.filter(id=self.kwargs['shoppinglist_id']).first()
return super(StockUpdateAfterShopppingBaseFormView, self).post(request, *args, **kwargs)
def get_context_data(self, **kwargs): def form_valid(self, form):
kwargs = super(StockUpdateAfterShopppingBaseFormView, self).get_context_data(**kwargs) """
if 'form' not in kwargs.keys(): We handle here the redirection
kwargs['form'] = self.get_form() """
kwargs['shoppinglist'] = self.shoppinglist return super(StockUpdateAfterShopppingBaseFormView, self).form_valid(form)
kwargs['stock'] = self.shoppinglist.stock_owner
return kwargs
def get_success_url(self): def get_context_data(self, **kwargs):
self.kwargs.pop('shoppinglist_id', None) kwargs = super(StockUpdateAfterShopppingBaseFormView, self).get_context_data(**kwargs)
return reverse_lazy('stock:shoppinglist_list', args=self.args, kwargs=self.kwargs) if 'form' not in kwargs.keys():
kwargs['form'] = self.get_form()
kwargs['shoppinglist'] = self.shoppinglist
kwargs['stock'] = self.shoppinglist.stock_owner
return kwargs
def get_success_url(self):
self.kwargs.pop('shoppinglist_id', None)
return reverse_lazy('stock:shoppinglist_list', args=self.args, kwargs=self.kwargs)
class StockTakeItemsForm(forms.BaseForm): class StockTakeItemsForm(forms.BaseForm):
""" """
docstring for StockTakeItemsFormView docstring for StockTakeItemsFormView
""" """
def clean(self): def clean(self):
with transaction.atomic(): with transaction.atomic():
for k,t in self.cleaned_data.items(): for k,t in self.cleaned_data.items():
item_id = int(k[5:]) item_id = int(k[5:])
if t > 0 : if t > 0 :
item = StockItem.objects.filter(id=item_id).first() item = StockItem.objects.filter(id=item_id).first()
item.effective_quantity -= t item.effective_quantity -= t
item.save() item.save()
return self.cleaned_data return self.cleaned_data
class StockTakeItemsBaseFormView(CounterTabsMixin, CanEditMixin, DetailView, BaseFormView): class StockTakeItemsBaseFormView(CounterTabsMixin, CanEditMixin, DetailView, BaseFormView):
""" """
docstring for StockTakeItemsBaseFormView docstring for StockTakeItemsBaseFormView
""" """
model = StockItem model = StockItem
template_name = "stock/stock_take_items.jinja" template_name = "stock/stock_take_items.jinja"
pk_url_kwarg = "stock_id" pk_url_kwarg = "stock_id"
current_tab = "take_items_from_stock" current_tab = "take_items_from_stock"
def get_form_class(self): def get_form_class(self):
fields = OrderedDict() fields = OrderedDict()
kwargs = {} kwargs = {}
for t in ProductType.objects.order_by('name').all(): for t in ProductType.objects.order_by('name').all():
for i in self.stock.items.filter(type=t).order_by('name').all(): for i in self.stock.items.filter(type=t).order_by('name').all():
field_name = "item-%s" % (str(i.id)) field_name = "item-%s" % (str(i.id))
fields[field_name] = forms.IntegerField(required=False, label=str(i), initial=0, min_value=0, max_value=i.effective_quantity, fields[field_name] = forms.IntegerField(required=False, label=str(i), initial=0, min_value=0, max_value=i.effective_quantity,
help_text=_("("+ str(i.effective_quantity) + " left)")) help_text=_("(%(effective_quantity)s left" % {"effective_quantity": str(i.effective_quantity)}))
kwargs[field_name] = i.effective_quantity kwargs[field_name] = i.effective_quantity
kwargs['stock_id'] = self.stock.id kwargs['stock_id'] = self.stock.id
kwargs['counter_id'] = self.stock.counter.id kwargs['counter_id'] = self.stock.counter.id
kwargs['base_fields'] = fields kwargs['base_fields'] = fields
return type('StockTakeItemsForm', (StockTakeItemsForm,), kwargs) return type('StockTakeItemsForm', (StockTakeItemsForm,), kwargs)
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
""" """
Simple get view Simple get view
""" """
self.stock = Stock.objects.filter(id=self.kwargs['stock_id']).first() self.stock = Stock.objects.filter(id=self.kwargs['stock_id']).first()
return super(StockTakeItemsBaseFormView, self).get(request, *args, **kwargs) return super(StockTakeItemsBaseFormView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
"""
Handle the many possibilities of the post request
"""
self.object = self.get_object()
self.stock = Stock.objects.filter(id=self.kwargs['stock_id']).first()
if self.stock.counter.type == "BAR" and not ('counter_token' in self.request.session.keys() and
self.request.session['counter_token'] == self.stock.counter.token): # Also check the token to avoid the bar to be stolen
return HttpResponseRedirect(reverse_lazy('counter:details', args=self.args,
kwargs={'counter_id': self.stock.counter.id})+'?bad_location')
return super(StockTakeItemsBaseFormView, self).post(request, *args, **kwargs)
def form_valid(self, form): def post(self, request, *args, **kwargs):
return super(StockTakeItemsBaseFormView, self).form_valid(form) """
Handle the many possibilities of the post request
"""
self.object = self.get_object()
self.stock = Stock.objects.filter(id=self.kwargs['stock_id']).first()
if self.stock.counter.type == "BAR" and not ('counter_token' in self.request.session.keys() and
self.request.session['counter_token'] == self.stock.counter.token): # Also check the token to avoid the bar to be stolen
return HttpResponseRedirect(reverse_lazy('counter:details', args=self.args,
kwargs={'counter_id': self.stock.counter.id})+'?bad_location')
return super(StockTakeItemsBaseFormView, self).post(request, *args, **kwargs)
def get_context_data(self, **kwargs): def form_valid(self, form):
kwargs = super(StockTakeItemsBaseFormView, self).get_context_data(**kwargs) return super(StockTakeItemsBaseFormView, self).form_valid(form)
if 'form' not in kwargs.keys():
kwargs['form'] = self.get_form()
kwargs['stock'] = self.stock
kwargs['counter'] = self.stock.counter
return kwargs
def get_success_url(self): def get_context_data(self, **kwargs):
stock = Stock.objects.filter(id=self.kwargs['stock_id']).first() kwargs = super(StockTakeItemsBaseFormView, self).get_context_data(**kwargs)
self.kwargs['counter_id'] = stock.counter.id if 'form' not in kwargs.keys():
self.kwargs.pop('stock_id', None) kwargs['form'] = self.get_form()
return reverse_lazy('counter:details', args=self.args, kwargs=self.kwargs) kwargs['stock'] = self.stock
kwargs['counter'] = self.stock.counter
return kwargs
def get_success_url(self):
stock = Stock.objects.filter(id=self.kwargs['stock_id']).first()
self.kwargs['counter_id'] = stock.counter.id
self.kwargs.pop('stock_id', None)
return reverse_lazy('counter:details', args=self.args, kwargs=self.kwargs)