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"
|
2016-05-09 10:11:05 +08:00
|
|
|
"github.com/thewhitetulip/Tasks/sessions"
|
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
|
2016-05-09 10:11:05 +08:00
|
|
|
var loginTemplate *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) {
|
2016-05-12 01:19:32 +08:00
|
|
|
if r.Method == "GET" {
|
|
|
|
context, err := db.GetTasks("pending", "")
|
|
|
|
categories := db.GetCategories()
|
|
|
|
if err != nil {
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
if message != "" {
|
|
|
|
context.Message = message
|
2016-05-10 01:36:35 +08:00
|
|
|
}
|
2016-05-12 01:19:32 +08:00
|
|
|
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)
|
2016-02-01 21:45:04 +08:00
|
|
|
}
|
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) {
|
2016-05-12 01:19:32 +08:00
|
|
|
if r.Method == "GET" {
|
|
|
|
context, err := db.GetTasks("deleted", "")
|
|
|
|
categories := db.GetCategories()
|
|
|
|
context.Categories = categories
|
|
|
|
if err != nil {
|
|
|
|
http.Redirect(w, r, "/trash", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
if message != "" {
|
|
|
|
context.Message = message
|
|
|
|
message = ""
|
2015-11-22 11:51:29 +08:00
|
|
|
}
|
2016-05-12 01:19:32 +08:00
|
|
|
deletedTemplate.Execute(w, context)
|
2015-11-21 15:58:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
|
|
|
|
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
|
2016-05-12 01:19:32 +08:00
|
|
|
if r.Method == "GET" {
|
|
|
|
context, err := db.GetTasks("completed", "")
|
|
|
|
categories := db.GetCategories()
|
|
|
|
context.Categories = categories
|
|
|
|
if err != nil {
|
|
|
|
http.Redirect(w, r, "/completed", http.StatusInternalServerError)
|
2016-02-01 21:45:04 +08:00
|
|
|
}
|
2016-05-12 01:19:32 +08:00
|
|
|
completedTemplate.Execute(w, context)
|
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) {
|
2016-05-12 01:19:32 +08:00
|
|
|
if r.Method == "GET" && sessions.IsLoggedIn(r) {
|
|
|
|
category := r.URL.Path[len("/category/"):]
|
|
|
|
context, err := db.GetTasks("", category)
|
|
|
|
categories := db.GetCategories()
|
2016-02-03 01:40:44 +08:00
|
|
|
|
2016-05-12 01:19:32 +08:00
|
|
|
if err != nil {
|
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
if message != "" {
|
|
|
|
context.Message = message
|
2016-02-03 01:40:44 +08:00
|
|
|
}
|
2016-05-12 01:19:32 +08:00
|
|
|
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)
|
2016-02-03 01:40:44 +08:00
|
|
|
}
|
|
|
|
}
|