Skip to content

Commit 993cbd1

Browse files
committed
Convert new/<foo>/ pages to modal dialogs
Use a modal dialog on the "list" page for a model, instead of having a separate form page at new/<foo>/ This removes the duplication of nav path from the code when creating new model instances.
1 parent 63ffdde commit 993cbd1

26 files changed

Lines changed: 768 additions & 687 deletions

quicktill/tillweb/stocktake.py

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from decimal import Decimal
22
from django.http import Http404
33
from django.http import HttpResponseRedirect
4-
from django.http import HttpResponseForbidden
54
from django.http import JsonResponse
65
from django.urls import reverse
76
from django import forms
87
from django.contrib import messages
8+
from django.shortcuts import redirect
99
from sqlalchemy import inspect
1010
from sqlalchemy.sql import desc
1111
from sqlalchemy.orm import joinedload
@@ -35,6 +35,24 @@ class StockTakeSetupForm(forms.Form):
3535
def stocktakelist(request, info):
3636
may_start = info.user_has_perm("stocktake")
3737

38+
form = None
39+
40+
if may_start:
41+
if request.method == "POST":
42+
form = StockTakeSetupForm(request.POST)
43+
if form.is_valid():
44+
cd = form.cleaned_data
45+
st = StockTake(description=cd['description'],
46+
create_user=info.user)
47+
td.s.add(st)
48+
td.s.flush()
49+
user.log(f"Created stock take {st.logref}")
50+
td.s.commit()
51+
messages.success(request, "Stock take created.")
52+
return redirect(st)
53+
else:
54+
form = StockTakeSetupForm()
55+
3856
pending = td.s.query(StockTake)\
3957
.filter(StockTake.start_time == None)\
4058
.order_by(StockTake.id)\
@@ -59,36 +77,8 @@ def stocktakelist(request, info):
5977
'pending': pending,
6078
'in_progress': in_progress,
6179
'completed': completed,
62-
'may_start': may_start,
63-
'tillobject': StockTake,
64-
})
65-
66-
67-
@tillweb_view
68-
def create_stocktake(request, info):
69-
if not info.user_has_perm("stocktake"):
70-
return HttpResponseForbidden(
71-
"You don't have permission to start a stock take")
72-
73-
if request.method == "POST":
74-
form = StockTakeSetupForm(request.POST)
75-
if form.is_valid():
76-
cd = form.cleaned_data
77-
st = StockTake(description=cd['description'],
78-
create_user=info.user)
79-
td.s.add(st)
80-
td.s.flush()
81-
user.log(f"Created stock take {st.logref}")
82-
td.s.commit()
83-
messages.success(request, "Stock take created.")
84-
return HttpResponseRedirect(st.get_absolute_url())
85-
else:
86-
form = StockTakeSetupForm()
87-
88-
return ('new-stocktake.html', {
89-
'nav': [("Stock takes", reverse("tillweb-stocktakes")),
90-
("New", reverse("tillweb-create-stocktake"))],
9180
'form': form,
81+
'tillobject': StockTake,
9282
})
9383

9484

quicktill/tillweb/templates/tillweb/deliveries.html

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
{% extends "tillweb/tillweb.html" %}
2+
{% load tillweb_utils %}
23

34
{% block title %}{{till}} — Deliveries{% endblock %}
45

56
{% block tillcontent %}
67

7-
{% if may_create_delivery %}
8-
<a class="btn btn-primary mb-2" href="{% url "tillweb-create-delivery" %}">Add new delivery</a>
8+
{% if form %}
9+
<button class="btn btn-primary mb-2" type="button" data-bs-toggle="modal" data-bs-target="#createModal">
10+
Add new delivery
11+
</button>
12+
13+
{% modal "createModal" title="Add new delivery" %}
14+
<form action="" method="post">{% csrf_token %}
15+
<div class="modal-body">
16+
{% include "form-horizontal.html" %}
17+
</div>
18+
<div class="modal-footer">
19+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
20+
Cancel
21+
</button>
22+
<button class="btn btn-primary" type="submit" name="submit_create">
23+
Add delivery
24+
</button>
25+
</div>
26+
</form>
27+
{% endmodal %}
28+
{% if form.errors %}
29+
<script type="text/javascript">
30+
$(document).ready(function() {
31+
$('#createModal').modal('show');
32+
});
33+
</script>
34+
{% endif %}
935
{% endif %}
1036

1137
<table class="table table-striped table-sm" id="deliveries">

quicktill/tillweb/templates/tillweb/departmentlist.html

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% extends "tillweb/tillweb.html" %}
2+
{% load tillweb_utils %}
23

34
{% block title %}{{till}} — Departments{% endblock %}
45

@@ -33,14 +34,43 @@
3334
</tbody>
3435
</table>
3536

