Guest User

Untitled

a guest
Oct 14th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. from __future__ import absolute_import, unicode_literals
  2. from flask import Flask, session
  3. import os
  4. import logging.config
  5. import mongoengine
  6. from flask_celery import make_celery
  7.  
  8. template_path = os.path.dirname(os.path.abspath(__file__)) + "/apis/templates"
  9. #print("Application name"+os.environ.get("APPLICATION_NAME"))
  10. #print(os.environ.get('DB_NAME'), os.environ.get('DB_HOST'))
  11. #application = Flask(os.environ.get("APPLICATION_NAME"), template_folder=template_path)
  12. application = Flask("flask-scaffolding", template_folder=template_path)
  13. SETTINGS_FILE = os.environ.get("SETTINGS_FILE", "settings.local_settings")
  14.  
  15. application.config.from_object(SETTINGS_FILE)
  16.  
  17. application.config.update(
  18. CELERY_BROKER_URL='redis://localhost:6379',
  19. CELERY_RESULT_BACKEND='redis://localhost:6379')
  20. celery = make_celery(application)
  21.  
  22. with application.app_context():
  23. # this loads all the views with the app context
  24. # this is also helpful when the views import other
  25. # modules, this will load everything under the application
  26. # context and then one can use the current_app configuration
  27. # parameters
  28. from apis.urls import all_urls
  29. from scripts import ALL_CLI_COMMANDS
  30.  
  31. for cli_name, cli_command in ALL_CLI_COMMANDS.items():
  32. application.cli.add_command(cli_command, name=cli_name)
  33.  
  34.  
  35. # Adding all the url rules in the api application
  36. for url, view, methods, _ in all_urls:
  37. application.add_url_rule(url, view_func=view, methods=methods)
  38.  
  39.  
  40. logging.config.dictConfig(application.config["LOGGING"])
  41.  
  42. mongoengine.connect(
  43. alias='default',
  44. db=application.config['MONGO_SETTINGS']['DB_NAME'],
  45. host=application.config['MONGO_SETTINGS']['DB_HOST'],
  46. port=application.config['MONGO_SETTINGS']['DB_PORT'],
  47. username=application.config['MONGO_SETTINGS']['DB_USERNAME'],
  48. password=application.config['MONGO_SETTINGS']['DB_PASSWORD'],
  49. )
  50.  
  51. from apis import views
  52. import sys
  53. import os
  54. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
  55. import services.user_service as us
  56. from flask import session
  57.  
  58.  
  59. api_urls = [
  60. ("/", views.index, ["GET"], "flask scaffolding index url"),
  61. ("/login", views.login_page, ["GET"], "Generates login page html"),
  62. ("/login", us.UserService.login_user, ["POST"], "Logs the login data"),
  63. ("/signup", views.signup_page, ["GET"], "Generates the sign up page"),
  64. ("/signup", us.UserService.signup_user, ["POST"], "Logs the sign up details"), ("/logout", us.UserService.logout_user, ["POST"], "Clears the authentication"),
  65. ("/logout", views.logout_page, ["GET"], "Generates the logout page.")
  66. ]
  67.  
  68. other_urls = []
  69.  
  70. all_urls = api_urls + other_urls
  71.  
  72. from app import celery
  73.  
  74. @celery.task()
  75. def add_together(a, b):
  76. return a+b
  77.  
  78.  
  79.  
  80. class UserService(object):
  81.  
  82. def login_user():
  83. try :
  84. user_id = request.form['username']
  85. passwd = request.form['password']
  86. print(add_celery(23, 42))
  87. #Check for the user in the database
  88. connect('scaffolding')
  89. print(os.environ.get('DB_NAME'), os.environ.get('DB_HOST'), int(os.environ.get('DB_PORT', 27017)), os.environ.get('DB_USERNAME'), os.environ.get('DB_PASSWORD'))
  90. user_flag = False ; user_index = -1
  91. for index, _user in enumerate(User.objects):
  92. if (_user.user_id == user_id):
  93. user_flag = True; user_info = index
  94.  
  95. if (user_flag == False):
  96. raise LookupError("User does not exist. Please sign in first.")
  97. #Compare the password with hash
  98. user_flag = bcrypt.checkpw(passwd, User.objects[index].passwd_hash)
  99. if (user_flag == False):
  100. raise AssertionError("Incorrect Password ")
  101.  
  102. session['logged_in'] = True
  103.  
  104. return "You are logged in!"
  105. except LookupError as err:
  106. return flask.abort(401, err.args[0])
  107. except AssertionError as err:
  108. return flask.abort(401, err.args[0])
  109. except AttributeError as err:
  110. return flask.abort(406, err.args[0])
  111. except Exception as err:
  112. return flask.abort(500, err.args[0])
  113.  
  114. Traceback (most recent call last):
  115. File "app.py", line 50, in <module>
  116. from apis.urls import all_urls
  117. File "/src/apis/urls.py", line 5, in <module>
  118. import services.user_service as us
  119. File "/src/services/user_service.py", line 84, in <module>
  120. import app
  121. File "/src/app.py", line 50, in <module>
  122. from apis.urls import all_urls
  123. ImportError: cannot import name 'all_urls'
  124.  
  125. def add_celery(a, b):
  126. @celery.task()
  127. def add_together(a, b):
  128. return a + b
  129. return add_together.delay(a, b)
  130.  
  131. [2018-10-14 23:28:55,650: ERROR/MainProcess] Received unregistered task of type 'services.user_service.add_together'.
  132. The message has been ignored and discarded.
  133.  
  134. Did you remember to import the module containing this task?
  135. Or maybe you're using relative imports?
  136.  
  137. Please see
  138. http://docs.celeryq.org/en/latest/internals/protocol.html
  139. for more information.
  140.  
  141. The full contents of the message body was:
  142. b'[[23, 42], {}, {"callbacks": null, "chord": null, "chain": null, "errbacks": null}]' (83b)
  143. Traceback (most recent call last):
  144. File "/usr/local/lib/python3.5/dist-packages/celery/worker/consumer/consumer.py", line 558, in on_task_received
  145. strategy = strategies[type_]
  146. KeyError: 'services.user_service.add_together'
  147. [2018-10-14 23:29:35,366: ERROR/MainProcess] Received unregistered task of type 'services.user_service.add_together'.
  148. The message has been ignored and discarded.
  149.  
  150. Did you remember to import the module containing this task?
  151. Or maybe you're using relative imports?
  152.  
  153. Please see
  154. http://docs.celeryq.org/en/latest/internals/protocol.html
  155. for more information.
  156.  
  157. The full contents of the message body was:
  158. b'[[23, 42], {}, {"callbacks": null, "chord": null, "chain": null, "errbacks": null}]' (83b)
  159. Traceback (most recent call last):
  160. File "/usr/local/lib/python3.5/dist-packages/celery/worker/consumer/consumer.py", line 558, in on_task_received
  161. strategy = strategies[type_]
  162. KeyError: 'services.user_service.add_together'
Add Comment
Please, Sign In to add comment