Package: ecart
E-Cart is a framework agnostic, redis backed, cart system, built in
Python. It is not a POS, or a full fledged ecommerce system.Installationpip install ecartRequirementsAs E-Cart harnesses the power of Redis.You should have runningRedis
Server. and installedredis-pyredis-pypackage.Basic UsageImport theCartclass from theecartpackage:from ecart.ecart import CartCreate a new shopping cart:cart_obj = Cart(user_id, redis_connection, ttl)user_id: This a required parameter which acts as a unique
identifier for the cart. If you don’t want a user to have more than
one cart at a time, it’s generally best to set this to the user’s id.redis_connection: This too is a required field and is used to
communicate with the Redis database. This is basically a redis
connection object obtained by calling theredis.Redis()function
of theredis
package. An sample
function to create such object is available atredis_connection.pyttl: This field used to set the expiry time of the user cart in
the Redis in seconds. This is an optional field with a default value
of 604800.To add an item to the cart:cart_obj.add(product_id, unit_cost, quantity)This function takeproduct_id,unit_costandquantityand
other details (**kwargs) if you like. Once executed this function
add the given product and its details into the user cart in redis db.To Retrieve the items:cart_obj.get()This function returns the complete cart of the user, basically a dictionary of product_id to product_details dictionary.Functions ExposedFollowing are the complete details of the methods exposed bycart_objobject:addadd(product_id, unit_cost, quantity, **extra_details)This function is the life blood of E-Cart. Below are the details of
the arguments for theaddfunction:product_id: (required) to store the ID of the model you’re
addingquantity: (required) which will let you use theCart#quantityandCart#totalmethods without any extra
configuration.unit_cost: (required) which will help you to calculate total
value of cart.extra_details:(optional) if you want to store any extra
information about the cart item just pass the details as
**kwargs, ecart will take care of it.removeremove(product_id)As you would have guessed, removes an item of the inputproduct_idcontainscontains(product_id)Returns a Boolean indicating whether an item of the givenproduct_idis in the cart or not.get_productget_product(product_id)For the inputproduct_id, this function will return theItemdictionary withunit_cost,quantityetc details.getget()This function returns the complete cart of the user, basically a
dictionary of product_id to product_details dictionary.total_costtotal_cost()This will return the sum all of thecostreturn values of all of
the items in the cart. For this to work, the:unit_costand:quantityfields need to be set for all items.countcount()This will return the total number of items in the cart. Faster thancart.items.sizebecause it doesn’t load all of the item data from
redis.quantityquantity()This will return the total quantity of all the items. The quantity
field is set in the config block, by default it’s:quantityget_ttlget_ttl()This will return the number of seconds until the cart expires.set_ttlset_ttl(ttl_value)This will set the ttl of the user cart in redis tottl_value.ttl_valuemust be integer and is in secondsdestroydestroy()This will delete the cart, and all the line_items out of it.copycopy(new_id)This method will copy the current cart to the new unique_id. Will be
useful for copying guest user’s cart to logged-in user’s cart or
simply creating a copy of the cart.ExampleLets walk through an example below:fromecart.ecartimportCartcart=Cart(user_id,reddis_connection)# ttl is optional default is 604800cart.add(product_id,unit_cost,quantity)# quantity defaults to 1, also you can pass optional dict(extra info)cart.totalcart.quantityHow to ContributeFork itCreate your feature branch (git checkout-bmy-new-feature)Commit your changes (git commit-am'Add some feature')Push to the branch (git push originmy-new-feature)Create new Pull Request