36-
{% if may_create_department %}
37-
<a href="{% url "tillweb-create-department" %}" class="btn btn-secondary">Add new department</a>
38-
{% endif %}
39-
4037
<script type="text/javascript">
4138
$(document).ready(function(){
4239
$("#departments").DataTable();
4340
});
4441
</script>
4542

43+
{% if form %}
44+
<button class="btn btn-secondary" type="button" data-bs-toggle="modal" data-bs-target="#createModal">
45+
Add new department
46+
</button>
47+
48+
{% modal "createModal" dialog_class="modal-lg" title="Add new department" %}
49+
<form action="" method="post" class="mb-2">{% csrf_token %}
50+
<div class="modal-body">
51+
{% include "form-horizontal.html" %}
52+
<p class="mb-0">The VAT band determines which VAT rate applies to
53+
sales in this department, and also which business receives the funds
54+
for these sales.</p>
55+
</div>
56+
<div class="modal-footer">
57+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
58+
Cancel
59+
</button>
60+
<button class="btn btn-primary" type="submit" name="submit_create">
61+
Create department
62+
</button>
63+
</div>
64+
</form>
65+
{% endmodal %}
66+
67+
{% if form.errors %}
68+
<script type="text/javascript">
69+
$(document).ready(function() {
70+
$('#createModal').modal('show');
71+
});
72+
</script>
73+
{% endif %}
74+
{% endif %}
75+
4676
{% endblock %}

quicktill/tillweb/templates/tillweb/edit-delivery.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
<button class="btn btn-primary mb-2" type="submit" name="submit_update">
114114
Update
115115
</button>
116-
<a class="btn btn-secondary mb-2" role="button" href="{% url "tillweb-create-stocktype" %}">New stock type</a>
116+
<a class="btn btn-secondary mb-2" role="button" href="{% url "tillweb-stocktype-search" %}">New stock type</a>
117117
<button class="btn btn-danger mb-2" type="submit" name="submit_delete"{% if delivery.items %} disabled{% endif %}>
118118
Delete this delivery
119119
</button>

quicktill/tillweb/templates/tillweb/grouplist.html

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,54 @@
11
{% extends "tillweb/tillweb.html" %}
2+
{% load static %}
3+
{% load tillweb_utils %}
4+
5+
{% if form %}
6+
{% block style %}
7+
{{ block.super }}
8+
<link rel="STYLESHEET" type="text/css" href="{% static "tillweb/multi-select/css/multi-select.css" %}" />
9+
{% endblock %}
10+
11+
{% block jquery %}
12+
{{ block.super }}
13+
<script type="text/javascript" src="{% static "tillweb/multi-select/js/jquery.multi-select.js" %}"></script>
14+
{% endblock %}
15+
{% endif %}
216

317
{% block title %}{{till}} — Groups{% endblock %}
418

519
{% block tillcontent %}
620

7-
{% if may_create_group %}
8-
<a class="btn btn-primary mb-2" href="{% url "tillweb-create-till-group" %}">Add new group</a>
21+
{% if form %}
22+
<button class="btn btn-primary mb-2" type="button" data-bs-toggle="modal" data-bs-target="#createModal">
23+
Add new group
24+
</button>
25+
26+
{% modal "createModal" dialog_class="modal-xl" title="Add a new group" %}
27+
<form action="" method="post">{% csrf_token %}
28+
<div class="modal-body">
29+
{% include "form-horizontal.html" %}
30+
</div>
31+
<div class="modal-footer">
32+
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
33+
Cancel
34+
</button>
35+
<button class="btn btn-primary" type="submit" name="submit_create">
36+
Create group
37+
</button>
38+
</div>
39+
</form>
40+
{% endmodal %}
41+
42+
<script type="text/javascript">
43+
$(document).ready(function(){
44+
$("#id_permissions").multiSelect({
45+
selectableHeader: "<div>Available permissions</div>",
46+
selectionHeader: "<div>Selected permissions</div>"});
47+
{% if form.errors %}
48+
$('#createModal').modal('show');
49+
{% endif %}
50+
});
51+
</script>
952
{% endif %}
1053

1154
{% if groups %}

quicktill/tillweb/templates/tillweb/new-delivery.html

Lines changed: 0 additions & 14 deletions
This file was deleted.

quicktill/tillweb/templates/tillweb/new-department.html

Lines changed: 0 additions & 18 deletions
This file was deleted.

quicktill/tillweb/templates/tillweb/new-group.html

Lines changed: 0 additions & 32 deletions
This file was deleted.

quicktill/tillweb/templates/tillweb/new-paytype.html

Lines changed: 0 additions & 14 deletions
This file was deleted.

quicktill/tillweb/templates/tillweb/new-plu.html

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)