--- app/api/__init__.py 2023-02-12 14:43:36.430334 +0000 +++ app/api/__init__.py 2023-02-12 14:43:39.830360 +0000 @@ -1,2 +1 @@ -__version__ = '0.1.0' - +__version__ = "0.1.0" --- app/api/migrations/env.py 2023-02-12 14:43:36.430334 +0000 +++ app/api/migrations/env.py 2023-02-12 14:43:39.926810 +0000 @@ -12,21 +12,21 @@ config = context.config # Interpret the config file for Python logging. # This line sets up loggers basically. fileConfig(config.config_file_name) -logger = logging.getLogger('alembic.env') +logger = logging.getLogger("alembic.env") # add your model's MetaData object here # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata config.set_main_option( - 'sqlalchemy.url', - str(current_app.extensions['migrate'].db.get_engine().url).replace( - '%', '%%')) -target_metadata = current_app.extensions['migrate'].db.metadata + "sqlalchemy.url", + str(current_app.extensions["migrate"].db.get_engine().url).replace("%", "%%"), +) +target_metadata = current_app.extensions["migrate"].db.metadata # other values from the config, defined by the needs of env.py, # can be acquired: # my_important_option = config.get_main_option("my_important_option") # ... etc. @@ -43,13 +43,11 @@ Calls to context.execute() here emit the given string to the script output. """ url = config.get_main_option("sqlalchemy.url") - context.configure( - url=url, target_metadata=target_metadata, literal_binds=True - ) + context.configure(url=url, target_metadata=target_metadata, literal_binds=True) with context.begin_transaction(): context.run_migrations() @@ -63,24 +61,24 @@ # this callback is used to prevent an auto-migration from being generated # when there are no changes to the schema # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html def process_revision_directives(context, revision, directives): - if getattr(config.cmd_opts, 'autogenerate', False): + if getattr(config.cmd_opts, "autogenerate", False): script = directives[0] if script.upgrade_ops.is_empty(): directives[:] = [] - logger.info('No changes in schema detected.') + logger.info("No changes in schema detected.") - connectable = current_app.extensions['migrate'].db.get_engine() + connectable = current_app.extensions["migrate"].db.get_engine() with connectable.connect() as connection: context.configure( connection=connection, target_metadata=target_metadata, process_revision_directives=process_revision_directives, - **current_app.extensions['migrate'].configure_args + **current_app.extensions["migrate"].configure_args ) with context.begin_transaction(): context.run_migrations() --- app/api/app.py 2023-02-12 14:43:36.430334 +0000 +++ app/api/app.py 2023-02-12 14:43:39.944973 +0000 @@ -15,19 +15,21 @@ from datetime import datetime # Define app app = Flask(__name__) -env_settings_file = os.environ.get('SETTINGS_FILE', 'development') -app.logger.warning(F"Overriding base configuration with {env_settings_file}.py") -app.config.from_pyfile(F"settings/base.py") -app.config.from_pyfile(F"settings/{env_settings_file}.py") +env_settings_file = os.environ.get("SETTINGS_FILE", "development") +app.logger.warning(f"Overriding base configuration with {env_settings_file}.py") +app.config.from_pyfile(f"settings/base.py") +app.config.from_pyfile(f"settings/{env_settings_file}.py") # Cross-Origins features CORS(app) -app.logger.warning(F"CORS Allowed domains : {app.config['ALLOWED_CORS_ORIGINS']}") -cors = CORS(app, resources={ r"/*": {"origins": F"{app.config['ALLOWED_CORS_ORIGINS']}"} }) +app.logger.warning(f"CORS Allowed domains : {app.config['ALLOWED_CORS_ORIGINS']}") +cors = CORS( + app, resources={r"/*": {"origins": f"{app.config['ALLOWED_CORS_ORIGINS']}"}} +) # Init ORM modules db = SQLAlchemy(app) migrate = Migrate(app, db) @@ -44,98 +46,97 @@ longitude : compris entre 180 et -180, 0 à Greenwich element_name : Nom de l'objet a marquer comment : Non utilisé date : Date de la marque """ - id = db.Column('point_id', db.Integer, primary_key = True) + + id = db.Column("point_id", db.Integer, primary_key=True) element_name = db.Column(db.String(128), nullable=False) longitude = db.Column(db.Float, nullable=False) latitude = db.Column(db.Float, nullable=False) comment = db.Column(db.String(128)) - date = db.Column(db.DateTime, - default=datetime.utcnow, - onupdate=datetime.utcnow, - nullable=False) + date = db.Column( + db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False + ) + class PointSchema(ma.Schema): class Meta: - fields = ('id', 'element_name', 'longitude', 'latitude', 'comment', 'date') + fields = ("id", "element_name", "longitude", "latitude", "comment", "date") class PointListResource(Resource): def get(self): points = Point.query.all() return points_schema.dump(points) - + def post(self): - #import pdb - #pdb.set_trace() - + # import pdb + # pdb.set_trace() + app.logger.warning("POST") - date = request.form.get('date') + date = request.form.get("date") if date: try: - date=datetime.fromisoformat(request.form.get('date')) + date = datetime.fromisoformat(request.form.get("date")) except Exception as e: app.logger.warning(e) - return F'KeyError! ({e})', 400 + return f"KeyError! ({e})", 400 # Serialize new point ! try: new_point = Point( - element_name=request.form['element_name'], - latitude=request.form['latitude'], - longitude=request.form['longitude'], - comment=request.form.get('comment'), - date=date - + element_name=request.form["element_name"], + latitude=request.form["latitude"], + longitude=request.form["longitude"], + comment=request.form.get("comment"), + date=date, ) except KeyError as e: app.logger.warning(e) - return F'KeyError! ({e})', 400 + return f"KeyError! ({e})", 400 except Exception as e: app.logger.warning(e) - return F'Bad request ! ({e})', 400 + return f"Bad request ! ({e})", 400 # Storing date ton try: db.session.add(new_point) db.session.commit() except Exception as e: app.logger.warning(e) - return 'failed to store in database!', 500 + return "failed to store in database!", 500 return point_schema.dump(new_point) + class PointResource(Resource): def get(self, point_id): point = Point.query.get_or_404(point_id) return point_schema.dump(point) - + def delete(self, point_id): point = Point.query.get_or_404(point_id) db.session.delete(point) db.session.commit() - return '', 204 - + return "", 204 point_schema = PointSchema() points_schema = PointSchema(many=True) -api.add_resource(PointListResource, '/api/points/') -api.add_resource(PointResource, '/api/points//') +api.add_resource(PointListResource, "/api/points/") +api.add_resource(PointResource, "/api/points//") -@app.route('/geojson') +@app.route("/geojson") def geojson_view(): queryset = Point.query.all() catalog = {} attributes = {} for p in queryset: - url = F"{request.url_root}/api/points/{p.id}/" + url = f"{request.url_root}/api/points/{p.id}/" if p.element_name not in catalog.keys(): - catalog[p.element_name] = [(p.longitude, p.latitude)] - attributes[p.element_name] = { "url": url, - "name": p.element_name } + catalog[p.element_name] = [(p.longitude, p.latitude)] + attributes[p.element_name] = {"url": url, "name": p.element_name} # Sinon, ajout du point à la clef else: catalog[p.element_name].append((p.longitude, p.latitude)) # Transformation du catalog en objet GeoJSON @@ -150,20 +151,18 @@ point = geojson.Point(e[0]) # Add trajectory feature = geojson.Feature(geometry=line) elements.append(feature) # Add marker for first position - feature = geojson.Feature(geometry=point, - properties=attributes[key]) + feature = geojson.Feature(geometry=point, properties=attributes[key]) elements.append(feature) else: - # Cas d'un élement + # Cas d'un élement point = geojson.Point(e[0]) - feature = geojson.Feature(geometry=point, - properties=attributes[key]) + feature = geojson.Feature(geometry=point, properties=attributes[key]) elements.append(feature) geo_collection = geojson.FeatureCollection(elements) return geo_collection if __name__ == "__main__": - app.run(host='0.0.0.0') + app.run(host="0.0.0.0") --- app/api/migrations/versions/dca1b25755c9_.py 2023-02-12 14:43:36.430334 +0000 +++ app/api/migrations/versions/dca1b25755c9_.py 2023-02-12 14:43:39.965377 +0000 @@ -8,29 +8,30 @@ from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = 'dca1b25755c9' +revision = "dca1b25755c9" down_revision = None branch_labels = None depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### - op.create_table('point', - sa.Column('point_id', sa.Integer(), nullable=False), - sa.Column('element_name', sa.String(length=128), nullable=False), - sa.Column('longitude', sa.Float(), nullable=False), - sa.Column('latitude', sa.Float(), nullable=False), - sa.Column('comment', sa.String(length=128), nullable=True), - sa.Column('date', sa.DateTime(), nullable=False), - sa.PrimaryKeyConstraint('point_id') + op.create_table( + "point", + sa.Column("point_id", sa.Integer(), nullable=False), + sa.Column("element_name", sa.String(length=128), nullable=False), + sa.Column("longitude", sa.Float(), nullable=False), + sa.Column("latitude", sa.Float(), nullable=False), + sa.Column("comment", sa.String(length=128), nullable=True), + sa.Column("date", sa.DateTime(), nullable=False), + sa.PrimaryKeyConstraint("point_id"), ) # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('point') + op.drop_table("point") # ### end Alembic commands ### --- app/api/settings/base.py 2023-02-12 14:43:36.430334 +0000 +++ app/api/settings/base.py 2023-02-12 14:43:39.967015 +0000 @@ -1 +1 @@ -ALLOWED_CORS_ORIGINS = "*" \ No newline at end of file +ALLOWED_CORS_ORIGINS = "*" --- app/api/settings/development.py 2023-02-12 14:43:36.430334 +0000 +++ app/api/settings/development.py 2023-02-12 14:43:39.971352 +0000 @@ -1,2 +1,2 @@ -SECRET_KEY = 'testseckey' -SQLALCHEMY_DATABASE_URI = "sqlite:////tmp/foo.db" \ No newline at end of file +SECRET_KEY = "testseckey" +SQLALCHEMY_DATABASE_URI = "sqlite:////tmp/foo.db" --- app/api/settings/production.py 2023-02-12 14:43:36.430334 +0000 +++ app/api/settings/production.py 2023-02-12 14:43:39.984114 +0000 @@ -1,10 +1,12 @@ import os -dbuser = os.environ.get('VAPOR_DBUSER', 'user_vapormap') -password = os.environ.get('VAPOR_DBPASS', 'vapormap') -dbhost = os.environ.get('VAPOR_DBHOST', 'localhost') -dbname = os.environ.get('VAPOR_DBNAME', 'db_vapormap') +dbuser = os.environ.get("VAPOR_DBUSER", "user_vapormap") +password = os.environ.get("VAPOR_DBPASS", "vapormap") +dbhost = os.environ.get("VAPOR_DBHOST", "localhost") +dbname = os.environ.get("VAPOR_DBNAME", "db_vapormap") -SECRET_KEY = os.environ.get('VAPOR_SECRET_KEY', 'gjhliksqsdfghsdgfbhsdkjgnlkdsfj:nglbksjdhnbk') +SECRET_KEY = os.environ.get( + "VAPOR_SECRET_KEY", "gjhliksqsdfghsdgfbhsdkjgnlkdsfj:nglbksjdhnbk" +) -SQLALCHEMY_DATABASE_URI = F'mysql://{dbuser}:{password}@{dbhost}/{dbname}' \ No newline at end of file +SQLALCHEMY_DATABASE_URI = f"mysql://{dbuser}:{password}@{dbhost}/{dbname}"