Spaces:
Running
Running
from django.shortcuts import render | |
from django.http import JsonResponse, HttpResponse | |
import requests | |
import json | |
import base64 | |
from authentication.models import UserData, Coupon | |
from django.views.decorators.csrf import csrf_exempt | |
from django.contrib.auth.decorators import login_required | |
from rest_framework.permissions import IsAuthenticated | |
from rest_framework.views import APIView | |
from io import BytesIO | |
from PIL import Image, ImageDraw | |
from barcode import Code128 | |
authToken = 'fab57498544244e38bfc2741880f8d61:ed9628295b0642e1b38308795c9cdadd58012df4ceb84a3b9d441c017a1eeac0' | |
def create_barcode_from_binary(binary_text, height=100, line_width=2): | |
""" | |
Create a barcode-like image from binary text where: | |
1 = black line | |
0 = white space | |
Parameters: | |
binary_text (str): String of 1s and 0s | |
height (int): Height of the barcode in pixels | |
line_width (int): Width of each line in pixels | |
output_path (str): Path where the image will be saved | |
""" | |
# Validate input | |
if not all(c in '01' for c in binary_text): | |
raise ValueError("Input must contain only 0s and 1s") | |
# Calculate dimensions | |
width = len(binary_text) * line_width | |
# Create new white image | |
image = Image.new('RGB', (width, height), 'white') | |
draw = ImageDraw.Draw(image) | |
# Draw black lines for each '1' in the binary text | |
for i, bit in enumerate(binary_text): | |
if bit == '1': | |
x_position = i * line_width | |
draw.line( | |
[(x_position, 0), (x_position, height)], | |
fill='black', | |
width=line_width | |
) | |
return image | |
class HomeView(APIView): | |
def get(self, request): | |
message = "Welcome at home!" | |
return Response({'message': message}) | |
class BarCodeView(APIView): | |
def get(self, request,code): | |
barcode = Code128(code) | |
encode = barcode.build()[0] | |
barcode_image = create_barcode_from_binary( | |
binary_text=encode, | |
height=100, | |
line_width=2 | |
) | |
byte_io = BytesIO() | |
barcode_image.save(byte_io, 'PNG') | |
byte_io.seek(0) | |
return HttpResponse(byte_io, content_type='image/png') | |
class EstablishmentListView(APIView): | |
global authToken | |
def get(self, request): | |
url = "https://101smokeshop-uat.revelup.com/enterprise/Establishment/?order_by=id&limit=10&offset=0" | |
headers = { | |
'API-AUTHENTICATION': authToken | |
} | |
response = requests.get(url, headers=headers) | |
data = response.json().get("objects", []) | |
urlBase = "https://101smokeshop-uat.revelup.com" | |
for i in range(len(data)): | |
try: | |
imgData = requests.get(urlBase + data[i]["logo_img"], headers=headers).json() | |
data[i]["logo_img"] = imgData.get("image_url", "") | |
except: | |
pass | |
try: | |
data[i]["address"] = requests.get(urlBase + data[i]["address"], headers=headers).json() | |
except: | |
pass | |
return JsonResponse({"data": data}) | |
def resources_forward(request, resource_name,image_id): | |
url = "https://101smokeshop-uat.revelup.com/resources/"+resource_name+"/"+image_id | |
headers = { | |
'API-AUTHENTICATION': authToken | |
} | |
response = requests.request("GET", url, headers=headers) | |
imageUrl = response.json()["image_url"] | |
response = requests.request("GET", imageUrl) | |
return HttpResponse(response.content, content_type=response.headers['Content-Type']) | |
def product_category(request): | |
global authToken | |
url = "https://101smokeshop-uat.revelup.com/products/ProductCategory/" | |
headers = { | |
'API-AUTHENTICATION': authToken | |
} | |
response = requests.request("GET", url, headers=headers) | |
data = response.json()["objects"] | |
return JsonResponse({"data": data}) | |
class CouponListView(APIView): | |
permission_classes = [IsAuthenticated] | |
def get(self, request): | |
discount_functions = ["CORE", "CUSTOMER", "LOYALTY", "GIFT_WITH_PURCHASE"] | |
discount_methods = ["POINT_REDEMPTION", "REDEEMED", "GIVE_AWAY", "BONUS_COUPON", "VOUCHER_GENERIC", "VOUCHER_LOYALTY", "VOUCHER_FUEL", "VOUCHER_THIRDPARTY"] | |
discount_types = ["AMOUNT", "PERCENT", "RE_PRICE", "ALT_PRICE"] | |
how_often_apply = ["ALL_APPLICABLE", "ONCE_PER_ORDER", "ONCE_PER_SELECTION"] | |
qualification_types = ["ALL", "ITEM", "ORDER"] | |
rewards_types = ["PURCHASES", "ITEMS", "VISIT"] | |
user = UserData.objects.get(user=request.user) | |
coupons = user.coupons.all() | |
redeemedCoupons = user.activatedCoupons.all() | |
data = [] | |
for coupon in coupons: | |
data.append({ | |
"name": coupon.name, | |
"discription": coupon.discription, | |
"pointsNeededForRedemption": coupon.pointsNeededForRedemption, | |
"couponCode": coupon.couponCode, | |
"loyaltyCode": coupon.loyaltyCode, | |
"discount": coupon.discount, | |
"isExpired": coupon.isExpired, | |
"isRedeemed": coupon in redeemedCoupons, | |
"expiryDate": coupon.expiryDate, | |
"discountFunction": discount_functions[coupon.discountFunction], | |
"discountMethod": discount_methods[coupon.discountMethod], | |
"discountType": discount_types[coupon.discountType], | |
"howOftenApply": how_often_apply[coupon.howOftenApply], | |
"qualificationType": qualification_types[coupon.qualificationType], | |
"rewardsType": rewards_types[coupon.rewardsType], | |
}) | |
return JsonResponse({"data": data}) | |
class CouponRedeemView(APIView): | |
permission_classes = [IsAuthenticated] | |
def post(self, request): | |
couponCode = request.data["couponCode"] | |
user = UserData.objects.get(user=request.user) | |
coupon = Coupon.objects.get(couponCode=couponCode) | |
if coupon not in user.coupons.all() or coupon == None: | |
return JsonResponse({"message": "Coupon is not available for this user"}) | |
if coupon.isExpired: | |
return JsonResponse({"message": "Coupon is expired"}) | |
if user.rewardPoints < coupon.pointsNeededForRedemption: | |
return JsonResponse({"message": "You don't have enough points to redeem this coupon"}) | |
user.rewardPoints -= coupon.pointsNeededForRedemption | |
user.save() | |
user.activatedCoupons.add(coupon) | |
return JsonResponse({"message": "Coupon redeemed successfully"}) |