def update_item(item_id, item_dict, user_id): if not (Store.objects.filter(items__id=item_id).exists() and User.objects.get(id=user_id).has_perm( 'EDIT_ITEM', Store.objects.filter(items__id=item_id)[0])): return [ False, "you don't have the permission to update an item or the item doesn't exists!" ] c_Item.get_item(item_id=item_id).update(item_dict=item_dict) return True
def delete_item(item_id, user_id): if not (Store.objects.filter(items__id=item_id).exists() and User.objects.get(id=user_id).has_perm( 'REMOVE_ITEM', Store.objects.filter(items__id=item_id)[0])): return [ False, "you don't have the permission to delete this item from the store or the item doesn't exists!" ] c_Item.get_item(item_id=item_id).delete() return True
def buy_logic(item_id, amount, amount_in_db, is_auth, username, shipping_details, card_details, is_cart, user_id, total_amount): pay_transaction_id = -1 supply_transaction_id = -1 messages_ = '' c_item = c_Item.get_item(item_id=item_id) amount_in_db1 = Item.objects.get(id=item_id).quantity if c_item.has_available_amount(amount): total = c_item.calc_total(amount=amount) if not c_item.check_rules(amount=amount): messages_ += "you can't buy due to item policies" return False, 0, 0, messages_ store_of_item = c_Store.get_item_store(item_pk=item_id) if not store_of_item.check_rules(total_amount, shipping_details['country'], is_auth): messages_ += "you can't buy due to store policies" return False, 0, 0, messages_ total_after_discount = "" if is_cart else store_of_item.apply_discounts( c_item=c_item, amount=int(amount)) # if (is_cart is False): # total_after_discount = store_of_item.apply_discounts(c_item=c_item, amount=int(amount)) try: if pay_system.handshake(): print("pay hand shake") pay_transaction_id = pay_system.pay( str(card_details['card_number']), str(card_details['month']), str(card_details['year']), str(card_details['holder']), str(card_details['cvc']), str(card_details['id'])) if pay_transaction_id == '-1': messages_ += '\n' + 'can`t pay !' return False, 0, 0, messages_ else: messages_ += '\n' + 'can`t connect to pay system!' return False, 0, 0, messages_ if supply_system.handshake(): print("supply hand shake") supply_transaction_id = supply_system.supply( str(shipping_details['name']), str(shipping_details['address']), str(shipping_details['city']), str(shipping_details['country']), str(shipping_details['zip'])) if supply_transaction_id == '-1': pay_system.cancel_pay(pay_transaction_id) messages_ += '\n' + 'can`t supply abort payment!' return False, 0, 0, messages_ else: pay_system.cancel_pay(pay_transaction_id) messages_ += '\n' + 'can`t connect to supply system abort payment!' return False, 0, 0, messages_ c_item.quantity = amount_in_db1 - amount c_item.save() try: item_subject = ItemSubject(c_item.pk) if (is_auth): notification = Notification.objects.create( msg=username + ' bought ' + str(amount) + ' pieces of ' + c_item.name) notification.save() item_subject.subject_state = item_subject.subject_state + [ notification.pk ] else: notification = Notification.objects.create( msg='A guest bought ' + str(amount) + ' pieces of ' + c_item.name) notification.save() item_subject.subject_state = item_subject.subject_state + [ notification.pk ] except Exception as e: messages_ += 'cant connect websocket ' + str(e) _item_name = c_item.name if c_item.quantity == 0: c_item.delete() messages_ += '\n' + 'Thank you! you bought ' + _item_name + '\n' + 'Total after discount: ' \ + str(total_after_discount) + ' $' + '\n' + 'Total before: ' + str(total) + ' $' return True, total, total_after_discount, messages_ except Exception as a: c_item.quantity = amount_in_db1 c_item.save() if not (pay_transaction_id == '-1'): messages_ += '\n' + 'failed and aborted pay! please try again!' pay_system.cancel_pay(pay_transaction_id) if not (supply_transaction_id == '-1'): messages_ += '\n' + 'failed and aborted supply! please try again!' supply_system.cancel_supply(supply_transaction_id) messages_ = "Exception! " ' : ' + str(a) return False, 0, 0, messages_ else: messages_ = "no such amount for item : " + str( item_id) + ' messages_ : ' + messages_ return False, 0, 0, messages_
def get_store_items(store_id): return list( map(lambda i_d: c_Item.get_item(item_id=i_d).to_dict(), c_Store.get_store(store_id=store_id).all_items_ids()))
def remove_item_from_cart(user_id, item_id): c_Cart.get_cart(user_id=user_id).remove_item(item_id=item_id) item = c_Item.get_item(item_id=item_id) if item.quantity == 0: item.delete()
def amount_in_db(item_id): return c_Item.get_item(item_id=item_id).quantity > 0
def get_item_details(item_id): return c_Item.get_item(item_id=item_id).get_details()
def search(txt): return c_Item.search(txt=txt)
def items(self): return list( map(lambda i: c_Item.get_item(item_id=i.pk), list(self._model.items.all())))