Model Helpers¶
Fetching Records¶
- mediacore.model.fetch_row(mapped_class, pk=None, extra_filter=None, **kwargs)¶
Fetch a single row from the database or else trigger a 404.
Typical usage is to fetch a single row for display or editing:
class PageController(object): @expose() def index(self, id): page = fetch_row(Page, id) return page.name @expose() def works_with_slugs_too(self, slug): page = fetch_row(Page, slug=slug) return page.name
If the pk is string new then an empty instance of mapped_class is created and returned. This is helpful in admin controllers where you may reuse your edit action for adding too.
Parameters: - mapped_class – An ORM-controlled model
- pk (int, None or "new") – A particular primary key to filter by.
- extra_filter – Extra filter arguments.
- **kwargs – Any extra args are treated as column names to filter by. See sqlalchemy.orm.Query.filter_by().
Returns: An instance of mapped_class.
Raises webob.exc.HTTPNotFound: If no result is found
Slugs¶
- mediacore.model.slugify(string)¶
Produce a URL-friendly string from the input.
XHTML entities are converted to unicode, and then replaced with the best-choice ascii equivalents.
Parameter: string (unicode) – A title, name, etc Returns: Ascii URL-friendly slug Return type: unicode
- mediacore.model.get_available_slug(mapped_class, string, ignore=None)¶
Return a unique slug based on the provided string.
Works by appending an int in sequence starting with 2:
- awesome-stuff
- awesome-stuff-2
- awesome-stuff-3
Parameters: - mapped_class – The ORM-controlled model that the slug is for
- string (unicode) – A title, name, etc
- ignore (Int ID, mapped_class instance or None) – A record which doesn’t count as a collision
Returns: A unique slug
Return type: unicode