|
|
|
from mongoengine import *
|
|
|
|
from werkzeug.security import generate_password_hash
|
|
|
|
from string import ascii_letters, digits
|
|
|
|
from random import choices
|
|
|
|
|
|
|
|
from models.EmbededDocuments import ApiKey
|
|
|
|
from restapi import Methods
|
|
|
|
|
|
|
|
|
|
|
|
class Union(Document):
|
|
|
|
meta = {
|
|
|
|
'index_background': True,
|
|
|
|
'index_cls': False,
|
|
|
|
'auto_create_index': True,
|
|
|
|
'can_query': True,
|
|
|
|
'with_sub_docs': True,
|
|
|
|
'methods': [Methods.Get, Methods.List, Methods.Create],
|
|
|
|
"indexes": [
|
|
|
|
('name'),
|
|
|
|
("-name"),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
name = StringField()
|
|
|
|
logo = ImageField(thumbnail_size=(120, 120))
|
|
|
|
description = StringField()
|
|
|
|
legal_registration_number = StringField()
|
|
|
|
headquarter = StringField()
|
|
|
|
email = StringField()
|
|
|
|
api_key = ListField(EmbeddedDocumentField(ApiKey))
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
super(Union, self).save(*args, **kwargs)
|
|
|
|
from .Group import Group
|
|
|
|
from .User import User
|
|
|
|
group = Group()
|
|
|
|
group.union = self
|
|
|
|
group.name = 'Root'
|
|
|
|
group.rights = ["*::*::{}/*".format(self.id)]
|
|
|
|
group.save()
|
|
|
|
user = User()
|
|
|
|
user.gov_id = 'root'
|
|
|
|
user.username = "{}@root".format(self.legal_registration_number)
|
|
|
|
user.user_group = group
|
|
|
|
user.union = self
|
|
|
|
password = ''.join(choices(ascii_letters+digits, k=10))
|
|
|
|
print("New union password : {}".format(password))
|
|
|
|
user.password = generate_password_hash(password)
|
|
|
|
# TODO: send password via mail or sms or nothing
|
|
|
|
user.save()
|