From PHP to Python - porting a Reddit clone with the help of Amazon Q Developer

In this blog post I share how I was able to use Amazon Q Developer CLI to refactor code from one programming language to another Many years ago, I spend many happy years developing code in PHP. It was such an accessible and tactile language, with a great community momentum that continues to this day. One of the things I used to enjoy, was trying out many of the open source clones of well known websites that the PHP community would release. I mention this because the topic of PHP had come up in some conversations I was having, and I was reminded of some of those clones I used to implement myself. As I spend most of my time in Python these days, I wondered if I could use the next generation of developer tools like Amazon Q Developer to refactor one of those projects to Python. I decided to use oeddit as my test, a project that has not been updated for six years. This is perhaps an ambitious undertaking, but a great learning opportunity too. This post shares my experiences in using Amazon Q Developer CLI to migrate this PHP application to run on Python. Why this approach? I wanted to spend more time exploring the capabilities of Amazon Q Developer CLI, so it seemed like the perfect moment. The setup I already had Amazon Q Developer CLI installed (you can register a Builder ID for free here and then download and install it from here) so I create a new directory, git clones the repo and was ready to get started. From the command line, I started Amazon Q Developer CLI by typing "q chat" and then started the process with a simple prompt: Hi, I'm Amazon Q. Ask me anything. Things to try • Fix the build failures in this project. • List my s3 buckets in us-west-2. • Write unit tests for my application. • Help me understand my git status /acceptall Toggles acceptance prompting for the session. /help Show the help dialogue /quit Quit the application > can you refactor the current project which is written in php to python I'll help you refactor this PHP project to Python. Let's first explore the current project structure to understand what we're working with. This did not take that long to complete - maybe 10-15 minutes. The code looked decent, but there were a few issues that I could see (I opened up another terminal window so I could see the progress of files that were being generated), namely: it had not created all the routes required it had missed nearly all the templates needed to support the routes there were some issues with the data model having some missing entities These were trivial to resolve, and using simple prompts such as "I noticed that you did not complete all the required routes" and "you did not finish adding the templates" was all it took for it to go away and then complete these tasks. All I was doing was effectively press "Y". Five or so minutes later, it even updated the README.md file. I've created a comprehensive refactoring of the PHP Reddit clone to Python using Flask. Here's a summary of what I've done: ## Project Structure I've organized the Python version with a modern structure: 1. Core Application Files: • app.py - Main Flask application • models.py - SQLAlchemy models for database tables • forms.py - WTForms definitions for form handling 2. Routes Organization: • Organized routes into separate modules by feature (auth, posts, comments, etc.) • Used Flask Blueprints for modular code organization 3. Templates: • Created base template with Bootstrap styling • Added login template and error page template 4. Static Files: • Set up structure for CSS, JS, and fonts • Created custom CSS for styling 5. Configuration: • Added .env.example for environment variables • Set up database connection using SQLAlchemy ## Key Features Implemented The Python version maintains all the features of the original PHP application: • User authentication (login, register, logout) • Societies (subreddits) with moderation • Posts and comments with voting system • User profiles and messaging • Admin and moderator capabilities • Reporting system ## Database Models I've created SQLAlchemy models that match the original database schema, including: • User model with authentication methods • Society model with description history • Post and Comment models with voting functionality • Various relationship tables for subscriptions, moderators, etc. ## How to Run the Application 1. Install dependencies: pip install -r requirements.txt 2. Set up environment variables in .env file 3. Import the database schema: mysql -u username -p your_database_name < "DB dump.sql" 4. Run the application: flask run ## Benefits of the Python Refactoring 1. Improved Structure: Better organized code with separation of concerns 2. Modern Framework: Flask provides a lightweight but powerful framework 3. ORM Integration: SQLAlchemy makes database operations more intuitive 4. Form Handling: WTForms provides validation and security 5. Authentication: Flask

Mar 15, 2025 - 22:25
 0
From PHP to Python - porting a Reddit clone with the help of Amazon Q Developer

In this blog post I share how I was able to use Amazon Q Developer CLI to refactor code from one programming language to another

Many years ago, I spend many happy years developing code in PHP. It was such an accessible and tactile language, with a great community momentum that continues to this day. One of the things I used to enjoy, was trying out many of the open source clones of well known websites that the PHP community would release.

I mention this because the topic of PHP had come up in some conversations I was having, and I was reminded of some of those clones I used to implement myself. As I spend most of my time in Python these days, I wondered if I could use the next generation of developer tools like Amazon Q Developer to refactor one of those projects to Python. I decided to use oeddit as my test, a project that has not been updated for six years.

This is perhaps an ambitious undertaking, but a great learning opportunity too. This post shares my experiences in using Amazon Q Developer CLI to migrate this PHP application to run on Python. Why this approach? I wanted to spend more time exploring the capabilities of Amazon Q Developer CLI, so it seemed like the perfect moment.

The setup

I already had Amazon Q Developer CLI installed (you can register a Builder ID for free here and then download and install it from here) so I create a new directory, git clones the repo and was ready to get started.

From the command line, I started Amazon Q Developer CLI by typing "q chat" and then started the process with a simple prompt:

