My Blog

My WordPress Blog

My Blog

My WordPress Blog

Customer Relationship Management (CRM)

Step 1: Set Up Your Environment

  1. Install Required Packages: Make sure you have Python installed, then set up a virtual environment and install Flask and SQLAlchemy.
mkdir crm_app cd crm_app python -m venv venv source venv/bin/activate # On Windows use venv\Scripts\activate pip install Flask Flask-SQLAlchemy

Step 2: Create the Database Model

Create a file named app.py:

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///crm.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

class Customer(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), unique=True, nullable=False)
phone = db.Column(db.String(20), nullable=True)
def to_dict(self):
    return {"id": self.id, "name": self.name, "email": self.email, "phone": self.phone}
with app.app_context():
db.create_all()

Step 3: Create API Endpoints

Add the following code to app.py to create API endpoints for managing customers:

@app.route('/customers', methods=['POST'])
def add_customer():
data = request.get_json()
new_customer = Customer(name=data['name'], email=data['email'], phone=data.get('phone'))
db.session.add(new_customer)
db.session.commit()
return jsonify(new_customer.to_dict()), 201
@app.route('/customers', methods=['GET']) def get_customers():
customers = Customer.query.all()
return jsonify([customer.to_dict() for customer in customers])
@app.route('/customers/<int:id>', methods=['GET']) def get_customer(id):
customer = Customer.query.get_or_404(id)
return jsonify(customer.to_dict())
@app.route('/customers/<int:id>', methods=['PUT']) def update_customer(id):
data = request.get_json()
customer = Customer.query.get_or_404(id)
customer.name = data&#91;'name']
customer.email = data&#91;'email']
customer.phone = data.get('phone')
db.session.commit()
return jsonify(customer.to_dict())
@app.route('/customers/<int:id>', methods=['DELETE']) def delete_customer(id):
customer = Customer.query.get_or_404(id)
db.session.delete(customer)
db.session.commit()
return '', 204
if __name__ == '__main__':
app.run(debug=True)

Step 4: Run the Application

Run your Flask app:

python app.py

Step 5: Test the API

You can test your API using tools like Postman or cURL. Here are some example requests:

  1. Add a Customer:
curl -X POST http://127.0.0.1:5000/customers -H "Content-Type: application/json" -d '{"name": "John Doe", "email": "[email protected]", "phone": "123-456-7890"}'
  1. Get All Customers:
curl http://127.0.0.1:5000/customers
  1. Get a Specific Customer:
curl http://127.0.0.1:5000/customers/1
  1. Update a Customer:
curl -X PUT http://127.0.0.1:5000/customers/1 -H "Content-Type: application/json" -d '{"name": "Jane Doe", "email": "[email protected]", "phone": "098-765-4321"}'
  1. Delete a Customer:
curl -X DELETE http://127.0.0.1:5000/customers/1
Customer Relationship Management (CRM)

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top