2017-09-12 12:18:13 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-05-08 19:16:34 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2017-09-12 12:18:13 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-02-15 22:20:10 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-04-07 01:14:05 +05:30
|
|
|
"code.gitea.io/gitea/services/forms"
|
2017-09-12 12:18:13 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// AddTimeManually tracks time manually
|
2021-01-26 21:06:53 +05:30
|
|
|
func AddTimeManually(c *context.Context) {
|
2021-02-19 16:22:11 +05:30
|
|
|
form := web.GetForm(c).(*forms.AddTimeManuallyForm)
|
2017-10-16 13:25:43 +05:30
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 12:33:22 +05:30
|
|
|
if !c.Repo.CanUseTimetracker(issue, c.Doer) {
|
2018-01-11 03:04:17 +05:30
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
2017-09-12 12:18:13 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
url := issue.HTMLURL()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
if _, err := issues_model.AddTime(c.Doer, issue, int64(total.Seconds()), time.Now()); err != nil {
|
2018-01-11 03:04:17 +05:30
|
|
|
c.ServerError("AddTime", err)
|
2017-09-12 12:18:13 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Redirect(url, http.StatusSeeOther)
|
|
|
|
}
|
2021-02-19 16:22:11 +05:30
|
|
|
|
|
|
|
// DeleteTime deletes tracked time
|
|
|
|
func DeleteTime(c *context.Context) {
|
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 12:33:22 +05:30
|
|
|
if !c.Repo.CanUseTimetracker(issue, c.Doer) {
|
2021-02-19 16:22:11 +05:30
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
t, err := issues_model.GetTrackedTimeByID(c.ParamsInt64(":timeid"))
|
2021-02-19 16:22:11 +05:30
|
|
|
if err != nil {
|
2022-05-08 19:16:34 +05:30
|
|
|
if db.IsErrNotExist(err) {
|
2021-02-19 16:22:11 +05:30
|
|
|
c.NotFound("time not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Error(http.StatusInternalServerError, "GetTrackedTimeByID", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// only OP or admin may delete
|
2022-03-22 12:33:22 +05:30
|
|
|
if !c.IsSigned || (!c.IsUserSiteAdmin() && c.Doer.ID != t.UserID) {
|
2021-02-19 16:22:11 +05:30
|
|
|
c.Error(http.StatusForbidden, "not allowed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
if err = issues_model.DeleteTime(t); err != nil {
|
2021-02-19 16:22:11 +05:30
|
|
|
c.ServerError("DeleteTime", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-15 22:20:10 +05:30
|
|
|
c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToTime(t.Time)))
|
2021-02-19 16:22:11 +05:30
|
|
|
c.Redirect(issue.HTMLURL())
|
|
|
|
}
|