2025 Trimester 3 CPT and PPR requirements
College Board requirements
Component A: Program Code
from flask import request, jsonify, Blueprint
from model.factsbase import db, Facts
# Blueprint for handling user-submitted facts via API endpoints
user_facts = Blueprint('user_facts', __name__)
@user_facts.route('/api/userfacts', methods=['POST'])
def add_user_fact():
"""
Adds a new fact to the database.
- Extracts JSON data from the request.
- Validates required fields ('name' and 'fact').
- Creates a new database entry and commits it.
- Returns a success message if successful.
"""
data = request.get_json()
name = data.get('name')
fact = data.get('fact')
if not all([name, fact]):
return jsonify({'error': 'Missing data'}), 400
new_fact = Facts(name=name, fact=fact)
db.session.add(new_fact)
db.session.commit()
return jsonify({'message': 'Fact added successfully'}), 201
@user_facts.route('/api/userfacts', methods=['GET'])
def get_facts():
"""
Retrieves all stored facts.
- Queries the database.
- Formats and returns structured JSON data.
"""
facts = Facts.query.all()
result = [{'id': fact.id, 'name': fact.name, 'fact': fact.fact} for fact in facts]
return jsonify(result), 200
@user_facts.route('/api/userfacts/<int:id>', methods=['DELETE'])
def delete_fact(id):
"""
Deletes a fact from the database by ID.
"""
fact = Facts.query.get(id)
if not fact:
return jsonify({'error': 'Fact not found'}), 404
db.session.delete(fact)
db.session.commit()
return jsonify({'message': 'Fact deleted successfully'}), 200
@user_facts.route('/api/userfacts/<int:id>', methods=['PUT'])
def update_fact(id):
"""
Updates an existing fact in the database.
"""
data = request.get_json()
name = data.get('name')
fact = data.get('fact')
if not all([name, fact]):
return jsonify({'error': 'Missing data'}), 400
existing_fact = Facts.query.get(id)
if not existing_fact:
return jsonify({'error': 'Fact not found'}), 404
existing_fact.name = name
existing_fact.fact = fact
db.session.commit()
return jsonify({'message': 'Fact updated successfully'}), 200
How This Code Represents Component A
Program Functionality
- Implements a RESTful API in Flask to manage user-submitted facts.
- Supports CRUD (Create, Read, Update, Delete) operations using SQLAlchemy.
Algorithm Implementation
add_user_fact(): Processes input, validates, and stores data.get_facts(): Retrieves and formats database entries.update_fact(): Modifies stored data.delete_fact(): Removes a record.
Data Abstraction
- The
Factsclass (frommodel/factsbase.py) abstracts the database structure. - The SQLAlchemy ORM handles data persistence.
1.Input from User
data = request.get_json() # The user sends input as a JSON object.
name = data.get('name')
fact = data.get('fact')
2.Use of list to Represent Data
facts = Facts.query.all() # Querying all facts from the database
result = [
{
'id': fact.id,
'name': fact._name,
'fact': fact._fact,
}
for fact in facts
]
3.Student-Dveloped Procedure
def add_user():
data = request.get_json()
name = data.get('name')
fact = data.get('fact')
if not all([name, fact]):
return jsonify({'error': 'Missing data'}), 400
new_user = Facts(name=name, fact=fact)
db.session.add(new_user)
db.session.commit()
return jsonify({'message': 'Fact added successfully'}), 201
4.Algorithm with sequencing, selction, and iteration
def add_user():
data = request.get_json()
name = data.get('name')
fact = data.get('fact')
if not all([name, fact]):
return jsonify({'error': 'Missing data'}), 400
new_user = Facts(name=name, fact=fact)
db.session.add(new_user)
db.session.commit()
return jsonify({'message': 'Fact added successfully'}), 201
5.Calls to Procedure
@userfacts.route('/api/userfacts', methods=['POST'])
def add_user_route():
return add_user() # Calling the procedure defined above
6. Output Based on Input
return jsonify({'message': 'Fact added successfully'}), 201
Component B: Video
Demonstrates:
- Input into the program
- Functionality of CRUD operations
- Output of the API responses
Requirements Followed:
- Format:
.mp4, .webm, .wmv, .avi, or .mov - Max Length: 1 minute
- Max File Size: 30MB
Component C: {Personalized Project Reference (PPR)}
1.Student-Developed Procedure:
This procedure is a core algorithm in the program, containing:
- A procedure name
(add_user_fact) - Parameters that affect its functionality
- Algorithm with sequencing, selection, and iteration
# Student-developed procedure
def add_user_fact(data):
"""
Adds a fact to the database after validation.
"""
name = data.get('name')
fact = data.get('fact')
if not all([name, fact]):
return {'error': 'Missing data'}, 400
new_fact = Facts(name=name, fact=fact)
db.session.add(new_fact)
db.session.commit()
return {'message': 'Fact added successfully'}, 201
Key Aspects:
- Procedure Name:
add_user_fact - Parameters:
data(dictionary containingnameandfact)
Algorithm Features:
- Sequencing: Checks data, creates a new entry, and commits it.
- Selection: Ensures both
nameandfactare provided. - Iteration: Used in other parts of the program where multiple facts are processed.
2.Calling the Procedure:
@user_facts.route('/api/userfacts', methods=['POST'])
def add_user_fact_route():
data = request.get_json()
response = add_user_fact(data)
return jsonify(response)
- Calls
add_user_factinside the POST/api/userfactsroute. - Passes user input as
data. - Returns the response as JSON.
3.Using a List to Manage Complexity
nigerias = [
Nigeria(title='Kilimanjaro', comment='It was ok! The enviornment was great though.', content={'type': 'announcement'}, user_id=1, group_id=1),
Nigeria(title='McFestine', comment='Enjoyed the diverse food options!', content={'type': 'announcement'}, user_id=2, group_id=2),
Nigeria(title='Unity', comment='Amazing staff, liked the food!', content={'type': 'announcement'}, user_id=3, group_id=3),
]
- List of Dictionaries stores multiple facts efficiently.
- Purpose: Manages multiple data entries for retrieval and manipulation.
4.Accessing Data in the List:
@user_facts.route('/api/userfacts', methods=['GET'])
def get_fact():
return jsonify([{'id': fact['id'], 'name': fact['name'], 'fact': fact['fact']} for fact in facts_list]), 200
- Iteration: Loops through
facts_listto retrieve data. - Purpose: Enables easy fact retrieval for users.
Summary of Component C
- Student-Developed Procedure: Defines an algorithm with sequencing, selection, and iteration.
- Calling the Procedure: The function is invoked in the program, passing user data.
- Using a List for Data Management: A structured collection is used for organizing facts.
- Accessing Data in the List: The list is utilized to fulfill the program’s purpose.
Final Thoughts
This project reflects my problem-solving skills, use of APIs, and data management. It showcases real-world programming concepts and how user data can be efficiently processed!