Spaces:
Sleeping
Sleeping
| 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 = '54bef1bd2916433d8f1d821a8822c377:b0be3750efa7486288df03b53076b3c59d710d9f85a34f1a9594d08dda4c4b61' | |
| RevelUrl = "https://101smokeshop.revelup.com" | |
| wooCommerceAuth = "consumer_key=ck_be3c4f80816fa3750154af5c100ad03e450b10b1&consumer_secret=cs_c513ff4e21e426234ca7b2b589c6a88a3749dee2" | |
| TrueServer = False | |
| 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 JsonResponse({'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 = RevelUrl + "/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 = RevelUrl + "" | |
| 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 | |
| data = json.dumps(data).replace(" smoke ", " ").replace("smoke", "").replace(" Smoke ", " ").replace("Smoke", "") | |
| data = json.loads(data) | |
| return JsonResponse({"data": data}) | |
| def resources_forward(request, resource_name,image_id): | |
| url = RevelUrl + "/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 = RevelUrl + "/products/ProductCategory/" | |
| headers = { | |
| 'API-AUTHENTICATION': authToken | |
| } | |
| response = requests.request("GET", url, headers=headers) | |
| data = response.json()["objects"] | |
| return JsonResponse({"data": data}) | |
| class ProductListView(APIView): | |
| permission_classes = [IsAuthenticated] | |
| def get(self, request): | |
| if TrueServer: | |
| url = "https://101smokeshop.com/wp-json/wc/v3/products?"+wooCommerceAuth+"&orderby=popularity" | |
| response = requests.request("GET", url) | |
| try: | |
| data = response.json() | |
| productData = [] | |
| for product in data: | |
| temp={ | |
| "id": product["id"], | |
| "name": product["name"], | |
| "image": product["images"][0]["src"], | |
| "link": product["permalink"] | |
| } | |
| productData.append(temp) | |
| return JsonResponse({"data": productData}) | |
| except json.JSONDecodeError: | |
| print(response.text) | |
| else: | |
| with open("./api/trendingProducts.json", "r") as f: | |
| data = json.load(f) | |
| return JsonResponse({"data": data}) | |
| class BestSellersView(APIView): | |
| permission_classes = [IsAuthenticated] | |
| def get(self, request): | |
| if TrueServer: | |
| url = "https://101smokeshop.com/wp-json/wc/v3/products?"+wooCommerceAuth+"&orderby=menu_order" | |
| response = requests.request("GET", url) | |
| try: | |
| data = response.json() | |
| best_sellers = [] | |
| for product in data: | |
| temp={ | |
| "id": product["id"], | |
| "name": product["name"], | |
| "image": product["images"][0]["src"], | |
| "link": product["permalink"], | |
| "price": product["price"] if product["price"] else product["regular_price"] if product["regular_price"] else product["sale_price"] if product["sale_price"] else 0 | |
| } | |
| best_sellers.append(temp) | |
| print(best_sellers) | |
| return JsonResponse({"data": best_sellers}) | |
| except json.JSONDecodeError: | |
| print(response.text) | |
| return JsonResponse({"data": []}) | |
| else: | |
| with open("./api/bestSellers.json", "r") as f: | |
| data = json.load(f) | |
| 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"}) | |
| class ImagesView(APIView): | |
| permission_classes = [] | |
| def get(self, request): | |
| catImages = ["https://d2p2lri02rh20e.cloudfront.net/product-images/cyrine-cartridgebattery-biride-510voltage-5ctbx1711652088702.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/geekvape-an-2-vape-device-kit-e-juice-device-1100mah1703199105406.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/pod-juice-e-liquid-nicotine-tfn-3mg-ml-100ml-ct1713545069848.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/free-noms-e-liquid-nicotine-ice-3mg-ml-120ml-ct1713370750626.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/candy-king-e-liquid-salt-nicotine-ice-35mg-ml-30ml-ct1713363531128.", "https://d2p2lri02rh20e.cloudfront.net/product-images/air-factory-e-liquid-nicotine-ice-3mg-ml-60ml-ct1713360735189.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/rc-upload-1699734904189-42--mints_e_liquid_nicotine_3mg_60ml_2ct_bx.", "https://d2p2lri02rh20e.cloudfront.net/product-images/pod-juice-e-liquid-nicotine-tfn-3mg-ml-100ml-ct1713545069848.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/recon-4g-by-wulf-mods-vaporizer-device-kit-9ct-bx1717079743309.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/ganling-vaporizerdevicekit-withbluetooth-gunv2nu1709414588915.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/ooze-brink-1800mah1708709429277.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/puffco-coloredglass-replacement1704843967439.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/recon-4g-by-wulf-mods-vaporizer-device-kit-9ct-bx1717079743309.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/nbc-glasspipe-acc-nectar-collecter-4-5-1ct1722177340675.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/getlost-glasspipe-waterpipe-15-1ct-ray-gun1721248337222.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/nbc-hookah-1-hose-19-1ct-robot1720721034430.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/nbc-hookah-kit-32-1ct-pressure-gauge-with-tap1720722809372.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/nbc-hookah-kit-1h-21-1ct-panther-black1719261388482.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/zebrasmoke-hookahkit-1h20-1ct-pistol1711749911268.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/mya-hookah-acc-qt-base-5.5-1ct1702596340867.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/nbc-hookah-1-hose-19-1ct-robot1720721034430.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/nbc-hookah-kit-32-1ct-pressure-gauge-with-tap1720722809372.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/rc-upload-1674168341195-2--fumari_shisha_100gm.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/rc-upload-1680990149489-2--eternal_smoke_al_flavored_molasses_250gm.", "https://d2p2lri02rh20e.cloudfront.net/product-images/rc-upload-1674161380200-2--starbuzz_bold.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/rc-upload-1674167817817-3--ror_shisha.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/rc-upload-1674168341195-2--fumari_shisha_100gm.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/rc-upload-1680990149489-2--eternal_smoke_al_flavored_molasses_250gm.", "https://d2p2lri02rh20e.cloudfront.net/product-images/rc-upload-1697039328485-2--coconara_hookah_charcoal_60ct_pk.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/rc-upload-1682604308875-2--black_owl_charcoal_31mmm_xl_1kg_36ct_bx.", "https://d2p2lri02rh20e.cloudfront.net/product-images/titanium-flats-hookah-charcoal-108ct-bx-coconut1701901803262.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/rc-upload-1688161551426-2--charcoblaze_150ct.webp", "https://d2p2lri02rh20e.cloudfront.net/product-images/rc-upload-1697039328485-2--coconara_hookah_charcoal_60ct_pk.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/rc-upload-1682604308875-2--black_owl_charcoal_31mmm_xl_1kg_36ct_bx.", "https://d2p2lri02rh20e.cloudfront.net/product-images/get-lost-smoking-acc-4-in-1-glow-jar-1ct-galaxy1721249518049.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/get-lost-smoking-acc-kit-all-in-one-7pc-pk-1pk1717509223381.png", "https://d2p2lri02rh20e.cloudfront.net/product-images/v-syndicate-smoking-acc-rolling-tray-metal-1ct-medium1714234747472.", "https://d2p2lri02rh20e.cloudfront.net/product-images/get-lost-smoking-acc-4-in-1-glow-jar-1ct-galaxy1721249518049.png"] | |
| imageOrignal = ["/static/home/1.jpg", "/static/home/2.jpg", "/static/home/3.jpg", "/static/home/4.jpg", "/static/home/5.jpg"] | |
| imageGas = ["/static/home/1.png", "/static/home/2.png", "/static/home/3.png", "/static/home/4.png", "/static/home/5.png"] | |
| return JsonResponse({"data": {"catImages": catImages, "posterImage": imageGas if not TrueServer else imageOrignal}}) |