def number_remaining(self, interval, end_date, **post): """ Return the number of auto shipments remaining based on end_date and interval """ fail_check = jsend.FailCheck() if not tools.isnumeric(interval): fail_check.add("interval", "Interval should be a number") if not interval: fail_check.add("interval", "Interval should be greater than 0") if not isinstance(end_date, (str, unicode)): fail_check.add("end_date", "End Date was invalid") if not end_date: fail_check.add("end_date", "End Date is required") try: end_date = datetime.strptime(end_date, '%Y-%m-%d').date() except ValueError: fail_check.add( "end_date", "End Date must be a valid date in YYYY-MM-DD format") if fail_check.failed(): return fail_check.fail() nr = str(request.registry['ip_web_addons.auto_ship']. _calculate_number_remaining(float(interval), end_date)) return jsend.jsend_success({'number_remaining': nr})
def update_auto_ship(self, auto_ship_id, interval, end_date, **post): """ Update an auto ship's interval and end date """ fail_check = jsend.FailCheck() if not isinstance(auto_ship_id, (int, float)): fail_check.add("auto_ship_id", "Auto ship ID should be a number") if not auto_ship_id: fail_check.add("auto_ship_id", "Auto ship ID must be greater than 0") if not tools.isnumeric(interval): fail_check.add("interval", "Interval should be a number") if not interval: fail_check.add("interval", "Interval should be greater than 0") if not isinstance(end_date, (str, unicode)): fail_check.add("end_date", "End Date was invalid") if not end_date: fail_check.add("end_date", "End Date is required") try: datetime.strptime(end_date, '%Y-%m-%d') except ValueError: fail_check.add("end_date", "End Date must be in the format YYYY-MM-DD") if (fail_check.failed()): return fail_check.fail() cr, uid, pool = request.cr, request.uid, request.registry # TODO: do we need to check permission? pool['ip_web_addons.auto_ship'].write(cr, uid, auto_ship_id, { 'interval': interval, 'end_date': end_date })
def set_auto_ship(self, auto_ship, interval=0, end_date=None): """ Saves onto customers quotation the auto_ship setting and if 'true', also sets associated interval and end_date. """ fail_check = jsend.FailCheck() # must have auto shippable order order = request.website.sale_get_order() if not order: fail_check.add('order', 'Customer has no orders in progress') if not self._can_auto_ship(order): fail_check.add('auto_ship', 'Not all products in the cart are marked as Auto Ship') if not tools.isnumeric(interval): fail_check.add('interval', 'Interval must be numeric') if fail_check.failed(): return fail_check.fail() # set auto ship variables on sale order auto_ship = True if auto_ship == 'true' else False interval = int(interval) vals = {} if auto_ship: if not interval: fail_check.add('interval', 'Interval has to be 1 week or higher') if not end_date: fail_check.add('end_date', 'End Date must have a value') else: try: end_date = datetime.strptime(end_date, '%Y-%m-%d').date() except ValueError: fail_check.add('end_date', 'End Date was not a valid date - it must be in format YYYY-MM-DD') if fail_check.failed(): return fail_check.fail() vals['draft_auto_ship'] = auto_ship vals['draft_auto_ship_interval'] = interval vals['draft_auto_ship_end_date'] = end_date else: vals['draft_auto_ship'] = auto_ship vals['draft_auto_ship_interval'] = 0 vals['draft_auto_ship_end_date'] = None order.write(vals)
def add_cart_multi(self, **product_quantity_map): """ Add multiple products to the cart with specified quantities @param dict product_quantity_map: A dictionary keyed by product IDs where values represent quantities """ cr, uid, context, pool = request.cr, request.uid, request.context, request.registry fail_check = jsend.FailCheck() if not hasattr(product_quantity_map, '__iter__'): fail_check.add('product_quantity_map', 'product_quantity_map should be a json object mapping product IDs to quantities to add to the cart') for product_id, quantity in product_quantity_map.items(): # data validation if not tools.isnumeric(product_id): fail_check.add('product_id', 'Product IDS must be numeric') if not tools.isnumeric(quantity): fail_check.add('quantity', 'Quantity must be numeric') if fail_check.failed(): return fail_check.fail() product_id = int(product_id) quantity = float(quantity) new_quantity = self.cart_update( product_id, add_qty=quantity ) # bug in _ecommerce_add_product_to_cart means that if adding a new product to the cart, # the quantity is always set to 1. Handle this by checking returned qty against specified # quantity and adjust it accordingly print new_quantity '''if new_quantity < quantity: self.cart_update(product_id, add_qty=quantity - new_quantity )''' return self.get_cart_info()
def delete_auto_ship(self, auto_ship_id, **post): """ delete an auto ship """ fail_check = jsend.FailCheck() cr, uid, context, pool = request.cr, request.uid, request.context, request.registry if not tools.isnumeric(auto_ship_id): fail_check.add("auto_ship_id", "Auto ship ID should be a number") elif not auto_ship_id: fail_check.add("auto_ship_id", "Auto ship ID should be greater than 0") elif not pool['ip_web_addons.auto_ship'].search( cr, uid, [('id', '=', auto_ship_id)]): fail_check.add("auto_ship_id", "Auto Ship does not exist") if fail_check.failed(): return fail_check.fail() # TODO: do we need to check permission? pool['ip_web_addons.auto_ship'].unlink(cr, uid, auto_ship_id, context=context)
def update_address(self, name, title, gender, birthdate, disease_ids, phone, street, street2, city, zip, state_id, country_id, id): """ JSON route to update the address fields on a partner """ fail_check = jsend.FailCheck() # check partner exists if not tools.isnumeric(id): fail_check.add('id', 'Partner ID must be a number') else: id = int(id) # make some text fields mandatory if not name: fail_check.add('name', 'Name is a required field') if not street: fail_check.add('street', 'Street is a required field') if not city: fail_check.add('city', 'City is a required field') if not country_id: fail_check.add('country_id', 'Country is a required field') # validate birthdate format if birthdate: try: datetime.strptime(birthdate, '%Y-%m-%d') except ValueError: fail_check.add( 'birthdate', 'Date of Birth should be in the format YYYY-MM-DD') # validate select2 if disease_ids: if ',' in disease_ids: disease_ids = disease_ids.split(',') disease_ids = map(lambda disease: int(disease), disease_ids) elif tools.isnumeric(disease_ids): disease_ids = [int(disease_ids)] else: fail_check.add( 'disease_ids', 'There was a problem with the disease_ids field') cr, uid, context, pool = request.cr, request.uid, request.context, request.registry partner_obj = pool['res.partner'] partner_id = id # TODO: make sure user has permission to update partner object partner_exists = bool( partner_obj.search(cr, SUPERUSER_ID, [('id', '=', partner_id)], context=context)) if not partner_exists: fail_check.add('partner_id', 'That partner does not exist') if (fail_check.failed()): return fail_check.fail() vals = { 'name': name, 'title': title, 'gender': gender, 'birthdate': birthdate, 'phone': phone, 'disease_ids': [(6, 0, disease_ids)], 'street': street, 'street2': street2, 'city': city, 'zip': zip, 'state_id': state_id, 'country_id': country_id, } partner_obj.write(cr, SUPERUSER_ID, [partner_id], vals)