Skip to content

Instantly share code, notes, and snippets.

@danielwatson6
Created January 24, 2014 23:38
Show Gist options
  • Save danielwatson6/8609123 to your computer and use it in GitHub Desktop.
Save danielwatson6/8609123 to your computer and use it in GitHub Desktop.
from lib import db, utils
from lib.db import validators
BOOK_EXTENSIONS = ['azw', 'azw1', 'azw4', 'epub', 'kf8', 'mobi', 'pdb', 'pdf', 'prc', 'tpz']
class BookModel(db.BlobModel):
### Data:
title = db.string(required = True)
author = db.string(required = True)
genre = db.string(required = True)
submitted_at = db.date(auto_now_add = True)
last_edit_at = db.date(auto_now = True)
description = db.text()
author_description = db.text()
blob = db.string(required = True)
### Form:
form = db.Form({
"title": [validators.required()],
"author": [validators.required()],
"genre": [validators.required()],
"description": [validators.length(0, 500)],
"author_description": [validators.length(0, 500)],
# TO-DO: Add blob_extension validator
"blob": [validators.required(), validators.blob_extension(BOOK_EXTENSIONS)],
})
### Functions used by templates:
def small_description(self, max_len = 300):
"""Return a reduced size of the description. Used in templates."""
if len(self.description) <= max_len:
return self.description
return self.description[:max_len]
def get_size(self):
"""Size getter, but converts from bytes to human-readable size."""
return utils.file_size(self.size())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment