Spaces:
Runtime error
Runtime error
File size: 6,745 Bytes
7c53168 5868895 9f09b01 7c53168 9f09b01 5868895 7c53168 9f09b01 5868895 7c53168 5868895 7c53168 5868895 085ce5c 5868895 085ce5c 5868895 085ce5c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
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"}) |