def add_product(): json_dict = request.json item = Product() error = item.update(json_dict['product']) if len(error) > 0: return Responses.OPERATION_FAILED() product = Product.query.filter_by( name=json_dict['product']['name']).first() variations = [] variations_dict = json_dict['variations'] for variation_dict in variations_dict: variation = Variation() if variation.update_from_dict(variation_dict): variation.product_id = product.id if variation.next_available_date != None: # ensure this is a date string if is_date(variation.next_available_date): variation.next_available_date = datetime.datetime.strptime( variation.next_available_date, '%Y-%m-%d %H:%M:%S') variations.append(variation) for variation in variations: if len(variation.update()) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def confirm_user_subscription(token): secret_key = ConfigValues.get_config_value( 'EMAIL_PASSWORD_RESET_SECRET_KEY') confirm_serializer = URLSafeTimedSerializer(secret_key) try: email = confirm_serializer.loads(token, salt='email-confirm', max_age=3600) except SignatureExpired: return Responses.TOKEN_EXPIRED() json_dict = request.json email = json_dict['email'] user = User.get_user_by_email(email) if not user: return Responses.NOT_EXIST() subscription_type = json_dict['subscription_type'] subscription = SubscriptionType.get_items(subscription_type) start_date = date.today() end_date = start_date + relativedelta(months=int(1)) #end_date = start_date + relativedelta(months=int(subscription.plan)) user_subscription = UserSubscription(user.id, end_date, subscription.id) error = user_subscription.update() if len(error) > 0: Responses.OPERATION_FAILED() user.subscribed = True user_error = user.update() if len(user_error) > 0: Responses.OPERATION_FAILED() return Responses.SUCCESS()
def update_product(id): item = Product.query.get(id) if not item: return Responses.NOT_EXIST() json_dict = request.json if len(item.update(json_dict['product'])) > 0: return Responses.OPERATION_FAILED() current_variations = Variation.query.filter_by(product_id=item.id).all() current_variations_names = [] for variation in current_variations: current_variations_names.append(variation.name) variations_dict = json_dict['variations'] for variation_dict in variations_dict: variation = Variation() variation.update_from_dict(variation_dict) if variation.next_available_date != None: # ensure this is a date string if is_date(variation.next_available_date): variation.next_available_date = datetime.datetime.strptime( variation.next_available_date, '%Y-%m-%d %H:%M:%S') if variation.name in current_variations_names: for current_variation in current_variations: if variation.name == current_variation.name: if len(current_variation.update(variation_dict)) > 0: return Responses.OPERATION_FAILED() else: #a new variation is being added to this product if len(variation.update()) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def delete_product(id=None): """ get_products returns all product or the product with specific id Args: id ([type]): product id Returns: [type]: [description] """ item = Product.query.get(id) variations = Variation.get_items_for_id(product_id=item.id) # delete variations before product for variation in variations: if len(variation.delete()) > 0: return Responses.OPERATION_FAILED() # deletes product error = item.delete() if len(error) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def add_subscription_type(): json_dict = request.json if 'plan' not in json_dict: return Responses.OPERATION_FAILED(Messages.NEEDED_FIELD_EMPTY) if 'price' not in json_dict: return Responses.OPERATION_FAILED(Messages.NEEDED_FIELD_EMPTY) existing_item = SubscriptionType.get_items(plan=json_dict['plan']) if len(existing_item) > 0: return Responses.OBJECT_EXIST() item = SubscriptionType(json_dict['plan'], json_dict['price']) error = item.insert_as_new_item(json_dict, ['plan', 'price']) if len(error) > 0: return Responses.OPERATION_FAILED(error) return res(item.as_dict())
def add_article_category(): json_dict = request.json item = ArticleCategory() error = item.update(json_dict) if len(error) > 0: return Responses.OPERATION_FAILED() return res(item.as_dict())
def add_order_status(): json_dict = request.json item = OrderStatus() error = item.update(json_dict) if len(error) > 0: return Responses.OPERATION_FAILED() return res(item.as_dict())
def get_user_orders(id=None): page, per_page = get_page_from_args() sort_by = request.args.get('sort_by') is_desc = parse_int(request.args.get('is_desc')) user_id = parse_int(request.args.get('user_id')) # order_id = parse_int(request.args.get('order_id')) status_id = parse_int(request.args.get('status')) if user_id: items = Order.get_items(user_id=user_id, status_id=status_id, page=page, per_page=per_page, sort_by=sort_by, is_desc=is_desc) return res([item.as_dict() for item in items]) elif id: items = [Order.query.get(id)] if id else Order.get_items( user_id=user_id, status_id=status_id, page=page, per_page=per_page, sort_by=sort_by, is_desc=is_desc) return res([item.as_dict() for item in items][0]) else: return Responses.OPERATION_FAILED()
def update_order(id): item = Order.query.get(id) if not item: return Responses.NOT_EXIST() json_dict = request.json order_items_dict = json_dict['order_items'] for order_item_dict in order_items_dict: order_item = OrderItem() if "id" in order_item_dict.keys(): order_item = OrderItem.query.get(order_item_dict['id']) order_item.update_from_dict(order_item_dict) if order_item.date_returned != None: # ensure this is a date string if is_date(order_item.date_returned): order_item.date_returned = datetime.datetime.strptime(order_item.date_returned, '%Y-%m-%d %H:%M:%S') if not "id" in order_item_dict.keys(): item.order_items.append(order_item) # else: # for oid in item.order_items: # if(oid.id == order_item_dict['id']): # oid = order_item item.update_from_dict(json_dict) # re enable if customers can call in to order # currently orders can only be made from customer UI # only edit on the order is to the status #item.calculate_cost() if len(item.update(force_insert=True)) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def add_user(): json_dict = request.json if 'email' not in json_dict: return Responses.OPERATION_FAILED(Messages.EMAIL_EMPTY) existing_item = User.get_user_by_email(json_dict['email']) if existing_item: return Responses.OBJECT_EXIST(Messages.EMAIL_EXIST) # set admin email to confirmed role = Role.query.get(json_dict['role_id']) if (role.name == "admin"): json_dict['email_confirmed'] = True item = User() error = item.insert_as_new_item(json_dict) if len(error) > 0: return Responses.OPERATION_FAILED(error) return res(item.as_dict())
def update_user_order(): item = Order.query.get(id) if not item: return Responses.NOT_EXIST() json_dict = request.json if json_dict['order_items']: order_items_dict = json_dict['order_items'] for order_item_dict in order_items_dict: order_item = OrderItem() if "id" in order_item_dict.keys(): order_item = OrderItem.query.get(order_item_dict['id']) order_item.update_from_dict(order_item_dict) if not "id" in order_item_dict.keys(): item.order_items.append(order_item) else: for oid in item.order_items: if (oid.id == order_item_dict['id']): oid = order_item item.calculate_cost() if len(item.update(force_insert=True)) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def update_order_status(id): item = OrderStatus.query.get(id) if not item: return Responses.NOT_EXIST() json_dict = request.json if len(item.update(json_dict)) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def update_product_category(id): item = ProductCategory.query.get(id) if not item: return Responses.NOT_EXIST() json_dict = request.json if len(item.update(json_dict)) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def update_article_categories(id): item = ArticleCategory.query.get(id) if not item: return Responses.NOT_EXIST() json_dict = request.json if len(item.update(json_dict)) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def validate_order(): json_dict = request.json item = Order() item.user_id = json_dict['user_id'] order_items_dict = json_dict['order_items'] for order_item_dict in order_items_dict: order_item = OrderItem() if order_item.update_from_dict(order_item_dict): item.order_items.append(order_item) item.update_from_dict(json_dict) if not item.check_stock(): return Responses.NO_STOCK() user = User.query.get(item.user_id) #check if user is blacklisted if user.blacklist: return Responses.BLACKLISTED() if user.subscribed: if not UserSubscription.check_subscription_active(user.id): user.update({'subscribed': 0}) #return Responses.SUBSCRIPTION_INACTIVE() userSub = UserSubscription.get_subscription(user_id=item.user_id) userSubscription = {} if len(userSub) > 0: userSubscription = userSub[0] max_per_order_for_lifestyle = int( ConfigValues.get_config_value( 'max_no_of_items_per_order_adayalifestyle')) subscriptionType = SubscriptionType.query.get( userSubscription.subscription_type_id) if (subscriptionType.plan == 'Adaya Lifestyle' ): #AdayaLifestyle plan if item.check_quantity_products(max_per_order_for_lifestyle): return Responses.OPERATION_FAILED() details = Order.calculate_cost_for_users(item) if details == -1: return Responses.OPERATION_FAILED() return res(details)
def add_voucher(): json_dict = request.json if 'name' not in json_dict: return Responses.OPERATION_FAILED(Messages.VOUCHER_NAME_EMPTY) existing_item = Voucher.get_voucher(json_dict['name']) if existing_item: return Responses.OBJECT_EXIST(Messages.VOUCHER_EXISTS) if (('discount_percent_off' in json_dict and float(json_dict['discount_percent_off']) > 0) and ('discount_fixed_amount' in json_dict and float(json_dict['discount_fixed_amount']) > 0)): return Responses.OPERATION_FAILED(Messages.VOUCHER_DETAILS_WRONG) item = Voucher(json_dict['name']) error = item.insert_as_new_item(json_dict, ['name', 'redeem_by']) if len(error) > 0: return Responses.OPERATION_FAILED(error) return res(item.as_dict())
def remove_unconfirmed_user_order(id): item = Order.query.get(id) if not item: return Responses.NOT_EXIST() item.delete() if len(item.delete()) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def add_subscription(): json_dict = request.json item = UserSubscription( user_id=json_dict['user_id'], end_date=json_dict['end_date'], subscription_type_id=json_dict['subscription_type_id']) error = item.insert_as_new_item(json_dict) if len(error) > 0: return Responses.OPERATION_FAILED(error) return res(item.as_dict())
def create_order(): json_dict = request.json item = Order() item.user_id = json_dict['user_id'] order_items_dict = json_dict['order_items'] for order_item_dict in order_items_dict: order_item = OrderItem() if order_item.update_from_dict(order_item_dict): item.order_items.append(order_item) item.update_from_dict(json_dict) user = User.query.get(item.user_id) #TODO renable when voucher functionality is done # if user.subscribed: # if UserSubscription.check_subscription_active(user.id): # return Responses.SUBSCRIPTION_INACTIVE() # if 'voucher_codes' in json_dict.keys(): # voucher_codes = json_dict['voucher_codes'] # number_vouchers_allowed = int(ConfigValues.get_config_value('max_no_of_vouchers')) # if len(voucher_codes) > number_vouchers_allowed: # return Responses.NO_VOUCHERS_EXCEEDED() # vouchers = Voucher.get_vouchers(voucher_codes) # if not vouchers[0]: # return Responses.INVALID_VOUCHER() # valid = Voucher.validate_voucher(vouchers) # if valid: # item.vouchers = vouchers # item.calculate_discounted_cost() # else: details = item.calculate_cost() if details != -1: user.update({ 'number_of_items_ordered_this_month': int(details['no_items_this_month']), 'month_first_order': details['month_first_order'] }) # convert date if len(item.update(json_dict, force_insert=True)) > 0: return Responses.OPERATION_FAILED() #send email confirmation to user Order.send_order_confirmation_email(order_number=item.id, user_email=user.email) # for item in item.order_items: # item.m date_first_month_order.strftime('%Y-%m-%d %H:%M:%S') return res(item.as_dict())
def delete_subscription_types(plan=None): """ get_subscription_types gets all subscription types """ page, per_page = get_page_from_args() item = SubscriptionType.get_items(plan=plan, page=page, per_page=per_page) error = item[0].delete() if len(error) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def update_confirmed_user_order(id): item = Order.query.get(id) if not item: return Responses.NOT_EXIST() json_dict = request.json item.update_from_dict(json_dict) # if not json_dict['payment_ref']: # return Responses.OPERATION_FAILED() if len(item.update(force_insert=True)) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def delete_vouchers(id=None): """ delete voucher """ page, per_page = get_page_from_args() sort_by = request.args.get('sort_by') is_desc = parse_int(request.args.get('is_desc')) item = Voucher.query.get(id) error = item.delete() if len(error) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def update_config_value(name): """ update_config_value updates setting by using name """ item = ConfigValues.query.filter_by(name=name).first() if not item: return Responses.NOT_EXIST() json_dict = request.json if len(item.update(json_dict)) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def delete_user(email=None): """ get_users gets all user or specify user Args: email ([type], optional): [description]. Defaults to None. Returns: [type]: [description] """ item = User.get_user_by_email(email) error = item.delete() if len(error) > 0: return Responses.OPERATION_FAILED(error) return Responses.SUCCESS()
def update_vouchers(name): """ update_voucher updates voucher by using name Args: name (string): Returns: (string,int): update succesful, otherwise response no need to update """ item = Voucher.get_voucher(name) if not item: return Responses.NOT_EXIST() json_dict = request.json if (('discount_percent_off' in json_dict and float(json_dict['discount_percent_off']) > 0) and ('discount_fixed_amount' in json_dict and float(json_dict['discount_fixed_amount']) > 0)): return Responses.OPERATION_FAILED(Messages.VOUCHER_DETAILS_WRONG) if len(item.update(json_dict)) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def delete_variation(id=None): """ get_products returns all product or the product with specific id Args: id ([type]): product id Returns: [type]: [description] """ variation = Variation.get_variation_from_id(id) # delete variation before product if len(variation.delete()) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def update_subscription_type(plan): """ update_subscription_type updates subscription type by using duration in months Args: plan (string): Returns: (string,int): update succesful, otherwise response no need to update """ item = SubscriptionType.get_items(plan=plan)[0] if not item: return Responses.NOT_EXIST() json_dict = request.json if len(item.update(json_dict)) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def update_user(email): """ update_user updates user by using email Args: email (string): Returns: (string,int): user info if update succesful, otherwise response no need to update """ item = User.get_user_by_email(email) if not item: return Responses.NOT_EXIST() json_dict = request.json if len(item.update(json_dict)) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()
def add_order(): json_dict = request.json item = Order() item.user_id = json_dict['user_id'] item.status_id = json_dict['status_id'] order_items_dict = json_dict['order_items'] for order_item_dict in order_items_dict: order_item = OrderItem() order_item.update_from_dict(order_item_dict) item.order_items.append(order_item) item.calculate_cost() if len(item.update(force_insert=True)) > 0: return Responses.OPERATION_FAILED() return res(item.as_dict())
def update_subscription(id): """ update_subscription updates user subscription by using id Args: id (int): Returns: (string,int): update succesful, otherwise response no need to update """ item = UserSubscription.query.get(id) if not item: return Responses.NOT_EXIST() json_dict = request.json if len(item.update(json_dict)) > 0: return Responses.OPERATION_FAILED() return Responses.SUCCESS()