2021-07-30 20:52:18 +02:00
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.urls import resolve, reverse
|
2021-08-07 17:37:36 +02:00
|
|
|
from django.contrib.auth import logout, login
|
2021-10-24 21:14:11 +02:00
|
|
|
from django.contrib.auth.models import User
|
2021-07-30 20:52:18 +02:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from .navbar import NavBar
|
2021-08-07 17:37:36 +02:00
|
|
|
from django.contrib.auth.forms import AuthenticationForm as AuthForm
|
|
|
|
from django.views import View
|
|
|
|
import django.forms as forms
|
|
|
|
from django.views.generic import TemplateView, DetailView
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
|
|
|
|
from .models import Storage, Stock
|
|
|
|
from .qr_parser import QrCodeValidator
|
2021-08-14 00:25:19 +02:00
|
|
|
from django.core.paginator import Paginator
|
2021-10-24 21:14:11 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2021-08-07 17:37:36 +02:00
|
|
|
|
|
|
|
class QrSearchForm(forms.Form):
|
|
|
|
my_qr_validator = QrCodeValidator()
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
qr_search = forms.CharField(label='qr_search', validators=[my_qr_validator])
|
|
|
|
|
|
|
|
class BaseTemplateMixin(object):
|
|
|
|
navbar_selected = ''
|
|
|
|
base_title = ''
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
base_context = {
|
|
|
|
'navbar': NavBar.get_navbar(self.navbar_selected, self.request.user),
|
|
|
|
'title': NavBar.get_brand()+' / '+ self.base_title,
|
|
|
|
'login_active': False,
|
|
|
|
}
|
|
|
|
context['base'] = base_context
|
|
|
|
return context
|
2021-07-30 20:52:18 +02:00
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
data = request.POST
|
|
|
|
if 'qr_search' not in data:
|
|
|
|
super().post(request, *args, **kwargs)
|
2021-07-30 20:52:18 +02:00
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
print('QR',data['qr_search'])
|
|
|
|
f = QrSearchForm(data)
|
|
|
|
if f.is_valid():
|
|
|
|
return redirect(f.my_qr_validator.get_redirect_url(f.cleaned_data['qr_search']))
|
2021-07-30 20:52:18 +02:00
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
return self.get(request)
|
|
|
|
|
|
|
|
class MainView(BaseTemplateMixin, TemplateView):
|
|
|
|
template_name = 'parts/main.html'
|
|
|
|
navbar_selected = 'Main'
|
|
|
|
base_title = 'Main'
|
|
|
|
|
|
|
|
def logout_view(request):
|
|
|
|
logout(request)
|
|
|
|
return redirect('parts-main')
|
|
|
|
|
|
|
|
def login_view(request):
|
2021-07-30 20:52:18 +02:00
|
|
|
base_context = {
|
2021-08-07 17:37:36 +02:00
|
|
|
'navbar': NavBar.get_navbar('Login', request.user),
|
|
|
|
'title': NavBar.get_brand()+' / '+'Login',
|
|
|
|
'login_active': True,
|
2021-07-30 20:52:18 +02:00
|
|
|
}
|
2021-08-07 17:37:36 +02:00
|
|
|
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
next_param = request.GET.get('next')
|
|
|
|
if next_param is not None:
|
|
|
|
return redirect(next_param)
|
|
|
|
else:
|
|
|
|
return redirect('parts-main')
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = AuthForm(data=request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
valid_user = form.get_user()
|
|
|
|
login(request, valid_user)
|
|
|
|
next_param = request.GET.get('next')
|
|
|
|
if next_param is not None:
|
|
|
|
return redirect(next_param)
|
|
|
|
else:
|
|
|
|
return redirect('parts-main')
|
|
|
|
else:
|
|
|
|
form = AuthForm()
|
|
|
|
|
|
|
|
|
2021-07-30 20:52:18 +02:00
|
|
|
context = {
|
|
|
|
'base': base_context,
|
2021-08-07 17:37:36 +02:00
|
|
|
'form': form,
|
2021-07-30 20:52:18 +02:00
|
|
|
}
|
2021-08-07 17:37:36 +02:00
|
|
|
|
|
|
|
return render(request, 'parts/login.html', context)
|
2021-07-30 20:52:18 +02:00
|
|
|
|
|
|
|
# Create your views here.
|
2021-08-07 17:37:36 +02:00
|
|
|
|
|
|
|
class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
|
|
|
template_name = 'parts/components.html'
|
|
|
|
base_title = 'Components'
|
|
|
|
navbar_selected = 'Components'
|
|
|
|
|
|
|
|
class StockView(LoginRequiredMixin, BaseTemplateMixin, TemplateView):
|
|
|
|
template_name = 'parts/stocks.html'
|
|
|
|
base_title = 'Stocks'
|
|
|
|
navbar_selected = 'Stocks'
|
2021-08-14 00:25:19 +02:00
|
|
|
default_pagination_size = 25
|
2021-08-07 17:37:36 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2021-08-14 00:25:19 +02:00
|
|
|
|
|
|
|
storage_page = self.request.GET.get('storage_page')
|
|
|
|
if storage_page is None:
|
|
|
|
storage_page = 1
|
|
|
|
low_stock_page = self.request.GET.get('low_stock_page')
|
|
|
|
if low_stock_page is None:
|
|
|
|
low_stock_page = 1
|
|
|
|
|
|
|
|
storage_paginator = Paginator(Storage.objects.filter(parent_storage=None), self.default_pagination_size)
|
|
|
|
low_stock_paginator = Paginator(Stock.get_under_watermark(),
|
|
|
|
self.default_pagination_size)
|
|
|
|
|
|
|
|
context['low_stocks'] = low_stock_paginator.get_page(low_stock_page)
|
|
|
|
context['storages'] = storage_paginator.get_page(storage_page)
|
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
return context
|
|
|
|
|
2021-10-24 21:14:11 +02:00
|
|
|
class AddSubStorageForm(forms.Form):
|
|
|
|
storage_name = forms.CharField(label="storage_name", initial='')
|
|
|
|
responsible = forms.CharField(label='responsible_user')
|
|
|
|
|
2021-08-07 17:37:36 +02:00
|
|
|
class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView):
|
|
|
|
template_name = 'parts/stocks-detail.html'
|
|
|
|
model = Storage
|
|
|
|
pk_url_kwarg = 'uuid'
|
|
|
|
base_title = ''
|
|
|
|
navbar_selected = 'Stocks'
|
2021-08-14 00:25:19 +02:00
|
|
|
default_pagination_size = 8
|
2021-08-07 17:37:36 +02:00
|
|
|
|
|
|
|
def get_breadcrumbs(self):
|
|
|
|
crumbs = self.object.get_path_components()
|
|
|
|
# Reverse list and drop the last element of the reversed list
|
|
|
|
crumbs = crumbs[::-1][:-1]
|
|
|
|
return crumbs
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
self.base_title = 'Stocks / ' + self.object.name
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['breadcrumbs'] = self.get_breadcrumbs()
|
2021-08-14 00:25:19 +02:00
|
|
|
|
|
|
|
storage_page = self.request.GET.get('storage_page')
|
|
|
|
if storage_page is None:
|
|
|
|
storage_page = 1
|
|
|
|
storage_paginator = Paginator(Storage.objects.filter(parent_storage=self.object), self.default_pagination_size)
|
|
|
|
|
|
|
|
context['storages'] = storage_paginator.get_page(storage_page)
|
2021-10-24 21:14:11 +02:00
|
|
|
add_storage_form = AddSubStorageForm()
|
|
|
|
add_storage_form.fields['responsible'].initial = self.request.user.username
|
|
|
|
context['add_storage_form'] = add_storage_form
|
2021-08-07 17:37:36 +02:00
|
|
|
return context
|
2021-08-14 02:35:09 +02:00
|
|
|
|
2021-10-24 21:14:11 +02:00
|
|
|
def handle_add_storage_post(self, request, **kwargs):
|
|
|
|
f = AddSubStorageForm(data=request.POST)
|
|
|
|
if f.is_valid():
|
|
|
|
sub_name = f.cleaned_data['storage_name']
|
|
|
|
try:
|
|
|
|
user = User.objects.get(username=f.cleaned_data['responsible'])
|
|
|
|
try:
|
|
|
|
Storage.objects.create(name=sub_name, parent_storage=self.object, responsible=user)
|
|
|
|
except ValidationError as v_err:
|
|
|
|
f.add_error('storage_name', '. '.join(v_err.messages))
|
|
|
|
except:
|
|
|
|
f.add_error('responsible', 'Invalid user')
|
|
|
|
context = self.get_context_data(**kwargs)
|
|
|
|
context['add_storage_form'] = f
|
|
|
|
return self.render_to_response(context)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
|
|
|
|
if 'submit-add-storage' in request.POST:
|
|
|
|
return self.handle_add_storage_post(request)
|
|
|
|
|
|
|
|
return super().post(request, *args, **kwargs)
|