π AP CSP Personalized Project Reference (PPR) FRQ
Below are my code segments that meet the AP CSP requirements.
π Summary
β List Creation: Initializes and stores student grade data.
β List Processing: Retrieves and processes stored grade data.
β Function with Sequencing, Selection, and Iteration: A function that iterates over a list, applies conditional logic, and processes data.
β Call to Function: A function call that modifies a userβs grade log dynamically.
π List Creation
This segment initializes student grade logs and creates a list of entries.
entry1 = GradeLog(user_id=1, subject="Math", grade=95, notes="Test 1: Easy exam")
entry2 = GradeLog(user_id=2, subject="History", grade=88, notes="Project Presentation")
entry3 = GradeLog(user_id=3, subject="Science", grade=92, notes="Lab Report")
entry1.create()
entry2.create()
entry3.create()
π Explanation: This code initializes multiple grade log entries and assigns them a subject, grade, and notes. The .create()
method ensures that each entry is stored in the database.
π List Processing
This function retrieves all stored grade logs and processes their data.
def get(self):
try:
gradelogs = GradeLog.query.all()
return jsonify([log.read() for log in gradelogs])
except Exception as e:
return {"message": f"An error occurred: {str(e)}"}, 500
π Explanation: This function retrieves all grade logs from the database and converts them into JSON format using log.read()
. This allows the grade logs to be accessed dynamically.
π οΈ Function with Sequencing, Selection & Iteration
This function modifies a studentβs grade log by iterating over it and applying conditional logic.
def update(self, data):
for key, value in data.items():
if key == "subject":
self.subject = value
if key == "grade":
self.grade = value
if key == "notes":
self.notes = value
db.session.commit()
return self
π Explanation of Key Concepts:
β Sequencing: The function follows a structured processβiterating through the data, checking conditions, modifying the log, and committing changes to the database.
β Selection: The if
statements ensure only relevant fields are updated.
β Iteration: The for
loop iterates over the data fields dynamically.
π Call to Function
This function call updates a studentβs grade log dynamically.
entry = GradeLog.query.get(data["id"])
if entry:
entry.update(data)
π Explanation: This retrieves a grade log by ID and updates it with new data. The function ensures only valid fields are modified.
π Queries
This snippet defines how the API retrieves grade logs by querying the database.
class GradelogAPI:
class _CRUD(Resource):
@token_required()
def get(self):
current_user = g.current_user
all_gradelogs = GradeLog.query.filter_by(user_id=current_user.id).all()
gradelog = [log.read() for log in all_gradelogs]
return jsonify(gradelog)
π Explanation: This function queries all grade logs for a user and converts them into a list of dictionaries before returning a JSON response.
β‘ CRUD Methods
These methods enable database manipulation for grade logs.
def create(self):
try:
db.session.add(self)
db.session.commit()
return self
except IntegrityError:
db.session.rollback()
return None
def read(self):
return {
"id": self.id,
"user_id": self.user_id,
"subject": self.subject,
"grade": self.grade,
"notes": self.notes,
"date": self.date.strftime('%Y-%m-%d %H:%M:%S')
}
def update(self, data):
for key, value in data.items():
if key == "subject":
self.subject = value
if key == "grade":
self.grade = value
if key == "notes":
self.notes = value
db.session.commit()
return self
def delete(self):
db.session.delete(self)
db.session.commit()
π Algorithmic Code Request
Methods: CRUD operations such as get()
, update()
, delete()
.
- Sequencing: The method follows structured steps:
- Retrieve the authenticated user.
- Query the database for grade logs.
- Convert logs to a list of dictionaries.
- Return a JSON response.
- Selection: Filtering by
user_id
ensures only authorized logs are retrieved. - Iteration: The
for
loop processes each grade log.
π Call to Algorithm Request
try {
const response = await fetch('http://127.0.0.1:8887/api/gradelog', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
credentials: 'include',
});
if (response.ok) {
alert('Grade Log added successfully!');
gradeLogForm.reset();
loadGradeLogs(); // Refresh logs
} else {
const errorText = await response.text();
alert('Failed to add grade log: ' + errorText);
}
} catch (error) {
console.error('Error:', error);
alert('An error occurred while adding the grade log.');
}
π Explanation: This fetch request sends new grade log data to the backend, handles responses, and refreshes the grade log list dynamically.
This markdown document maintains the structured approach while effectively translating the original code into the AP CSP Personalized Project Reference format.