Spaces:
Runtime error
Runtime error
Upload 12 files
Browse files- api/__pycache__/urls.cpython-39.pyc +0 -0
- api/__pycache__/views.cpython-39.pyc +0 -0
- api/urls.py +1 -0
- api/views.py +51 -0
- requirements.txt +2 -0
api/__pycache__/urls.cpython-39.pyc
CHANGED
Binary files a/api/__pycache__/urls.cpython-39.pyc and b/api/__pycache__/urls.cpython-39.pyc differ
|
|
api/__pycache__/views.cpython-39.pyc
CHANGED
Binary files a/api/__pycache__/views.cpython-39.pyc and b/api/__pycache__/views.cpython-39.pyc differ
|
|
api/urls.py
CHANGED
@@ -3,6 +3,7 @@ from .views import *
|
|
3 |
|
4 |
urlpatterns = [
|
5 |
path('', HomeView.as_view(), name='home'),
|
|
|
6 |
path('establishments', EstablishmentListView.as_view(), name='establishment_List'),
|
7 |
path('resources-forward/<str:resource_name>/<str:image_id>', resources_forward, name='resources_forward'),
|
8 |
path('product-category', product_category, name='product_category'),
|
|
|
3 |
|
4 |
urlpatterns = [
|
5 |
path('', HomeView.as_view(), name='home'),
|
6 |
+
path('barcode/<str:code>', BarCodeView.as_view(), name='home'),
|
7 |
path('establishments', EstablishmentListView.as_view(), name='establishment_List'),
|
8 |
path('resources-forward/<str:resource_name>/<str:image_id>', resources_forward, name='resources_forward'),
|
9 |
path('product-category', product_category, name='product_category'),
|
api/views.py
CHANGED
@@ -8,15 +8,66 @@ from django.views.decorators.csrf import csrf_exempt
|
|
8 |
from django.contrib.auth.decorators import login_required
|
9 |
from rest_framework.permissions import IsAuthenticated
|
10 |
from rest_framework.views import APIView
|
|
|
|
|
|
|
11 |
|
12 |
authToken = 'fab57498544244e38bfc2741880f8d61:ed9628295b0642e1b38308795c9cdadd58012df4ceb84a3b9d441c017a1eeac0'
|
13 |
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
class HomeView(APIView):
|
16 |
def get(self, request):
|
17 |
message = "Welcome at home!"
|
18 |
return Response({'message': message})
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
class EstablishmentListView(APIView):
|
22 |
global authToken
|
|
|
8 |
from django.contrib.auth.decorators import login_required
|
9 |
from rest_framework.permissions import IsAuthenticated
|
10 |
from rest_framework.views import APIView
|
11 |
+
from io import BytesIO
|
12 |
+
from PIL import Image, ImageDraw
|
13 |
+
from barcode import Code128
|
14 |
|
15 |
authToken = 'fab57498544244e38bfc2741880f8d61:ed9628295b0642e1b38308795c9cdadd58012df4ceb84a3b9d441c017a1eeac0'
|
16 |
|
17 |
|
18 |
+
def create_barcode_from_binary(binary_text, height=100, line_width=2):
|
19 |
+
"""
|
20 |
+
Create a barcode-like image from binary text where:
|
21 |
+
1 = black line
|
22 |
+
0 = white space
|
23 |
+
|
24 |
+
Parameters:
|
25 |
+
binary_text (str): String of 1s and 0s
|
26 |
+
height (int): Height of the barcode in pixels
|
27 |
+
line_width (int): Width of each line in pixels
|
28 |
+
output_path (str): Path where the image will be saved
|
29 |
+
"""
|
30 |
+
# Validate input
|
31 |
+
if not all(c in '01' for c in binary_text):
|
32 |
+
raise ValueError("Input must contain only 0s and 1s")
|
33 |
+
|
34 |
+
# Calculate dimensions
|
35 |
+
width = len(binary_text) * line_width
|
36 |
+
|
37 |
+
# Create new white image
|
38 |
+
image = Image.new('RGB', (width, height), 'white')
|
39 |
+
draw = ImageDraw.Draw(image)
|
40 |
+
|
41 |
+
# Draw black lines for each '1' in the binary text
|
42 |
+
for i, bit in enumerate(binary_text):
|
43 |
+
if bit == '1':
|
44 |
+
x_position = i * line_width
|
45 |
+
draw.line(
|
46 |
+
[(x_position, 0), (x_position, height)],
|
47 |
+
fill='black',
|
48 |
+
width=line_width
|
49 |
+
)
|
50 |
+
return image
|
51 |
+
|
52 |
class HomeView(APIView):
|
53 |
def get(self, request):
|
54 |
message = "Welcome at home!"
|
55 |
return Response({'message': message})
|
56 |
|
57 |
+
class BarCodeView(APIView):
|
58 |
+
def get(self, request,code):
|
59 |
+
barcode = Code128(code)
|
60 |
+
encode = barcode.build()[0]
|
61 |
+
barcode_image = create_barcode_from_binary(
|
62 |
+
binary_text=encode,
|
63 |
+
height=100,
|
64 |
+
line_width=2
|
65 |
+
)
|
66 |
+
byte_io = BytesIO()
|
67 |
+
barcode_image.save(byte_io, 'PNG')
|
68 |
+
byte_io.seek(0)
|
69 |
+
|
70 |
+
return HttpResponse(byte_io, content_type='image/png')
|
71 |
|
72 |
class EstablishmentListView(APIView):
|
73 |
global authToken
|
requirements.txt
CHANGED
@@ -20,3 +20,5 @@ typing_extensions==4.12.2
|
|
20 |
tzdata==2024.2
|
21 |
urllib3==2.2.3
|
22 |
zipp==3.20.2
|
|
|
|
|
|
20 |
tzdata==2024.2
|
21 |
urllib3==2.2.3
|
22 |
zipp==3.20.2
|
23 |
+
python-barcode==0.15.1
|
24 |
+
pillow==11.0.0
|