-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
77 lines (60 loc) · 2.59 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from datetime import datetime
from slugify import slugify
from flask_security import Security, UserMixin, RoleMixin, MongoEngineUserDatastore
from passlib.hash import bcrypt
from init import app, db
class Role(db.Document, RoleMixin):
"""
класс для фласк секьюрити
"""
name = db.StringField(max_length=150, verbose_name='Название роли', unique=True)
description = db.StringField(verbose_name='Описание')
def __unicode__(self):
return self.name
class User(db.Document, UserMixin):
"""
модель пользователя
"""
email = db.StringField(max_length=50, verbose_name='E-mail', unique=True)
password = db.StringField(verbose_name='Пароль')
active = db.BooleanField(default=True)
roles = db.ListField(db.ReferenceField(Role), default=[])
@classmethod
def authenticate(cls, email, password):
user = cls.objects(email=email).first()
if not bcrypt.verify(password, user.password):
raise Exception('Invalid password')
return user
def save(self):
if not self.id:
self.password = bcrypt.hash(self.password)
return super(User, self).save(self)
else:
return super(User, self).update(email=self.email, active=self.active, roles=self.roles)
class Photo(db.EmbeddedDocument):
"""
фотография
"""
file = db.StringField(verbose_name='Название файла')
created = db.DateTimeField(verbose_name='Дата и время создания')
device = db.StringField(verbose_name='Устройство')
class Album(db.Document):
"""
альбомы с фотографиями пользователя
"""
user = db.ReferenceField(User, verbose_name='Пользователь')
name = db.StringField(verbose_name='Название альбома')
slug = db.StringField(verbose_name='Slug альбома')
created = db.DateTimeField(default=datetime.now, verbose_name='Дата и время создания')
description = db.StringField(verbose_name='Описание альбома')
photos = db.SortedListField(db.EmbeddedDocumentField(Photo), verbose_name='Фотографии')
def save(self):
self.slug = slugify(self.name, to_lower=True)
return super(Album, self).save(self)
meta = {
'indexes': [
{'fields': ('user', 'name', 'slug'), 'unique': True}
]
}
user_datastore = MongoEngineUserDatastore(db, User, Role)
security = Security(app, user_datastore)