<s> import json <EOL> import logging <EOL> import uuid <EOL> from lingobarter . core . models . config import Config , Lingobarter <EOL> from lingobarter . core . models . custom_values import CustomValue <EOL> from lingobarter . modules . accounts . models import User , Role <EOL> from mongoengine import DoesNotExist <EOL> logger = logging . getLogger ( ) <EOL> class Populate ( object ) : <EOL> def __init__ ( self , db , * args , ** kwargs ) : <EOL> self . db = db <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . json_data = None <EOL> self . roles = { } <EOL> self . users = { } <EOL> self . custom_values = { } <EOL> self . load_fixtures ( ) <EOL> self . baseurl = self . kwargs . get ( '<STR_LIT>' ) <EOL> self . app = self . kwargs . get ( '<STR_LIT>' ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> if self . baseurl and self . app : <EOL> with self . app . test_request_context ( base_url = self . baseurl ) : <EOL> self . pipeline ( ) <EOL> else : <EOL> self . pipeline ( ) <EOL> def pipeline ( self ) : <EOL> self . load_existing_users ( ) <EOL> self . create_users ( ) <EOL> self . create_configs ( ) <EOL> @ staticmethod <EOL> def generate_random_password ( ) : <EOL> return uuid . uuid4 ( ) . hex <EOL> def create_initial_superuser ( self ) : <EOL> password = self . generate_random_password ( ) <EOL> user_data = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT:email>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:password>" : password [ : <NUM_LIT:6> ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:0> <EOL> } , <EOL> { <EOL> "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:0> <EOL> } , <EOL> { <EOL> "<STR_LIT:title>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT:0> <EOL> } <EOL> ] <EOL> } <EOL> user_obj = self . create_user ( user_data ) <EOL> return user_data , user_obj <EOL> def load_fixtures ( self ) : <EOL> filepath = self . kwargs . get ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> _file = open ( filepath ) <EOL> self . json_data = json . load ( _file ) <EOL> def role ( self , name ) : <EOL> if name not in self . roles : <EOL> try : <EOL> role = Role . objects . get ( name = name ) <EOL> created = False <EOL> except DoesNotExist : <EOL> role = Role ( name = name , <EOL> description = '<STR_LIT>' ) <EOL> role . save ( ) <EOL> created = True <EOL> self . roles [ name ] = role <EOL> if created : <EOL> logger . info ( "<STR_LIT>" , name ) <EOL> return self . roles . get ( name ) <EOL> def load_existing_users ( self ) : <EOL> users = User . objects . all ( ) <EOL> for user in users : <EOL> self . users [ user . name ] = user <EOL> def create_user ( self , data ) : <EOL> name = data . get ( '<STR_LIT:name>' ) <EOL> if name not in self . users : <EOL> pwd = data . get ( "<STR_LIT:password>" ) <EOL> data [ '<STR_LIT>' ] = [ self . role ( role ) for role in data . get ( '<STR_LIT>' ) ] <EOL> user = User . createuser ( ** data ) <EOL> self . users [ name ] = user <EOL> logger . info ( "<STR_LIT>" , user . email , pwd ) <EOL> return user <EOL> else : <EOL> logger . info ( "<STR_LIT>" , data . get ( '<STR_LIT:email>' ) ) <EOL> def create_users ( self , data = None ) : <EOL> users_data = data or self . json_data . get ( '<STR_LIT>' ) <EOL> for data in users_data : <EOL> self . create_user ( data ) <EOL> @ staticmethod <EOL> def create_config ( data ) : <EOL> try : <EOL> return Config . objects . get ( group = data . get ( '<STR_LIT>' ) ) <EOL> except DoesNotExist : <EOL> return Config . objects . create ( ** data ) <EOL> def custom_value ( self , ** data ) : <EOL> if data . get ( '<STR_LIT:name>' ) in self . custom_values : <EOL> return self . custom_values [ data . get ( '<STR_LIT:name>' ) ] <EOL> value = CustomValue ( ** data ) <EOL> self . custom_values [ value . name ] = value <EOL> return value <EOL> def create_configs ( self ) : <EOL> configs_data = self . json_data . get ( '<STR_LIT>' ) <EOL> for config in configs_data : <EOL> config [ '<STR_LIT>' ] = [ self . custom_value ( ** args ) <EOL> for args in config . get ( '<STR_LIT>' ) ] <EOL> self . create_config ( config ) <EOL> def reset ( self ) : <EOL> User . objects ( <EOL> email__in = [ item [ '<STR_LIT:email>' ] for item in self . json_data . get ( '<STR_LIT>' ) ] <EOL> ) . delete ( ) <EOL> if self . kwargs . get ( '<STR_LIT>' ) : <EOL> Lingobarter . objects . delete ( ) </s>