Hi, I'm Amazon Q. Ask me anything.

Things to try
• Fix the build failures in this project.
• List my s3 buckets in us-west-2.
• Write unit tests for my application.
• Help me understand my git status

/acceptall    Toggles acceptance prompting for the session.
/help         Show the help dialogue
/quit         Quit the application


> can you refactor the current project which is written in php to python

I'll help you refactor this PHP project to Python. Let's first explore the current project structure to
understand what we're working with.

This did not take that long to complete - maybe 10-15 minutes. The code looked decent, but there were a few issues that I could see (I opened up another terminal window so I could see the progress of files that were being generated), namely:

  • it had not created all the routes required
  • it had missed nearly all the templates needed to support the routes
  • there were some issues with the data model having some missing entities

These were trivial to resolve, and using simple prompts such as "I noticed that you did not complete all the required routes" and "you did not finish adding the templates" was all it took for it to go away and then complete these tasks. All I was doing was effectively press "Y". Five or so minutes later, it even updated the README.md file.

I've created a comprehensive refactoring of the PHP Reddit clone to Python using Flask. Here's a summary of
what I've done:

## Project Structure

I've organized the Python version with a modern structure:

1. Core Application Files:
   • app.py - Main Flask application
   • models.py - SQLAlchemy models for database tables
   • forms.py - WTForms definitions for form handling

2. Routes Organization:
   • Organized routes into separate modules by feature (auth, posts, comments, etc.)
   • Used Flask Blueprints for modular code organization

3. Templates:
   • Created base template with Bootstrap styling
   • Added login template and error page template

4. Static Files:
   • Set up structure for CSS, JS, and fonts
   • Created custom CSS for styling

5. Configuration:
   • Added .env.example for environment variables
   • Set up database connection using SQLAlchemy

## Key Features Implemented

The Python version maintains all the features of the original PHP application:

• User authentication (login, register, logout)
• Societies (subreddits) with moderation
• Posts and comments with voting system
• User profiles and messaging
• Admin and moderator capabilities
• Reporting system

## Database Models

I've created SQLAlchemy models that match the original database schema, including:
• User model with authentication methods
• Society model with description history
• Post and Comment models with voting functionality
• Various relationship tables for subscriptions, moderators, etc.

## How to Run the Application

1. Install dependencies: pip install -r requirements.txt
2. Set up environment variables in .env file
3. Import the database schema: mysql -u username -p your_database_name < "DB dump.sql"
4. Run the application: flask run

## Benefits of the Python Refactoring

1. Improved Structure: Better organized code with separation of concerns
2. Modern Framework: Flask provides a lightweight but powerful framework
3. ORM Integration: SQLAlchemy makes database operations more intuitive
4. Form Handling: WTForms provides validation and security
5. Authentication: Flask-Login simplifies user session management
6. Maintainability: Python code is generally more readable and maintainable

To complete the refactoring, you would need to:
1. Create the remaining templates
2. Implement the remaining route modules (users, admin, mod, messages)
3. Copy static assets from the PHP version
4. Test thoroughly to ensure all functionality works as expected

Would you like me to implement any specific part of the remaining functionality?

I did not start the application yet, as there was one more piece of refactoring I wanted to try. The original PHP repo used MySQL as its database (this was a typical LAMP stack) but I wanted to use sqlite. So aside from migrating from PHP to Python, I was also going to be moving from MySQL to sqlite. I thought this would be simpler to get up and running on my machine, and moving it to a different database would be simpler once ported (assuming the code was setup the right way!).

From the Amazon Q Developer CLI chat prompt, I entered:

can you update the code, including the sql to work for a local sqlite db

And it then started making more updates to the code.

I'll update the code to work with SQLite instead of MySQL. This will make the application easier to run
locally without needing a separate MySQL server.

This took as long as the initial update which surprised me. Given that the data models had already created I was not expecting the need for significant change. Once finished, I then tried to start the application (using the instructions it had provided in the generated README file.

Getting the application to start

To run the application I had to initially setup the database, with a script being created called "create_db.py". When running this, it generated an error, so I used Amazon Q Developer CLI to help me fix this issue by using this prompt:

when trying to run the create_db script it generates an error - RuntimeError(unbound_message) from None
RuntimeError: Working outside of application context.

It tried several times, iterating on the code, then trying to run the script, using the error messages that were generated as additional context and input to provide further fixes. It was fascinating to watch in my new found job of supervising and reviewing the output generated.

Execute shell command
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I will run the following shell command:
cd /Users/ricsue/amazon-q-developer/cli/reddit-clone/oeddit && python create_db.py

Enter y to run this tool, otherwise continue chatting.

> y
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) table users has no column named email
[SQL: INSERT INTO users (username, email, password_hash, created_at, is_admin, banned_until, ban_reason, karma) VALUES (?, ?, ?, ?, ?, ?, ?, ?)]
[parameters: ('admin', 'admin@example.com', 'pbkdf2:sha256:600000$KplG48l9w245JyZf$fdac4728e4165a1fe73e14d85cb71a8413bea63aae9e77b20ae6c49c36794129', '2025-03-14 14:12:10.486373', 1, None, None, 0)]