Sith/stock/views.py

580 lines
18 KiB
Python
Raw Normal View History

2017-04-25 14:17:04 +00:00
#
# Copyright 2016,2017
# - Guillaume "Lo-J" Renaud <renaudg779@gmail.com>
# - Skia <skia@libskia.so>
#
# Ce fichier fait partie du site de l'Association des Étudiants de l'UTBM,
# http://ae.utbm.fr.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License a published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Sofware Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#
from collections import OrderedDict
from django import forms
2024-06-24 11:07:36 +00:00
from django.db import transaction
from django.forms.models import modelform_factory
2024-06-24 11:07:36 +00:00
from django.http import HttpResponseRedirect
from django.urls import reverse, reverse_lazy
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.views.generic import DetailView, ListView
from django.views.generic.edit import BaseFormView, CreateView, DeleteView, UpdateView
2016-10-26 15:20:42 +00:00
2024-06-24 11:07:36 +00:00
from core.views import CanCreateMixin, CanEditMixin, CanEditPropMixin, CanViewMixin
from counter.models import ProductType
2017-01-03 13:50:49 +00:00
from counter.views import CounterAdminTabsMixin, CounterTabsMixin
2024-06-24 11:07:36 +00:00
from stock.models import ShoppingList, ShoppingListItem, Stock, StockItem
2016-11-24 09:40:42 +00:00
class StockItemList(CounterAdminTabsMixin, CanCreateMixin, ListView):
2017-04-25 06:57:07 +00:00
"""
The stockitems list view for the counter owner
"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
model = Stock
2018-10-04 19:29:19 +00:00
template_name = "stock/stock_item_list.jinja"
2017-04-25 06:57:07 +00:00
pk_url_kwarg = "stock_id"
current_tab = "stocks"
def get_context_data(self):
2024-06-27 12:46:43 +00:00
ret = super().get_context_data()
2018-10-04 19:29:19 +00:00
if "stock_id" in self.kwargs.keys():
ret["stock"] = Stock.objects.filter(id=self.kwargs["stock_id"]).first()
2017-04-25 06:57:07 +00:00
return ret
2018-10-04 19:29:19 +00:00
class StockListView(CounterAdminTabsMixin, CanViewMixin, ListView):
2017-04-25 06:57:07 +00:00
"""
A list view for the admins
"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
model = Stock
2018-10-04 19:29:19 +00:00
template_name = "stock/stock_list.jinja"
2017-04-25 06:57:07 +00:00
current_tab = "stocks"
class StockEditForm(forms.ModelForm):
2017-04-25 06:57:07 +00:00
"""
A form to change stock's characteristics
"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
class Meta:
model = Stock
2018-10-04 19:29:19 +00:00
fields = ["name", "counter"]
2017-04-25 06:57:07 +00:00
def __init__(self, *args, **kwargs):
2024-06-27 12:46:43 +00:00
super().__init__(*args, **kwargs)
2017-04-25 06:57:07 +00:00
def save(self, *args, **kwargs):
2024-06-27 12:46:43 +00:00
return super().save(*args, **kwargs)
2017-04-25 06:57:07 +00:00
Addition of the StockItem class addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Stock application creation Addition of the StockItem class addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Initial StockItem create form value addition general modifications Stock admin gestion, items list views, create and edit items remove stock_main.jinja Stock application creation Addition of the StockItem class addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Addition of the StockItem class addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Initial StockItem create form value addition general modifications Stock admin gestion, items list views, create and edit items Shopping list structure view addition correct missing endif a correct missing endif Stock application creation addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Stock application creation addition of Stock app, model, templates, urls Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Initial StockItem create form value addition general modifications Stock admin gestion, items list views, create and edit items remove stock_main.jinja Stock application creation addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Initial StockItem create form value addition general modifications Shopping list structure view addition correct missing endif
2016-10-26 20:12:56 +00:00
class StockEditView(CounterAdminTabsMixin, CanEditPropMixin, UpdateView):
2017-04-25 06:57:07 +00:00
"""
An edit view for the stock
"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
model = Stock
2018-10-04 19:29:19 +00:00
form_class = modelform_factory(Stock, fields=["name", "counter"])
2017-04-25 06:57:07 +00:00
pk_url_kwarg = "stock_id"
2018-10-04 19:29:19 +00:00
template_name = "core/edit.jinja"
2017-04-25 06:57:07 +00:00
current_tab = "stocks"
Addition of the StockItem class addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Stock application creation Addition of the StockItem class addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Initial StockItem create form value addition general modifications Stock admin gestion, items list views, create and edit items remove stock_main.jinja Stock application creation Addition of the StockItem class addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Addition of the StockItem class addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Initial StockItem create form value addition general modifications Stock admin gestion, items list views, create and edit items Shopping list structure view addition correct missing endif a correct missing endif Stock application creation addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Stock application creation addition of Stock app, model, templates, urls Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Initial StockItem create form value addition general modifications Stock admin gestion, items list views, create and edit items remove stock_main.jinja Stock application creation addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Initial StockItem create form value addition general modifications Shopping list structure view addition correct missing endif
2016-10-26 20:12:56 +00:00
class StockItemEditView(CounterAdminTabsMixin, CanEditPropMixin, UpdateView):
2017-04-25 06:57:07 +00:00
"""
An edit view for a stock item
"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
model = StockItem
2018-10-04 19:29:19 +00:00
form_class = modelform_factory(
StockItem,
fields=[
"name",
"unit_quantity",
"effective_quantity",
"minimal_quantity",
"type",
"stock_owner",
],
)
2017-04-25 06:57:07 +00:00
pk_url_kwarg = "item_id"
2018-10-04 19:29:19 +00:00
template_name = "core/edit.jinja"
2017-04-25 06:57:07 +00:00
current_tab = "stocks"
class StockCreateView(CounterAdminTabsMixin, CanCreateMixin, CreateView):
2017-04-25 06:57:07 +00:00
"""
A create view for a new Stock
"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
model = Stock
2018-10-04 19:29:19 +00:00
form_class = modelform_factory(Stock, fields=["name", "counter"])
template_name = "core/create.jinja"
2017-04-25 06:57:07 +00:00
pk_url_kwarg = "counter_id"
current_tab = "stocks"
2018-10-04 19:29:19 +00:00
success_url = reverse_lazy("stock:list")
2017-04-25 06:57:07 +00:00
def get_initial(self):
2024-06-27 12:46:43 +00:00
ret = super().get_initial()
2018-10-04 19:29:19 +00:00
if "counter_id" in self.kwargs.keys():
ret["counter"] = self.kwargs["counter_id"]
2017-04-25 06:57:07 +00:00
return ret
2018-10-04 19:29:19 +00:00
class StockItemCreateView(CounterAdminTabsMixin, CanCreateMixin, CreateView):
2017-04-25 06:57:07 +00:00
"""
A create view for a new StockItem
"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
model = StockItem
2018-10-04 19:29:19 +00:00
form_class = modelform_factory(
StockItem,
fields=[
"name",
"unit_quantity",
"effective_quantity",
"minimal_quantity",
"type",
"stock_owner",
],
)
template_name = "core/create.jinja"
2017-04-25 06:57:07 +00:00
pk_url_kwarg = "stock_id"
current_tab = "stocks"
2017-04-25 06:57:07 +00:00
def get_initial(self):
2024-06-27 12:46:43 +00:00
ret = super().get_initial()
2018-10-04 19:29:19 +00:00
if "stock_id" in self.kwargs.keys():
ret["stock_owner"] = self.kwargs["stock_id"]
2017-04-25 06:57:07 +00:00
return ret
2017-04-25 06:57:07 +00:00
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return reverse_lazy(
"stock:items_list", kwargs={"stock_id": self.object.stock_owner.id}
)
2016-11-24 09:40:42 +00:00
class StockShoppingListView(CounterAdminTabsMixin, CanViewMixin, ListView):
2017-04-25 06:57:07 +00:00
"""
A list view for the people to know the item to buy
"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
model = Stock
template_name = "stock/stock_shopping_list.jinja"
pk_url_kwarg = "stock_id"
current_tab = "stocks"
2016-11-24 09:40:42 +00:00
2017-04-25 06:57:07 +00:00
def get_context_data(self):
2024-06-27 12:46:43 +00:00
ret = super().get_context_data()
2018-10-04 19:29:19 +00:00
if "stock_id" in self.kwargs.keys():
ret["stock"] = Stock.objects.filter(id=self.kwargs["stock_id"]).first()
2017-04-25 06:57:07 +00:00
return ret
Addition of the StockItem class addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Stock application creation Addition of the StockItem class addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Initial StockItem create form value addition general modifications Stock admin gestion, items list views, create and edit items remove stock_main.jinja Stock application creation Addition of the StockItem class addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Addition of the StockItem class addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Initial StockItem create form value addition general modifications Stock admin gestion, items list views, create and edit items Shopping list structure view addition correct missing endif a correct missing endif Stock application creation addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Stock application creation addition of Stock app, model, templates, urls Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Initial StockItem create form value addition general modifications Stock admin gestion, items list views, create and edit items remove stock_main.jinja Stock application creation addition of Stock app, model, templates, urls Addition of the stock parameter to the counter admin list Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Fix translation files Creation of the Stock list, edit, create views and creation StockItem create view Initial StockItem create form value addition general modifications Shopping list structure view addition correct missing endif
2016-10-26 20:12:56 +00:00
class StockItemQuantityForm(forms.BaseForm):
2017-04-25 06:57:07 +00:00
def clean(self):
with transaction.atomic():
self.stock = Stock.objects.filter(id=self.stock_id).first()
2018-10-04 19:29:19 +00:00
shopping_list = ShoppingList(
name="Courses " + self.stock.counter.name,
date=timezone.now(),
todo=True,
)
2017-04-25 06:57:07 +00:00
shopping_list.save()
shopping_list.stock_owner = self.stock
shopping_list.save()
2018-10-04 19:29:19 +00:00
for k, t in self.cleaned_data.items():
if k == "name":
2017-04-25 06:57:07 +00:00
shopping_list.name = t
shopping_list.save()
elif k == "comment":
shopping_list.comment = t
shopping_list.save()
else:
2018-10-04 19:29:19 +00:00
if t > 0:
2017-04-25 06:57:07 +00:00
item_id = int(k[5:])
item = StockItem.objects.filter(id=item_id).first()
2018-10-04 19:29:19 +00:00
shoppinglist_item = ShoppingListItem(
stockitem_owner=item,
name=item.name,
type=item.type,
tobuy_quantity=t,
)
2017-04-25 06:57:07 +00:00
shoppinglist_item.save()
shoppinglist_item.shopping_lists.add(shopping_list)
shoppinglist_item.save()
return self.cleaned_data
2018-10-04 19:29:19 +00:00
class StockItemQuantityBaseFormView(
CounterAdminTabsMixin, CanEditMixin, DetailView, BaseFormView
):
2017-04-25 06:57:07 +00:00
"""
docstring for StockItemOutList
"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
model = StockItem
template_name = "stock/shopping_list_quantity.jinja"
pk_url_kwarg = "stock_id"
current_tab = "stocks"
def get_form_class(self):
fields = OrderedDict()
kwargs = {}
2018-10-04 19:29:19 +00:00
fields["name"] = forms.CharField(
max_length=30, required=True, label=_("Shopping list name")
)
for t in ProductType.objects.order_by("name").all():
for i in self.stock.items.filter(type=t).order_by("name").all():
2017-04-25 06:57:07 +00:00
if i.effective_quantity <= i.minimal_quantity:
field_name = "item-%s" % (str(i.id))
2018-10-04 19:29:19 +00:00
fields[field_name] = forms.IntegerField(
required=True,
label=str(i),
initial=0,
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 ...)"
)
}
),
required=False,
label=_("Comments"),
)
kwargs["stock_id"] = self.stock.id
kwargs["base_fields"] = fields
return type("StockItemQuantityForm", (StockItemQuantityForm,), kwargs)
2017-04-25 06:57:07 +00:00
def get(self, request, *args, **kwargs):
"""
Simple get view
"""
2018-10-04 19:29:19 +00:00
self.stock = Stock.objects.filter(id=self.kwargs["stock_id"]).first()
2024-06-27 12:46:43 +00:00
return super().get(request, *args, **kwargs)
2017-04-25 06:57:07 +00:00
def post(self, request, *args, **kwargs):
"""
Handle the many possibilities of the post request
"""
self.object = self.get_object()
2018-10-04 19:29:19 +00:00
self.stock = Stock.objects.filter(id=self.kwargs["stock_id"]).first()
2024-06-27 12:46:43 +00:00
return super().post(request, *args, **kwargs)
2017-04-25 06:57:07 +00:00
def form_valid(self, form):
2024-06-27 12:46:43 +00:00
return super().form_valid(form)
2017-04-25 06:57:07 +00:00
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
if "form" not in kwargs.keys():
kwargs["form"] = self.get_form()
kwargs["stock"] = self.stock
2017-04-25 06:57:07 +00:00
return kwargs
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return reverse_lazy(
"stock:shoppinglist_list", args=self.args, kwargs=self.kwargs
)
class StockShoppingListItemListView(CounterAdminTabsMixin, CanViewMixin, ListView):
2017-04-25 06:57:07 +00:00
"""docstring for StockShoppingListItemListView"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
model = ShoppingList
template_name = "stock/shopping_list_items.jinja"
pk_url_kwarg = "shoppinglist_id"
current_tab = "stocks"
def get_context_data(self):
2024-06-27 12:46:43 +00:00
ret = super().get_context_data()
2018-10-04 19:29:19 +00:00
if "shoppinglist_id" in self.kwargs.keys():
ret["shoppinglist"] = ShoppingList.objects.filter(
id=self.kwargs["shoppinglist_id"]
).first()
2017-04-25 06:57:07 +00:00
return ret
2018-10-04 19:29:19 +00:00
class StockShoppingListDeleteView(CounterAdminTabsMixin, CanEditMixin, DeleteView):
"""
Delete a ShoppingList (for the resonsible account)
"""
2018-10-04 19:29:19 +00:00
model = ShoppingList
pk_url_kwarg = "shoppinglist_id"
2018-10-04 19:29:19 +00:00
template_name = "core/delete_confirm.jinja"
current_tab = "stocks"
def get_success_url(self):
2018-10-04 19:29:19 +00:00
return reverse_lazy(
"stock:shoppinglist_list", kwargs={"stock_id": self.object.stock_owner.id}
)
class StockShopppingListSetDone(CanEditMixin, DetailView):
"""
Set a ShoppingList as done
"""
2018-10-04 19:29:19 +00:00
model = ShoppingList
pk_url_kwarg = "shoppinglist_id"
def get(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.todo = False
self.object.save()
2018-10-04 19:29:19 +00:00
return HttpResponseRedirect(
reverse(
"stock:shoppinglist_list",
args=self.args,
kwargs={"stock_id": self.object.stock_owner.id},
)
)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
2018-10-04 19:29:19 +00:00
return HttpResponseRedirect(
reverse(
"stock:shoppinglist_list",
args=self.args,
kwargs={"stock_id": self.object.stock_owner.id},
)
)
class StockShopppingListSetTodo(CanEditMixin, DetailView):
"""
Set a ShoppingList as done
"""
2018-10-04 19:29:19 +00:00
model = ShoppingList
pk_url_kwarg = "shoppinglist_id"
def get(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.todo = True
self.object.save()
2018-10-04 19:29:19 +00:00
return HttpResponseRedirect(
reverse(
"stock:shoppinglist_list",
args=self.args,
kwargs={"stock_id": self.object.stock_owner.id},
)
)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
2018-10-04 19:29:19 +00:00
return HttpResponseRedirect(
reverse(
"stock:shoppinglist_list",
args=self.args,
kwargs={"stock_id": self.object.stock_owner.id},
)
)
class StockUpdateAfterShopppingForm(forms.BaseForm):
2017-04-25 06:57:07 +00:00
def clean(self):
with transaction.atomic():
2018-10-04 19:29:19 +00:00
self.shoppinglist = ShoppingList.objects.filter(
id=self.shoppinglist_id
).first()
for k, t in self.cleaned_data.items():
2017-04-25 06:57:07 +00:00
shoppinglist_item_id = int(k[5:])
2018-10-04 19:29:19 +00:00
if int(t) > 0:
shoppinglist_item = ShoppingListItem.objects.filter(
id=shoppinglist_item_id
).first()
2017-04-25 06:57:07 +00:00
shoppinglist_item.bought_quantity = int(t)
shoppinglist_item.save()
shoppinglist_item.stockitem_owner.effective_quantity += int(t)
shoppinglist_item.stockitem_owner.save()
self.shoppinglist.todo = False
self.shoppinglist.save()
return self.cleaned_data
2018-10-04 19:29:19 +00:00
class StockUpdateAfterShopppingBaseFormView(
CounterAdminTabsMixin, CanEditMixin, DetailView, BaseFormView
):
2017-04-25 06:57:07 +00:00
"""
docstring for StockUpdateAfterShopppingBaseFormView
"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
model = ShoppingList
template_name = "stock/update_after_shopping.jinja"
pk_url_kwarg = "shoppinglist_id"
current_tab = "stocks"
def get_form_class(self):
fields = OrderedDict()
kwargs = {}
2018-10-04 19:29:19 +00:00
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()
):
2017-04-25 06:57:07 +00:00
field_name = "item-%s" % (str(i.id))
2018-10-04 19:29:19 +00:00
fields[field_name] = forms.CharField(
max_length=30,
required=True,
label=str(i),
help_text=_(str(i.tobuy_quantity) + " asked"),
)
kwargs["shoppinglist_id"] = self.shoppinglist.id
kwargs["base_fields"] = fields
return type(
"StockUpdateAfterShopppingForm", (StockUpdateAfterShopppingForm,), kwargs
)
2017-04-25 06:57:07 +00:00
def get(self, request, *args, **kwargs):
2018-10-04 19:29:19 +00:00
self.shoppinglist = ShoppingList.objects.filter(
id=self.kwargs["shoppinglist_id"]
).first()
2024-06-27 12:46:43 +00:00
return super().get(request, *args, **kwargs)
2017-04-25 06:57:07 +00:00
def post(self, request, *args, **kwargs):
"""
Handle the many possibilities of the post request
"""
self.object = self.get_object()
2018-10-04 19:29:19 +00:00
self.shoppinglist = ShoppingList.objects.filter(
id=self.kwargs["shoppinglist_id"]
).first()
2024-06-27 12:46:43 +00:00
return super().post(request, *args, **kwargs)
2017-04-25 06:57:07 +00:00
def form_valid(self, form):
"""
We handle here the redirection
"""
2024-06-27 12:46:43 +00:00
return super().form_valid(form)
2017-04-25 06:57:07 +00:00
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
if "form" not in kwargs:
2018-10-04 19:29:19 +00:00
kwargs["form"] = self.get_form()
kwargs["shoppinglist"] = self.shoppinglist
kwargs["stock"] = self.shoppinglist.stock_owner
2017-04-25 06:57:07 +00:00
return kwargs
def get_success_url(self):
2018-10-04 19:29:19 +00:00
self.kwargs.pop("shoppinglist_id", None)
return reverse_lazy(
"stock:shoppinglist_list", args=self.args, kwargs=self.kwargs
)
2017-01-03 13:50:49 +00:00
class StockTakeItemsForm(forms.BaseForm):
2017-04-25 06:57:07 +00:00
"""
docstring for StockTakeItemsFormView
"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
def clean(self):
with transaction.atomic():
2018-10-04 19:29:19 +00:00
for k, t in self.cleaned_data.items():
2017-04-25 06:57:07 +00:00
item_id = int(k[5:])
2018-10-04 19:29:19 +00:00
if t > 0:
2017-04-25 06:57:07 +00:00
item = StockItem.objects.filter(id=item_id).first()
item.effective_quantity -= t
item.save()
return self.cleaned_data
2017-01-03 13:50:49 +00:00
2018-10-04 19:29:19 +00:00
class StockTakeItemsBaseFormView(
CounterTabsMixin, CanEditMixin, DetailView, BaseFormView
):
2017-04-25 06:57:07 +00:00
"""
docstring for StockTakeItemsBaseFormView
"""
2018-10-04 19:29:19 +00:00
2017-04-25 06:57:07 +00:00
model = StockItem
template_name = "stock/stock_take_items.jinja"
pk_url_kwarg = "stock_id"
current_tab = "take_items_from_stock"
def get_form_class(self):
fields = OrderedDict()
kwargs = {}
2018-10-04 19:29:19 +00:00
for t in ProductType.objects.order_by("name").all():
for i in self.stock.items.filter(type=t).order_by("name").all():
2017-04-25 06:57:07 +00:00
field_name = "item-%s" % (str(i.id))
2018-10-04 19:29:19 +00:00
fields[field_name] = forms.IntegerField(
required=False,
label=str(i),
initial=0,
min_value=0,
max_value=i.effective_quantity,
help_text=_(
"%(effective_quantity)s left"
% {"effective_quantity": str(i.effective_quantity)}
),
)
2017-04-25 06:57:07 +00:00
kwargs[field_name] = i.effective_quantity
2018-10-04 19:29:19 +00:00
kwargs["stock_id"] = self.stock.id
kwargs["counter_id"] = self.stock.counter.id
kwargs["base_fields"] = fields
return type("StockTakeItemsForm", (StockTakeItemsForm,), kwargs)
2017-04-25 06:57:07 +00:00
def get(self, request, *args, **kwargs):
"""
Simple get view
"""
2018-10-04 19:29:19 +00:00
self.stock = Stock.objects.filter(id=self.kwargs["stock_id"]).first()
2024-06-27 12:46:43 +00:00
return super().get(request, *args, **kwargs)
2017-04-25 06:57:07 +00:00
def post(self, request, *args, **kwargs):
"""
Handle the many possibilities of the post request
"""
self.object = self.get_object()
2018-10-04 19:29:19 +00:00
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"
)
2024-06-27 12:46:43 +00:00
return super().post(request, *args, **kwargs)
2017-04-25 06:57:07 +00:00
def form_valid(self, form):
2024-06-27 12:46:43 +00:00
return super().form_valid(form)
2017-04-25 06:57:07 +00:00
def get_context_data(self, **kwargs):
2024-06-27 12:46:43 +00:00
kwargs = super().get_context_data(**kwargs)
2018-10-04 19:29:19 +00:00
if "form" not in kwargs.keys():
kwargs["form"] = self.get_form()
kwargs["stock"] = self.stock
kwargs["counter"] = self.stock.counter
2017-04-25 06:57:07 +00:00
return kwargs
def get_success_url(self):
2018-10-04 19:29:19 +00:00
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)