RevoU-FSSE-2 / Week-21-Hill-Man-1

Week-21-Hill-Man-1 created by GitHub Classroom

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FLASK SOCIAL MEDIA APP

Overview This project is a Flask-based web application that utilizes SQLAlchemy as its ORM (Object-Relational Mapping) for database interactions. The application models include User, Tweet, and Following to implement a basic social media platform. The project structure is organized into three main files: pipfile, db.py, and model files (user_model.py, tweet_model.py, following_model.py).

Getting Started

Prerequisites

  • Python 3.12
  • PostgreSQL database (You can use the provided .env file for the connection details)

Dependencies

The project utilizes the following Python packages:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
flask = "*"
flask-sqlalchemy = "*"
psycopg2-binary = "*"
bcrypt = "*"
flask-bcrypt = "*"
marshmallow = "*"
injector = "*"
pyjwt = "*"

[dev-packages]

[requires]
python_version = "3.12"

Installation

  1. Clone the repository:
git clone https://github.com/RevoU-FSSE-2/Week-21-Hill-Man-1.git
cd flask-social-media-app

Install dependencies:

pip install -r requirements.txt
  1. Set up the database: Uncomment the following lines in app.py to initialize the database:
# with app.app_context():
#     db_init()

Create and populate the .env file:

Create a file named .env in the project root and add the following:

DATABASE_URL=postgresql://postgres:RevouWeek21***@db.dxtvuacfifnhoufyzvca.supabase.co:5432/postgres
SECRET_KEY=thisis*****

Replace placeholders with your actual database connection details.

Running the App

Run the Flask application with the following command:

python app.py

The app will be accessible at http://localhost:5000.

API Endpoints

1 User Registration:

POST /auth/registration

Register a new user with a unique username.

  1. User Login:
POST /auth/login

Log in with a registered username and password.

  1. Get User Profile:
GET /user/

Retrieve the user's profile information, including followers, following, and recent tweets.

  1. Post a Tweet:
POST /tweet/

Post a new tweet on the user's timeline.

  1. Follow/Unfollow User:
POST /following/

Follow or unfollow another user.

Database Initialization (db.py)

The db.py file initializes the SQLAlchemy database and defines a base model class (Base) for other models to inherit from. Additionally, it provides a db_init function to drop and recreate all tables, useful during development and testing.

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase

class Base(DeclarativeBase):
    pass

db = SQLAlchemy(model_class=Base)

def db_init():
    db.drop_all()
    db.create_all()

User Model (user_model.py)

The User model represents user data and relationships. It includes fields such as id, username, password, bio, followers, and following. The model also establishes a relationship with the Following model to manage follower/following associations.

from db import db
from following.model import Following

class User(db.Model):
    # ... (fields definition)
    followers = db.Column(db.Integer, default=0)
    following = db.Column(db.Integer, default=0)
    following_relations = db.relationship('Following', back_populates='user', foreign_keys=[Following.user_id])

Tweet Model (tweet_model.py)

The Tweet model represents a user's tweet. It includes fields such as id, tweet, user_id, and publish_at. The model establishes a relationship with the User model to associate tweets with specific users.

from db import db
import datetime

class Tweet(db.Model):
    # ... (fields definition)
    user = db.relationship("User", backref=db.backref('tweets', lazy=True))
    publish_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow())

Following Model (following_model.py)

The Following model manages the relationships between users who follow each other. It includes fields such as id, user_id, and following_user_id. This model establishes relationships with the User model to maintain follower/following associations.

from db import db

class Following(db.Model):
    # ... (fields definition)
    user = db.relationship('User', back_populates='following_relations', foreign_keys=[user_id])

POSTMAN TEST

Check this API using Postman CLICK HERE!

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Week-21-Hill-Man-1 created by GitHub Classroom


Languages

Language:Python 100.0%