Skip to content

Commit d1421f5

Browse files
committed
Minor Fixes
1 parent 2f6e48b commit d1421f5

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.2.7 on 2025-10-26 17:00
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('inventory', '0015_userprofile_last_seen'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='item',
15+
name='barcode',
16+
field=models.CharField(blank=True, editable=False, max_length=13, null=True, unique=True),
17+
),
18+
]

inventory/models.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class Item(TimeStampedModel):
114114
default=0,
115115
help_text="Minimum quantity to keep in stock. This amount cannot be checked out."
116116
)
117-
barcode = models.CharField(max_length=12, blank=True, editable=False)
117+
barcode = models.CharField(max_length=13, blank=True, editable=False, unique=True, null=True)
118118
original_section_code = models.PositiveIntegerField(editable=False, null=True)
119119
original_space_code = models.PositiveIntegerField(editable=False, null=True)
120120
search_entry = GenericRelation('SearchEntry', object_id_field='object_id', content_type_field='content_type')
@@ -130,11 +130,16 @@ def save(self, *args, **kwargs):
130130
self.original_section_code = self.space.section.section_code
131131
self.original_space_code = self.space.space_code
132132

133-
self.barcode = (
133+
base_code = (
134134
f"{self.original_section_code:04d}"
135135
f"{self.original_space_code:04d}"
136136
f"{self.item_code:04d}"
137137
)
138+
139+
EAN = barcode.get_barcode_class('ean13')
140+
ean_barcode = EAN(base_code)
141+
142+
self.barcode = ean_barcode.get_fullcode()
138143
super().save(*args, **kwargs)
139144

140145
search_entry, created = SearchEntry.objects.get_or_create(

inventory/views.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from django.db.models.functions import TruncDay
1515
from django.contrib.auth.models import User
1616
from django.contrib.sessions.models import Session
17+
from django.http import HttpResponse, Http404
1718

1819
from .models import Section, Space, Item, PrintQueue, PrintQueueItem, SearchEntry, Student, CheckoutLog, CheckInLog, ItemLog, UserProfile
1920
from .forms import SectionForm, SpaceForm, ItemForm, StudentForm, StockAdjustmentForm, UserUpdateForm, UserRoleForm
@@ -47,8 +48,8 @@ def signup(request):
4748

4849
def universal_lookup(request):
4950
"""
50-
Receives a scanned code and redirects to the appropriate
51-
Item, Space, or Section detail page.
51+
Receives a scanned code and redirects to the appropriate detail page.
52+
Handles multiple barcode formats and shows a user-friendly error if not found.
5253
"""
5354
code = request.GET.get('code', '').strip()
5455

@@ -57,11 +58,12 @@ def universal_lookup(request):
5758
return redirect('homepage')
5859

5960
try:
60-
if code.isdigit() and len(code) >= 12:
61-
item = Item.objects.get(barcode=code[:12])
62-
messages.success(request, f"Found Item: {item.name}")
63-
return redirect(item.get_absolute_url())
6461

62+
if code.isdigit() and len(code) == 13:
63+
item = Item.objects.get(barcode=code)
64+
messages.success(request, f"Scan successful: Found item '{item.name}'.")
65+
return redirect(item.get_absolute_url())
66+
6567
if code.startswith('SHERLOCK;'):
6668
parts = code.split(';')
6769
section_code_str = next((p.split(':')[1] for p in parts if p.startswith('SECTIONCODE:')), None)
@@ -71,19 +73,20 @@ def universal_lookup(request):
7173
section_code = int(section_code_str)
7274
if space_code_str:
7375
space_code = int(space_code_str)
74-
space = get_object_or_404(Space, section__section_code=section_code, space_code=space_code)
75-
messages.success(request, f"Found Space: {space.name}")
76+
space = Space.objects.get(section__section_code=section_code, space_code=space_code)
77+
messages.success(request, f"Scan successful: Found space '{space.name}'.")
7678
return redirect(space.get_absolute_url())
7779
else:
78-
section = get_object_or_404(Section, section_code=section_code)
79-
messages.success(request, f"Found Section: {section.name}")
80+
section = Section.objects.get(section_code=section_code)
81+
messages.success(request, f"Scan successful: Found section '{section.name}'.")
8082
return redirect(section.get_absolute_url())
8183

82-
messages.error(request, f"Could not find any Item, Section, or Space matching the code.")
83-
return redirect('homepage')
8484

85-
except (Item.DoesNotExist, Section.DoesNotExist, Space.DoesNotExist):
86-
messages.error(request, f"Could not find any Item, Section, or Space matching the code.")
85+
raise Item.DoesNotExist
86+
87+
except (Item.DoesNotExist, Section.DoesNotExist, Space.DoesNotExist, ValueError):
88+
89+
messages.error(request, f"Could not find any item, section, or space matching the scanned code.")
8790
return redirect('homepage')
8891

8992
@login_required

0 commit comments

Comments
 (0)