class User(db.DynamicDocument, UserMixin): name = db.StringField(max_length=255) email = db.EmailField(max_length=255, unique=True) password = db.StringField(max_length=255) active = db.BooleanField(default=True) confirmed_at = db.DateTimeField() roles = db.ListField(db.ReferenceField(Role, reverse_delete_rule=db.DENY), default=[]) last_login_at = db.DateTimeField() current_login_at = db.DateTimeField() last_login_ip = db.StringField(max_length=255) current_login_ip = db.StringField(max_length=255) login_count = db.IntField() username = db.StringField(max_length=50, required=False, unique=True) remember_token = db.StringField(max_length=255) authentication_token = db.StringField(max_length=255) tagline = db.StringField(max_length=255) bio = db.StringField() links = db.ListField(db.EmbeddedDocumentField(UserLink)) gravatar_email = db.EmailField(max_length=255) def get_gravatar_email(self): return self.gravatar_email or self.email def clean(self, *args, **kwargs): if not self.username: self.username = User.generate_username(self.name) try: super(User, self).clean(*args, **kwargs) except: pass @classmethod def generate_username(cls, name): # username = email.lower() # for item in ['@', '.', '-', '+']: # username = username.replace(item, '_') # return username username = slugify(name) if cls.objects.filter(username=username).count(): username = "******".format(username, randint(1, 1000)) return username def set_password(self, password, save=False): self.password = encrypt_password(password) if save: self.save() @classmethod def createuser(cls, name, email, password, active=True, roles=None, username=None, *args, **kwargs): username = username or cls.generate_username(name) if "links" in kwargs: kwargs["links"] = [UserLink(**link) for link in kwargs['links']] return cls.objects.create(name=name, email=email, password=encrypt_password(password), active=active, roles=roles, username=username, *args, **kwargs) @property def display_name(self): return abbreviate(self.name) or self.email def __unicode__(self): return u"{0} <{1}>".format(self.name or '', self.email) @property def connections(self): return Connection.objects(user_id=str(self.id))
class User(db.DynamicDocument, ThemeChanger, HasCustomValue, UserMixin): name = db.StringField(max_length=255) email = db.EmailField(max_length=255, unique=True) password = db.StringField(max_length=255) active = db.BooleanField(default=True) confirmed_at = db.DateTimeField() roles = db.ListField(db.ReferenceField(Role, reverse_delete_rule=db.DENY), default=[]) last_login_at = db.DateTimeField() current_login_at = db.DateTimeField() last_login_ip = db.StringField(max_length=255) current_login_ip = db.StringField(max_length=255) login_count = db.IntField() username = db.StringField(max_length=50, required=False, unique=True) remember_token = db.StringField(max_length=255) authentication_token = db.StringField(max_length=255) tagline = db.StringField(max_length=255) bio = db.StringField() links = db.ListField(db.EmbeddedDocumentField(UserLink)) use_avatar_from = db.StringField(choices=(("gravatar", "gravatar"), ("url", "url"), ("upload", "upload"), ("facebook", "facebook")), default='gravatar') gravatar_email = db.EmailField(max_length=255) avatar_file_path = db.StringField() avatar_url = db.StringField(max_length=255) def get_avatar_url(self, *args, **kwargs): if self.use_avatar_from == 'url': return self.avatar_url elif self.use_avatar_from == 'upload': return url_for('quokka.core.media', filename=self.avatar_file_path) elif self.use_avatar_from == 'facebook': try: return Connection.objects( provider_id='facebook', user_id=self.id, ).first().image_url except Exception as e: logger.warning( '%s use_avatar_from is set to facebook but: Error: %s' % (self.display_name, str(e))) return Gravatar()(self.get_gravatar_email(), *args, **kwargs) @property def summary(self): return (self.bio or self.tagline or '')[:255] def get_gravatar_email(self): return self.gravatar_email or self.email def clean(self, *args, **kwargs): if not self.username: self.username = User.generate_username(self.name) super(User, self).clean(*args, **kwargs) @classmethod def generate_username(cls, name, user=None): name = name or '' username = slugify(name) filters = {"username": username} if user: filters["id__ne"] = user.id if cls.objects.filter(**filters).count(): username = "******".format(username, randint(1, 1000)) return username def set_password(self, password, save=False): self.password = encrypt_password(password) if save: self.save() @classmethod def createuser(cls, name, email, password, active=True, roles=None, username=None, *args, **kwargs): username = username or cls.generate_username(name) if 'links' in kwargs: kwargs['links'] = [UserLink(**link) for link in kwargs['links']] return cls.objects.create(name=name, email=email, password=encrypt_password(password), active=active, roles=roles, username=username, *args, **kwargs) @property def display_name(self): return abbreviate(self.name) or self.email def __unicode__(self): return u"{0} <{1}>".format(self.name or '', self.email) @property def connections(self): return Connection.objects(user_id=str(self.id))
class Dated(object): available_at = db.DateTimeField(default=datetime.datetime.now) available_until = db.DateTimeField(required=False) created_at = db.DateTimeField(default=datetime.datetime.now) updated_at = db.DateTimeField(default=datetime.datetime.now)
class User(db.DynamicDocument, UserMixin): name = db.StringField(max_length=255, verbose_name=_l('Name')) email = db.EmailField(max_length=255, unique=True, verbose_name=_l('Email')) password = db.StringField(max_length=255, verbose_name=_l('Password')) active = db.BooleanField(default=True, verbose_name=_l('Active')) confirmed_at = db.DateTimeField(verbose_name=_l('Confirmed At')) roles = db.ListField(db.ReferenceField(Role, reverse_delete_rule=db.DENY), default=[], verbose_name=_l('Roles')) last_login_at = db.DateTimeField(verbose_name=_l('Last Login At')) current_login_at = db.DateTimeField(verbose_name=_l('Current Login At')) last_login_ip = db.StringField(max_length=255, verbose_name=_l('Last Login IP')) current_login_ip = db.StringField(max_length=255, verbose_name=_l('Current Login IP')) login_count = db.IntField(verbose_name=_l('Login Count')) username = db.StringField(max_length=50, required=False, unique=True, verbose_name=_l('Username')) remember_token = db.StringField(max_length=255, verbose_name=_l('Remember Token')) authentication_token = db.StringField( max_length=255, verbose_name=_l('Authentication Tokern')) def clean(self, *args, **kwargs): if not self.username: self.username = User.generate_username(self.email) try: super(User, self).clean(*args, **kwargs) except: pass @classmethod def generate_username(cls, email): username = email.lower() for item in ['@', '.', '-', '+']: username = username.replace(item, '_') return username def set_password(self, password, save=False): self.password = encrypt_password(password) if save: self.save() @classmethod def createuser(cls, name, email, password, active=True, roles=None, username=None): username = username or cls.generate_username(email) return cls.objects.create(name=name, email=email, password=encrypt_password(password), active=active, roles=roles, username=username) @property def display_name(self): return self.name or self.email def __unicode__(self): return u"{0} <{1}>".format(self.name or '', self.email) @property def connections(self): return Connection.objects(user_id=str(self.id))
class PageView(db.Document): time = db.DateTimeField(default=datetime.now) ip = db.EmbeddedDocumentField(IPAddress) path = db.StringField(max_length=255)
class Dated(object): available_at = db.DateTimeField(default=datetime.datetime.now, verbose_name=_l('Available At')) available_until = db.DateTimeField(required=False, verbose_name=_l('Available Until')) created_at = db.DateTimeField(default=datetime.datetime.now, verbose_name=_l('Created At')) updated_at = db.DateTimeField(default=datetime.datetime.now, verbose_name=_l('Updated At'))