123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- #
- # create default user in PoLZy DB
- #
- from polzybackend.models import User, Role, Company, UserToCompany, CompanyToCompany#, \
- # GamificationEvent, GamificationBadgeLevel, GamificationBadgeType, GamificationBadge, GamificationBadgeDescription
- from random import sample, choice
- from polzybackend.utils.auth_utils import generate_token
- import uuid
- from datetime import datetime, timedelta
- from flask_sqlalchemy import SQLAlchemy
- from polzybackend import create_app
- from app import Config
- import json
- app = create_app(Config)
- db = SQLAlchemy(app)
- print(db)
- # Gamification Badges
- #### replaced by populate_gamification tables.py ####
- '''
- print('Creating Gamification Badges...')
- badge_levels = [
- "Bronze",
- "Silver",
- "Gold",
- ]
- levels = []
- next_level = None
- for name in reversed(badge_levels):
- level = GamificationBadgeLevel(name=name, next_level=next_level, is_lowest=(name == 'Bronze'))
- levels.append(level)
- next_level = level
- db.session.add_all(levels)
- # add many types
- total_badge_number = 20
- types = [
- GamificationBadgeType(
- name=f"type{i+1}",
- title=f"Type {i+1}",
- ) for i in range(total_badge_number)]
- db.session.add_all(types)
- # type-level descriptions
- db.session.add_all([
- GamificationBadgeDescription(
- level=level,
- type=type,
- description=f"Requirements to earn {level.name} Badge {type.title}",
- ) for level in levels for type in types]
- )
- '''
- # create roles
- print('Creating roles...')
- admin_role = Role(name='admin', is_supervisor=True)
- agent_role = Role(name='agent')
- clerk_role = Role(name='clerk')
- db.session.add_all([admin_role, agent_role])
- # create users
- print('Creating users...')
- admin = User(
- email='admin@polzy.com',
- #oauth_provider=provider,
- #oauth_user_id=str(uuid.uuid4()),
- #oauth_token=generate_token(16),
- key_expired=datetime.now() + timedelta(days=360),
- )
- db.session.add(admin)
- agent = User(
- email='agent@polzy.com',
- displayed_name='Agent',
- #oauth_provider=provider,
- #oauth_user_id=str(uuid.uuid4()),
- #oauth_token=generate_token(16),
- key_expired=datetime.now() + timedelta(days=360),
- )
- db.session.add(agent)
- clerk = User(
- email='clerk@polzy.com',
- key_expired=datetime.now() + timedelta(days=360),
- )
- db.session.add(clerk)
- # create companies
- print('Creating companies...')
- company = Company(
- name='AllIns',
- displayed_name='AllIns - whatever happens - you`re covered',
- email='dummy@dummy.com',
- phone='+357 95 11 55 44',
- country='CY',
- post_code='6020',
- city='Larnaca',
- address='Finikoudes 15',
- attributes=json.dumps({
- 'theme': {
- 'palette': {
- 'primary': {
- 'main': '#3c6496',
- },
- 'secondary': {
- 'main': '#3c6496',
- },
- },
- },
- 'logo': {
- 'top': 'polzy.png',
- 'policy': 'LeZySEM Blue.png',
- 'antrag': 'LeZyTOR Blue.png',
- },
- 'hitList': True,
- })
- )
- # bind admin to company
- company_admin = UserToCompany(
- user=admin,
- company=company,
- roles=[admin_role],
- )
- # bind agent to organization
- organization_agent = UserToCompany(
- user=agent,
- company=company,
- roles=[agent_role],
- )
- organization_clerk = UserToCompany(
- user=clerk,
- company=company,
- roles=[clerk_role],
- )
- db.session.add(company)
- db.session.add(company_admin)
- db.session.add(organization_agent)
- db.session.add(organization_clerk)
- # add badges
- '''
- user_badge_num = 10
- db.session.add_all([
- GamificationBadge(
- user=admin,
- company=company,
- type_id=_+1,
- level=choice(levels),
- ) for _ in sample(range(total_badge_number), user_badge_num)
- ])
- '''
- company = Company(
- name="SportIns",
- displayed_name="Sports Insurance - just do it and be save",
- email="dummy@dummy.com.cy",
- phone="+357 91 23 12",
- country="CY",
- post_code='6020',
- city='Limassol',
- address='TestDummy 12',
- attributes=json.dumps({
- 'theme': {
- 'palette': {
- 'primary': {
- 'main': '#9e3c3a',
- },
- 'secondary': {
- 'main': '#9e3c3a',
- },
- },
- },
- 'logo': {
- 'top': 'polzy.png',
- 'policy': 'LeZySEM Red.png',
- 'antrag': 'LeZyTOR Red.png',
- }
- })
- )
- # bind admin to company
- company_admin = UserToCompany(
- user=admin,
- company=company,
- roles=[admin_role, agent_role],
- )
- db.session.add(company)
- db.session.add(company_admin)
- organization = Company(
- name='Sample Organization',
- #displayed_name='',
- #email='',
- #phone='',
- #country='',
- #post_code='',
- #city='',
- #address='',
- )
- # bind admin to organization
- organization_admin = UserToCompany(
- user=admin,
- company=organization,
- roles=[admin_role],
- )
- # bind organization to company
- organization_structure = CompanyToCompany(
- parent=company,
- child=organization,
- attributes=json.dumps({
- 'policy': [
- 'admin',
- 'agent',
- ],
- 'antrag': [
- 'admin',
- ],
- }),
- )
- db.session.add(organization)
- db.session.add(organization_admin)
- db.session.add(organization_structure)
- try:
- db.session.commit()
- except Exception as e:
- print(f"Error during commit: {e}")
- pass
- print('Done.')
|