class Alert(MonDoc): user_id = FK(userdb.User, desc="who this alert is for") alertType = ChoiceField(choices=ALERT_TYPES, desc="type of this alert", allowNull=False, readOnly=True) message_id = FK(Message, desc="the message this alert relates to") live = BoolField(default=True, desc="an alert is live until the user clicks to view it") created = DateTimeField(desc="when this alert was created", readOnly=True) doer_id = FK(userdb.User, desc="user who starred/replied") reply_id = FK(Message, desc="the reply", allowNull=True) def logo(self): if self.live: return "<i class='fa fa-bell'></i> " else: return "<i class='fa fa-bell-o'></i> " @classmethod def classLogo(self): return "<i class='fa fa-bell-o'></i> " def preCreate(self): """ before saving, create the bioHtml """ self.created = BzDateTime.now()
class Foo(MonDoc): name = StrField() description = TextAreaField(monospaced=True) aNumber = IntField(minValue=0, maxValue=100) minSpeed = FloatField(title="Minimum Speed, mph", minValue=0.0) maxSpeed = FloatField(title="Maximim Speed, mph", minValue=0.0) favouriteDrink = ChoiceField(choices=DRINK_CHOICES, showNull=True, allowNull=True) fruitsLiked = MultiChoiceField(choices=FRUIT_CHOICES, desc="tick all fruits this person likes") tickyBox = BoolField() aDate = DateField() lastSaved = DateTimeField(desc="when this foo was last saved", readOnly=True) aDateTime = DateTimeField(title="A Date and Time") anything = ObjectField(desc="can contain anything", default=["any", "thing"]) favouriteBook = FK(models.Book, allowNull=True, showNull=True) @classmethod def classLogo(self): return "<i class='fa fa-star-o'></i> " def formWideErrorMessage(self): if self.minSpeed > self.maxSpeed: return "Minimum speed cannot be greater than maximum speed" return "" # no error message, validates OK def preSave(self): self.lastSaved = BzDateTime.now() d = self.mongoDict() d.pop('anything', "") # remove the anything field dpr("d=%r", d) self.anything = d
class TheTestForm(FormDoc): aaa = StrField() aNumber = IntField(minValue=0, maxValue=100) cost = FloatField(title="Cost, £", formatStr="{:.2f}") tickyBox = BoolField() toggleSwitch1 = BoolField(widget='toggleSwitch') toggleSwitch2 = BoolField(widget='toggleSwitch') showBody = BoolField(widget='toggleSwitch') order = BoolField(widget='toggleSwitch', showTitle=False, onText="Oldest First", offText="Newest First") favouriteFruit = ChoiceField(choices=FRUIT_CHOICES, showNull=True, allowNull=False) slots = MultiChoiceField(choices=SLOT_CHOICES, required=True) note = TextAreaField() dateOfBirth = DateField(required=True)
class FormattingOptionsForm(FormDoc): oneLine = BoolField(desc="show one line summary", widget='toggleSwitch', showTitle=False, offText="Show Message", onText="1-Line Summary") headOnly = BoolField(desc="show head posts only", widget='toggleSwitch', showTitle=False, default=True, offText="All Posts", onText="Head Posts") mrf = BoolField(desc="most recent posts first", widget='toggleSwitch', showTitle=False, default=True, offText="Oldest First", onText="Most Recent First") au = BoolField(desc="Auto Update", widget='toggleSwitch', showTitle=False, offText="Static", onText="Auto Update") def setFromUrl(self): """ Set the values of the fields in the form to that from the GET parameters in the URL """ x = request.args.get('x', "") if not x: # no form fields, don't change anything return self.oneLine = bool(request.args.get('oneLine', False)) self.headOnly = bool(request.args.get('headOnly', False)) self.mrf = bool(request.args.get('mrf', False)) self.au = bool(request.args.get('au', False))
class User(MonDoc): userName = StrField(charsAllowed=string.ascii_letters + string.digits + "_", minLength=2, desc="user name") hashedPassword = StrField() pw = StrField(desc="unencrypted password, for testing") password = StrField(default=HIDDEN) # for altering on /user page email = StrField(monospaced=True) isAdmin = BoolField(desc="is this user an admin?", title="Is Admin?", default=False) isActive = BoolField(desc="is this an active user?", title="Is Active?", default=True) @classmethod def classLogo(cls): return "<i class='fa fa-user'></i> " def __repr__(self): """ Return a string representation of myself. @return::str """ s = "<User %r pw=%r email=%r>" % (self.userName, self.pw, self.email) return s #========== stuff Flask-login needs: ========== """ see <https://flask-login.readthedocs.org/en/latest/#your-user-class> """ def get_id(self): return self.userName @property def is_authenticated(self): return self.isAuthenticated() @property def is_anonymous(self): return not self.has_key('_id') @property def is_active(self): return True def isAuthenticated(self): """ Do we have a logged-in user? @return::bool """ return self.has_key('_id') #========== def preSave(self): """ We don't want to save the plaintext password to the database. """ if self.password != HIDDEN: self.pw = self.password self.hashedPassword = hashPassword(self.password) self.password = HIDDEN # userName is a unique identifier, so use this as the _id self._id = self.userName #========== for display ========== def blogLink(self) -> str: """ return a link to the user's blog """ h = form("<a href='/blog/{userName}'>@{userName}</a>", userName=htmlEsc(self.userName)) return h