Tasks/views/views.go

109 lines
3.1 KiB
Go
Raw Normal View History

2015-11-21 15:58:23 +08:00
package views
2016-02-01 22:34:01 +08:00
/*Holds the fetch related view handlers*/
2015-11-21 15:58:23 +08:00
import (
"net/http"
"text/template"
2016-01-18 10:28:08 +08:00
"time"
2016-02-01 21:45:04 +08:00
"github.com/thewhitetulip/Tasks/db"
2015-11-21 15:58:23 +08:00
)
var homeTemplate *template.Template
var deletedTemplate *template.Template
var completedTemplate *template.Template
var editTemplate *template.Template
var searchTemplate *template.Template
var templates *template.Template
2015-11-22 11:51:29 +08:00
var message string //message will store the message to be shown as notification
2015-11-21 15:58:23 +08:00
var err error
//ShowAllTasksFunc is used to handle the "/" URL which is the default ons
//TODO add http404 error
func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
2016-02-03 01:40:44 +08:00
context, err := db.GetTasks("pending", "")
categories := db.GetCategories()
2016-02-01 21:45:04 +08:00
if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
2015-11-22 11:51:29 +08:00
if message != "" {
context.Message = message
}
2016-01-18 10:28:08 +08:00
context.CSRFToken = "abcd"
2016-02-03 01:40:44 +08:00
context.Categories = categories
2015-11-22 11:51:29 +08:00
message = ""
2016-01-18 09:02:15 +08:00
expiration := time.Now().Add(365 * 24 * time.Hour)
2016-01-18 10:28:08 +08:00
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
2016-01-18 09:02:15 +08:00
http.SetCookie(w, &cookie)
homeTemplate.Execute(w, context)
2015-11-22 11:51:29 +08:00
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks
func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
2016-02-03 01:40:44 +08:00
context, err := db.GetTasks("deleted", "")
categories := db.GetCategories()
context.Categories = categories
2016-02-01 21:45:04 +08:00
if err != nil {
http.Redirect(w, r, "/trash", http.StatusInternalServerError)
}
2015-11-22 11:51:29 +08:00
if message != "" {
context.Message = message
message = ""
}
2015-11-21 15:58:23 +08:00
deletedTemplate.Execute(w, context)
2015-11-22 11:51:29 +08:00
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
2016-02-03 01:40:44 +08:00
context, err := db.GetTasks("completed", "")
categories := db.GetCategories()
context.Categories = categories
2016-02-01 21:45:04 +08:00
if err != nil {
http.Redirect(w, r, "/completed", http.StatusInternalServerError)
}
2015-11-21 15:58:23 +08:00
completedTemplate.Execute(w, context)
2015-11-21 19:38:08 +08:00
} else {
2015-11-22 11:51:29 +08:00
message = "Method not allowed"
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
2016-02-03 01:40:44 +08:00
//ShowCategoryFunc will populate the /category/<id> URL which shows all the tasks related
// to that particular category
func ShowCategoryFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
category := r.URL.Path[len("/category/"):]
context, err := db.GetTasks("", category)
categories := db.GetCategories()
if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
if message != "" {
context.Message = message
}
context.CSRFToken = "abcd"
context.Categories = categories
message = ""
expiration := time.Now().Add(365 * 24 * time.Hour)
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
http.SetCookie(w, &cookie)
homeTemplate.Execute(w, context)
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
}
}