paginacja, popup przy dacie, zachowanie danych przy zlym formularzu

This commit is contained in:
Hokan 2019-01-27 00:49:14 +01:00
parent 7f0efb044e
commit e3bb9dd669
10 changed files with 106 additions and 35 deletions

Binary file not shown.

View File

@ -1,7 +1,6 @@
from django.forms import ModelForm from django.forms import ModelForm
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from .models import * from .models import *
from django import forms
class ClientForm(ModelForm): class ClientForm(ModelForm):
@ -13,3 +12,5 @@ class ClientForm(ModelForm):
'name': _('Imię '), 'name': _('Imię '),
'surname': _('Nazwisko ') 'surname': _('Nazwisko ')
} }

View File

@ -1,5 +1,6 @@
from django.db import models from django.db import models
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator, RegexValidator
class RoomType(models.Model): class RoomType(models.Model):
@ -30,7 +31,9 @@ class Room(models.Model):
class Client(models.Model): class Client(models.Model):
id_number = models.CharField(max_length=9, primary_key=True) id_number = models.CharField(max_length=9,
primary_key=True)
name = models.CharField(max_length=40) name = models.CharField(max_length=40)
surname = models.CharField(max_length=40) surname = models.CharField(max_length=40)
@ -38,8 +41,10 @@ class Client(models.Model):
return self.name + self.surname return self.name + self.surname
class Reservation(models.Model): class Reservation(models.Model):
room_number = models.ForeignKey(Room, on_delete=models.CASCADE) room_number = models.ForeignKey(Room, on_delete=models.CASCADE)
client_id = models.ForeignKey(Client, on_delete=models.CASCADE) client_id = models.ForeignKey(Client, on_delete=models.CASCADE)
begin_date = models.DateField() begin_date = models.DateField()
end_date = models.DateField() end_date = models.DateField()

View File

@ -17,9 +17,37 @@
} }
} }
$( function() { $( function() {
$( ".datepicker" ).datepicker({ dateFormat: 'dd-mm-yy' }).val(); $( ".datepicker" ).datepicker({ dateFormat: 'dd-mm-yy' }).val();
$( ".dateform" ).submit(function(event){
var begindate = $(this).find('input[name=begindate]').val();
var enddate = $(this).find('input[name=enddate]').val();
var beginpop = $(this).find('[name=beginPopup]');
var endpop = $(this).find('[name=endPopup]');
beginpop.addClass('popup')
var today = $.datepicker.formatDate('dd-mm-yy', new Date());
if (begindate.getDate() < today.getDate()){
alert('Data początkowa jest z przeszłości');
return false;
}
if (enddate.getDate() < today.getDate()){
alert('Data końcowa jest z przeszłości');
return false;
}
if (begindate.getDate() > enddate.getDate()){
alert('Data końcowa jest przed początkową');
return false;
}
});
} ); } );
</script> </script>
</head> </head>
<body onLoad="changeBackground(imageUrl = localStorage.backgroundImage)"> <body onLoad="changeBackground(imageUrl = localStorage.backgroundImage)">
@ -42,12 +70,12 @@
<td>{{ room.price }} zł</td> <td>{{ room.price }} zł</td>
<td> <td>
<form action="{% url 'reservation' %}" method="POST"> <form action="{% url 'reservation' %}" class="dateform" method="POST">
{% csrf_token %} {% csrf_token %}
<input type="hidden" name="room" value="{{ room.room_number }}"> <input type="hidden" name="room" value="{{ room.room_number }}">
<div class="button-box" > <div class="button-box" >
Od: <input type="text" class="datepicker" name="begindate" size="3"> Od: <input type="text" class="datepicker" name="begindate" size="3" required>
Do: <input type="text" class="datepicker" name="enddate" size="3"> Do: <input type="text" class="datepicker" name="enddate" size="3" required>
<button>Rezerwuj</button> <button>Rezerwuj</button>
</div> </div>
</form> </form>
@ -58,6 +86,19 @@
</table> </table>
<br><br> <br><br>
<center> <center>
{% if firstpage == False %}
<a href="{% url 'indexpage' currentpage|add:'-1' %}"><button>Poprzednia strona</button></a>
{% endif %}
{% for p in pages %}
<a href="{% url 'indexpage' p %}">{{ p }}</a>
{% endfor %}
{% if lastpage == False %}
<a href="{% url 'indexpage' currentpage|add:'+1' %}"><button>Następna strona</button></a>
{% endif %}
<br />
<table id="images"> <table id="images">
<th><input type="image" src="static/images/background1.jpg" height="150px" width="250px" onClick="changeBackground(imageUrl='static/images/background1.jpg')"/></th> <th><input type="image" src="static/images/background1.jpg" height="150px" width="250px" onClick="changeBackground(imageUrl='static/images/background1.jpg')"/></th>
<th><input type="image" src="static/images/background2.jpg" height="150px" width="250px" onClick="changeBackground(imageUrl='static/images/background2.jpg')"/></th> <th><input type="image" src="static/images/background2.jpg" height="150px" width="250px" onClick="changeBackground(imageUrl='static/images/background2.jpg')"/></th>

