populate_db.py 5.2 KB

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