Final Project: Twitter Clone
In Project 0, you used HTML and CSS to publish your own page on the web. For the final project, you’ll build a small version of Twitter where the people using the site write the content. You’ll choose a community and decide what its members can do on your site.
For a post you write on your computer to show up in someone else’s feed, the site needs somewhere to keep it. Your browser sends the post to your Python server, which saves it in SQLite. When someone else opens the feed, the server reads the posts from the database and builds the HTML page they see.
Live demo: I’ll share a Google Calendar link later so you can choose an appointment.
Submission due: Wednesday, December 16 at 11:59pm.
Worth: 48 points (25 for accounts and posts, 15 for features you choose, 5 for tests, and 3 for hosting). Additional features can earn up to 4 extra points.
Start with your FastAPI lab
Start with the feed and menu you build in the FastAPI lab. For the project, you’ll make it possible for someone to sign up and add their own post to that feed. Open the same twitter-clone repository. If your feed is only on the branch you submitted for the lab, bring it into your main branch before adding features.
Starting before the lab? Download the starter and publish it as twitter-clone.
Run these commands from the repository folder with your Python environment active.
python -m pip install -r requirements.txt
python -m uvicorn app:app --reloadOpen your feed in the browser. If you’re using a fresh starter, open its health page to check that the server is running. Keep your routes inside create_app and use with connect(request) as db: for queries. The starter creates social.db on first use and lets your tests use a separate database.
Accounts and posts
-
Everyone can reach Home from the menu. Show all posts, newest first, with the author, time, and message together. A message like
What's the plan for "Friday"?should appear as typed. -
Ask for a username and have the person enter their password twice. If the passwords differ or the username is taken, explain what needs to change. After a successful signup, they can log in with that account.
-
Show the login link only when the visitor is logged out. Hide the password as they type it. If the username and password match, redirect them to Home. Otherwise, explain that the login failed.
-
A signed-in member’s new post appears on Home with their name and the time. The site rejects posts from logged-out visitors.
-
End the session and switch the menu back to what a visitor sees. Try posting again after logging out. The server should reject the post.
Use SQL placeholders for form values and escape user text in your templates. If a feature produces a server error during the demo, it earns zero for that feature. SQL injection that bypasses login or changes a query costs 10 points. User text that the browser interprets as HTML or runs as a script costs 4 points. I cap the score at 52, then apply each deduction at most once. The lowest score is zero.
Features you choose
Choose 15 points’ worth of features. Edit/delete, profiles, and search would give you 15, as would interactive updates and replies. You can earn up to 4 more points for additional features.
-
Members can edit or delete their own posts. Mark edited posts and check ownership on the server so one member can’t change another’s post.
-
Link each author’s name to a page with their recent posts and a short description. Only the owner can edit that description.
-
Use SQL
LIKEto search the text of posts. Show the matches or a message if the search turns up nothing. You can also offer a filter by author. -
Use SQL
LIMITandOFFSETto show 50 posts per page. Give readers Next and Previous controls. Have at least 101 posts ready so we can try three pages and check for skipped or repeated posts. You can generate the sample posts. -
Let members reply to a particular post. Display the reply with that post so readers can follow the conversation.
-
Let authors use headings, emphasis, links, and code in their posts. Use your Project 1 compiler or a Markdown library. Raw HTML in a message should appear as text.
-
Use JavaScript to send requests to JSON endpoints. Creating, editing, and deleting a post should update the feed without a page reload. If the server rejects a change, show that it failed. You can earn these points along with the 5 for editing and deleting posts.
Tests
Use FastAPI’s TestClient to submit a form from Python and check what comes back. Give each test its own app with app = create_app(tmp_path / "test.db"), then make requests inside with TestClient(app) as client:. The notes app example shows a working test. Run it with python -m pytest database_test_example.py.
-
Create an account through the app, log in, and post a message. Check that the feed shows the text you sent with the name of the account that sent it.
-
Check that a wrong password leaves the visitor logged out and that a logged-out visitor’s post never reaches the feed.
-
Check that an attempted SQL injection fails to log a visitor in and that the account’s real credentials still work.
-
Run
python -m pytesttwice. Both runs should pass, each test should create its own data, and the accounts and posts you use for the demo should still be there. -
Pick one small change to make on your laptop during the demo. For example, make the posting route save the wrong text. Your test should fail when it looks for the message you submitted. Undo the edit and run the same test to show it passes again. Keep the bug local so your Render site stays on the working version.
Put your site online
-
Follow the deployment guide to get a public HTTPS address. Stop your local server and open that address. On a fresh deployment you should be able to read the feed, create an account, log in, and post.
Render’s free service can discard its local files when it sleeps or restarts, including your SQLite database. Those resets won’t cost you points. Your app should create the database when it’s missing and keep any data it finds when it starts. On your own computer, accounts and posts should survive a server restart.
What to bring to the final
Open your Render site a few minutes early so it can wake up. Bring your laptop with your project open. We’ll use both computers during your appointment.
- On my laptop, I’ll open your public Render link, create an account, write a post, and log out. We’ll try the features you chose, and you’ll show me the route and SQL query behind one of them.
- On your laptop, you’ll run
python -m pytesttwice, using your test database. I’ll watch as you make your prepared edit, run the test that catches it, undo the edit, and run the test again.
Push the version you’ll show me, including the app, templates, styles, database setup, tests, and dependencies. Use your README as the checklist for our appointment.
- Name your community and link to your public Render site.
- List the features you’d like me to grade and their point values.
- Give the commands to install dependencies, initialize the database, run the server, and run the tests with a separate database.
- Explain how to recreate any sample accounts or posts your demo needs after a Render reset.
Submit your GitHub repository URL, public Render URL, and the commit ID for your demo on the course’s final-project submission page.
FAQs
Why does my post appear twice when I refresh?
If the browser is still on the response to the original POST, refreshing can send the form again. After saving the post, have the route return a 303 redirect to Home. The browser then loads the feed with GET, so refreshing just reads the posts again.
Is hiding the edit button enough to protect someone else’s post?
No. The server can receive an edit request even if the page has no button. Before saving the change, compare the account in the session with the author stored on the post. Do the same for deletion.
Why does a test pass by itself and fail after another test?
Two tests may be using the same database or staying logged in as the same user. Start each test with its own tmp_path and TestClient. If a username still shows up in both tests, look for a route that opens social.db directly.
I added a column to my schema. Why can’t my app find it?
The starter reads schema.sql when it creates a database. Editing that file changes future databases. To add the column to the local database you already use, run the matching ALTER TABLE there too. If accounts disappear on a local restart, check that the app opens the same file each time and initializes it only when it’s missing.