def add_to_cart(req): try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error': 'Please login first'},status=401) if(verify_auth(token)): if req.method == 'POST': data = json.loads(req.body) productId = data['productId'] username = data['username'] product = Product.objects.get(pk=productId) user = User.objects.get(username=username) Cart.objects.create(owner=user,product=product) cartList = list(Cart.objects.filter(owner=user).values()) for each in cartList: product_id = each['product_id'] product = Product.objects.get(pk=product_id) each['product_name'] = product.product_name each['product_pic'] = product.product_pic.url each['store'] = product.store.name each['store_id'] = product.store.id each['price'] = product.price each['description'] = product.description return JsonResponse({'inCart':cartList},status=200) else: return JsonResponse({'error':'Method not allowed'},status=405) else: return JsonResponse({'error':'Please login First'},status=401)
def update_profile_skills_view(req): data = json.loads(req.body) try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error': 'Please login first'}) if (verify_auth(token) and is_owner(token, data['username'])): if req.method == 'POST': username = data['username'] skill = data['skill'] user = User.objects.get(username=username) profile = Profile.objects.get(user=user) flag = skill_is_in_profile(skill, profile) if flag[0]: return JsonResponse({'skills': flag[1]}, status=200) else: return JsonResponse({'error': flag[1]}, status=403) elif req.method == 'DELETE': data = json.loads(req.body) username = data['username'] skill_id = data['skill_id'] user = User.objects.get(username=username) profile = Profile.objects.get(user=user) skill = Skill.objects.get(id=skill_id) skill_from_profile = HasSkill.objects.get(profile=profile, skill=skill) skill_from_profile.delete() return JsonResponse({'success': 'Succesfully Deleted'}, status=200) else: return JsonResponse({'error': 'Method Not Allowed'}) else: JsonResponse({'error': 'You are not the owner'})
def get_products(req,pk): try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error':'Please Login first'},status=401) if(verify_auth(token)): if req.method=='GET': store = Store.objects.get(pk=pk) store_id = store.id store_pic = store.picture.url store_name = store.name store_desc = store.description product_list = list(Product.objects.filter(store=store).values()) try: loyalty = LoyalityProgram.objects.get(store=store) for each in product_list: each['store_pic'] = store_pic each['store_id'] = store_id each['store_name'] = store_name each['store_desc'] = store_desc each['loyalty'] = loyalty.name each['loyalty_disc'] = loyalty.discount each['loyalty_id'] = loyalty.id return JsonResponse({'products':product_list},status=200) except: return JsonResponse({'products':product_list},status=200) else: return JsonResponse({'error':'Method not allowed'},status=405) else: return JsonResponse({'error':'Please login first'},status=401)
def buy_product(req): data = json.loads(req.body) try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error': 'Please Login First'},status=401) if (verify_auth(token)): if req.method == 'POST': data = json.loads(req.body) cartProducts = data['data'] for each in cartProducts: print(each) product_id = each['product_id'] user_id = each['owner_id'] store_id = each['store_id'] product = Product.objects.get(pk=product_id) store = Store.objects.get(pk=store_id) user = User.objects.get(pk=user_id) BoughtBy.objects.create(product=product,user=user) loyalP = LoyalityProgram.objects.filter(store=store) if(len(loyalP)!=0) : lp_discount = loyalP[0].discount m = MyLoyality.objects.filter(user=user,loyality=loyalP[0]) if(len(m)!=0) : wallet = Wallet.objects.get(owner=user) wallet.money = wallet.money + (lp_discount*each['price'])/100 wallet.save() Cart.objects.filter(owner=user).delete() cartList = list(Cart.objects.filter(owner=user).values()) return JsonResponse({'inCart': cartList}, status=200) return JsonResponse({'error': 'Method Not Allowed'}, status=405) else: return JsonResponse({'error': 'You are not the owner'}, status=401)
def load_profile(req,username): try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error':'Please login First'},status=401) if req.method == 'GET': if(verify_auth(token)): user = User.objects.get(username=username) profile = Profile.objects.get(user=user) try: propic = profile.picture.url except: propic = '' wallet = Wallet.objects.get(owner=user).money data = { 'username':user.username, 'email':user.email, 'propic':propic, 'wallet':wallet } return JsonResponse(data,status=200) return JsonResponse({'error':'Please login First'},status=401) else: return JsonResponse({'error':'Method Not Allowed'},status=405)
def get_stores(req): try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error':'Please Login First'},status=401) if(verify_auth(token)): if req.method == 'GET': all_stores = Store.objects.all().values() stores_list = list(all_stores) return JsonResponse({'data':stores_list},status=200) else: return JsonResponse({'error':'Method not allowed'},status=405) else: return JsonResponse({'error':'Please Login First'},status=401)
def add_review(req): try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error':'Please Login First'},status=401) if req.method == 'POST': if(verify_auth(token)): data = json.loads(req.body) user = User.objects.get(username=data['username']) product = Product.objects.get(pk=data['productId']) review = Review.objects.create(user=user,product=product,comment=data['content']) return JsonResponse({'success':'Review Submitted'},status=200) else: return JsonResponse({'error':'Please Login First'},status=401) else: return JsonResponse({'error':'Method Not allowed'},status=405)
def check_enrollment(req, loyality_id, username): try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error': 'Please login first'}, status=401) if req.method == 'GET': if (verify_auth(token)): user = User.objects.get(username=username) loyality = LoyalityProgram.objects.get(pk=loyality_id) myloyality = MyLoyality.objects.filter(user=user, loyality=loyality) if len(myloyality) != 0: return JsonResponse({'value': 1}, status=200) else: return JsonResponse({'value': 0}, status=200) return JsonResponse({'error': 'Please Login first'}, status=401) return JsonResponse({'error': 'Method Not allowed'}, status=405)
def load_my_transactions(req,username): try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error':'Please login first'},status=401) if(verify_auth(token)): if req.method=='GET': user = User.objects.get(username=username) bought = list(BoughtBy.objects.filter(user=user).values()) for each in bought: product = Product.objects.get(pk=each['product_id']) each['name'] = product.product_name each['price'] = product.price each['pic'] = product.product_pic.url return JsonResponse({"data":bought}) else: return JsonResponse({"error":"Method Not Allowed"},status=405) else: return JsonResponse({"error":"Please login first"},status=401)
def load_review(req,id): try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error':'Please Login first'},status=401) if req.method == 'GET': if(verify_auth(token)): product = Product.objects.get(pk=id) reviews = list(Review.objects.filter(product=product).values()) for each in reviews: user = User.objects.get(pk=each['user_id']) profile = Profile.objects.get(user=user) try: each['profile_pic'] = profile.picture.url except: each['profile_pic'] = '' each['username'] = user.username return JsonResponse({'data':reviews},status=200) return JsonResponse({'error':'Please Login first'},status=401) return JsonResponse({'error':'Method Not allowed'},status=405)
def update_profile_view(req): data = json.loads(req.body) try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error': 'Please Login First'}) if (verify_auth(token) and is_owner(token, data['username'])): if req.method == 'PUT': data = json.loads(req.body) username = data['username'] college = data['college'] picture = data['picture'] user = User.objects.get(username=username) profile = Profile.objects.get(user=user) profile.college = college profile.picture = picture profile.save() return JsonResponse({'success': 'Profile Updated'}, status=200) return JsonResponse({'error': 'Method Not Allowed'}, status=405) else: return JsonResponse({'error': 'You are not the owner'}, status=401)
def enroll_in_loyality(req): if (req.method == 'POST'): data = json.loads(req.body) try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error': 'Please login first'}) if (verify_auth(token)): loyaltyId = data['loyaltyId'] username = data['username'] loyality = LoyalityProgram.objects.get(pk=loyaltyId) user = User.objects.get(username=username) MyLoyality.objects.create(user=user, loyality=loyality) data = { "message": "Enrolled Successfully", "value": 1, "mode": "success" } return JsonResponse(data, status=200) else: return JsonResponse({"message": "Method not allowed"}, status=405)
def unfollow_view(req): try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error': 'Please login first'}) data = json.loads(req.body) if (verify_auth(token) and is_owner(token, data['username'])): if req.method == 'DELETE': username = data['username'] following = data['following'] profile = User.objects.get(username=following) follower = User.objects.get(username=username) try: unfollow = Following.objects.get(profile=profile, follower=follower) unfollow.delete() return JsonResponse( {'message': 'Unfollowed' + ' ' + following}) except: return JsonResponse({'error': 'You do not follow the user'}) return JsonResponse({'error': 'Method Not allowed'}) else: return JsonResponse({'error': 'You are not the owner'})
def remove_from_cart(req,cart_id,username): try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error': 'Please loginn First'},status=401) if(verify_auth(token)): if req.method == 'DELETE': user = User.objects.get(username=username) cartObj = Cart.objects.get(pk=cart_id) cartObj.delete() cartList = list(Cart.objects.filter(owner=user).values()) for each in cartList: product_id = each['product_id'] product = Product.objects.get(pk=product_id) each['product_name'] = product.product_name each['product_pic'] = product.product_pic.url each['store'] = product.store.name each['price'] = product.price each['description'] = product.description return JsonResponse({'inCart':cartList},status=200) else: return JsonResponse({'error':'Method Not allowed'},status=405) else: return JsonResponse({'error':'Please login First'},status=401)
def follow_view(req): try: token = req.META['HTTP_AUTHORIZATION'] except: return JsonResponse({'error': 'Please login first'}) data = json.loads(req.body) if (verify_auth(token) and is_owner(token, data['follower'])): if req.method == 'POST': username = data['username'] follower = data['follower'] profile = User.objects.get(username=username) follower = User.objects.get(username=follower) try: following = Following.objects.get(profile=profile, follower=follower) return JsonResponse({'msg': 'already following'}) except Exception as e: following = Following.objects.create(profile=profile, follower=follower) return JsonResponse({'msg': 'following'}) else: return JsonResponse({'error': 'Method Not Allowed'}) else: return JsonResponse({'error': 'You are not the owner'})