class Collection(db.Document): name = db.StringField(max_length=128) description = db.StringField() owner = db.StringField(max_length=128) is_private = db.BooleanField(default=False) repos = db.ListField(db.DictField()) last_update = db.DateTimeField() create_date = db.DateTimeField() meta = { 'allow_inheritance': True, 'indexes': ['name'], 'ordering': ['-create_date'] } def save(self, *args, **kwargs): if not self.create_date: self.create_date = datetime.now() self.last_update = datetime.now() return super(Collection, self).save(*args, **kwargs) def __unicode__(self): return u'{0} -> {1}'.format(self.owner, self.name) def __str__(self): return u'{0} -> {1}'.format(self.owner, self.name)
class Collection(db.Document): name = db.StringField(max_length=128) description = db.StringField() owner = db.StringField(max_length=128) is_private = db.BooleanField(default=False) repos = db.ListField(db.DictField()) last_update = db.DateTimeField() create_date = db.DateTimeField() followers = db.ListField(db.StringField()) follower_count = db.IntField(default=0) tags = db.ListField(db.StringField()) meta = { 'allow_inheritance': True, 'indexes': ['name'], 'ordering': ['-create_date'] } def save(self, *args, **kwargs): if not self.create_date: self.create_date = datetime.now() self.last_update = datetime.now() return super(Collection, self).save(*args, **kwargs) def to_dict(self, *args, **kwargs): def updata_field(dict_): dict_['id'] = str(dict_['id']) return dict_ data = { 'id': str(self.id), 'name': self.name, 'description': self.description, 'owner': self.owner, 'is_private': self.is_private, 'repos': map(updata_field, self.repos) if sys.version_info < (3, 0) else list(map(updata_field, self.repos)), 'last_update': self.last_update.strftime('%Y-%m-%d %H:%M:%S'), 'create_date': self.create_date.strftime('%Y-%m-%d %H:%M:%S'), 'followers': self.followers, } return data def __unicode__(self): return u'{0} -> {1}'.format(self.owner, self.name) def __str__(self): return u'{0} -> {1}'.format(self.owner, self.name)
class Repo(db.Document): name = db.StringField(max_length=128) full_name = db.StringField(max_length=128, unique=True) link = db.URLField() author = db.StringField(max_length=128) author_link = db.URLField(required=False) desc = db.StringField() language = db.StringField(max_length=64) create_time = db.DateTimeField() update_time = db.DateTimeField() starred_users = db.ListField(db.StringField()) tags = db.ListField(db.StringField(max_length=30)) def save(self, *args, **kwargs): if not self.create_time: self.create_time = datetime.now() self.update_time = datetime.now() return super(Repo, self).save(*args, **kwargs) def __unicode__(self): return self.full_name def __str__(self): return self.full_name meta = { 'allow_inheritance': True, 'indexes': ['full_name'], 'ordering': ['-create_time'] }
class User(UserMixin, db.Document): username = db.StringField(max_length=255, required=True) email = db.EmailField(max_length=255) password_hash = db.StringField(required=True) create_time = db.DateTimeField(default=datetime.datetime.now, required=True) last_login = db.DateTimeField(default=datetime.datetime.now, required=True) is_email_confirmed = db.BooleanField(default=False) # is_active = db.BooleanField(default=True) is_superuser = db.BooleanField(default=False) role = db.StringField(max_length=32, default='general', choices=ROLES) display_name = db.StringField(max_length=255, default='Anonymous') github_username = db.StringField(max_length=255) github = db.URLField() biography = db.StringField() social_networks = db.DictField(default=SOCIAL_NETWORKS) homepage_url = db.URLField() avatar_url = db.URLField() confirm_email_sent_time = db.DateTimeField() @property def password(self): raise AttributeError('password is not a readle attribute') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def generate_confirmation_token(self, expiration=3600): serializer = TimedJSONWebSignatureSerializer( current_app.config['SECRET_KEY'], expiration) return serializer.dumps({'confirm': self.username}) def confirm_email(self, token, expiration=3600): s = TimedJSONWebSignatureSerializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) except Exception: return False if data.get('confirm') != self.username: return False self.is_email_confirmed = True self.save() return True def get_id(self): try: return str(self.username) except AttributeError: raise NotImplementedError( 'No `username` attribute - override `get_id`') def __unicode__(self): return self.username def __str__(self): return self.username
class GitmarkMeta(db.Document): key = db.StringField(max_length=64) value = db.StringField() value_list = db.ListField(db.StringField()) value_dict = db.DictField()