View File

@ -4,5 +4,6 @@ from . import views
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', views.index, name='index'),
path('<int:page>', views.index, name='indexpage'),
path('reservation', views.reservation, name='reservation') path('reservation', views.reservation, name='reservation')
] ]

View File

@ -4,30 +4,41 @@ from django.shortcuts import get_object_or_404, render
from .models import * from .models import *
from .forms import * from .forms import *
from django.db.models import Q from django.db.models import Q
import re
import datetime import datetime
import math
import re
# Create your views here. # Create your views here.
def index(request): def index(request, page=1):
first_page, last_page = False, False
if page == 1:
first_page = True
rooms = Room.objects.all() rooms = Room.objects.all()
rooms = rooms.order_by('room_type', 'room_number') rooms = rooms.order_by('room_type', 'room_number')
template = loader.get_template('index.html')
return HttpResponse(template.render({'rooms': rooms}, request)) if page*10 >= len(rooms):
last_page = True
max_pages = math.ceil(len(rooms)/10)
rooms = rooms[10*(page-1):10*page]
template = loader.get_template('index.html')
print (first_page,last_page)
return HttpResponse(template.render({'rooms': rooms,
'firstpage': first_page,
'lastpage': last_page,
'pages': range(1,max_pages+1),
'currentpage': page}, request))
def reservation(request): def reservation(request):
room_number, begin_date, end_date = request.POST['room'], request.POST['begindate'], request.POST['enddate'] room_number, begin_date, end_date = request.POST['room'], request.POST['begindate'], request.POST['enddate']
try:
begin_datetime = datetime.datetime.strptime(begin_date, "%d-%m-%Y") begin_datetime = datetime.datetime.strptime(begin_date, "%d-%m-%Y")
end_datetime = datetime.datetime.strptime(end_date, "%d-%m-%Y") end_datetime = datetime.datetime.strptime(end_date, "%d-%m-%Y")
except: clientform = {}
return HttpResponse(loader.get_template('error.html').render({'error': "Błąd przy datach"}, request))
if begin_datetime == '' or end_datetime == '' or end_datetime < begin_datetime or begin_datetime < datetime.datetime.today() or end_datetime < datetime.datetime.today():
return HttpResponse(loader.get_template('error.html').render({'error': "Błąd przy datach"}, request))
template = loader.get_template('reservation.html') template = loader.get_template('reservation.html')
error = "" error = ""
reservation = Reservation.objects.filter(room_number=room_number).filter(Q(begin_date__range=(begin_datetime, end_datetime)) | Q(end_date__range=(begin_datetime, end_datetime))).first() reservation = Reservation.objects.filter(room_number=room_number).filter(Q(begin_date__range=(begin_datetime, end_datetime)) | Q(end_date__range=(begin_datetime, end_datetime))).first()
@ -37,12 +48,19 @@ def reservation(request):
else: else:
if request.method == "POST": if request.method == "POST":
try: try:
client_id = request.POST['id_number'] client_id = request.POST['id_number']
if check(client_id):
if Client.objects.filter(id_number=client_id).first() is None: if Client.objects.filter(id_number=client_id).first() is None:
client = ClientForm(request.POST) client = ClientForm(request.POST)
if client.is_valid():
id_number = client.cleaned_data['id_number']
clientform = {'id_number': client.cleaned_data['id_number'],
'name': client.cleaned_data['name'],
'surname': client.cleaned_data['surname']}
if check(id_number):
client.save() client.save()
r = Reservation() r = Reservation()
@ -54,14 +72,18 @@ def reservation(request):
status = "Pokój " + str(room_number) + " został pomyślnie zarejestrowany" status = "Pokój " + str(room_number) + " został pomyślnie zarejestrowany"
return HttpResponse(template.render({'status': status}, request)) return HttpResponse(template.render({'status': status}, request))
else:
error = "Numer dowodu nieprawidłowy" error = "Zły numer dowodu"
except: except:
pass pass
form = ClientForm()
template = loader.get_template('form.html')
return HttpResponse(template.render({'form': form, 'error': error, 'room_number': room_number, 'begin_date': begin_date, 'end_date': end_date}, request))
form = ClientForm(initial=clientform)
template = loader.get_template('form.html')
return HttpResponse(template.render({'form': form,
'error': error,
'room_number': room_number,
'begin_date': begin_date,
'end_date': end_date}, request))
def check(numer): def check(numer):
pattern = re.compile(r'[A-Z][A-Z][A-Z]\d\d\d\d\d\d') pattern = re.compile(r'[A-Z][A-Z][A-Z]\d\d\d\d\d\d')
@ -70,3 +92,4 @@ def check(numer):