class Category(db.DynamicDocument): """ Why dynamic document ? So we can add other languages fields dynamically (e.g. Tamazight language) and add the new translation to all existing categories, see: docs.mongoengine.org/guide/defining-documents.html#dynamic-document-schemas """ created_at = db.DateTimeField(default=datetime.datetime.now, required=True) category_id = db.SequenceField(primary_key=True) # you can add your name_[lang] at runtime with no worries, # it's a Dynamic Document! name_ar = db.StringField(max_length=50) name_fr = db.StringField(max_length=50) name_en = db.StringField(max_length=50) description = db.StringField() def get_name(self, lang): # make this better, latter return eval('self.name_%s' % lang) or 'Unknown'
class License(db.DynamicDocument): created_at = db.DateTimeField(default=datetime.datetime.now, required=True) license_id = db.SequenceField(primary_key=True) name = db.StringField() link = db.URLField() description = db.StringField()
class Item(db.Document): item_id = db.SequenceField(primary_key=True) submitted_at = db.DateTimeField(default=datetime.datetime.now, required=True) titles = db.ListField(db.EmbeddedDocumentField(Title), required=True) submitter = db.ReferenceField(User) category = db.ReferenceField(Category) files = db.ListField(db.FileField()) github = db.URLField() blog_post = db.URLField() tags = db.ListField(db.StringField(max_length=30)) description = db.StringField() thumbnail = db.ImageField(thumbnail_size=(230, 330, True)) license = db.ReferenceField(License) has_api = db.BooleanField() api_url = db.URLField() def get_thumbnail(self): from flask import url_for if self.thumbnail: return url_for('items.serve_thumbnail', item_id=self.item_id, filename=self.thumbnail.filename) return url_for('static', filename='images/no-thumbnail.png') def get_title(self, lang): for title in self.titles: if title.lang == lang: return title.title return '' meta = { 'indexes': ['-submitted_at', 'tags'], 'ordering': ['-submitted_at'] }
class User(db.Document): created_at = db.DateTimeField(default=datetime.datetime.now, required=True) user_id = db.SequenceField(unique=True) is_admin = db.BooleanField(default=False) email = db.StringField() name = db.StringField(max_length=50) lang = db.StringField(max_length=3) bio = db.StringField() location = db.StringField() # Available for hire or not hireable = db.BooleanField(default=False) github_username = db.StringField(max_length=50) github_id = db.IntField() twitter_username = db.StringField(max_length=50) facebook_username = db.StringField(max_length=100) website = db.StringField(max_length=50) oauth_token = db.StringField() oauth_secret = db.StringField() _password = db.StringField() @property def password(self): return self._password @password.setter def password(self, password): self._password = bcrypt.generate_password_hash(password) def check_password(self, password): return bcrypt.check_password_hash(self._password, password) # Flask-Login integration def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return str(self.id) # helpers def getGithub(self): if self.github_username: return "https://github.com/%s" % (self.github_username) return '' def getTwitter(self): if self.twitter_username: return "https://twitter.com/%s" % (self.twitter_username) return '' def getFacebook(self): if self.facebook_username: return "https://facebook.com/%s" % (self.facebook_username) return '' def getName(self): return '%s' % self.name or self.github_username # Required for administrative interface def __unicode__(self): return self.name meta = {'ordering': ['-created_at']}