117 lines
2.9 KiB
Go
117 lines
2.9 KiB
Go
package models
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"strconv"
|
|
|
|
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/models/utils"
|
|
. "git.andr3h3nriqu3s.com/andr3/fyp/logic/utils"
|
|
)
|
|
|
|
func deleteModel(handle *Handle, id string, w http.ResponseWriter, c *Context, model BaseModel) {
|
|
c.Logger.Warnf("Removing model with id: %s", id)
|
|
_, err := handle.Db.Exec("delete from models where id=$1;", id)
|
|
if err != nil {
|
|
c.Logger.Error(err)
|
|
panic("TODO handle better deleteModel failed delete database query")
|
|
}
|
|
|
|
model_path := path.Join("./savedData", id)
|
|
c.Logger.Warnf("Removing folder of model with id: %s at %s", id, model_path)
|
|
err = os.RemoveAll(model_path)
|
|
if err != nil {
|
|
c.Logger.Error(err)
|
|
panic("TODO handle better deleteModel failed to delete folder")
|
|
}
|
|
|
|
if c.Mode == HTML {
|
|
// TODO move this to a constant so i don't forget
|
|
w.WriteHeader(309)
|
|
c.Mode = HTMLFULL
|
|
}
|
|
|
|
LoadBasedOnAnswer(c.Mode, w, "/models/delete.html", c.AddMap(AnyMap{
|
|
"Model": model,
|
|
}))
|
|
}
|
|
|
|
func handleDelete(handle *Handle) {
|
|
handle.Delete("/models/delete", func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
|
|
if c.Mode == JSON {
|
|
panic("TODO handle json on models/delete")
|
|
}
|
|
|
|
// This is required to parse delete forms with bodies
|
|
f, err := MyParseForm(r)
|
|
if err != nil {
|
|
return c.ErrorCode(err, 400, nil)
|
|
}
|
|
|
|
if !CheckId(f, "id") {
|
|
return c.ErrorCode(nil, http.StatusNotFound, AnyMap{
|
|
"NotFoundMessage": "Model not found",
|
|
"GoBackLink": "/models",
|
|
})
|
|
}
|
|
|
|
id := f.Get("id")
|
|
|
|
// TODO handle admin users
|
|
rows, err := handle.Db.Query("select name, status from models where id=$1 and user_id=$2;", id, c.User.Id)
|
|
if err != nil {
|
|
return c.ErrorCode(err, http.StatusInternalServerError, nil)
|
|
}
|
|
defer rows.Close()
|
|
|
|
if !rows.Next() {
|
|
c.Logger.Warn("Could not find model for", id, c.User.Id)
|
|
return c.ErrorCode(nil, http.StatusNotFound, AnyMap{
|
|
"NotFoundMessage": "Model not found",
|
|
"GoBackLink": "/models",
|
|
})
|
|
}
|
|
|
|
model := BaseModel{}
|
|
model.Id = id
|
|
|
|
if err = rows.Scan(&model.Name, &model.Status); err != nil {
|
|
return c.Error500(err)
|
|
}
|
|
|
|
switch model.Status {
|
|
case FAILED_TRAINING:
|
|
fallthrough
|
|
case FAILED_PREPARING_TRAINING:
|
|
fallthrough
|
|
case FAILED_PREPARING:
|
|
deleteModel(handle, id, w, c, model)
|
|
return nil
|
|
case READY:
|
|
fallthrough
|
|
case CONFIRM_PRE_TRAINING:
|
|
if CheckEmpty(f, "name") {
|
|
return c.Error400(nil, "Name is empty", w, "/models/edit.html", "delete-model-card", AnyMap{
|
|
"NameDoesNotMatch": true,
|
|
"Model": model,
|
|
})
|
|
}
|
|
|
|
name := f.Get("name")
|
|
if name != model.Name {
|
|
LoadDefineTemplate(w, "/models/edit.html", "delete-model-card", c.AddMap(AnyMap{
|
|
"NameDoesNotMatch": true,
|
|
"Model": model,
|
|
}))
|
|
return nil
|
|
}
|
|
|
|
deleteModel(handle, id, w, c, model)
|
|
return nil
|
|
default:
|
|
panic("Do not know how to handle model in status:" + strconv.Itoa(model.Status))
|
|
}
|
|
})
|
|
}
|