Skip to content

Commit

Permalink
Deal with creating a duplicate stock type through the web interface
Browse files Browse the repository at this point in the history
Creating a duplicate stock type no longer crashes. Instead it redirects
to the existing stock type page with a helpful error message.

Closes #265 on github.
  • Loading branch information
sde1000 committed Jan 25, 2025
1 parent f1902a3 commit c8e47fd
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions quicktill/tillweb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,16 +1384,31 @@ def create_stocktype(request, info):
form = NewStockTypeForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
s = StockType(
department=cd['department'],
manufacturer=cd['manufacturer'],
name=cd['name'],
abv=cd['abv'],
unit=cd['unit'],
saleprice=cd['saleprice'],
)
td.s.add(s)
td.s.flush()
try:
s = StockType(
department=cd['department'],
manufacturer=cd['manufacturer'],
name=cd['name'],
abv=cd['abv'],
unit=cd['unit'],
saleprice=cd['saleprice'],
)
td.s.add(s)
td.s.commit()
except sqlalchemy.exc.IntegrityError:
td.s.rollback()
messages.error(
request, "Could not create a new stock type: there is "
"another stock type that's an exact match for the new "
"details. This stock type is shown below.")
s = td.s.query(StockType)\
.filter(StockType.department == cd['department'])\
.filter(StockType.manufacturer == cd['manufacturer'])\
.filter(StockType.name == cd['name'])\
.filter(StockType.abv == cd['abv'])\
.filter(StockType.unit == cd['unit'])\
.one()
return HttpResponseRedirect(s.get_absolute_url())
user.log(f"Created stock type {s.logref}")
td.s.commit()
messages.success(request, "New stock type created")
Expand Down

0 comments on commit c8e47fd

Please sign in to comment.