Ejemplo n.º 1
0
class User(BaseDocument):
    username = dbm.StringField(verbose_name='login',
                               max_length=30,
                               required=True,
                               unique=True)
    name = dbm.StringField(verbose_name='name', max_length=100, required=True)
    email = dbm.EmailField(verbose_name='e-mail',
                           max_length=100,
                           required=True,
                           unique=True)
    pw_hash = dbm.StringField(verbose_name='pwhash',
                              max_length=100,
                              required=True)
    is_active = dbm.BooleanField(verbose_name='is active',
                                 default=True,
                                 required=True)
    is_superuser = dbm.BooleanField(verbose_name='is admin',
                                    default=False,
                                    required=True)
    last_login = dbm.DateTimeField(verbose_name='last login date',
                                   default=datetime.now(),
                                   required=False)
    friends = dbm.ListField(dbm.EmbeddedDocumentField(Friend))
    viewed_posts = dbm.ListField(field=dbm.StringField(required=False),
                                 required=False)
    visit_count = dbm.IntField(min_value=0, default=0, required=True)

    meta = {'indexes': ['username', 'email']}

    # def __unicode__(self):
    #    return self.username

    def __init__(self, *args, **kwargs):
        password = kwargs.pop('password', None)
        super(User, self).__init__(*args, **kwargs)
        if password:
            self.set_password(password)

    def set_password(self, password):
        self.pw_hash = generate_password_hash(
            password,
            method=current_app.config['PROJECT_PASSWORD_HASH_METHOD'])

    def check_password(self, password):
        return check_password_hash(self.pw_hash, password)

    def refresh_last_login(self):
        self.last_login = datetime.now()
        self.visit_count += 1
        self.save()
Ejemplo n.º 2
0
class Post(BaseDocument):
    created_at = dbm.DateTimeField(default=datetime.now(), required=True)
    title = dbm.StringField(verbose_name="Title",
                            max_length=255,
                            required=True)
    author = dbm.ReferenceField(User, required=True)
    body = dbm.StringField(verbose_name="Data", required=True)
    slug = dbm.StringField(max_length=255, required=True)
    comments = dbm.ListField(dbm.EmbeddedDocumentField('Comment'))
    views_count = dbm.IntField(min_value=0, default=0, required=True)
    anon_views_count = dbm.IntField(min_value=0, default=0, required=True)
    viewers = dbm.ListField(dbm.ReferenceField(User),
                            default=None,
                            unique=False,
                            required=False)

    def __init__(self, *args, **kwargs):
        if not 'slug' in kwargs:
            kwargs['slug'] = slugify(kwargs.get('title', ''))
        super().__init__(*args, **kwargs)

    def get_absolute_url(self):
        return url_for('post', kwargs={"slug": self.slug})

    def __unicode__(self):
        return self.title

    @property
    def post_type(self):
        return self.__class__.__name__

    meta = {
        'allow_inheritance': True,
        'indexes': ['-created_at', 'slug'],
        'ordering': ['-created_at']
    }
Ejemplo n.º 3
0
class Friend(dbm.EmbeddedDocument):
    added_at = dbm.DateTimeField(default=datetime.now(), required=True)
    friend_user_id = dbm.ObjectIdField(unique=False, required=True)
    status = dbm.StringField(verbose_name="status",
                             max_length=30,
                             required=True)
Ejemplo n.º 4
0
class Comment(dbm.EmbeddedDocument):
    created_at = dbm.DateTimeField(default=datetime.now(), required=True)
    body = dbm.StringField(verbose_name="Comment", required=True)
    author = dbm.ReferenceField(User, required=True)
Ejemplo n.º 5
0
class Image(Post):
    image = dbm.FileField(verbose_name="image",
                          default=None,
                          unique=False,
                          required=False)
    image_file_name = dbm.StringField(verbose_name="Filename", required=False)
Ejemplo n.º 6
0
class ImageURL(Post):
    image_url = dbm.StringField(verbose_name="URL of picture", required=True)
Ejemplo n.º 7
0
class Video(Post):
    embed_code = dbm.StringField(verbose_name="Code for blog", required=True)