applications
This commit is contained in:
1
app.py
1
app.py
@@ -29,6 +29,7 @@ blueprints = [
|
|||||||
(routes.printing_blueprint, '/printing'),
|
(routes.printing_blueprint, '/printing'),
|
||||||
(routes.assembly_blueprint, '/assembly'),
|
(routes.assembly_blueprint, '/assembly'),
|
||||||
(routes.general_blueprint, '/general'),
|
(routes.general_blueprint, '/general'),
|
||||||
|
(routes.application_blueprint, '/application'),
|
||||||
]
|
]
|
||||||
|
|
||||||
for blueprint, url_prefix in blueprints:
|
for blueprint, url_prefix in blueprints:
|
||||||
|
|||||||
@@ -7,3 +7,7 @@ APP_PATH = os.path.dirname(sys.executable) if getattr(sys, 'frozen', False) else
|
|||||||
LOGGER_NAME = 'assemblr'
|
LOGGER_NAME = 'assemblr'
|
||||||
LOG_FILE = Path(APP_PATH) / Path(f'{LOGGER_NAME}.log')
|
LOG_FILE = Path(APP_PATH) / Path(f'{LOGGER_NAME}.log')
|
||||||
MAX_LOG_FILE_SIZE_BYTES = 400 * 1024 ** 2
|
MAX_LOG_FILE_SIZE_BYTES = 400 * 1024 ** 2
|
||||||
|
APKS_PATH = os.path.join(APP_PATH, 'apks')
|
||||||
|
|
||||||
|
if not os.path.isdir(APKS_PATH):
|
||||||
|
os.mkdir(APKS_PATH)
|
||||||
|
|||||||
@@ -33,3 +33,12 @@ class Barcode(db.Model):
|
|||||||
id = db.Column(db.Integer, primary_key=True, comment='ID пользователя')
|
id = db.Column(db.Integer, primary_key=True, comment='ID пользователя')
|
||||||
denco_article = db.Column(db.Integer, nullable=False, comment='Артикул', index=True)
|
denco_article = db.Column(db.Integer, nullable=False, comment='Артикул', index=True)
|
||||||
barcode = db.Column(db.String, nullable=False, comment='Баркод', index=True)
|
barcode = db.Column(db.String, nullable=False, comment='Баркод', index=True)
|
||||||
|
|
||||||
|
|
||||||
|
class Application(db.Model):
|
||||||
|
__tablename__ = 'applications'
|
||||||
|
id = db.Column(db.Integer, primary_key=True, comment='')
|
||||||
|
name = db.Column(db.String(40), nullable=False)
|
||||||
|
version = db.Column(db.String(10), nullable=False)
|
||||||
|
uploaded = db.Column(db.DateTime, nullable=False)
|
||||||
|
filename = db.Column(db.String, nullable=False)
|
||||||
|
|||||||
@@ -4,3 +4,4 @@ from routes.barcode import barcode_blueprint
|
|||||||
from routes.printing import printing_blueprint
|
from routes.printing import printing_blueprint
|
||||||
from routes.assembly import assembly_blueprint
|
from routes.assembly import assembly_blueprint
|
||||||
from routes.general import general_blueprint
|
from routes.general import general_blueprint
|
||||||
|
from routes.application import application_blueprint
|
||||||
|
|||||||
61
routes/application.py
Normal file
61
routes/application.py
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import datetime
|
||||||
|
import os.path
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from flask import Blueprint, request
|
||||||
|
|
||||||
|
import database
|
||||||
|
from constants import APKS_PATH
|
||||||
|
|
||||||
|
application_blueprint = Blueprint('application', __name__)
|
||||||
|
|
||||||
|
|
||||||
|
@application_blueprint.before_request
|
||||||
|
def auth():
|
||||||
|
API_KEY = 'AF9A20DD9264C134CDA0ADACED834368'
|
||||||
|
if request.headers.get('Authorization') != API_KEY:
|
||||||
|
return {'error': 'Unauthorized'}, 401
|
||||||
|
|
||||||
|
|
||||||
|
@application_blueprint.get('<string:application_name>/download/<string:version>')
|
||||||
|
def download_version(application_name: str, version: str):
|
||||||
|
application = database.Application.query.filter_by(version=version,
|
||||||
|
name=application_name).first()
|
||||||
|
if not application:
|
||||||
|
return {
|
||||||
|
"error": f"Application version '{version}' not found."
|
||||||
|
}, 404
|
||||||
|
|
||||||
|
|
||||||
|
@application_blueprint.get('<string:application_name>/version')
|
||||||
|
def get_version(application_name: str):
|
||||||
|
version = (database.Application.query.
|
||||||
|
filter_by(name=application_name).
|
||||||
|
order_by(database.Application.uploaded.desc()).
|
||||||
|
with_entities(database.Application.version).
|
||||||
|
limit(1).
|
||||||
|
scalar())
|
||||||
|
return {'latest_version': version}
|
||||||
|
|
||||||
|
|
||||||
|
@application_blueprint.post('<string:application_name>/upload')
|
||||||
|
def upload(application_name: str):
|
||||||
|
file = request.files.get('file')
|
||||||
|
version = request.form.get('version')
|
||||||
|
if (not file) or (not version):
|
||||||
|
return {"error": "Invalid form data. There is no file or version field"}, 400
|
||||||
|
version = version.strip()
|
||||||
|
application = (database.Application.query.
|
||||||
|
filter_by(version=version).
|
||||||
|
with_entities(database.Application.id).first())
|
||||||
|
if application:
|
||||||
|
return {"error": f"Specified version ({version}) already uploaded"}, 400
|
||||||
|
filename = uuid.uuid4().hex + '.apk'
|
||||||
|
file.save(os.path.join(APKS_PATH, filename))
|
||||||
|
application = database.Application(name=application_name,
|
||||||
|
version=version,
|
||||||
|
uploaded=datetime.datetime.now(),
|
||||||
|
filename=filename)
|
||||||
|
database.db.session.add(application)
|
||||||
|
database.db.session.commit()
|
||||||
|
return {'filename': filename, 'version': version}
|
||||||
Reference in New Issue
Block a user