views.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # django imports
  2. from django.shortcuts import render
  3. from django.http import HttpResponse
  4. from django.conf import settings
  5. from django.shortcuts import redirect
  6. from django.contrib.auth.decorators import login_required
  7. from django.http import Http404
  8. # main imports
  9. import os
  10. import json
  11. import base64
  12. # db imports
  13. from pymongo import MongoClient
  14. # generate mongo db connection and let access to collection
  15. connection = MongoClient()
  16. db = connection['sin3d']
  17. contributors_collection = db['sin3d-contributors']
  18. configurations_collection = db['sin3d-configuration']
  19. def encode_data(data):
  20. json_data = json.dumps(data)
  21. link_data = base64.b64encode(str(json_data).encode('utf-8'))
  22. return link_data
  23. def generate_link(data):
  24. # generate custom link
  25. generated_link_info = encode_data(data)
  26. generated_link = data['hostConfig'] + '/#/?q=' + bytes(generated_link_info).decode("utf-8")
  27. return generated_link
  28. def get_base_data(expe_name=None):
  29. '''
  30. Used to store default data to send for each view
  31. '''
  32. data = {}
  33. data['BASE'] = settings.WEB_PREFIX_URL
  34. return data
  35. def load_index(request):
  36. '''
  37. Prepare data information for main page
  38. - experiment identifiers available from configuration
  39. '''
  40. data = get_base_data()
  41. # get all configurations
  42. configurations = configurations_collection.find()
  43. data['configurations'] = {}
  44. for config in configurations:
  45. guild_id = config['guild_id']
  46. experiment_id = config['config']['experimentId']
  47. data['configurations'][guild_id] = experiment_id
  48. return render(request, 'links/index.html', data)
  49. def check_user_id(request):
  50. data = get_base_data()
  51. data['status'] = True
  52. data['message'] = None
  53. if request.method == 'POST':
  54. form_input = json.loads(request.body)
  55. guild_id = form_input['guildId']
  56. user_id = form_input['userId']
  57. # check guild id
  58. if len(user_id) > 5:
  59. guild = configurations_collection.find_one({'guild_id': int(guild_id)})
  60. if ' ' in user_id:
  61. data['message'] = 'Space character is not authorized'
  62. data['status'] = False
  63. elif guild is None:
  64. data['message'] = 'Experiment identifier does not exist'
  65. data['status'] = False
  66. else:
  67. user_account = contributors_collection.find_one({'guild_id': int(guild_id), 'user_id': user_id})
  68. if user_account is not None:
  69. # create user link
  70. data['message'] = 'User identifier already used'
  71. data['status'] = False
  72. else:
  73. # user already exists
  74. data['message'] = 'Valid user identifier'
  75. data['status'] = True
  76. else:
  77. data['message'] = 'User identifier need at least 6 characters'
  78. data['status'] = False
  79. return HttpResponse(json.dumps(data), content_type='application/json')
  80. def generate_user_link(request):
  81. '''
  82. Generate link is possible for user, otherwise send an issue with error code
  83. '''
  84. data = get_base_data()
  85. data['status'] = True
  86. data['message'] = None
  87. # create link and add it to local storage of navigator
  88. if request.method == 'POST':
  89. form_input = json.loads(request.body)
  90. guild_id = form_input['guildId']
  91. user_id = form_input['userId']
  92. if len(user_id) > 5 and ' ' not in user_id:
  93. guild_config = configurations_collection.find_one({'guild_id': int(guild_id)})
  94. if guild_config is not None:
  95. user_account = contributors_collection.find_one({'guild_id': int(guild_id), 'user_id': user_id})
  96. print(user_account)
  97. if user_account is None:
  98. # we can create link and save it
  99. user_config = guild_config['config']
  100. user_config['userId'] = user_id
  101. contributors_collection.insert_one({
  102. 'user_id': user_id,
  103. 'username': user_id,
  104. 'guild_id': guild_config['guild_id'],
  105. 'guild_name': guild_config['guild_name'],
  106. 'discord': False,
  107. 'config': user_config
  108. })
  109. data['config'] = user_config
  110. data['link'] = generate_link(user_config)
  111. data['status'] = True
  112. data['message'] = 'Your experiment link was generated'
  113. else:
  114. data['message'] = 'Experiment link already generated'
  115. else:
  116. data['message'] = 'Invalid experiment id or user id'
  117. return HttpResponse(json.dumps(data), content_type='application/json')