diff --git a/accounts/forms.py b/accounts/forms.py index b52895a..05f2585 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -1,7 +1,47 @@ from django.contrib.auth import get_user_model from django.contrib.auth.forms import AuthenticationForm +from django.core.exceptions import ValidationError +from django.forms import CharField +from django.forms.models import ModelForm +from django.forms.widgets import PasswordInput + class LoginForm(AuthenticationForm): field_order = ['username', 'password'] class Meta: model = get_user_model() + + +class RegisterForm(ModelForm): + password1 = CharField(label='Password', widget=PasswordInput) + password2 = CharField(label='Confirm Password', widget=PasswordInput) + + field_order = ['username', 'email', 'password1', 'password2', 'phone_number', 'line', 'company_permissions'] + + + class Meta: + model = get_user_model() + fields = ['username', 'email', 'password1', 'password2', 'phone_number', 'line', 'company_permissions'] + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.fields['password1'].widget.attrs.update({'autocomplete': 'new-password'}) + self.fields['password2'].widget.attrs.update({'autocomplete': 'new-password'}) + + + def clean(self): + cleaned_data = super().clean() + password1 = cleaned_data.get('password1') + password2 = cleaned_data.get('password2') + + if password1 and password2 and password1 != password2: + raise ValidationError("Passwords don't match") + return cleaned_data + + def save(self, commit=True): + user = super().save(commit=False) + user.set_password(self.cleaned_data['password1']) + if commit: + user.save() + return user + diff --git a/accounts/models.py b/accounts/models.py index 2d6a632..0f8329a 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -2,6 +2,9 @@ from django.contrib.auth.models import AbstractUser from django.db import models class ClientPermission(models.Model): + codename = models.CharField(max_length=100, default='') + name = models.CharField(max_length=255, default='') + class Meta: managed = True default_permissions = () @@ -10,7 +13,8 @@ class ClientPermission(models.Model): ('can_view_bookings', 'Can view bookings'), ('can_manage_company_users', 'Can manage company users'), ) - + def __str__(self): + return self.name class DepotUser(AbstractUser): diff --git a/accounts/urls.py b/accounts/urls.py index ac59a5c..2e13b1a 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -5,4 +5,5 @@ from accounts import views urlpatterns = [ path('login/', views.DepotLoginView.as_view(), name='login'), path('relogin/', auth_views.logout_then_login, name='relogin'), + path('register/', views.RegisterView.as_view(), name='register'), ] \ No newline at end of file diff --git a/accounts/views.py b/accounts/views.py index 509ff0a..6881e58 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -1,9 +1,10 @@ +from django.contrib.auth import get_user_model from django.contrib.auth.views import LoginView from django.shortcuts import render from django.urls import reverse_lazy -from django.views.generic import TemplateView, FormView +from django.views.generic import TemplateView, FormView, ListView, UpdateView -from accounts.forms import LoginForm +from accounts.forms import LoginForm, RegisterForm # Create your views here. @@ -14,4 +15,27 @@ class DepotLoginView(LoginView): next_page = reverse_lazy('dashboard') +class RegisterView(FormView): + template_name = 'registration/register.html' + form_class = RegisterForm + # model = get_user_model() + success_url = reverse_lazy('dashboard') + def form_valid(self, form): + # Create user from form data + user = form.save(commit=False) + # user.set_password(form.cleaned_data['password']) + user.save() + return super().form_valid(form) + +class UserListView(ListView): + template_name = 'registration/register.html' + # form_class = RegisterForm + model = get_user_model() + success_url = reverse_lazy('dashboard') + +class UserEditView(UpdateView): + template_name = 'registration/register.html' + form_class = RegisterForm + model = get_user_model() + success_url = reverse_lazy('dashboard') diff --git a/booking/models.py b/booking/models.py index a8e4262..76d0fa9 100644 --- a/booking/models.py +++ b/booking/models.py @@ -3,6 +3,13 @@ from common.models import ContainerTypeModel, LinesModel, OperationModel # Create your models here. class Booking(models.Model): + + STATUS_CHOICES = [ + ('active', 'Active'), + ('finished', 'Finished'), + ('canceled', 'Canceled'), + ] + number = models.CharField(max_length=50, unique=True) vehicles = models.CharField(blank=True, null=True) container_type = models.ForeignKey( @@ -21,8 +28,17 @@ class Booking(models.Model): visible = models.BooleanField(default=True) is_new = models.BooleanField(default=True) container_number = models.CharField(max_length=11, blank=True, null=True) - vehicles_left = models.IntegerField(blank=True, null=True) + vehicles_left = models.CharField(blank=True, null=True) created_on = models.DateTimeField(auto_now_add=True) created_by = models.IntegerField() updated_on = models.DateTimeField(auto_now=True) - updated_by = models.IntegerField() \ No newline at end of file + updated_by = models.IntegerField() + status = models.CharField( + max_length=10, + choices=STATUS_CHOICES, + default='active' + ) + + @property + def containers_left(self): + return self.container_count - (self.container_expedited_count or 0) \ No newline at end of file diff --git a/booking/views.py b/booking/views.py index 396ad54..03a2fa5 100644 --- a/booking/views.py +++ b/booking/views.py @@ -12,7 +12,7 @@ class CreateBookingView(CreateView): template_name = 'client-booking-content.html' model = Booking form_class = BookingCreateForm - success_url = reverse_lazy('dashboard') + success_url = reverse_lazy('client_booking') def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -21,15 +21,25 @@ class CreateBookingView(CreateView): # !!! important queryset = filter_queryset_by_user( queryset, user)[:10] # !!! important - context['recent'] = queryset return context + def get_form(self, form_class=None): + form = super().get_form(form_class) + user = self.request.user + + # If user has a specific line, limit the line choices + if user.line: + form.fields['line'].queryset = form.fields['line'].queryset.filter(pk=user.line.pk) + form.fields['line'].initial = user.line + form.fields['line'].widget.readonly = True #attrs['disabled'] = True + return form def form_valid(self, form): # todo more validation form.instance.created_by = self.request.user.id form.instance.updated_by = self.request.user.id + form.instance.vehicles_left = form.cleaned_data.get('vehicles') if self.request.user.line: form.instance.line = self.request.user.line return super().form_valid(form) \ No newline at end of file diff --git a/preinfo/views.py b/preinfo/views.py index 6619cb4..c810e95 100644 --- a/preinfo/views.py +++ b/preinfo/views.py @@ -39,7 +39,7 @@ class ClientPreinfoView(LoginRequiredMixin, CreateView): if user.line: form.fields['line'].queryset = form.fields['line'].queryset.filter(pk=user.line.pk) form.fields['line'].initial = user.line - form.fields['line'].widget.attrs['disabled'] = True + form.fields['line'].widget.readonly = True #form.fields['line'].widget.attrs['disabled'] = True # Keep the value when form is submitted # form.fields['line'].widget = HiddenInput() diff --git a/templates/barrier-dashboard.html b/templates/barrier-dashboard.html index cdb0d15..646ea21 100644 --- a/templates/barrier-dashboard.html +++ b/templates/barrier-dashboard.html @@ -59,7 +59,14 @@
{% load static %} -