-
- #app/__init__.py
- from config import CELERY_BROKER_URL
-
- celery = Celery(__name__, broker=CELERY_BROKER_URL)
-
-
- def create_app(config_filename):
- app = Flask(__name__, static_folder='templates/static')
- app.config.from_object(config_filename)
-
- # Init Flask-SQLAlchemy
- from app.basemodels import db
- db.init_app(app)
-
- from app.users.views import users
- app.register_blueprint(users, url_prefix='/api/v1/users')
-
- from app.baseviews import login_required, login1, mail
- from flask import render_template, send_from_directory
- import os
-
- # Init Flask-Mail
- mail.init_app(app)
-
- celery.conf.update(app.config)
-
- @app.route('/')
- def index():
- return render_template('index.html')
-
- return app
-
-
- #app/celeries/views.py
-
- from app import celery
-
- @celery.task
- def send_async_email(cel):
- """Background task to send an email with Flask-Mail."""
- with app.app_context():
- print("Adding")
- cel.add(cel)
-
-
- class CreateListCeleries(Resource):
- """http://jsonapi.org/format/#fetching
- A server MUST respond to a successful request to fetch an individual resource or resource collection with a 200 OK response.
- A server MUST respond with 404 Not Found when processing a request to fetch a single resource that does not exist, except when the request warrants a 200 OK response with null as the primary data (as described above)
- a self link as part of the top-level links object"""
-
- def get(self):
- celeries_query = Celeries.query.all()
- results = schema.dump(celeries_query, many=True).data
- return results
-
- """http://jsonapi.org/format/#crud
- A resource can be created by sending a POST request to a URL that represents a collection of celeries. The request MUST include a single resource object as primary data. The resource object MUST contain at least a type member.
- If a POST request did not include a Client-Generated ID and the requested resource has been created successfully, the server MUST return a 201 Created status code"""
-
- def post(self):
- raw_dict = request.get_json(force=True)
- try:
- schema.validate(raw_dict)
- request_dict = raw_dict['data']['attributes']
- cel = Celeries(
- request_dict['hostname'], request_dict['ipaddress'],)
-
-
-
- send_async_email.apply_async(args=[cel], countdown=30)
- resp = jsonify({"error": "success"})
- resp.status_code = 201
- return resp
-
-
-
- except ValidationError as err:
- resp = jsonify({"error": err.messages})
- resp.status_code = 403
- return resp
-
- except SQLAlchemyError as e:
- db.session.rollback()
- resp = jsonify({"error": str(e)})
- resp.status_code = 403
- return resp
-
-
-
-
- api.add_resource(CreateListCeleries, '.json')
-