liveBook · Manning

AddBookmarkCommand

As explained in Practices of the Python Pro

AddBookmarkCommand is a Bark command object that adds a bookmark record to the database.

AddBookmarkCommand is a business-logic command that turns bookmark data into a stored database record. It accepts a dictionary containing a title, URL, and optional notes, adds a UTC timestamp, and passes the result to the persistence layer. section 6.3.2

Its execute operation follows a short, useful workflow:

  • Receive bookmark fields through the shared dictionary interface. section 6.3.2
  • Add date_added using datetime.utcnow().isoformat() when no timestamp is supplied. section 6.3.2 section 7.3
  • Insert the completed dictionary into the bookmarks table through DatabaseManager.add. section 6.3.2
  • Return a success message for the presentation layer. section 6.3.2

Basic implementation

class AddBookmarkCommand: def execute(self, data): data['date_added'] = datetime.utcnow().isoformat() db.add('bookmarks', data) return 'Bookmark added!'

Preserving imported timestamps

An optional timestamp makes the command reusable for imports such as GitHub stars: data['date_added'] = timestamp or datetime.utcnow().isoformat(). This preserves an original timestamp when available while retaining normal creation behavior otherwise. section 7.3

The command separates presentation concerns from bookmark creation, but its direct call to db.add still couples business logic to the database. Its user-facing return string also leaks presentation concerns into the command. section 10.3

How Practices of the Python Pro covers it

How deep it goes
Explains inputs, timestamp generation, database insertion, and response handling, then stops short of decoupling the database dependency.
Examples
Demonstrates ordinary bookmark creation and GitHub stars importing with optionally preserved timestamps.
What you can do after
Build it from scratch, extend it for imported timestamps, and recognize its database and presentation coupling.
How it connects
Connects the command pattern with business logic, persistence, presentation, inversion of control, and loose coupling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.