Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add model #4332

Merged
merged 8 commits into from
Jul 12, 2023
133 changes: 132 additions & 1 deletion webapp/admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import os
import json
import flask
from canonicalwebteam.store_api.exceptions import StoreApiResponseErrorList
from flask import make_response
from canonicalwebteam.store_api.exceptions import (
StoreApiResponseErrorList,
StoreApiResourceNotFound,
)
from canonicalwebteam.store_api.stores.snapstore import SnapStoreAdmin
from flask.json import jsonify
from webapp.decorators import login_required
Expand Down Expand Up @@ -226,3 +230,130 @@ def update_invite_status(store_id):
flask.flash(msg, "negative")

return jsonify(res)


# ---------------------- MODELS SERVICES ----------------------
@admin.route("/admin/store/<store_id>/models")
@login_required
def get_models(store_id):
"""
Retrieves models associated with a given store ID.

Args:
store_id (int): The ID of the store for which to retrieve models.

Returns:
dict: A dictionary containing the response message, success status,
and data.
"""
res = {}
try:
models = admin_api.get_store_models(flask.session, store_id)
res["success"] = True
res["data"] = models
response = make_response(res, 200)
response.cache_control.max_age = "3600"
return response
except StoreApiResponseErrorList as error_list:
error_messages = [
f"{error.get('message', 'An error occurred')}"
for error in error_list.errors
]
if "unauthorized" in error_messages:
res["message"] = "Store not found"
else:
res["message"] = " ".join(error_messages)
res["success"] = False

response = make_response(res, 500)


@admin.route("/admin/store/<store_id>/models", methods=["POST"])
@login_required
def create_models(store_id: str):
"""
Create a model for a given store.

Args:
store_id (str): The ID of the store.

Returns:
dict: A dictionary containing the response message and success
status.
"""

# TO DO: Addn validation that name does not exist already

res = {}

try:
name = flask.request.form.get("name")
api_key = flask.request.form.get("api_key", "")

if len(name) > 128:
res["message"] = "Name is too long. Limit 128 characters"
res["success"] = False
return jsonify(res)

if api_key and len(api_key) != 50 and not api_key.isalpha():
res["message"] = "Invalid API key"
res["success"] = False
return jsonify(res)

admin_api.create_store_model(flask.session, store_id, name, api_key)
res["success"] = True

return make_response(res, 201)
except StoreApiResponseErrorList as error_list:
res["success"] = False
messages = [
f"{error.get('message', 'An error occurred')}"
for error in error_list.errors
]
res["message"] = (" ").join(messages)

except Exception:
res["success"] = False
res["message"] = "An error occurred"

return make_response(res, 500)


@admin.route("/admin/store/<store_id>/models/<model_name>", methods=["PATCH"])
@login_required
def update_model(store_id: str, model_name: str):
"""
Update a model for a given store.

Args:
store_id (str): The ID of the store.
model_name (str): The name of the model.

Returns:
dict: A dictionary containing the response message and success
status.
"""
res = {}

try:
api_key = flask.request.form.get("api_key", "")

if len(api_key) != 50 and not api_key.isalpha():
res["message"] = "Invalid API key"
res["success"] = False
return jsonify(res)

admin_api.update_store_model(
flask.session, store_id, model_name, api_key
)
res["success"] = True

except StoreApiResponseErrorList as error_list:
res["success"] = False
res["message"] = error_list.errors[0]["message"]

except StoreApiResourceNotFound:
res["success"] = False
res["message"] = "Model not found"

return make_response(res, 200)