Spaces:
Sleeping
Sleeping
File size: 17,095 Bytes
97c790a | 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | from rest_framework.decorators import api_view
from rest_framework.response import Response
from django.db.models import Q
from datetime import timedelta
from django.utils import timezone
from django.core.files.storage import FileSystemStorage
from django.conf import settings
from rest_framework.views import APIView
from rest_framework import status
from .models import BusinessRegistration
from django.db import models
from django.contrib.auth import get_user_model
import jwt
from django.contrib.auth.hashers import make_password
from .models import *
import json
import requests
import os
@api_view()
def categoryData(request):
categories = Category.objects.all()
data = []
for cat in categories:
subCat = []
subcats = SubCategory.objects.filter(category=cat).all()
for sub in subcats:
subCat.append({
"name": sub.name,
"link": sub.link
})
data.append({
"name": cat.name,
"link": cat.link,
"subCategory": subCat,
"isMain": cat.display_category
})
return Response(data)
@api_view()
def productData(request):
page = request.GET.get("page", 1)
size = request.GET.get("size", 25)
products = Product.objects.all()[int(page) * int(size) - int(size):int(page) * int(size)]
data = []
for product in products:
data.append({
"name": product.name,
"link": product.link,
"price": product.basePrice,
"image": product.image_url,
"merchant": product.merchant.name
})
return Response(data)
@api_view()
def searchProduct(request):
query = request.GET.get("query", "")
page = request.GET.get("page", 1)
size = request.GET.get("size", 25)
products = Product.objects.filter(name__icontains=query)[int(page) * int(size) - int(size):int(page) * int(size)]
data = []
for product in products:
data.append({
"name": product.name,
"link": product.link,
"price": product.basePrice,
"image": product.image_url,
"merchant": product.merchant.name
})
return Response(data)
@api_view()
def categoryProduct(request):
category = request.GET.get("category", "")
page = request.GET.get("page", 1)
size = request.GET.get("size", 25)
products = Product.objects.filter(category__link=category)[int(page) * int(size) - int(size):int(page) * int(size)]
if len(products) == 0:
return Response({"error": "No products found in this category"})
data = []
for product in products:
offers = ProductOffer.objects.filter(product=product).all()
data.append({
"name": product.name,
"link": product.link,
"price": product.basePrice,
"image": product.image_url,
"offers": len(offers)
})
return Response(data)
@api_view()
def subCategoryProduct(request):
subcategory = request.GET.get("subcategory", "")
page = request.GET.get("page", 1)
size = request.GET.get("size", 25)
products = Product.objects.filter(subcategory__link=subcategory)[int(page) * int(size) - int(size):int(page) * int(size)]
if len(products) == 0:
return Response({"error": "No products found in this category"})
data = []
for product in products:
data.append({
"name": product.name,
"link": product.link,
"price": product.basePrice,
"image": product.image_url,
"merchant": product.merchant.name
})
return Response(data)
@api_view()
def categoryProductSort(request):
category_link = request.GET.get("category", "")
page = int(request.GET.get("page", 1))
size = int(request.GET.get("size", 25))
order_by = request.GET.get("order_by", "asc")
if order_by == "desc":
sort_order = "-basePrice"
else:
sort_order = "basePrice"
products = Product.objects.filter(category__link=category_link).order_by(sort_order)[(page - 1) * size: page * size]
if not products:
return Response({"error": "No products found in this category"})
data = []
for product in products:
offers = ProductOffer.objects.filter(product=product)
data.append({
"name": product.name,
"link": product.link,
"price": product.basePrice,
"image": product.image_url,
"offers": len(offers)
})
return Response(data)
@api_view()
def subCategoryProductSort(request):
subcategory_link = request.GET.get("subcategory", "")
page = int(request.GET.get("page", 1))
size = int(request.GET.get("size", 25))
order_by = request.GET.get("order_by", "asc")
if order_by == "desc":
sort_order = "-basePrice"
else:
sort_order = "basePrice"
products = Product.objects.filter(subcategory__link=subcategory_link).order_by(sort_order)[(page - 1) * size: page * size]
if not products:
return Response({"error": "No products found in this category"})
data = []
for product in products:
data.append({
"name": product.name,
"link": product.link,
"price": product.basePrice,
"image": product.image_url,
"merchant": product.merchant.name
})
return Response(data)
@api_view()
def filterProducts(request):
query = request.GET.get("query", "")
category = request.GET.get("category", "")
subcategory = request.GET.get("subcategory", "")
rating = request.GET.get("rating", None)
min_price = request.GET.get("min_price", None)
max_price = request.GET.get("max_price", None)
page = int(request.GET.get("page", 1))
size = int(request.GET.get("size", 25))
product_query = Product.objects.all()
if query:
product_query = product_query.filter(Q(name__icontains=query) | Q(description__icontains=query))
if category:
product_query = product_query.filter(category__name=category)
if subcategory:
product_query = product_query.filter(subcategory__name=subcategory)
if min_price is not None:
product_query = product_query.filter(basePrice__gte=min_price)
if max_price is not None:
product_query = product_query.filter(basePrice__lte=max_price)
start = (page - 1) * size
end = page * size
products = product_query[start:end]
data = []
for product in products:
data.append({
"name": product.name,
"link": product.link,
"price": product.basePrice,
"image": product.image_url,
"merchant": product.merchant.name
})
return Response(data)
@api_view()
def topDeals(request):
num_deals = int(request.GET.get("num_deals", 10))
top_deals = Product.objects.all() \
.annotate(max_discount=models.Max('offers__discount')) \
.order_by('-max_discount')[:num_deals]
data = []
for deal in top_deals:
offers = ProductOffer.objects.filter(product=deal).order_by('-discount')
best_offer = offers[0] if offers.exists() else None
data.append({
"name": deal.name,
"link": deal.link,
"price": deal.basePrice,
"image": deal.image_url,
"merchant": deal.merchant.name,
"best_offer": {
"price": best_offer.price if best_offer else None,
"discount": best_offer.discount if best_offer else None,
}
})
return Response(data)
@api_view()
def todayDeals(request):
num_deals = int(request.GET.get("num_deals", 10))
current_date = timezone.now().date()
today_deals = Product.objects.filter(offers__start_date__lte=current_date, offers__end_date__gte=current_date) \
.distinct()[:num_deals]
data = []
for deal in today_deals:
offers = ProductOffer.objects.filter(product=deal, start_date__lte=current_date, end_date__gte=current_date)
best_offer = offers.order_by('-discount').first()
data.append({
"name": deal.name,
"link": deal.link,
"price": deal.basePrice,
"image": deal.image_url,
"merchant": deal.merchant.name,
"best_offer": {
"price": best_offer.price if best_offer else None,
"discount": best_offer.discount if best_offer else None,
}
})
return Response(data)
@api_view()
def topKitchenAppliances(request):
num_deals = int(request.GET.get("num_deals", 10))
kitchen_category = "Kitchen Appliances"
kitchen_appliances = Product.objects.filter(
Q(category__name=kitchen_category) | Q(subcategory__name=kitchen_category)
)[:num_deals]
data = []
for appliance in kitchen_appliances:
offers = ProductOffer.objects.filter(product=appliance)
best_offer = offers.order_by('-discount').first()
data.append({
"name": appliance.name,
"link": appliance.link,
"price": appliance.basePrice,
"image": appliance.image_url,
"merchant": appliance.merchant.name,
"best_offer": {
"price": best_offer.price if best_offer else None,
"discount": best_offer.discount if best_offer else None,
}
})
return Response(data)
# @api_view()
# def register(self, request):
# try:
# data = json.loads(request.body.decode('utf-8'))
# name = data.get('name')
# email = data.get('email')
# mobile_no = data.get('mobile_no')
# password = data.get('password')
# if not all([name, email, mobile_no, password]):
# return Response({'error': 'All fields are required'}, status=400)
# if CustomUser.objects.filter(email=email).exists():
# return Response({'error': 'Email is already in use'}, status=400)
# user = CustomUser.objects.create(
# name=name,
# email=email,
# mobile_no=mobile_no,
# password=make_password(password)
# )
# # Store user data in session
# request.session['user_id'] = user.id
# request.session['name'] = user.name
# request.session['email'] = user.email
# request.session['mobile_no'] = user.mobile_no
# token = self.generate_jwt_token(user)
# return Response({'message': 'User created successfully', 'token': token}, status=201)
# except Exception as e:
# return Response({'error': str(e)}, status=500)
# def generate_jwt_token(self, user):
# expiration_time = timezone.now() + timedelta(days=1) # Token expires in 1 day
# payload = {
# 'user_id': user.id,
# 'exp': expiration_time,
# 'iat': timezone.now(),
# }
# token = jwt.encode(payload, settings.SECRET_KEY, algorithm='HS256')
# return token
User = get_user_model()
class CreateAccountView(APIView):
def post(self, request):
data = request.data
name = data.get('name')
email = data.get('email')
mobile_no = data.get('mobile_no')
password = data.get('password')
confirm_password = data.get('confirm_password')
if not all([name, email, mobile_no, password, confirm_password]):
return Response({"error": "All fields are required."}, status=status.HTTP_400_BAD_REQUEST)
if password != confirm_password:
return Response({"error": "Passwords do not match."}, status=status.HTTP_400_BAD_REQUEST)
request.session['account_data'] = {
'name': name,
'email': email,
'mobile_no': mobile_no,
'password': password,
}
return Response({"message": "Account information stored in session."}, status=status.HTTP_200_OK)
class BusinessRegistrationView(APIView):
def post(self, request):
data = request.data.copy()
file = request.FILES.get('signature_image')
if file:
fs = FileSystemStorage()
filename = fs.save(file.name, file)
file_url = fs.url(filename)
data['signature_image'] = file_url
else:
data['signature_image'] = ''
required_fields = [
'gstin_choice', 'business_name', 'pan_number', 'business_type',
'business_email', 'business_mobile_number', 'address_line_1',
'address_line_2', 'state', 'city', 'pincode', 'signature_image'
]
if all(field in data for field in required_fields):
request.session['business_data'] = data
return Response({"message": "Business registration information stored in session."}, status=status.HTTP_200_OK)
else:
return Response({"error": "Missing required fields."}, status=status.HTTP_400_BAD_REQUEST)
class BankDetailsView(APIView):
def post(self, request):
data = request.data
bank_account_number = data.get('bank_account_number')
bank_ifsc_code = data.get('bank_ifsc_code')
bank_account_holder_name = data.get('bank_account_holder_name')
bank_account_type = data.get('bank_account_type')
if not all([bank_account_number, bank_ifsc_code, bank_account_holder_name, bank_account_type]):
return Response({"error": "All fields are required."}, status=status.HTTP_400_BAD_REQUEST)
request.session['bank_data'] = {
'bank_account_number': bank_account_number,
'bank_ifsc_code': bank_ifsc_code,
'bank_account_holder_name': bank_account_holder_name,
'bank_account_type': bank_account_type,
}
return Response({"message": "Bank details stored in session."}, status=status.HTTP_200_OK)
class StoreCreationView(APIView):
def post(self, request):
data = request.data
required_fields = ['store_display_name', 'primary_category', 'store_disc']
if all(field in data for field in required_fields):
request.session['store_data'] = data
# Process and save all collected data to the database
account_data = request.session.get('account_data')
business_data = request.session.get('business_data')
bank_data = request.session.get('bank_data')
store_data = request.session.get('store_data')
if not (account_data and business_data and bank_data and store_data):
return Response({"error": "Session data is missing."}, status=status.HTTP_400_BAD_REQUEST)
# Create the user
user = User.objects.create_user(
email=account_data['email'],
name=account_data['name'],
mobile_no=account_data['mobile_no'],
password=account_data['password']
)
# Create the business registration entry
BusinessRegistration.objects.create(
user=user,
gstin_choice=business_data['gstin_choice'],
business_name=business_data['business_name'],
pan_number=business_data['pan_number'],
business_type=business_data['business_type'],
business_email=business_data['business_email'],
business_mobile_number=business_data['business_mobile_number'],
address_line_1=business_data['address_line_1'],
address_line_2=business_data['address_line_2'],
state=business_data['state'],
city=business_data['city'],
pincode=business_data['pincode'],
gstin=business_data.get('gstin', ''),
signature_image=business_data['signature_image'],
bank_account_number=bank_data['bank_account_number'],
bank_ifsc_code=bank_data['bank_ifsc_code'],
bank_account_holder_name=bank_data['bank_account_holder_name'],
bank_account_type=bank_data['bank_account_type'],
store_display_name=store_data['store_display_name'],
primary_category=store_data['primary_category'],
store_disc=store_data['store_disc']
)
return Response({"message": "Store creation information stored and data saved."}, status=status.HTTP_200_OK)
else:
return Response({"error": "Missing required fields."}, status=status.HTTP_400_BAD_REQUEST)
|