2017-09-12 00:48:13 -06:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-09-12 00:48:13 -06:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-05-08 07:46:34 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 03:37:59 -06:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2017-09-12 00:48:13 -06:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-02-15 09:50:10 -07:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 08:36:53 -07:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-04-06 13:44:05 -06:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2017-09-12 00:48:13 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddTimeManually tracks time manually
|
2021-01-26 08:36:53 -07:00
|
|
|
func AddTimeManually(c *context.Context) {
|
2021-02-19 03:52:11 -07:00
|
|
|
form := web.GetForm(c).(*forms.AddTimeManuallyForm)
|
2017-10-16 01:55:43 -06:00
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
2023-10-14 02:37:24 -06:00
|
|
|
if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
|
2018-01-10 14:34:17 -07:00
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
2017-09-12 00:48:13 -06:00
|
|
|
return
|
|
|
|
}
|
2023-02-10 23:34:11 -07:00
|
|
|
url := issue.Link()
|
2017-09-12 00:48:13 -06:00
|
|
|
|
|
|
|
if c.HasError() {
|
|
|
|
c.Flash.Error(c.GetErrMsg())
|
|
|
|
c.Redirect(url)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
total := time.Duration(form.Hours)*time.Hour + time.Duration(form.Minutes)*time.Minute
|
|
|
|
|
|
|
|
if total <= 0 {
|
|
|
|
c.Flash.Error(c.Tr("repo.issues.add_time_sum_to_small"))
|
|
|
|
c.Redirect(url, http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-22 08:14:27 -06:00
|
|
|
if _, err := issues_model.AddTime(c, c.Doer, issue, int64(total.Seconds()), time.Now()); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
c.ServerError("AddTime", err)
|
2017-09-12 00:48:13 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Redirect(url, http.StatusSeeOther)
|
|
|
|
}
|
2021-02-19 03:52:11 -07:00
|
|
|
|
|
|
|
// DeleteTime deletes tracked time
|
|
|
|
func DeleteTime(c *context.Context) {
|
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
2023-10-14 02:37:24 -06:00
|
|
|
if !c.Repo.CanUseTimetracker(c, issue, c.Doer) {
|
2021-02-19 03:52:11 -07:00
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-03 04:30:41 -06:00
|
|
|
t, err := issues_model.GetTrackedTimeByID(c, c.ParamsInt64(":timeid"))
|
2021-02-19 03:52:11 -07:00
|
|
|
if err != nil {
|
2022-05-08 07:46:34 -06:00
|
|
|
if db.IsErrNotExist(err) {
|
2021-02-19 03:52:11 -07:00
|
|
|
c.NotFound("time not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Error(http.StatusInternalServerError, "GetTrackedTimeByID", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// only OP or admin may delete
|
2022-03-22 01:03:22 -06:00
|
|
|
if !c.IsSigned || (!c.IsUserSiteAdmin() && c.Doer.ID != t.UserID) {
|
2021-02-19 03:52:11 -07:00
|
|
|
c.Error(http.StatusForbidden, "not allowed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-03 04:30:41 -06:00
|
|
|
if err = issues_model.DeleteTime(c, t); err != nil {
|
2021-02-19 03:52:11 -07:00
|
|
|
c.ServerError("DeleteTime", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-15 09:50:10 -07:00
|
|
|
c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToTime(t.Time)))
|
2023-02-10 23:34:11 -07:00
|
|
|
c.Redirect(issue.Link())
|
2021-02-19 03:52:11 -07:00
|
|
|
}
|