Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import absolute_import, unicode_literals
- from flask import Flask, session
- import os
- import logging.config
- import mongoengine
- from flask_celery import make_celery
- template_path = os.path.dirname(os.path.abspath(__file__)) + "/apis/templates"
- #print("Application name"+os.environ.get("APPLICATION_NAME"))
- #print(os.environ.get('DB_NAME'), os.environ.get('DB_HOST'))
- #application = Flask(os.environ.get("APPLICATION_NAME"), template_folder=template_path)
- application = Flask("flask-scaffolding", template_folder=template_path)
- SETTINGS_FILE = os.environ.get("SETTINGS_FILE", "settings.local_settings")
- application.config.from_object(SETTINGS_FILE)
- application.config.update(
- CELERY_BROKER_URL='redis://localhost:6379',
- CELERY_RESULT_BACKEND='redis://localhost:6379')
- celery = make_celery(application)
- with application.app_context():
- # this loads all the views with the app context
- # this is also helpful when the views import other
- # modules, this will load everything under the application
- # context and then one can use the current_app configuration
- # parameters
- from apis.urls import all_urls
- from scripts import ALL_CLI_COMMANDS
- for cli_name, cli_command in ALL_CLI_COMMANDS.items():
- application.cli.add_command(cli_command, name=cli_name)
- # Adding all the url rules in the api application
- for url, view, methods, _ in all_urls:
- application.add_url_rule(url, view_func=view, methods=methods)
- logging.config.dictConfig(application.config["LOGGING"])
- mongoengine.connect(
- alias='default',
- db=application.config['MONGO_SETTINGS']['DB_NAME'],
- host=application.config['MONGO_SETTINGS']['DB_HOST'],
- port=application.config['MONGO_SETTINGS']['DB_PORT'],
- username=application.config['MONGO_SETTINGS']['DB_USERNAME'],
- password=application.config['MONGO_SETTINGS']['DB_PASSWORD'],
- )
- from apis import views
- import sys
- import os
- sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
- import services.user_service as us
- from flask import session
- api_urls = [
- ("/", views.index, ["GET"], "flask scaffolding index url"),
- ("/login", views.login_page, ["GET"], "Generates login page html"),
- ("/login", us.UserService.login_user, ["POST"], "Logs the login data"),
- ("/signup", views.signup_page, ["GET"], "Generates the sign up page"),
- ("/signup", us.UserService.signup_user, ["POST"], "Logs the sign up details"), ("/logout", us.UserService.logout_user, ["POST"], "Clears the authentication"),
- ("/logout", views.logout_page, ["GET"], "Generates the logout page.")
- ]
- other_urls = []
- all_urls = api_urls + other_urls
- from app import celery
- @celery.task()
- def add_together(a, b):
- return a+b
- class UserService(object):
- def login_user():
- try :
- user_id = request.form['username']
- passwd = request.form['password']
- print(add_celery(23, 42))
- #Check for the user in the database
- connect('scaffolding')
- 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'))
- user_flag = False ; user_index = -1
- for index, _user in enumerate(User.objects):
- if (_user.user_id == user_id):
- user_flag = True; user_info = index
- if (user_flag == False):
- raise LookupError("User does not exist. Please sign in first.")
- #Compare the password with hash
- user_flag = bcrypt.checkpw(passwd, User.objects[index].passwd_hash)
- if (user_flag == False):
- raise AssertionError("Incorrect Password ")
- session['logged_in'] = True
- return "You are logged in!"
- except LookupError as err:
- return flask.abort(401, err.args[0])
- except AssertionError as err:
- return flask.abort(401, err.args[0])
- except AttributeError as err:
- return flask.abort(406, err.args[0])
- except Exception as err:
- return flask.abort(500, err.args[0])
- Traceback (most recent call last):
- File "app.py", line 50, in <module>
- from apis.urls import all_urls
- File "/src/apis/urls.py", line 5, in <module>
- import services.user_service as us
- File "/src/services/user_service.py", line 84, in <module>
- import app
- File "/src/app.py", line 50, in <module>
- from apis.urls import all_urls
- ImportError: cannot import name 'all_urls'
- def add_celery(a, b):
- @celery.task()
- def add_together(a, b):
- return a + b
- return add_together.delay(a, b)
- [2018-10-14 23:28:55,650: ERROR/MainProcess] Received unregistered task of type 'services.user_service.add_together'.
- The message has been ignored and discarded.
- Did you remember to import the module containing this task?
- Or maybe you're using relative imports?
- Please see
- http://docs.celeryq.org/en/latest/internals/protocol.html
- for more information.
- The full contents of the message body was:
- b'[[23, 42], {}, {"callbacks": null, "chord": null, "chain": null, "errbacks": null}]' (83b)
- Traceback (most recent call last):
- File "/usr/local/lib/python3.5/dist-packages/celery/worker/consumer/consumer.py", line 558, in on_task_received
- strategy = strategies[type_]
- KeyError: 'services.user_service.add_together'
- [2018-10-14 23:29:35,366: ERROR/MainProcess] Received unregistered task of type 'services.user_service.add_together'.
- The message has been ignored and discarded.
- Did you remember to import the module containing this task?
- Or maybe you're using relative imports?
- Please see
- http://docs.celeryq.org/en/latest/internals/protocol.html
- for more information.
- The full contents of the message body was:
- b'[[23, 42], {}, {"callbacks": null, "chord": null, "chain": null, "errbacks": null}]' (83b)
- Traceback (most recent call last):
- File "/usr/local/lib/python3.5/dist-packages/celery/worker/consumer/consumer.py", line 558, in on_task_received
- strategy = strategies[type_]
- KeyError: 'services.user_service.add_together'
Add Comment
Please, Sign In to add comment