This repository contains the backend code for a plant identification system built with Flask. The backend provides APIs for user management, including creating new users and authenticating them, as well as functionality to fetch and store plant information.
- User registration with username, name, email, and password.
- Passwords are hashed before storage for security.
- Basic user authentication.
- Integration with plant identification services.
- Python 3.8 or higher
- pip (Python package installer)
- Virtualenv (optional, but recommended)
-
Clone the Repository
git clone https://github.com/yourusername/your-repository.git cd your-repository
-
Create and Activate a Virtual Environment
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install Dependencies
pip install -r requirements.txt
-
Set Up the Database
Make sure you have a PostgreSQL (or other supported) database set up. Update the
config.py
file with your database configuration. Then, run:flask db upgrade
-
Run the Application
flask run
The application will start on http://127.0.0.1:5000 by default.
Endpoint:
POST /create
Request Body:
{
"username": "user123",
"name": "John Doe",
"email": "[email protected]",
"password": "yourpassword"
}
Response:
201 Created
if the user is created successfully.409 Conflict
if the username is already taken.
Here is the code for creating a new user:
# Create a new user
@user_bp.route('/create', methods=['POST'])
def create_user():
data = request.get_json()
if db.session.query(db.exists().where(User.username == data['username'])).scalar():
return jsonify({'message': 'Username taken'}), 409
new_user = User(username=data['username'], name=data['name'], email=data['email'], password=data['password'])
db.session.add(new_user)
db.session.commit()
return jsonify({'message': 'User created successfully'}), 201
You need to set up the following environment variables:
FLASK_APP=app.py
FLASK_ENV=development
DATABASE_URL=your_database_url
Modify config.py
to update the database connection and other settings.
- Error Handling: Ensure comprehensive error handling for edge cases and validation errors.
- Password Hashing: Implement password hashing using
werkzeug.security
to ensure user passwords are securely stored. - Logging: Implement logging to track application errors and debug issues effectively.
Contributions are welcome! Please submit a pull request or open an issue to suggest improvements.
This project is licensed under the MIT License.
- Flask for the web framework.
- SQLAlchemy for ORM.
- Werkzeug for password hashing.
- PostgreSQL for the database management.