2017-09-12 14:48:13 +08:00
|
|
|
// 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"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-02-16 00:50:10 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 23:36:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-04-07 03:44:05 +08:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2017-09-12 14:48:13 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddTimeManually tracks time manually
|
2021-01-26 23:36:53 +08:00
|
|
|
func AddTimeManually(c *context.Context) {
|
2021-02-19 18:52:11 +08:00
|
|
|
form := web.GetForm(c).(*forms.AddTimeManuallyForm)
|
2017-10-16 15:55:43 +08:00
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !c.Repo.CanUseTimetracker(issue, c.User) {
|
2018-01-11 05:34:17 +08:00
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
2017-09-12 14:48:13 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-28 04:30:58 +08:00
|
|
|
if _, err := models.AddTime(c.User, issue, int64(total.Seconds()), time.Now()); err != nil {
|
2018-01-11 05:34:17 +08:00
|
|
|
c.ServerError("AddTime", err)
|
2017-09-12 14:48:13 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Redirect(url, http.StatusSeeOther)
|
|
|
|
}
|
2021-02-19 18:52:11 +08:00
|
|
|
|
|
|
|
// DeleteTime deletes tracked time
|
|
|
|
func DeleteTime(c *context.Context) {
|
|
|
|
issue := GetActionIssue(c)
|
|
|
|
if c.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !c.Repo.CanUseTimetracker(issue, c.User) {
|
|
|
|
c.NotFound("CanUseTimetracker", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := models.GetTrackedTimeByID(c.ParamsInt64(":timeid"))
|
|
|
|
if err != nil {
|
|
|
|
if models.IsErrNotExist(err) {
|
|
|
|
c.NotFound("time not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Error(http.StatusInternalServerError, "GetTrackedTimeByID", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// only OP or admin may delete
|
|
|
|
if !c.IsSigned || (!c.IsUserSiteAdmin() && c.User.ID != t.UserID) {
|
|
|
|
c.Error(http.StatusForbidden, "not allowed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = models.DeleteTime(t); err != nil {
|
|
|
|
c.ServerError("DeleteTime", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-16 00:50:10 +08:00
|
|
|
c.Flash.Success(c.Tr("repo.issues.del_time_history", util.SecToTime(t.Time)))
|
2021-02-19 18:52:11 +08:00
|
|
|
c.Redirect(issue.HTMLURL())
|
|
|
|
}
|