feat(templates): add IsTemplate field to Project model with filtering and validation
This commit is contained in:
parent
499941ed39
commit
2b4efef119
|
|
@ -528,6 +528,60 @@ func (err *ErrProjectViewDoesNotExist) HTTPError() web.HTTPError {
|
|||
}
|
||||
}
|
||||
|
||||
// ErrTemplateCannotHaveParentProject represents an error where a template project is being assigned a parent
|
||||
type ErrTemplateCannotHaveParentProject struct {
|
||||
ProjectID int64
|
||||
}
|
||||
|
||||
// IsErrTemplateCannotHaveParentProject checks if an error is a ErrTemplateCannotHaveParentProject.
|
||||
func IsErrTemplateCannotHaveParentProject(err error) bool {
|
||||
_, ok := err.(*ErrTemplateCannotHaveParentProject)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err *ErrTemplateCannotHaveParentProject) Error() string {
|
||||
return fmt.Sprintf("Template project cannot have a parent project [ProjectID: %d]", err.ProjectID)
|
||||
}
|
||||
|
||||
// ErrCodeTemplateCannotHaveParentProject holds the unique world-error code of this error
|
||||
const ErrCodeTemplateCannotHaveParentProject = 3015
|
||||
|
||||
// HTTPError holds the http error description
|
||||
func (err *ErrTemplateCannotHaveParentProject) HTTPError() web.HTTPError {
|
||||
return web.HTTPError{
|
||||
HTTPCode: http.StatusPreconditionFailed,
|
||||
Code: ErrCodeTemplateCannotHaveParentProject,
|
||||
Message: "Template projects cannot have a parent project.",
|
||||
}
|
||||
}
|
||||
|
||||
// ErrCannotMakeDefaultProjectTemplate represents an error where the default project is being made a template
|
||||
type ErrCannotMakeDefaultProjectTemplate struct {
|
||||
ProjectID int64
|
||||
}
|
||||
|
||||
// IsErrCannotMakeDefaultProjectTemplate checks if an error is a ErrCannotMakeDefaultProjectTemplate.
|
||||
func IsErrCannotMakeDefaultProjectTemplate(err error) bool {
|
||||
_, ok := err.(*ErrCannotMakeDefaultProjectTemplate)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err *ErrCannotMakeDefaultProjectTemplate) Error() string {
|
||||
return fmt.Sprintf("Default project cannot be made a template [ProjectID: %d]", err.ProjectID)
|
||||
}
|
||||
|
||||
// ErrCodeCannotMakeDefaultProjectTemplate holds the unique world-error code of this error
|
||||
const ErrCodeCannotMakeDefaultProjectTemplate = 3016
|
||||
|
||||
// HTTPError holds the http error description
|
||||
func (err *ErrCannotMakeDefaultProjectTemplate) HTTPError() web.HTTPError {
|
||||
return web.HTTPError{
|
||||
HTTPCode: http.StatusPreconditionFailed,
|
||||
Code: ErrCodeCannotMakeDefaultProjectTemplate,
|
||||
Message: "This project cannot be made a template because it is the default project of a user.",
|
||||
}
|
||||
}
|
||||
|
||||
// ==============
|
||||
// Task errors
|
||||
// ==============
|
||||
|
|
|
|||
|
|
@ -58,6 +58,9 @@ type Project struct {
|
|||
// Whether a project is archived.
|
||||
IsArchived bool `xorm:"not null default false" json:"is_archived" query:"is_archived"`
|
||||
|
||||
// Whether a project is a template.
|
||||
IsTemplate bool `xorm:"not null default false" json:"is_template" query:"is_template"`
|
||||
|
||||
// The id of the file this project has set as background
|
||||
BackgroundFileID int64 `xorm:"null" json:"-"`
|
||||
// Holds extra information about the background set since some background providers require attribution or similar. If not null, the background can be accessed at /projects/{projectID}/background
|
||||
|
|
@ -168,6 +171,7 @@ var FavoritesPseudoProject = Project{
|
|||
// @Param per_page query int false "The maximum number of items per page. Note this parameter is limited by the configured maximum of items per page."
|
||||
// @Param s query string false "Search projects by title."
|
||||
// @Param is_archived query bool false "If true, also returns all archived projects."
|
||||
// @Param is_template query bool false "If true, returns only template projects. If false (default), excludes templates."
|
||||
// @Param expand query string false "If set to `permissions`, Vikunja will return the max permission the current user has on this project. You can currently only set this to `permissions`."
|
||||
// @Security JWTKeyAuth
|
||||
// @Success 200 {array} models.Project "The projects"
|
||||
|
|
@ -175,7 +179,7 @@ var FavoritesPseudoProject = Project{
|
|||
// @Failure 500 {object} models.Message "Internal error"
|
||||
// @Router /projects [get]
|
||||
func (p *Project) ReadAll(s *xorm.Session, a web.Auth, search string, page int, perPage int) (result interface{}, resultCount int, totalItems int64, err error) {
|
||||
prs, resultCount, totalItems, err := getAllRawProjects(s, a, search, page, perPage, p.IsArchived, false)
|
||||
prs, resultCount, totalItems, err := getAllRawProjects(s, a, search, page, perPage, p.IsArchived, p.IsTemplate, false)
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
|
|
@ -216,9 +220,9 @@ func (p *Project) ReadAll(s *xorm.Session, a web.Auth, search string, page int,
|
|||
return prs, resultCount, totalItems, err
|
||||
}
|
||||
|
||||
func getAllRawProjects(s *xorm.Session, a web.Auth, search string, page int, perPage int, isArchived, listAll bool) (projects []*Project, resultCount int, totalItems int64, err error) {
|
||||
func getAllRawProjects(s *xorm.Session, a web.Auth, search string, page int, perPage int, isArchived, isTemplate, listAll bool) (projects []*Project, resultCount int, totalItems int64, err error) {
|
||||
if listAll {
|
||||
return getRawProjectsUnscoped(s, search, page, perPage, isArchived)
|
||||
return getRawProjectsUnscoped(s, search, page, perPage, isArchived, isTemplate)
|
||||
}
|
||||
|
||||
// Check if we're dealing with a share auth
|
||||
|
|
@ -244,11 +248,12 @@ func getAllRawProjects(s *xorm.Session, a web.Auth, search string, page int, per
|
|||
prs, resultCount, totalItems, err := getRawProjectsForUser(
|
||||
s,
|
||||
&projectOptions{
|
||||
search: search,
|
||||
user: doer,
|
||||
page: page,
|
||||
perPage: perPage,
|
||||
getArchived: isArchived,
|
||||
search: search,
|
||||
user: doer,
|
||||
page: page,
|
||||
perPage: perPage,
|
||||
getArchived: isArchived,
|
||||
getTemplates: isTemplate,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
|
|
@ -271,7 +276,7 @@ func getAllRawProjects(s *xorm.Session, a web.Auth, search string, page int, per
|
|||
|
||||
// ListAllProjects returns every project with owners hydrated; callers must authorize since this bypasses the per-user permission filter.
|
||||
func ListAllProjects(s *xorm.Session, search string, page, perPage int, isArchived bool) (projects []*Project, resultCount int, totalItems int64, err error) {
|
||||
projects, resultCount, totalItems, err = getAllRawProjects(s, nil, search, page, perPage, isArchived, true)
|
||||
projects, resultCount, totalItems, err = getAllRawProjects(s, nil, search, page, perPage, isArchived, true, true)
|
||||
if err != nil {
|
||||
return nil, 0, 0, err
|
||||
}
|
||||
|
|
@ -293,13 +298,16 @@ func ListAllProjects(s *xorm.Session, search string, page, perPage int, isArchiv
|
|||
return projects, resultCount, totalItems, nil
|
||||
}
|
||||
|
||||
func getRawProjectsUnscoped(s *xorm.Session, search string, page, perPage int, isArchived bool) (projects []*Project, resultCount int, totalItems int64, err error) {
|
||||
func getRawProjectsUnscoped(s *xorm.Session, search string, page, perPage int, isArchived, isTemplate bool) (projects []*Project, resultCount int, totalItems int64, err error) {
|
||||
limit, start := getLimitFromPageIndex(page, perPage)
|
||||
|
||||
conds := []builder.Cond{}
|
||||
if !isArchived {
|
||||
conds = append(conds, builder.Eq{"is_archived": false})
|
||||
}
|
||||
if !isTemplate {
|
||||
conds = append(conds, builder.Eq{"is_template": false})
|
||||
}
|
||||
if search != "" {
|
||||
ids := []int64{}
|
||||
for _, val := range strings.Split(search, ",") {
|
||||
|
|
@ -525,11 +533,12 @@ func GetProjectsByIDs(s *xorm.Session, projectIDs []int64) (projects []*Project,
|
|||
}
|
||||
|
||||
type projectOptions struct {
|
||||
search string
|
||||
user *user.User
|
||||
page int
|
||||
perPage int
|
||||
getArchived bool
|
||||
search string
|
||||
user *user.User
|
||||
page int
|
||||
perPage int
|
||||
getArchived bool
|
||||
getTemplates bool
|
||||
}
|
||||
|
||||
func getUserProjectsStatement(userID int64, search string) *builder.Builder {
|
||||
|
|
@ -661,6 +670,7 @@ INNER JOIN all_projects ap ON p.parent_project_id = ap.id`
|
|||
"all_projects.owner_id",
|
||||
"CASE WHEN all_projects.parent_project_id IS NULL THEN 0 ELSE all_projects.parent_project_id END AS parent_project_id",
|
||||
"MAX(CASE WHEN all_projects.is_archived THEN 1 ELSE 0 END) AS is_archived",
|
||||
"all_projects.is_template",
|
||||
"all_projects.background_file_id",
|
||||
"all_projects.background_blur_hash",
|
||||
"all_projects.position",
|
||||
|
|
@ -678,20 +688,28 @@ INNER JOIN all_projects ap ON p.parent_project_id = ap.id`
|
|||
"all_projects.parent_project_id",
|
||||
"all_projects.background_file_id",
|
||||
"all_projects.background_blur_hash",
|
||||
"all_projects.is_template",
|
||||
"all_projects.position",
|
||||
"all_projects.created",
|
||||
"all_projects.updated",
|
||||
}, ", ")
|
||||
|
||||
var archivedFilter string
|
||||
var havingClauses []string
|
||||
if !opts.getArchived {
|
||||
archivedFilter = "HAVING MAX(CASE WHEN all_projects.is_archived THEN 1 ELSE 0 END) = 0 "
|
||||
havingClauses = append(havingClauses, "MAX(CASE WHEN all_projects.is_archived THEN 1 ELSE 0 END) = 0")
|
||||
}
|
||||
if !opts.getTemplates {
|
||||
havingClauses = append(havingClauses, "all_projects.is_template = 0")
|
||||
}
|
||||
var havingFilter string
|
||||
if len(havingClauses) > 0 {
|
||||
havingFilter = "HAVING " + strings.Join(havingClauses, " AND ") + " "
|
||||
}
|
||||
|
||||
currentProjects := []*Project{}
|
||||
err = s.SQL(`WITH RECURSIVE all_projects as (`+baseQuery+`)
|
||||
SELECT `+columnStr+` FROM all_projects
|
||||
GROUP BY `+groupByStr+` `+archivedFilter+`ORDER BY all_projects.position `+limitSQL, args...).Find(¤tProjects)
|
||||
GROUP BY `+groupByStr+` `+havingFilter+`ORDER BY all_projects.position `+limitSQL, args...).Find(¤tProjects)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -702,7 +720,7 @@ GROUP BY `+groupByStr+` `+archivedFilter+`ORDER BY all_projects.position `+limit
|
|||
|
||||
totalCount, err = s.
|
||||
SQL(`WITH RECURSIVE all_projects as (`+baseQuery+`)
|
||||
SELECT COUNT(*) FROM (SELECT all_projects.id FROM all_projects GROUP BY all_projects.id `+archivedFilter+`) sub`, args...).
|
||||
SELECT COUNT(*) FROM (SELECT all_projects.id FROM all_projects GROUP BY all_projects.id `+havingFilter+`) sub`, args...).
|
||||
Count(&Project{})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
|
|
@ -956,6 +974,10 @@ func checkProjectBeforeUpdateOrDelete(s *xorm.Session, project *Project) (err er
|
|||
return &ErrProjectCannotBelongToAPseudoParentProject{ProjectID: project.ID, ParentProjectID: project.ParentProjectID}
|
||||
}
|
||||
|
||||
if project.IsTemplate && project.ParentProjectID != 0 {
|
||||
return &ErrTemplateCannotHaveParentProject{ProjectID: project.ID}
|
||||
}
|
||||
|
||||
// Check if the parent project exists
|
||||
if project.ParentProjectID > 0 {
|
||||
if project.ParentProjectID == project.ID {
|
||||
|
|
@ -1148,6 +1170,17 @@ func UpdateProject(s *xorm.Session, project *Project, auth web.Auth, updateProje
|
|||
}
|
||||
}
|
||||
|
||||
if project.IsTemplate {
|
||||
isDefaultProject, err := project.isDefaultProject(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if isDefaultProject {
|
||||
return &ErrCannotMakeDefaultProjectTemplate{ProjectID: project.ID}
|
||||
}
|
||||
}
|
||||
|
||||
err = setArchiveStateForProjectDescendants(s, project.ID, project.IsArchived)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -1157,6 +1190,7 @@ func UpdateProject(s *xorm.Session, project *Project, auth web.Auth, updateProje
|
|||
colsToUpdate := []string{
|
||||
"title",
|
||||
"is_archived",
|
||||
"is_template",
|
||||
"identifier",
|
||||
"hex_color",
|
||||
"parent_project_id",
|
||||
|
|
|
|||
Loading…
Reference in New Issue