populate_db.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #
  2. # create default user in PoLZy DB
  3. #
  4. from polzybackend.models import User, Role, Company, UserToCompany, CompanyToCompany#, \
  5. # GamificationEvent, GamificationBadgeLevel, GamificationBadgeType, GamificationBadge, GamificationBadgeDescription
  6. from random import sample, choice
  7. from polzybackend.utils.auth_utils import generate_token
  8. import uuid
  9. from datetime import datetime, timedelta
  10. from flask_sqlalchemy import SQLAlchemy
  11. from polzybackend import create_app
  12. from app import Config
  13. import json
  14. app = create_app(Config)
  15. db = SQLAlchemy(app)
  16. print(db)
  17. # Gamification Badges
  18. #### replaced by populate_gamification tables.py ####
  19. '''
  20. print('Creating Gamification Badges...')
  21. badge_levels = [
  22. "Bronze",
  23. "Silver",
  24. "Gold",
  25. ]
  26. levels = []
  27. next_level = None
  28. for name in reversed(badge_levels):
  29. level = GamificationBadgeLevel(name=name, next_level=next_level, is_lowest=(name == 'Bronze'))
  30. levels.append(level)
  31. next_level = level
  32. db.session.add_all(levels)
  33. # add many types
  34. total_badge_number = 20
  35. types = [
  36. GamificationBadgeType(
  37. name=f"type{i+1}",
  38. title=f"Type {i+1}",
  39. ) for i in range(total_badge_number)]
  40. db.session.add_all(types)
  41. # type-level descriptions
  42. db.session.add_all([
  43. GamificationBadgeDescription(
  44. level=level,
  45. type=type,
  46. description=f"Requirements to earn {level.name} Badge {type.title}",
  47. ) for level in levels for type in types]
  48. )
  49. '''
  50. # create roles
  51. print('Creating roles...')
  52. admin_role = Role(name='admin', is_supervisor=True)
  53. agent_role = Role(name='agent')
  54. clerk_role = Role(name='clerk')
  55. db.session.add_all([admin_role, agent_role])
  56. # create users
  57. print('Creating users...')
  58. admin = User(
  59. email='admin@polzy.com',
  60. #oauth_provider=provider,
  61. #oauth_user_id=str(uuid.uuid4()),
  62. #oauth_token=generate_token(16),
  63. key_expired=datetime.now() + timedelta(days=360),
  64. )
  65. db.session.add(admin)
  66. agent = User(
  67. email='agent@polzy.com',
  68. displayed_name='Agent',
  69. #oauth_provider=provider,
  70. #oauth_user_id=str(uuid.uuid4()),
  71. #oauth_token=generate_token(16),
  72. key_expired=datetime.now() + timedelta(days=360),
  73. )
  74. db.session.add(agent)
  75. clerk = User(
  76. email='clerk@polzy.com',
  77. key_expired=datetime.now() + timedelta(days=360),
  78. )
  79. db.session.add(clerk)
  80. # create companies
  81. print('Creating companies...')
  82. company = Company(
  83. name='AllIns',
  84. displayed_name='AllIns - whatever happens - you`re covered',
  85. email='dummy@dummy.com',
  86. phone='+357 95 11 55 44',
  87. country='CY',
  88. post_code='6020',
  89. city='Larnaca',
  90. address='Finikoudes 15',
  91. attributes=json.dumps({
  92. 'theme': {
  93. 'palette': {
  94. 'primary': {
  95. 'main': '#3c6496',
  96. },
  97. 'secondary': {
  98. 'main': '#3c6496',
  99. },
  100. },
  101. },
  102. 'logo': {
  103. 'top': 'polzy.png',
  104. 'policy': 'LeZySEM Blue.png',
  105. 'antrag': 'LeZyTOR Blue.png',
  106. },
  107. 'hitList': True,
  108. })
  109. )
  110. # bind admin to company
  111. company_admin = UserToCompany(
  112. user=admin,
  113. company=company,
  114. roles=[admin_role],
  115. )
  116. # bind agent to organization
  117. organization_agent = UserToCompany(
  118. user=agent,
  119. company=company,
  120. roles=[agent_role],
  121. )
  122. organization_clerk = UserToCompany(
  123. user=clerk,
  124. company=company,
  125. roles=[clerk_role],
  126. )
  127. db.session.add(company)
  128. db.session.add(company_admin)
  129. db.session.add(organization_agent)
  130. db.session.add(organization_clerk)
  131. # add badges
  132. '''
  133. user_badge_num = 10
  134. db.session.add_all([
  135. GamificationBadge(
  136. user=admin,
  137. company=company,
  138. type_id=_+1,
  139. level=choice(levels),
  140. ) for _ in sample(range(total_badge_number), user_badge_num)
  141. ])
  142. '''
  143. company = Company(
  144. name="SportIns",
  145. displayed_name="Sports Insurance - just do it and be save",
  146. email="dummy@dummy.com.cy",
  147. phone="+357 91 23 12",
  148. country="CY",
  149. post_code='6020',
  150. city='Limassol',
  151. address='TestDummy 12',
  152. attributes=json.dumps({
  153. 'theme': {
  154. 'palette': {
  155. 'primary': {
  156. 'main': '#9e3c3a',
  157. },
  158. 'secondary': {
  159. 'main': '#9e3c3a',
  160. },
  161. },
  162. },
  163. 'logo': {
  164. 'top': 'polzy.png',
  165. 'policy': 'LeZySEM Red.png',
  166. 'antrag': 'LeZyTOR Red.png',
  167. }
  168. })
  169. )
  170. # bind admin to company
  171. company_admin = UserToCompany(
  172. user=admin,
  173. company=company,
  174. roles=[admin_role, agent_role],
  175. )
  176. db.session.add(company)
  177. db.session.add(company_admin)
  178. organization = Company(
  179. name='Sample Organization',
  180. #displayed_name='',
  181. #email='',
  182. #phone='',
  183. #country='',
  184. #post_code='',
  185. #city='',
  186. #address='',
  187. )
  188. # bind admin to organization
  189. organization_admin = UserToCompany(
  190. user=admin,
  191. company=organization,
  192. roles=[admin_role],
  193. )
  194. # bind organization to company
  195. organization_structure = CompanyToCompany(
  196. parent=company,
  197. child=organization,
  198. attributes=json.dumps({
  199. 'policy': [
  200. 'admin',
  201. 'agent',
  202. ],
  203. 'antrag': [
  204. 'admin',
  205. ],
  206. }),
  207. )
  208. db.session.add(organization)
  209. db.session.add(organization_admin)
  210. db.session.add(organization_structure)
  211. try:
  212. db.session.commit()
  213. except Exception as e:
  214. print(f"Error during commit: {e}")
  215. pass
  216. print('Done.')