from datetime import datetime 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(required=True) logo = ImageField(thumbnail_size=(120, 120)) description = StringField() legal_registration_number = StringField(required=True) headquarter = StringField() email = StringField(required=True) api_key = ListField(EmbeddedDocumentField(ApiKey)) deleted = BooleanField(default=False) def __unicode__(self): return self.name 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.name = "Root" user.last_name = "Root" user.telephone = "+900000000000" user.gender = 'Agender' user.date_of_birth = "2019-09-09" user.username = "{}@root".format(self.legal_registration_number) user.user_group = group user.union = self from flask import current_app password = ''.join(choices(ascii_letters + digits, k=10)) current_app.logger.info("New union password : {}".format(password)) user.password = generate_password_hash(password) # TODO: send password via mail or sms or nothing user.save()